function initialize($sAddress, $iPrefix = 0) { $iRet = IP6_ERR_OK; // Address must be a string and prefix must be an integer if (is_string($sAddress) && is_numeric($iPrefix)) { // If no prefix was passed it should be in the address, if so split them up if ($iPrefix == 0) { $iPosition = strpos($sAddress, "/"); $sTemp = (int) substr($sAddress, $iPosition + 1); $sAddress = substr($sAddress, 0, $iPosition); if (is_numeric($sTemp)) { $iPrefix = $sTemp; } else { $iPrefix = 48; // No prefix was found, just default to 48 } } // Check that the address is valid if (isValidAddr($sAddress) == true) { // Check that the prefix is in bounds if ($iPrefix > 0 && $iPrefix <= 64) { // Reset the level and update the remaining prefix $this->iLevel = 0; $this->iRemainingPrefix = 64 - $iPrefix; // Allocate the base network $this->cSite = new CNetwork(new CAddress($sAddress), $iPrefix, $this->iLevel); if (!$this->tSite) { $iRet = IP6_ERR_OUTOFMEM; } } else { $iRet = IP6_ERR_BADPREFIX; } } else { $iRet = IP6_ERR_BADADDR; } } else { $iRet = IP6_ERR_BADARG; } return $iRet; }
function compressAddr($sAddress) { $vRet = IP6_ERR_OK; $cAddress = new CAddress(); // Address must be a string if (is_string($sAddress)) { // Check that the address is valid if (isValidAddr($sAddress)) { // Convert the address to a raw address $vRet = $cAddress . fromStr($sAddress); if ($vRet == IP6_ERR_OK) { // Convert the address back to a string and compress it $vRet = $cAddress->toStr(true); } } else { $vRet = IP6_ERR_BADADDR; } } else { $vRet = IP6_ERR_BADARG; } return $vRet; }
function fromStr($sAddress) { $iRet = IP6_ERR_OK; // Address must be a string if (is_string($sAddress)) { // Check if the address is valid $this->bValid = isValidAddr($sAddress); if ($this->bValid) { // Set all segments to 0 for ($i = 0; $i < 8; $i++) { $iSegments[$i] = 0; } // Check if there are omitted segments (compressed address) $iLength = strlen($sAddress); $cOmitted = strpos($sAddress, "::"); if ($cOmitted === false) { // Convert the 8 segments $sTemp = strtok($sAddress, ":"); for ($i = 0; $i < 8; $i++) { $this->iSegments[$i] = hexdec($sTemp); $sTemp = strtok(":"); } } else { // Find the start of the omitted group $iSegment = 0; $iStart = -1; for ($i = 0; $i < $iLength; $i++) { if ($sAddress[$i] == ':') { $iSegment++; if ($sAddress[$i + 1] == ':') { $iStart = $iSegment; break; } } } // Find the end of the omitted group $iSegment = 7; $iEnd = -1; for ($i = $iLength - 1; $i >= 0; $i--) { if ($sAddress[$i] == ':') { $iSegment--; if ($sAddress[$i - 1] == ':') { $iEnd = $iSegment; break; } } } // Convert the 8 segments but ignoring the omitted group $sTemp = strtok($sAddress, ":"); for ($i = 0; $i < 8; $i++) { if ($sTemp && ($i < $iStart || $i > $iEnd)) { $this->iSegments[$i] = hexdec($sTemp); $sTemp = strtok(":"); } } } } } else { $iRet = IP6_ERR_BADARG; } return $iRet; }