Project

General

Profile

Download (1.9 KB) Statistics
| Branch: | Tag: | Revision:
1 092462dc smos
<?php
2
3
$leases_file = "/var/dhcpd/var/db/dhcpd6.leases";
4 7d61beba Phil Davis
if (!file_exists($leases_file)) {
5 8dfb0c00 smos
	exit(1);
6
}
7 092462dc smos
8 74fe0ef9 Ermal LUÇI
$fd = fopen($leases_file, 'r');
9
10 2abfec49 Renato Botelho
$duid_arr = array();
11 74fe0ef9 Ermal LUÇI
while (( $line = fgets($fd, 4096)) !== false) {
12 092462dc smos
	// echo "$line";
13 7d61beba Phil Davis
	if (preg_match("/^(ia-[np][ad])[ ]+\"(.*?)\"/i", $line, $duidmatch)) {
14 092462dc smos
		$type = $duidmatch[1];
15
		$duid = $duidmatch[2];
16
		continue;
17
	}
18
19
	/* is it active? otherwise just discard */
20 7d61beba Phil Davis
	if (preg_match("/binding state active/i", $line, $activematch)) {
21 092462dc smos
		$active = true;
22
		continue;
23
	}
24
25 7d61beba Phil Davis
	if (preg_match("/iaaddr[ ]+([0-9a-f:]+)[ ]+/i", $line, $addressmatch)) {
26 092462dc smos
		$ia_na = $addressmatch[1];
27
		continue;
28
	}
29
30 7d61beba Phil Davis
	if (preg_match("/iaprefix[ ]+([0-9a-f:\/]+)[ ]+/i", $line, $prefixmatch)) {
31 092462dc smos
		$ia_pd = $prefixmatch[1];
32
		continue;
33
	}
34
35
	/* closing bracket */
36 7d61beba Phil Davis
	if (preg_match("/^}/i", $line)) {
37
		switch ($type) {
38 092462dc smos
			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 74fe0ef9 Ermal LUÇI
fclose($fd);
54 092462dc smos
55
$routes = array();
56
foreach ($duid_arr as $entry) {
57 7d61beba Phil Davis
	if (!empty($entry['ia-pd'])) {
58 092462dc smos
		$routes[$entry['ia-na']] = $entry['ia-pd'];
59
	}
60
}
61
62
// echo "add routes\n";
63 7d61beba Phil Davis
if (count($routes) > 0) {
64 69b6c2b5 smos
	foreach ($routes as $address => $prefix) {
65
		echo "/sbin/route change -inet6 {$prefix} {$address}\n";
66
	}
67 092462dc smos
}
68
69
/* get clog from dhcpd */
70
$dhcpdlogfile = "/var/log/dhcpd.log";
71
$expires = array();
72 7d61beba Phil Davis
if (file_exists($dhcpdlogfile)) {
73 74fe0ef9 Ermal LUÇI
	$fd = popen("clog $dhcpdlogfile", 'r');
74
	while (($line = fgets($fd)) !== false) {
75
		//echo $line;
76 7d61beba Phil Davis
		if (preg_match("/releases[ ]+prefix[ ]+([0-9a-f:]+\/[0-9]+)/i", $line, $expire)) {
77
			if (in_array($expire[1], $routes)) {
78 74fe0ef9 Ermal LUÇI
				continue;
79 7d61beba Phil Davis
			}
80 74fe0ef9 Ermal LUÇI
			$expires[$expire[1]] = $expire[1];
81
		}
82 092462dc smos
	}
83 74fe0ef9 Ermal LUÇI
	pclose($fd);
84 092462dc smos
}
85
86
// echo "remove routes\n";
87 7d61beba Phil Davis
if (count($expires) > 0) {
88 69b6c2b5 smos
	foreach ($expires as $prefix) {
89
		echo "/sbin/route delete -inet6 {$prefix['prefix']}\n";
90
	}
91 092462dc smos
}
92
93
?>