/** * convert an IP address into a hex value * * @param string $IP * @return string */ protected function convertIpToHex($host) { if (!isset($host) || empty($host) || !is_string($host)) { static::raiseError(__METHOD__ . '(), $host parameter is invalid!'); return false; } global $ms; $ipv4 = new Net_IPv4(); $parsed = $ipv4->parseAddress($host); // if CIDR contains no netmask or was unparsable, we assume /32 if (empty($parsed->netmask)) { $parsed->netmask = "255.255.255.255"; } if (!$ipv4->validateIP($parsed->ip)) { $ms->throwError(_("Incorrect IP address! Can not convert it to hex!")); } if (!$ipv4->validateNetmask($parsed->netmask)) { $ms->throwError(_("Incorrect Netmask! Can not convert it to hex!")); } if (($hex_host = $ipv4->atoh($parsed->ip)) == false) { $ms->throwError(_("Failed to convert " . $parsed->ip . " to hex!")); } if (($hex_subnet = $ipv4->atoh($parsed->netmask)) == false) { $ms->throwError(_("Failed to convert " . $parsed->netmask . " to hex!")); } return array('ip' => $hex_host, 'netmask' => $hex_subnet); }
/** * Parse a formatted IP address * * Given a network qualified IP address, attempt to parse out the parts * and calculate qualities of the address. * * The following formats are possible: * * [dot quad ip]/[ bitmask ] * [dot quad ip]/[ dot quad netmask ] * [dot quad ip]/[ hex string netmask ] * * The first would be [IP Address]/[BitMask]: * 192.168.0.0/16 * * The second would be [IP Address] [Subnet Mask in quad dot notation]: * 192.168.0.0/255.255.0.0 * * The third would be [IP Address] [Subnet Mask as Hex string] * 192.168.0.0/ffff0000 * * Usage: * * $cidr = '192.168.0.50/16'; * $net = Net_IPv4::parseAddress($cidr); * echo $net->network; // 192.168.0.0 * echo $net->ip; // 192.168.0.50 * echo $net->broadcast; // 192.168.255.255 * echo $net->bitmask; // 16 * echo $net->long; // 3232235520 (long/double version of 192.168.0.50) * echo $net->netmask; // 255.255.0.0 * * @param string $ip IP address netmask combination * @return object true if syntax is valid, otherwise false */ function parseAddress($address) { $myself = new Net_IPv4(); if (strchr($address, "/")) { $parts = explode("/", $address); if (!$myself->validateIP($parts[0])) { return PEAR::raiseError("invalid IP address"); } $myself->ip = $parts[0]; // Check the style of netmask that was entered /* * a hexadecimal string was entered */ if (eregi("^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})\$", $parts[1], $regs)) { // hexadecimal string $myself->netmask = hexdec($regs[1]) . "." . hexdec($regs[2]) . "." . hexdec($regs[3]) . "." . hexdec($regs[4]); /* * a standard dot quad netmask was entered. */ } else { if (strchr($parts[1], ".")) { if (!$myself->validateNetmask($parts[1])) { return PEAR::raiseError("invalid netmask value"); } $myself->netmask = $parts[1]; /* * a CIDR bitmask type was entered */ } else { if ($parts[1] >= 0 && $parts[1] <= 32) { // bitmask was entered $myself->bitmask = $parts[1]; /* * Some unknown format of netmask was entered */ } else { return PEAR::raiseError("invalid netmask value"); } } } $myself->calculate(); return $myself; } else { if ($myself->validateIP($address)) { $myself->ip = $address; return $myself; } else { return PEAR::raiseError("invalid IP address"); } } }
/** * verify ip address /mask 10.10.10.10./24 - CIDR * * if subnet == 0 we dont check if IP is subnet -> needed for ipCalc */ function verifyCidr($cidr, $issubnet = 1) { /* split it to network and subnet */ $temp = explode("/", $cidr); $network = $temp[0]; $netmask = $temp[1]; //if one part is missing die if (empty($network) || empty($netmask)) { $errors[] = _("Invalid CIDR format!"); } /* Identify address type */ $type = IdentifyAddress($network); /* IPv4 verification */ if ($type == 'IPv4') { require_once 'PEAR/Net/IPv4.php'; $Net_IPv4 = new Net_IPv4(); if ($net = $Net_IPv4->parseAddress($cidr)) { //validate IP if (!$Net_IPv4->validateIP($net->ip)) { $errors[] = _("Invalid IP address!"); } elseif ($net->network != $net->ip && $issubnet == 1) { $errors[] = _("IP address cannot be subnet! (Consider using") . " " . $net->network . ")"; } elseif (!$Net_IPv4->validateNetmask($net->netmask)) { $errors[] = _('Invalid netmask') . ' ' . $net->netmask; } } else { $errors[] = _('Invalid CIDR format!'); } } else { require_once 'PEAR/Net/IPv6.php'; $Net_IPv6 = new Net_IPv6(); //validate IPv6 if (!$Net_IPv6->checkIPv6($cidr)) { $errors[] = _("Invalid IPv6 address!"); } else { //validate subnet $subnet = $Net_IPv6->getNetmask($cidr); $subnet = $Net_IPv6->compress($subnet); //get subnet part $subnetParse = explode("/", $cidr); $subnetMask = $subnetParse[1]; $subnetNet = $subnetParse[0]; if ($subnetNet != $subnet && $issubnet == 1) { $errors[] = _("IP address cannot be subnet! (Consider using") . " " . $subnet . "/" . $subnetMask . ")"; } } } /* return array of errors */ return $errors; }