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 error_log("ipversion, $ip, $ipv4, $ipv6");
9 if($ipv4 == 1) return 4;
10 if($ipv6 == 1) return 6;
14 // this function tries to take a short representation of a ip6 address and converts it to a long one.
15 // this is not entirely going to work.. but we'll get back to this
16 function ip6ToLongStr($ip)
18 if(ipversion($ip)!=6) return false;
20 $pss = explode(":", $ip);
24 foreach($pss as $ele) {
28 if(strlen($ele < 1)) {
29 // this is where we pad 8-count of 0000:
31 for($i=0; $i < $pds; $i++) {
34 } else if(strlen($ele) < 4) {
35 $nstr .= str_pad($ele, 4, "0", STR_PAD_LEFT);
43 // this function converts an ip address to a comparable integer
44 // so we can bounds-check.
45 // it returns an array of [firstnum][lastnum]
46 function ip6ToComp($ip, $mask)
48 if($ipversion($ip)!=6) return false;
50 $rip = ip6ToLongStr($ip);
54 function ip4ToComp($ip, $mask)
61 static function isValid($ip, $mask = 0)
64 $ver = ipversion($ip);
66 if($ver == 0) return 0;
76 // now check that we have $sns number of subnets specified
77 $pss = explode(":", $ip);
80 // we need to specify an error we can throw back at the user
82 error_log("no 2, $ns, $sns, $ip");
86 for($i = 0; $i < $sns; $i++) {
87 if(strlen($pss[$i]) < 1) {
93 // we are still a valid ipv6 ip address/mask
107 static function truncateNetwork($ip, $mask)
109 $ver = ipversion($ip);
111 if($ver == 0) return false;
121 // now check that we have $sns number of subnets specified
122 $pss = explode(":", $ip);
125 // we need to specify an error we can throw back at the user
127 error_log("no 2, $ns, $sns, $ip");
133 for($i = 0; $i < $sns; $i++) {
134 if($i!=0) $slt .= ":";
135 if(strlen($pss[$i]) < 1) {
142 // we are still a valid ipv6 ip address/mask
156 function addSupernet($name, $sn, $mask, $desc)
160 if(ip::isValid($sn, $mask)) {
161 $sn = ip::truncateNetwork($sn, $mask);
162 $sql = "insert into supernet values (NULL, '$name', '$sn', '$mask', '$desc')";
163 $db->dbobject->query($sql);
169 function addSubnet($name, $subnet, $mask, $desc, $super)
172 if(ip::isValid($subnet, $mask)) {
173 $sn = ip::truncateNetwork($sn, $mask);
174 // ("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
175 //if(!isSubnet($subnet, $mask, $super)) return "Is not a valid subnet";
177 $sql = "insert into subnet values (NULL, '$super', '$subnet', '$mask', '$name', '$desc')";
178 error_log("sql: $sql");
179 $db->dbobject->query($sql);
184 function isSubnet($subnet, $mask, $superid)
189 // this function returns true if it is NOT conflicting
190 function isConflicting($subnet, $mask, $superid)