3 function ipversion($ip)
5 $ipv4 = ereg('^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$', $ip);
6 $ipv6 = ereg("^[0-9a-fA-F:]+$", $ip);
8 if($ipv4 == 1) return 4;
9 if($ipv6 == 1) return 6;
13 // this function tries to take a short representation of a ip6 address and converts it to a long one.
14 // this is not entirely going to work.. but we'll get back to this
15 function ip6ToLongStr($ip)
17 if(ipversion($ip)!=6) return false;
19 $pss = explode(":", $ip);
23 foreach($pss as $ele) {
27 if(strlen($ele < 1)) {
28 // this is where we pad 8-count of 0000:
30 for($i=0; $i < $pds; $i++) {
33 } else if(strlen($ele) < 4) {
34 $nstr .= str_pad($ele, 4, "0", STR_PAD_LEFT);
42 // this function converts an ip address to a comparable integer
43 // so we can bounds-check.
44 // it returns an array of [firstnum][lastnum]
45 function ip6ToComp($ip, $mask)
47 if($ipversion($ip)!=6) return false;
49 $rip = ip6ToLongStr($ip);
53 function ip4ToComp($ip, $mask)
60 static function isValid($ip, $mask = 0)
63 $ver = ipversion($ip);
65 if($ver == 0) return 0;
75 // now check that we have $sns number of subnets specified
76 $pss = explode(":", $ip);
79 // we need to specify an error we can throw back at the user
81 error_log("no 2, $ns, $sns, $ip");
85 for($i = 0; $i < $sns; $i++) {
86 if(strlen($pss[$i]) < 1) {
92 // we are still a valid ipv6 ip address/mask
106 static function truncateNetwork($ip, $mask)
108 $ver = ipversion($ip);
110 if($ver == 0) return false;
120 // now check that we have $sns number of subnets specified
121 $pss = explode(":", $ip);
124 // we need to specify an error we can throw back at the user
126 error_log("no 2, $ns, $sns, $ip");
132 for($i = 0; $i < $sns; $i++) {
133 if($i!=0) $slt .= ":";
134 if(strlen($pss[$i]) < 1) {
141 // we are still a valid ipv6 ip address/mask
155 function addSupernet($name, $sn, $mask, $desc)
159 if(ip::isValid($sn, $mask)) {
160 $sn = ip::truncateNetwork($sn, $mask);
161 $sql = "insert into supernet values (NULL, '$name', '$sn', '$mask', '$desc')";
162 $db->dbobject->query($sql);
168 function addSubnet($name, $subnet, $mask, $desc, $super)
171 if(ip::isValid($subnet, $mask)) {
172 $sn = ip::truncateNetwork($sn, $mask);
173 // ("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
174 //if(!isSubnet($subnet, $mask, $super)) return "Is not a valid subnet";
176 $sql = "insert into subnet values (NULL, '$super', '$subnet', '$mask', '$name', '$desc')";
177 error_log("sql: $sql");
178 $db->dbobject->query($sql);
183 function isSubnet($subnet, $mask, $superid)
188 // this function returns true if it is NOT conflicting
189 function isConflicting($subnet, $mask, $superid)