libIP2Proxy/IP2Proxy.c
// Parse IP address into binary address for lookup purpose
Parse IP address into binary address for lookup purpose
Parse IP address into binary address for lookup purpose
static ip_container IP2Proxy_parse_address(const char *ip)
{
	ip_container parsed;
	if (IP2Proxy_is_ipv4((char *) ip)) {
		parsed.version = 4;
		inet_pton(AF_INET, ip, &parsed.ipv4);
		parsed.ipv4 = htonl(parsed.ipv4);
	} else if (IP2Proxy_is_ipv6((char *) ip)) {
		inet_pton(AF_INET6, ip, &parsed.ipv6);
		if (parsed.ipv6.s6_addr[0] == 0 && parsed.ipv6.s6_addr[1] == 0 && parsed.ipv6.s6_addr[2] == 0 && parsed.ipv6.s6_addr[3] == 0 && parsed.ipv6.s6_addr[4] == 0 && parsed.ipv6.s6_addr[5] == 0 && parsed.ipv6.s6_addr[6] == 0 && parsed.ipv6.s6_addr[7] == 0 && parsed.ipv6.s6_addr[8] == 0 && parsed.ipv6.s6_addr[9] == 0 && parsed.ipv6.s6_addr[10] == 255 && parsed.ipv6.s6_addr[11] == 255) {
			parsed.version = 4;
			parsed.ipv4 = (parsed.ipv6.s6_addr[12] << 24) + (parsed.ipv6.s6_addr[13] << 16) + (parsed.ipv6.s6_addr[14] << 8) + parsed.ipv6.s6_addr[15];
		}
		else if (parsed.ipv6.s6_addr[0] == 32 && parsed.ipv6.s6_addr[1] == 2) {
			parsed.version = 4;
			parsed.ipv4 = (parsed.ipv6.s6_addr[2] << 24) + (parsed.ipv6.s6_addr[3] << 16) + (parsed.ipv6.s6_addr[4] << 8) + parsed.ipv6.s6_addr[5];
		}
		else if (parsed.ipv6.s6_addr[0] == 32 && parsed.ipv6.s6_addr[1] == 1 && parsed.ipv6.s6_addr[2] == 0 && parsed.ipv6.s6_addr[3] == 0) {
			parsed.version = 4;
			parsed.ipv4 = ~((parsed.ipv6.s6_addr[12] << 24) + (parsed.ipv6.s6_addr[13] << 16) + (parsed.ipv6.s6_addr[14] << 8) + parsed.ipv6.s6_addr[15]);
		}
		else {
			parsed.version = 6;
		}
	} else {
		parsed.version = -1;
	}
	return parsed;
}
static ip_container IP2Proxy_parse_address ( const char * ip ) { ip_container parsed ; if ( IP2Proxy_is_ipv4 ( ( char * ) ip ) ) { parsed . version = 4 ; inet_pton ( AF_INET , ip , & parsed . ipv4 ) ; parsed . ipv4 = htonl ( parsed . ipv4 ) ; } else if ( IP2Proxy_is_ipv6 ( ( char * ) ip ) ) { inet_pton ( AF_INET6 , ip , & parsed . ipv6 ) ; if ( parsed . ipv6 . s6_addr [ 0 ] == 0 && parsed . ipv6 . s6_addr [ 1 ] == 0 && parsed . ipv6 . s6_addr [ 2 ] == 0 && parsed . ipv6 . s6_addr [ 3 ] == 0 && parsed . ipv6 . s6_addr [ 4 ] == 0 && parsed . ipv6 . s6_addr [ 5 ] == 0 && parsed . ipv6 . s6_addr [ 6 ] == 0 && parsed . ipv6 . s6_addr [ 7 ] == 0 && parsed . ipv6 . s6_addr [ 8 ] == 0 && parsed . ipv6 . s6_addr [ 9 ] == 0 && parsed . ipv6 . s6_addr [ 10 ] == 255 && parsed . ipv6 . s6_addr [ 11 ] == 255 ) { parsed . version = 4 ; parsed . ipv4 = ( parsed . ipv6 . s6_addr [ 12 ] << 24 ) + ( parsed . ipv6 . s6_addr [ 13 ] << 16 ) + ( parsed . ipv6 . s6_addr [ 14 ] << 8 ) + parsed . ipv6 . s6_addr [ 15 ] ; } else if ( parsed . ipv6 . s6_addr [ 0 ] == 32 && parsed . ipv6 . s6_addr [ 1 ] == 2 ) { parsed . version = 4 ; parsed . ipv4 = ( parsed . ipv6 . s6_addr [ 2 ] << 24 ) + ( parsed . ipv6 . s6_addr [ 3 ] << 16 ) + ( parsed . ipv6 . s6_addr [ 4 ] << 8 ) + parsed . ipv6 . s6_addr [ 5 ] ; } else if ( parsed . ipv6 . s6_addr [ 0 ] == 32 && parsed . ipv6 . s6_addr [ 1 ] == 1 && parsed . ipv6 . s6_addr [ 2 ] == 0 && parsed . ipv6 . s6_addr [ 3 ] == 0 ) { parsed . version = 4 ; parsed . ipv4 = ~ ( ( parsed . ipv6 . s6_addr [ 12 ] << 24 ) + ( parsed . ipv6 . s6_addr [ 13 ] << 16 ) + ( parsed . ipv6 . s6_addr [ 14 ] << 8 ) + parsed . ipv6 . s6_addr [ 15 ] ) ; } else { parsed . version = 6 ; } } else { parsed . version = -1 ; } return parsed ; }
Parse IP address into binary address for lookup purpose
Parse IP address into binary address for lookup purpose
// Parse IPv4 address // Parse IPv6 address // IPv4 Address in IPv6 // 6to4 Address - 2002::/16 // Teredo Address - 2001:0::/32 // Common IPv6 Address // Invalid IP address