summaryrefslogtreecommitdiffstats
path: root/usr/local/sbin/prefixes.php
blob: b0920cbac372caff6e1506dfd6ff3810a43ddac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php

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

$duid_arr = array();
foreach(file($leases_file) as $line) {
	// echo "$line";
	if(preg_match("/^(ia-[np][ad])[ ]+\"(.*?)\"/i ", $line, $duidmatch)) {
		$type = $duidmatch[1];
		$duid = $duidmatch[2];
		continue;
	}

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

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

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

	/* closing bracket */
	if(preg_match("/^}/i ", $line)) {
		switch($type) {
			case "ia-na":
				$duid_arr[$duid][$type] = $ia_na;
				break;
			case "ia-pd":
				$duid_arr[$duid][$type] = $ia_pd;
				break;
		}
		unset($type);
		unset($duid);
		unset($active);
		unset($ia_na);
		unset($ia_pd);
		continue;
	}
}

$routes = array();
foreach ($duid_arr as $entry) {
	if($entry['ia-pd'] <> "") {
		$routes[$entry['ia-na']] = $entry['ia-pd'];
	}
	array_shift($duid_arr);
}

// echo "add routes\n";
if(count($routes) > 0) {
	foreach ($routes as $address => $prefix) {
		echo "/sbin/route change -inet6 {$prefix} {$address}\n";
	}
}

/* get clog from dhcpd */
$dhcpdlogfile = "/var/log/dhcpd.log";
$clog = array();
if(file_exists(dhcpdlogfile))
	exec("clog $dhcpdlogfile", $clog, $ret);

if($ret > 0)
	$clog = array();

$expires = array();
foreach($clog as $line) {
	if(preg_match("/releases[ ]+prefix[ ]+([0-9a-f:]+\/[0-9]+)/i", $line, $expire)) {
		if(in_array($expire[1], $routes))
			continue;
		$expires[$expire[1]] = $expire[1];
	}
	array_shift($clog);
}

// echo "remove routes\n";
if(count($expires) > 0) {
	foreach ($expires as $prefix) {
		echo "/sbin/route delete -inet6 {$prefix['prefix']}\n";
		array_shift($expires);
	}
}

?>
OpenPOWER on IntegriCloud