getting towards base functionality
[PHPIPManager.git] / lib / ip.php
index e07eb30..a0287f3 100644 (file)
 <?php
 
+function ipversion($ip)
+{
+       $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);
+       $ipv6 = ereg("^[0-9a-fA-F:]+$", $ip);
+       
+       if($ipv4 == 1) return 4;
+       if($ipv6 == 1) return 6;
+       return 0;
+}
+
+// this function tries to take a short representation of a ip6 address and converts it to a long one.
+// this is not entirely going to work.. but we'll get back to this
+function ip6ToLongStr($ip)
+{
+       if(ipversion($ip)!=6) return false;
+       
+       $pss = explode(":", $ip);
+       $ns = count($pss);
+       
+       $nstr = "";
+       foreach($pss as $ele) {
+               if($nstr != "") {
+                       $nstr .= ":";
+               }
+               if(strlen($ele < 1)) {
+                       // this is where we pad 8-count of 0000:
+                       $pds = 8-$ns+1;
+                       for($i=0; $i < $pds; $i++) {
+                               $nstr .= "0000";
+                       }
+               } else if(strlen($ele) < 4) {
+                       $nstr .= str_pad($ele, 4, "0", STR_PAD_LEFT);
+               } else {
+                       $nstr .= $ele;
+               }
+       }
+       return $nstr;
+}
+
+// this function converts an ip address to a comparable integer
+// so we can bounds-check.
+// it returns an array of [firstnum][lastnum]
+function ip6ToComp($ip, $mask)
+{
+       if($ipversion($ip)!=6) return false;
+       
+       $rip = ip6ToLongStr($ip);
+       
+}
+
+function ip4ToComp($ip, $mask)
+{
+       
+}
+
 class ip {
-       function addSupernet($supernet)
+       
+       static function isValid($ip, $mask = 0)
        {
                
+               $ver = ipversion($ip);
+               
+               if($ver == 0) return 0;
+
+               if($ver == 6) {
+                       if($mask != 0) {
+                               if($mask%16 != 0) {
+                                       error_log("no");
+                                       return false;
+                               } else {
+                                       $sns = $mask / 16;
+                                       
+                                       // now check that we have $sns number of subnets specified
+                                       $pss = explode(":", $ip);
+                                       $ns = count($pss);
+                                       
+                                       // we need to specify an error we can throw back at the user
+                                       if($ns < $sns) {
+                                               error_log("no 2, $ns, $sns, $ip");
+                                               print_r($pss);
+                                               return false;
+                                       }
+                                       for($i = 0; $i < $sns; $i++) {
+                                               if(strlen($pss[$i]) < 1) {
+                                                       error_log("no 3");
+                                                       return false;
+                                               }
+                                       }
+                                       
+                                       // we are still a valid ipv6 ip address/mask
+                                       error_log("Valid");
+                                       return true;
+                               }
+                       }
+               }
+               
+               if($ver == 4) {
+                       if($mask != 0) {
+                               
+                       }
+               }
        }
        
-       function addSubnet($subnet)
+       static function truncateNetwork($ip, $mask)
        {
+               $ver = ipversion($ip);
                
+               if($ver == 0) return false;
+               
+               if($ver == 6) {
+                       if($mask != 0) {
+                               if($mask%16 != 0) {
+                                       error_log("no");
+                                       return false;
+                               } else {
+                                       $sns = $mask / 16;
+                                       
+                                       // now check that we have $sns number of subnets specified
+                                       $pss = explode(":", $ip);
+                                       $ns = count($pss);
+                                       
+                                       // we need to specify an error we can throw back at the user
+                                       if($ns < $sns) {
+                                               error_log("no 2, $ns, $sns, $ip");
+                                               print_r($pss);
+                                               return false;
+                                       }
+                                       
+                                       $slt = "";
+                                       for($i = 0; $i < $sns; $i++) {
+                                               if($i!=0) $slt .= ":";
+                                               if(strlen($pss[$i]) < 1) {
+                                                       error_log("no 3");
+                                                       return false;
+                                               }
+                                               $slt .= "".$pss[$i];
+                                       }
+                                       
+                                       // we are still a valid ipv6 ip address/mask
+                                       error_log("Valid");
+                                       return $slt;
+                               }
+                       }
+               }
+               
+               if($ver == 4) {
+                       if($mask != 0) {
+                               
+                       }
+               }
        }
        
-       function isConflicting($subnet)
+       function addSupernet($name, $sn, $mask, $desc)
        {
+               global $db;
                
+               if(ip::isValid($sn, $mask)) {
+                       $sn = ip::truncateNetwork($sn, $mask);
+                       $sql = "insert into supernet values (NULL, '$name', '$sn', '$mask', '$desc')";
+                       $db->dbobject->query($sql);
+                       return true;
+               }
+               return false;
+       }
+       
+       function addSubnet($name, $subnet, $mask, $desc, $super)
+       {
+               global $db;
+               if(ip::isValid($subnet, $mask)) {
+                       $sn = ip::truncateNetwork($sn, $mask);
+                       // ("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
+                       //if(!isSubnet($subnet, $mask, $super)) return "Is not a valid subnet"; 
+                       
+                       $sql = "insert into subnet values (NULL, '$super', '$subnet', '$mask', '$name', '$desc')";
+                       error_log("sql: $sql");
+                       $db->dbobject->query($sql);
+                       return true;
+               }
+       }
+       
+       function isSubnet($subnet, $mask, $superid)
+       {
+               return true;
+       }
+       
+       // this function returns true if it is NOT conflicting
+       function isConflicting($subnet, $mask, $superid)
+       {
+               return true;
        }
        
        public $supers = "";