prefixes.php 2.21 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2 3
<?php

$leases_file = "/var/dhcpd/var/db/dhcpd6.leases";
4 5
if (!file_exists($leases_file)) {
    exit(1);
Ad Schellevis's avatar
Ad Schellevis committed
6 7 8
}

$duid_arr = array();
9 10 11 12 13 14 15
foreach (file($leases_file) as $line) {
    // echo "$line";
    if (preg_match("/^(ia-[np][ad])[ ]+\"(.*?)\"/i ", $line, $duidmatch)) {
        $type = $duidmatch[1];
        $duid = $duidmatch[2];
        continue;
    }
Ad Schellevis's avatar
Ad Schellevis committed
16

17 18 19 20 21
    /* is it active? otherwise just discard */
    if (preg_match("/binding state active/i", $line, $activematch)) {
        $active = true;
        continue;
    }
Ad Schellevis's avatar
Ad Schellevis committed
22

23 24 25 26
    if (preg_match("/iaaddr[ ]+([0-9a-f:]+)[ ]+/i", $line, $addressmatch)) {
        $ia_na = $addressmatch[1];
        continue;
    }
Ad Schellevis's avatar
Ad Schellevis committed
27

28 29 30 31
    if (preg_match("/iaprefix[ ]+([0-9a-f:\/]+)[ ]+/i", $line, $prefixmatch)) {
        $ia_pd = $prefixmatch[1];
        continue;
    }
Ad Schellevis's avatar
Ad Schellevis committed
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    /* 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;
    }
Ad Schellevis's avatar
Ad Schellevis committed
50 51 52 53
}

$routes = array();
foreach ($duid_arr as $entry) {
54 55 56 57
    if ($entry['ia-pd'] <> "") {
        $routes[$entry['ia-na']] = $entry['ia-pd'];
    }
    array_shift($duid_arr);
Ad Schellevis's avatar
Ad Schellevis committed
58 59 60
}

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

/* get clog from dhcpd */
$dhcpdlogfile = "/var/log/dhcpd.log";
$clog = array();
70 71 72
if (file_exists($dhcpdlogfile)) {
    exec("clog $dhcpdlogfile", $clog, $ret);
}
Ad Schellevis's avatar
Ad Schellevis committed
73

74 75 76
if ($ret > 0) {
    $clog = array();
}
Ad Schellevis's avatar
Ad Schellevis committed
77 78

$expires = array();
79 80 81 82 83 84 85 86
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);
Ad Schellevis's avatar
Ad Schellevis committed
87 88 89
}

// echo "remove routes\n";
90 91 92 93 94
if (count($expires) > 0) {
    foreach ($expires as $prefix) {
        echo "/sbin/route delete -inet6 {$prefix['prefix']}\n";
        array_shift($expires);
    }
Ad Schellevis's avatar
Ad Schellevis committed
95
}