Exemplo n.º 1
0
/**
 * ipCalc calculations
 */
function calculateIpCalcResult($cidr)
{
    /* first verify address type */
    $type = IdentifyAddress($cidr);
    /* IPv4 */
    if ($type == "IPv4") {
        $net = Net_IPv4::parseAddress($cidr);
        //set ip address type
        $out['Type'] = 'IPv4';
        //calculate network details
        $out['IP address'] = $net->ip;
        // 192.168.0.50
        $out['Network'] = $net->network;
        // 192.168.0.0
        $out['Broadcast'] = $net->broadcast;
        // 192.168.255.255
        $out['Subnet bitmask'] = $net->bitmask;
        // 16
        $out['Subnet netmask'] = $net->netmask;
        // 255.255.0.0
        $out['Subnet wildcard'] = long2ip(~ip2long($net->netmask));
        //0.0.255.255
        //calculate min/max IP address
        $out['Min host IP'] = long2ip(ip2long($out['Network']) + 1);
        $out['Max host IP'] = long2ip(ip2long($out['Broadcast']) - 1);
        $out['Number of hosts'] = ip2long($out['Broadcast']) - ip2long($out['Min host IP']);
        //subnet class
        $out['Subnet Class'] = checkIpv4AddressType($out['Network'], $out['Broadcast']);
        //if IP == subnet clear the Host fields
        if ($out['IP address'] == $out['Network']) {
            $out['IP address'] = "/";
        }
    } else {
        //set ip address type
        $out['Type'] = 'IPv6';
        //calculate network details
        /*         $out['Host address']              = Net_IPv6::removeNetmaskSpec ( $cidr );  */
        $out['Host address'] = $cidr;
        $out['Host address'] = Net_IPv6::compress($out['Host address'], 1);
        $out['Host address (uncompressed)'] = Net_IPv6::uncompress($out['Host address']);
        $mask = Net_IPv6::getNetmaskSpec($cidr);
        $subnet = Net_IPv6::getNetmask($cidr);
        $out['Subnet prefix'] = Net_IPv6::compress($subnet) . '/' . $mask;
        $out['Prefix length'] = Net_IPv6::getNetmaskSpec($cidr);
        // get reverse DNS entries
        $out['Host Reverse DNS'] = calculateReverseDNS6($out['Host address (uncompressed)']);
        $out['Subnet Reverse DNS'] = calculateReverseDNS6($subnet, $mask);
        //if IP == subnet clear the Host fields and Host Reverse DNS
        if ($out['Host address'] == $out['Subnet prefix']) {
            $out['Host address'] = '/';
            $out['Host address (uncompressed)'] = '/';
            unset($out['Host Reverse DNS']);
        }
        //min / max hosts
        $maxIp = gmp_strval(gmp_add(gmp_sub(gmp_pow(2, 128 - $mask), 1), ip2long6($subnet)));
        $out['Min host IP'] = long2ip6(gmp_strval(gmp_add(ip2long6($subnet), 1)));
        $out['Max host IP'] = long2ip6($maxIp);
        $out['Number of hosts'] = MaxHosts($mask, 1);
        //address type
        $out['Address type'] = Net_IPv6::getAddressType($cidr);
        $out['Address type'] = checkIpv6AddressType($out['Address type']);
    }
    /* return results */
    return $out;
}
Exemplo n.º 2
0
 /**
  * Compresses an IPv6 adress
  *
  * RFC 2373 allows you to compress zeros in an adress to '::'. This
  * function expects an valid IPv6 adress and compresses successive zeros
  * to '::'
  *
  * Example:  FF01:0:0:0:0:0:0:101   -> FF01::101
  *           0:0:0:0:0:0:0:1        -> ::1
  *
  * Whe $ip is an already compressed adress the methode returns the value as is,
  * also if the adress can be compressed further.
  *
  * Example: FF01::0:1 -> FF01::0:1
  *
  * To enforce maximum compression, you can set the second argument $force to true.
  *
  * Example: FF01::0:1 -> FF01::1
  *
  * @param String  $ip    a valid IPv6-adress (hex format)
  * @param boolean $force if true the adress will be compresses as best as possible (since 1.2.0)
  *
  * @return tring the compressed IPv6-adress (hex format)
  * @access public
  * @see    Uncompress()
  * @static
  * @author elfrink at introweb dot nl
  */
 public static function compress($ip, $force = false)
 {
     if (false !== strpos($ip, '::')) {
         // its already compressed
         if (true == $force) {
             $ip = Net_IPv6::uncompress($ip);
         } else {
             return $ip;
         }
     }
     $prefix = Net_IPv6::getPrefixLength($ip);
     if (false === $prefix) {
         $prefix = '';
     } else {
         $ip = Net_IPv6::removePrefixLength($ip);
         $prefix = '/' . $prefix;
     }
     $netmask = Net_IPv6::getNetmaskSpec($ip);
     $ip = Net_IPv6::removeNetmaskSpec($ip);
     $ipp = explode(':', $ip);
     for ($i = 0; $i < count($ipp); $i++) {
         $ipp[$i] = dechex(hexdec($ipp[$i]));
     }
     $cip = ':' . join(':', $ipp) . ':';
     preg_match_all("/(:0)(:0)+/", $cip, $zeros);
     if (count($zeros[0]) > 0) {
         $match = '';
         foreach ($zeros[0] as $zero) {
             if (strlen($zero) > strlen($match)) {
                 $match = $zero;
             }
         }
         $cip = preg_replace('/' . $match . '/', ':', $cip, 1);
     }
     $cip = preg_replace('/((^:)|(:$))/', '', $cip);
     $cip = preg_replace('/((^:)|(:$))/', '::', $cip);
     if ('' != $netmask) {
         $cip = $cip . '/' . $netmask;
     }
     return $cip . $prefix;
 }