Example #1
0
 /**
  * @param string $address
  *
  * @throws RdataException
  */
 public function setAddress($address)
 {
     if (!Validator::validateIpv6Address($address)) {
         throw new RdataException(sprintf('Address "%s" is not a valid IPv6 address', $address));
     }
     $this->address = $address;
 }
Example #2
0
 /**
  * Expands an IPv6 address to its full, non-short hand representation.
  *
  * @param $ip
  * @throws \InvalidArgumentException
  *
  * @return string
  */
 public static function expandIpv6($ip)
 {
     if (!Validator::validateIpv6Address($ip)) {
         throw new \InvalidArgumentException(sprintf('"%s" is not a valid IPv6 address.', $ip));
     }
     $hex = unpack('H*hex', inet_pton($ip));
     $ip = substr(preg_replace('/([A-f0-9]{4})/', '$1:', $hex['hex']), 0, -1);
     return $ip;
 }
Example #3
0
 /**
  * Takes a valid IPv6 address and contracts it
  * to its shorter version.
  *
  * E.g.: 2001:0000:0000:acad:0000:0000:0000:0001 -> 2001:0:0:acad::1
  *
  * Note: If there is more than one set of consecutive hextets, the function
  * will favour the larger of the sets. If both sets of zeroes are the same
  * the second will be favoured in the omission of zeroes.
  *
  * E.g.: 2001:0000:0000:ab80:2390:0000:0000:000a -> 2001:0:0:ab80:2390::a
  *
  * @param string $ip IPv6 address
  * @throws \InvalidArgumentException
  *
  * @return string Contracted IPv6 address
  */
 public static function contractIpv6($ip)
 {
     if (!Validator::validateIpv6Address($ip)) {
         throw new \InvalidArgumentException(sprintf('"%s" is not a valid IPv6 address.', $ip));
     }
     $ip = self::expandIpv6($ip);
     $decimals = array_map('hexdec', explode(':', $ip));
     //Find the larget streak of zeroes
     $streak = $longestStreak = 0;
     $streak_i = $longestStreak_i = -1;
     foreach ($decimals as $i => $decimal) {
         if (0 !== $decimal) {
             $streak_i = -1;
             $streak = 0;
             continue;
         }
         $streak_i = $streak_i === -1 ? $i : $streak_i;
         $streak += 1;
         if ($streak >= $longestStreak) {
             $longestStreak = $streak;
             $longestStreak_i = $streak_i;
         }
     }
     $ip = '';
     foreach ($decimals as $i => $decimal) {
         if ($i > $longestStreak_i && $i < $longestStreak_i + $longestStreak) {
             continue;
         }
         if ($i === $longestStreak_i) {
             $ip .= '::';
             continue;
         }
         $ip .= (string) dechex($decimal);
         $ip .= $i < 7 ? ':' : '';
     }
     return preg_replace('/\\:{3}/', '::', $ip);
 }
Example #4
0
 public function testValidateIpv6Address()
 {
     $valid1 = '2001:0db8:0000:0000:0000:ff00:0042:8329';
     $valid2 = '2001:db8:0:0:0:ff00:42:8329';
     $valid3 = '2001:db8::ff00:42:8329';
     $valid4 = '::1';
     $invalid1 = 'fffff:0db8:0000:0000:0000:ff00:0042:8329';
     $invalid2 = '172.10.255.1';
     $invalid3 = '192.168.0.0';
     $this->assertTrue(Validator::validateIpv6Address($valid1));
     $this->assertTrue(Validator::validateIpv6Address($valid2));
     $this->assertTrue(Validator::validateIpv6Address($valid3));
     $this->assertTrue(Validator::validateIpv6Address($valid4));
     $this->assertFalse(Validator::validateIpv6Address($invalid1));
     $this->assertFalse(Validator::validateIpv6Address($invalid2));
     $this->assertFalse(Validator::validateIpv6Address($invalid3));
 }