Project

General

Profile

Download (1.9 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
$leases_file = "/var/dhcpd/var/db/dhcpd6.leases";
4
if (!file_exists($leases_file)) {
5
	exit(1);
6
}
7

    
8
$fd = fopen($leases_file, 'r');
9

    
10
$duid_arr = array();
11
while (( $line = fgets($fd, 4096)) !== false) {
12
	// echo "$line";
13
	if (preg_match("/^(ia-[np][ad])[ ]+\"(.*?)\"/i", $line, $duidmatch)) {
14
		$type = $duidmatch[1];
15
		$duid = $duidmatch[2];
16
		continue;
17
	}
18

    
19
	/* is it active? otherwise just discard */
20
	if (preg_match("/binding state active/i", $line, $activematch)) {
21
		$active = true;
22
		continue;
23
	}
24

    
25
	if (preg_match("/iaaddr[ ]+([0-9a-f:]+)[ ]+/i", $line, $addressmatch)) {
26
		$ia_na = $addressmatch[1];
27
		continue;
28
	}
29

    
30
	if (preg_match("/iaprefix[ ]+([0-9a-f:\/]+)[ ]+/i", $line, $prefixmatch)) {
31
		$ia_pd = $prefixmatch[1];
32
		continue;
33
	}
34

    
35
	/* closing bracket */
36
	if (preg_match("/^}/i", $line)) {
37
		switch ($type) {
38
			case "ia-na":
39
				$duid_arr[$duid][$type] = $ia_na;
40
				break;
41
			case "ia-pd":
42
				$duid_arr[$duid][$type] = $ia_pd;
43
				break;
44
		}
45
		unset($type);
46
		unset($duid);
47
		unset($active);
48
		unset($ia_na);
49
		unset($ia_pd);
50
		continue;
51
	}
52
}
53
fclose($fd);
54

    
55
$routes = array();
56
foreach ($duid_arr as $entry) {
57
	if (!empty($entry['ia-pd'])) {
58
		$routes[$entry['ia-na']] = $entry['ia-pd'];
59
	}
60
}
61

    
62
// echo "add routes\n";
63
if (count($routes) > 0) {
64
	foreach ($routes as $address => $prefix) {
65
		echo "/sbin/route change -inet6 {$prefix} {$address}\n";
66
	}
67
}
68

    
69
/* get clog from dhcpd */
70
$dhcpdlogfile = "/var/log/dhcpd.log";
71
$expires = array();
72
if (file_exists($dhcpdlogfile)) {
73
	$fd = popen("clog $dhcpdlogfile", 'r');
74
	while (($line = fgets($fd)) !== false) {
75
		//echo $line;
76
		if (preg_match("/releases[ ]+prefix[ ]+([0-9a-f:]+\/[0-9]+)/i", $line, $expire)) {
77
			if (in_array($expire[1], $routes)) {
78
				continue;
79
			}
80
			$expires[$expire[1]] = $expire[1];
81
		}
82
	}
83
	pclose($fd);
84
}
85

    
86
// echo "remove routes\n";
87
if (count($expires) > 0) {
88
	foreach ($expires as $prefix) {
89
		echo "/sbin/route delete -inet6 {$prefix['prefix']}\n";
90
	}
91
}
92

    
93
?>
(17-17/23)