Example #1
0
 /**
  * Return the network mask for the prefix given.
  *
  * Normally this is only useful for IPv4 addresses but you can generate a
  * mask for IPv6 addresses as well, only because its mathematically
  * possible.
  *
  * @param integer $prefix CIDR prefix bits (0-128)
  * @param integer $version IP version. If null the version will be detected
  *                         based on the prefix length given.
  */
 public static function prefix_to_mask($prefix, $version = null)
 {
     if ($version === null) {
         $version = $prefix > 32 ? 6 : 4;
     }
     if ($prefix < 0 or $prefix > 128) {
         throw new \InvalidArgumentException("Invalid prefix length \"{$prefix}\"");
     }
     if ($version != 4 and $version != 6) {
         throw new \InvalidArgumentException("Invalid version \"{$version}\". Must be 4 or 6");
     }
     if ($version == 4) {
         return long2ip($prefix == 0 ? 0 : 0xffffffff >> 32 - $prefix << 32 - $prefix);
     } else {
         return IP::inet_dtop($prefix == 0 ? 0 : BC::bcleft(BC::bcright(BC::MAX_UINT_128, 128 - $prefix), 128 - $prefix));
     }
 }