Example #1
0
 function set_IP($ip, $family = "")
 {
     $is_negative = false;
     $this->ip = $ip;
     $match = explode("/", $this->ip);
     $this->n_ip = $match[0];
     $this->netmask = $match[1];
     if (strpos($this->n_ip, ':') === false && strpos($this->n_ip, '.') === false) {
         if ($family == 4) {
             $this->n_ip = long2ip($this->n_ip);
             $this->ip = $this->n_ip . "/" . $this->netmask;
         } else {
             if ($family == 6) {
                 $this->n_ip = Netblock::inet_dtop($this->n_ip);
                 $this->ip = $this->n_ip . "/" . $this->netmask;
             } else {
                 $this->n_ip = "0";
             }
         }
     }
     $bin = "";
     if (preg_match('/^\\d{1,3}(\\.\\d{1,3}){3,3}/', $this->n_ip)) {
         $this->family = 4;
         $this->max_bits = 32;
         $ip_octets = explode(".", $this->n_ip);
         foreach ($ip_octets as $id => $num) {
             $bin .= str_pad(decbin($num), 8, 0, STR_PAD_LEFT);
         }
     }
     if (preg_match(V6REG, $this->n_ip)) {
         $this->family = 6;
         $this->max_bits = 128;
         $hex = $this->expand_hex_ip6($this->n_ip);
         $ip_dual_octets = explode(":", $hex);
         foreach ($ip_dual_octets as $id => $num) {
             $bin .= str_pad(base_convert($num, 16, 2), 16, 0, STR_PAD_LEFT);
         }
         /*echo $this->n_ip."/".$this->netmask."<br/> ".$hex."<br/>";
         		echo $bin."<br/>";
         		echo $this->bin_to_ip6($bin)."<br/>";
         		exit;*/
     }
     if ($this->family == 4) {
         $this->bin_ip = $bin;
         //print "Address. ". $this->n_ip." -> ".chunk_split($bin,8,".")."<br/>";
         $this->long_ip = sprintf("%u", ip2long($this->n_ip));
         $bin = str_pad(str_pad("", $this->netmask, 1), $this->max_bits, 0);
         $this->netmask_ip = long2ip(bindec($bin));
         //print "NetMask. ". $this->netmask_ip." -> ".chunk_split($bin,8,".")."<br/>" ;
         $bin = str_pad(str_pad("", 32 - $this->netmask, 1), $this->max_bits, 0, STR_PAD_LEFT);
         $this->wildcard = long2ip(bindec($bin));
         $this->subnet_size = bindec($bin) + 1;
         //print "Wildcard. ". $this->wildcard." -> ".chunk_split($bin,8,".")."<br/>" ;
         $bin = str_pad(substr($this->bin_ip, 0, $this->netmask), $this->max_bits, 0, STR_PAD_RIGHT);
         $this->network = long2ip(bindec($bin));
         $this->bin_network = $bin;
         $this->long_network = bindec($bin);
         //print "Network. ". $this->network." -> ".chunk_split($bin,8,".")."<br/>" ;
         $bin = str_pad(substr($this->bin_ip, 0, $this->netmask), $this->max_bits, 1, STR_PAD_RIGHT);
         $this->broadcast = long2ip(bindec($bin));
         $this->bin_broadcast = $bin;
         //print "Broadcast. ". $this->broadcast." -> ".chunk_split($bin,8,".")."<br/>" ;
         if ($this->netmask == 32) {
             $this->hostmin = $this->network;
             $this->hostmax = $this->network;
             $this->hostPerNet = 1;
         } else {
             $bin = decbin(ip2long($this->network) + 1);
             $this->hostmin = long2ip(bindec($bin));
             //print "HostMin. ". $this->hostmin." -> ".chunk_split($bin,8,".")."<br/>" ;
             $bin = decbin(ip2long($this->broadcast) - 1);
             $this->hostmax = long2ip(bindec($bin));
             //print "HostMax. ". $this->hostmax." -> ".chunk_split($bin,8,".")."<br/>" ;
             $this->hostPerNet = bindec(str_pad("", $this->max_bits - $this->netmask, 1)) - 1;
             //print "Host/Net. ". (bindec(str_pad("",(32-$this->netmask),1))-1)."<br/><br/>";
         }
         if ($this->description == "") {
             $this->description = "IPV4 " . $this->n_ip;
         }
     } else {
         if ($this->family == 6) {
             $this->bin_ip = $bin;
             $this->long_ip = $this->inet_ptod($this->bin_to_ip6($bin));
             //print "Address. ". $this->n_ip." -> ".chunk_split($bin,16,".")."<br/>";
             $bin = str_pad(str_pad("", $this->netmask, 1), $this->max_bits, 0);
             $this->netmask_ip = $this->bin_to_ip6($bin);
             //print "NetMask. ". $this->netmask_ip." -> ".chunk_split($bin,16,".")."<br/>" ;
             $bin = str_pad(str_pad("", $this->max_bits - $this->netmask, 1), $this->max_bits, 0, STR_PAD_LEFT);
             $this->wildcard = $this->bin_to_ip6($bin);
             $this->subnet_size = $this->inet_ptod($this->bin_to_ip6($bin)) + 1;
             //print "Wildcard. ". $this->wildcard." -> ".chunk_split($bin,16,".")." ".$this->subnet_size."<br/>" ;
             $bin = str_pad(substr($this->bin_ip, 0, $this->netmask), $this->max_bits, 0, STR_PAD_RIGHT);
             $this->network = $this->bin_to_ip6($bin);
             $this->bin_network = $bin;
             $this->long_network = $this->inet_ptod($this->network);
             //print "Network. ". $this->network." -> ".chunk_split($bin,16,".")."<br/>" ;
             $bin = str_pad(substr($this->bin_ip, 0, $this->netmask), $this->max_bits, 1, STR_PAD_RIGHT);
             $this->broadcast = $this->bin_to_ip6($bin);
             $this->bin_broadcast = $bin;
             //print "Broadcast. ". $this->broadcast." -> ".chunk_split($bin,16,".")."<br/>" ;
             if ($this->netmask == 128) {
                 $this->hostmin = $this->network;
                 $this->hostmax = $this->network;
                 $this->hostPerNet = 1;
             } else {
                 $bin = $this->binary_add($this->ip6_to_bin($this->network), "1");
                 $this->hostmin = $this->bin_to_ip6($bin);
                 //print "HostMin. ". $this->hostmin." -> ".chunk_split($bin,16,".")."<br/>" ;
                 $bin = $this->ip6_to_bin($this->broadcast);
                 $this->hostmax = $this->bin_to_ip6($bin);
                 //print "HostMax. ". $this->hostmax." -> ".chunk_split($bin,8,".")."<br/>" ;
                 $this->hostPerNet = $this->inet_ptod($this->bin_to_ip6(str_pad("", $this->max_bits - $this->netmask, 1)));
                 //print "Host/Net. ". $this->hostPerNet."<br/><br/>";
             }
         } else {
             echo "INVALID";
         }
     }
 }