Esempio n. 1
0
 public function setNetwork($subnet, $netmask = null)
 {
     if ($netmask === null && strpos($subnet, '/') === false) {
         throw new Zend_Validate_Exception('If the netmask is not specified then the CIDR (e.g. /24) must be provided');
     }
     if ($netmask === null) {
         $cidr = substr($subnet, strpos($subnet, '/') + 1);
         $subnet = substr($subnet, 0, strpos($subnet, '/'));
         try {
             $ipVersion = App_Util_Ip::getIpVersion($subnet);
             if ($ipVersion == App_Util_Ip::v4) {
                 $netmask = App_Util_Ip::long2ipv4($cidr);
             } else {
                 $netmask = App_Util_Ip::long2ipv6($cidr);
             }
         } catch (\Application\Exceptions\InvalidArgumentException $e) {
             throw new Zend_Validate_Exception('Invalid CIDR specified');
         }
     } else {
         // Only for IPv6 we want to convert the prefix to IP, for other cases everything is fine
         if (App_Util_Ip::getIpVersion($subnet) == App_Util_Ip::v6) {
             $maskPrefix = explode("/", $netmask);
             if (count($maskPrefix) == 2) {
                 $netmask = $maskPrefix[1];
             }
             $netmask = App_Util_Ip::long2ipv6($netmask);
         }
     }
     // Final conversion to be able to make bytewise operations
     try {
         // this is kind of evil but it works fine for ipv4. For newer php
         // versions dtr_pton is not working as expected for some ips
         $this->_version = App_Util_Ip::getIpVersion($subnet);
         if ($this->_version == App_Util_Ip::v4) {
             $this->_subnet = ip2long($subnet);
             $this->_netmask = ip2long($netmask);
         } else {
             $this->_subnet = App_Util_Ip::dtr_pton($subnet);
             $this->_netmask = App_Util_Ip::dtr_pton($netmask);
         }
     } catch (Exception $e) {
         throw new Zend_Validate_Exception("Unexpected error. Incorrect address");
     }
 }
Esempio n. 2
0
 public function testLong2Ipv4()
 {
     $this->assertEquals("255.255.255.0", App_Util_Ip::long2ipv4(24));
 }