Parses an IP address.

@since 1.0.0

@param string $ipAddress The IP address.

@return IPAddressInterface|null The IPAddress instance if the $ipAddress parameter is a valid IP address, null otherwise.
348808
public static function tryParse(string $ipAddress): ?IPAddressInterface
    {
        if (!self::myParse($ipAddress, $octets)) {
            return null;
        }

        return new self($octets);
    }
597085
def _parse_ip(addr):
    

    # Parse the IP address
    try:
        address = netaddr.IPAddress(addr.strip())
    except (ValueError, netaddr.AddrFormatError):
        return None

    # Canonicalize it, if possible
    if address.version == 6:
        try:
            return address.ipv4()
        except netaddr.AddrConversionError:
            pass

    return address
1073447
public function getAddressType(AddressInterface $address)
    {
        $result = null;
        if ($this->range->contains($address)) {
            foreach ($this->exceptions as $exception) {
                $result = $exception->getAddressType($address);
                if ($result !== null) {
                    break;
                }
            }
            if ($result === null) {
                $result = $this->type;
            }
        }

        return $result;
    }
460303
public function ipToHex($ipAddress)
    {
        $hex = '';
        if (strpos($ipAddress, ',') !== false) {
            $splitIp = explode(',', $ipAddress);
            $ipAddress = trim($splitIp[0]);
        }
        $isIpV6 = false;
        $isIpV4 = false;
        if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
            $isIpV6 = true;
        } elseif (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
            $isIpV4 = true;
        }
        if (!$isIpV4 && !$isIpV6) {
            return false;
        }
        // IPv4 format
        if ($isIpV4) {
            $parts = explode('.', $ipAddress);
            for ($i = 0; $i < 4; $i++) {
                $parts[ $i ] = str_pad(dechex($parts[ $i ]), 2, '0', STR_PAD_LEFT);
            }
            $ipAddress = '::'.$parts[0].$parts[1].':'.$parts[2].$parts[3];
            $hex = implode('', $parts);
        } // IPv6 format
        else {
            $parts = explode(':', $ipAddress);
            // If this is mixed IPv6/IPv4, convert end to IPv6 value
            if (filter_var($parts[ count($parts) - 1 ], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
                $partsV4 = explode('.', $parts[ count($parts) - 1 ]);
                for ($i = 0; $i < 4; $i++) {
                    $partsV4[ $i ] = str_pad(dechex($partsV4[ $i ]), 2, '0', STR_PAD_LEFT);
                }
                $parts[ count($parts) - 1 ] = $partsV4[0].$partsV4[1];
                $parts[] = $partsV4[2].$partsV4[3];
            }
            $numMissing = 8 - count($parts);
            $expandedParts = [];
            $expansionDone = false;
            foreach ($parts as $part) {
                if (!$expansionDone && $part == '') {
                    for ($i = 0; $i <= $numMissing; $i++) {
                        $expandedParts[] = '0000';
                    }
                    $expansionDone = true;
                } else {
                    $expandedParts[] = $part;
                }
            }
            foreach ($expandedParts as &$part) {
                $part = str_pad($part, 4, '0', STR_PAD_LEFT);
            }
            $ipAddress = implode(':', $expandedParts);
            $hex = implode('', $expandedParts);
        }
        // Validate the final IP
        if (!filter_var($ipAddress, FILTER_VALIDATE_IP)) {
            return false;
        }

        return strtolower(str_pad($hex, 32, '0', STR_PAD_LEFT));
    }
456778
public function track($ipAddress)
    {
        if ($data = $this->getGeoIpDetector()->search($ipAddress)) {
            return $this->getModel()
                ->newQuery()
                ->firstOrCreate(Arr::only($data, ['latitude', 'longitude']), $data)
                ->getKey();
        }

        return null;
    }
1104879
protected static function checkIPAddressAgainstCIDRMask($ipAddress, $CIDRMask)
    {
        list( $mask, $CIDRSuffix ) = explode('/', $CIDRMask);

        return ( ip2long($ipAddress) & ~( ( 1 << ( 32 - $CIDRSuffix ) ) - 1 ) ) == ip2long($mask);
    }
811428
public static function ipAddress($ipAddress)
    {
        if (false === filter_var($ipAddress, FILTER_VALIDATE_IP)) {
            throw new InputValidationException('invalid "ip_address"');
        }

        // normalize the IP address (only makes a difference for IPv6)
        return inet_ntop(inet_pton($ipAddress));
    }
1023802
private boolean isAllowedAddress(String ipAddress) {
        if (allowedAddresses == null) {
            return false;
        }

        // IPv4
        int offset = ipAddress.lastIndexOf('.');
        if (offset == -1) {
            // IPv6
            offset = ipAddress.lastIndexOf(':');
            if (offset == -1) {
                return false;
            }
        }

        String ipAddressClass = ipAddress.substring(0, offset + 1) + '*';
        return (allowedAddresses.contains(ipAddressClass) || allowedAddresses.contains(ipAddress));
    }
