Is IP address already taken in subnet?

@param int $longIP IPv4 address converted to long integer

@return bool

@throws Exception on error
171434
protected function isUsed($longIP) {
        foreach ($this->taken as $taken) {
            $takenIPLong = $this->convertIPv4Address($taken);

            if ($takenIPLong === $longIP) {
                return true;
            }
        }

        return false;
    }
110056
protected function IPLongAnd($long1, $long2)
    {
        $result = 0;
        for ($bit = 0; $bit < static::NB_BITS; $bit++) {
            $div = bcpow(2, $bit);
            $and = bcmod(bcdiv($long1, $div, 0), 2) && bcmod(bcdiv($long2, $div, 0), 2);
            $result = bcadd($result, bcmul($and, $div));
        }
        return $result;
    }
901271
protected function validateIp($ip)
    {
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_IPV6) === false) {
            return false;
        }

        return true;
    }
50636
public function getIp2Decimal($ip = null)
    {
        $ip = $ip ?: $this->getIp();

        if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
            return false;
        }

        $ipEntryData = explode('.', $ip);

        return ((int)$ipEntryData[3]) + ($ipEntryData[2] * 256) + ($ipEntryData[1] * 256 * 256) + ($ipEntryData[0] * 256 * 256 * 256);
    }
1311520
public function containsIP($ip): bool
    {
        if (!$ip instanceof IP) {
            $ip = IP::create($ip);
        }

        return ($ip->numeric() >= $this->getFirstIp()->numeric()) && ($ip->numeric() <= $this->getLastIp()->numeric());
    }
391716
def _is_ipv4(self, ip):
        
        try:
            p = IPy.IP(ip)
        except ValueError:
            return False

        if p.version() == 4:
            return True
        return False
48533
public static function isIpInCidr(string $ip, string $cidr): bool
    {
        if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
            throw new \UnexpectedValueException(
                "invalid value for ip; expecting an IPv4 address"
            );
        }

        list($range, $netmask) = static::parseCidr($cidr);
        $netmask = ~((1 << (32 - $netmask)) - 1);
        return ((ip2long($ip) & $netmask) === ip2long($range));
    }
1364614
public function verifyIp($ip) {
        $data = $this->ip();
        return $data === false || in_array($ip, $data);
    }
85085
private function isLoopback(): bool
    {
        if ($this->isIPv6) {
            return $this->compact()->address == '::1';
        }

        return (ip2long($this->address) & 0xff000000) == 0x7f000000;
    }
783449
protected static function expandNotationIpv6($Ip)
    {
        if (strpos($Ip, '::') !== false)
        {
            $Ip = str_replace('::', str_repeat(':0', 8 - substr_count($Ip, ':')).':', $Ip);
        }
        if (strpos($Ip, ':') === 0)
        {
            $Ip = '0'.$Ip;
        }
        return $Ip;
    }
67619
private function validateInputs($ip_address, $network_size)
    {
        if (!filter_var($ip_address, FILTER_VALIDATE_IP)) {
            throw new \UnexpectedValueException("IP address $ip_address not valid.");
        }
        if (($network_size < 1) || ($network_size > 32)) {
            throw new \UnexpectedValueException("Network size $network_size not valid.");
        }
    }
4934
public function isIPAddressInSubnet($ip_address_string)
    {
        $ip_address = ip2long($ip_address_string);
        list($start_ip, $end_ip) = $this->getIPAddressRangeAsInts();

        return $ip_address >= $start_ip && $ip_address <= $end_ip
            ? true
            : false;
    }
1193576
public static boolean isValidLocalIP(InetAddress ip) throws SocketException   {
        String ipStr = ip.getHostAddress();
        
        Iterator ips = getAllIPAddresses().iterator();
        while (ips.hasNext()) {
            InetAddress ip2 = (InetAddress) ips.next();
            if (ip2.getHostAddress().equals(ipStr)) {
                return true;
            }
        }
        
        return false;
    }
415040
public static function isPrivateIp(string $ip): bool
    {
        foreach (self::PRIVATE_IPV4_RANGES as $range) {
            if (self::isIpInCidr($ip, $range)) {
                return true;
            }
        }

        return false;
    }
1212429
def is_ip(address):
    
    try:
        # Test to see if already an IPv4/IPv6 address
        address = netaddr.IPAddress(address)
        return True
    except (netaddr.AddrFormatError, ValueError):
        return False
1073523
public static function validateIP( $v ) {
		// if integer then convert to string
		if( $ip = filter_var($v, FILTER_VALIDATE_INT) )
			$v = long2ip($ip);
		return filter_var($v, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
	}
1155760
public function banIP($banIP)
	{
		if (!$this->checkBanIP($banIP))
			return false;
		file_put_contents($this->banFile, $banIP."\n", FILE_APPEND);
		return true;
	}