Check for a fake IP not working correctly with IPv6
-
The function _is_fake_ip generated some false positives. I did some fixes and it’s working now. Maybe it needs some further workarounds for old php versions to keep it working there.
/** * Check for a fake IP * * @since 2.0 * @change 2.6.2 * * @param string $ip Client IP * @param string $host Client Host [optional] * @return boolean TRUE if fake IP */ private static function _is_fake_ip($client_ip, $client_host = false) { /* Remote Host */ $host_by_ip = gethostbyaddr($client_ip); /* IPv6 special */ if(self::_is_ipv6($client_ip)) { if(self::_is_ipv6($host_by_ip) && inet_pton($client_ip) === inet_pton($host_by_ip)) { // no domain return false; } else { // has domain $record = dns_get_record($host_by_ip,DNS_AAAA); if(empty($record) || empty($record[0]['ipv6'])) { // no reverse entry return true; } else { return inet_pton($client_ip) !== inet_pton($record[0]['ipv6']); } } } /* IPv4 / Comment */ if ( empty($client_host) ) { $ip_by_host = gethostbyname($host_by_ip); if ( $ip_by_host === $host_by_ip ) { return false; } /* IPv4 / Trackback */ } else { if ( $host_by_ip === $client_ip ) { return true; } $ip_by_host = gethostbyname($client_host); } if ( strpos( $client_ip, self::_cut_ip($ip_by_host) ) === false ) { return true; } return false; } /** * Check for an IPv4 address * * @since 2.4 * @change 2.6.2 * * @param string $ip IP to validate * @return integer TRUE if IPv4 */ private static function _is_ipv4($ip) { //return preg_match('/^\d{1,3}(\.\d{1,3}){3,3}$/', $ip); return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false ; } /** * Check for an IPv6 address * * @since 2.6.2 * @change 2.6.2 * * @param string $ip IP to validate * @return boolean TRUE if IPv6 */ private static function _is_ipv6($ip) { //return ! self::_is_ipv4($ip); return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false ; }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Check for a fake IP not working correctly with IPv6’ is closed to new replies.