305548
protected function getUserIp()
    {
        if (!isset($_SERVER) || $this->captureIP === false) {
            return null;
        }
        
        $ipAddress = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
        
        $forwardFor = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : null;
        if ($forwardFor) {
            // return everything until the first comma
            $parts = explode(',', $forwardFor);
            $ipAddress = $parts[0];
        }
        $realIp = isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : null;
        if ($realIp) {
            $ipAddress = $realIp;
        }
        
        if ($this->captureIP === DataBuilder::ANONYMIZE_IP) {
            if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
                $parts = explode('.', $ipAddress);
                $ipAddress = $parts[0] . '.' . $parts[1] . '.' . $parts[2] . '.0';
            } elseif (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
                $parts = explode(':', $ipAddress);
                $ipAddress =
                    $parts[0] . ':' .
                    $parts[1] . ':' .
                    $parts[2] . ':' .
                    '0000:0000:0000:0000:0000';
            }
        }
        
        return $ipAddress;
    }
1074561
public static function parse(string $address)
    {
        static $regex = null;
        if (null === $regex) {
            $grammar = Grammar::getInstance();

            $addrSpec = $grammar->getDefinition('addr-spec');
            $cfws = $grammar->getDefinition('CFWS');
            $phrase = $grammar->getDefinition('phrase');

            $regex = '/(?<address>'.$addrSpec.')|(?:(?<name>'.$phrase.')(?:'.$cfws.'?)<(?<addressalt>'.$addrSpec.')>(?:'.$cfws.'?))/';
        }

        if (! \preg_match($regex, $address, $match)) {
            return null;
        }

        $addr = isset($match['addressalt']) ? $match['addressalt'] : $match['address'];

        $name = $match['name'] ?? '';
        $name = \trim($name, " \t\n\r\0\x0B\"");

        return [
            'address' => $addr,
            'personal' => $name,
        ];
    }
97330
protected function parseAddress(array $addresses)
    {
        return array_map(function ($address) {
            return (object) [
                'address' => $address->mailbox.'@'.$address->host,
                'domain' => $address->host,
                'name' => isset($address->personal)
                    ? static::convertBodyEncoding($address->personal)
                    : null
            ];
        }, array_filter($addresses, function ($address) {
            return property_exists($address, 'mailbox') && strtolower($address->mailbox) !== 'undisclosed-recipients';
        }));
    }
642723
public static function validate($ipAddress)
    {
        if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4|FILTER_FLAG_NO_PRIV_RANGE|FILTER_FLAG_NO_RES_RANGE))
            return true;

        if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6|FILTER_FLAG_NO_PRIV_RANGE))
            return true;

        return true;
    }
1137326
final public function getFrom(): ?EmailAddress
    {
        $from = $this->getHeaders()->get('from');

        return null !== $from ? $this->decodeEmailAddress($from[0]) : null;
    }
792383
public function getRawIpAddress($convertToInteger = FALSE)
    {
        $ip_keys = Repository\DataRepository::getData('ip_address_server');
        foreach ($ip_keys as $key) {
            if (array_key_exists($key, $_SERVER) === TRUE) {
                foreach (explode(',', $_SERVER[$key]) as $ip) {
                    $ip = trim($ip);
                    if ($this->ipValidate($ip)) {
                        if ($convertToInteger === TRUE) {
                            $result = ip2long($ip);

                            return $result;
                        }

                        return $ip;
                    }
                }
            }
        }

        return FALSE;
    }
17124
protected function determineClientIpAddress($request)
    {
        $ipAddress = null;

        $serverParams = $request->getServerParams();
        if (isset($serverParams['REMOTE_ADDR']) && $this->isValidIpAddress($serverParams['REMOTE_ADDR'])) {
            $ipAddress = $serverParams['REMOTE_ADDR'];
        }

        $checkProxyHeaders = $this->checkProxyHeaders;
        if ($checkProxyHeaders && !empty($this->trustedProxies)) {
            if (!in_array($ipAddress, $this->trustedProxies)) {
                $checkProxyHeaders = false;
            }
        }

        if ($checkProxyHeaders) {
            foreach ($this->headersToInspect as $header) {
                if ($request->hasHeader($header)) {
                    $ip = trim(current(explode(',', $request->getHeaderLine($header))));
                    if ($this->isValidIpAddress($ip)) {
                        $ipAddress = $ip;
                        break;
                    }
                }
            }
        }

        return $ipAddress;
    }
638904
protected function _getIp()
    {
        if (!$this->_request()) {
            return null;
        }
        $ip = $this->_request()->getClientIp();

        if ($this->_isValidIpv4($ip)) {
            return $ip;
        }

        // sometimes the value is multiple IPs separated by commas
        // (when there are multiple proxies, for example)
        $ips = explode(',', $ip, 2);
        $ip = trim($ips[0]);
        if ($this->_isValidIpv4($ip)) {
            return $ip;
        }

        return null;
    }
902523
static public function fromAddress($address)
    {
        if ($address instanceof Address) {
            $addr = $address->toClean();
            $prefix = substr($addr, 0, 1);
        }
        elseif (is_string($address)) {
            $prefix = substr($address, 0, 1);
        }
        else {
            throw new \InvalidArgumentException("Could not identify address format: " . var_export($address, true));
        }

        foreach (self::$networkInfos as $name => $spec) {
            $netChar = $spec['char'];

            if ($prefix == $netChar)
                return $spec["id"];
        }

        throw new \InvalidArgumentException("Could not identify network from provided address: " . var_export($address, true));
    }