I am trying to implement a function called "inet_pton" which will convert a string representation of an IPv4 or IPv6 (like "66.102.1.147" [google]) into binary network-byte ordered form. Here is the relevant part of my code:
#if defined WIN32
int inet_pton (int af, const char *src, void *dst)
{
const void *data;
size_t len;
struct addrinfo hints, *res;
hints.ai_family = af;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_NUMERICHOST;
if (getaddrinfo (src, NULL, &hints, &res))
{
std::cout << "ERROR : inet_pton() in " << __FILE__ << " at line " << __LINE__ << std::endl;
std::cout << " : getaddrinfo() failed to get IP address info for \"" << src << "\"" << std::endl;
return 0;
}
...
So src is the incoming IP string. However, I always get an error like
getaddrinfo() failed to get IP address info for "66.102.1.147"
Can anyone with winsock experience comment? I also tried another method, the function
WSAStringToAddress ((LPTSTR)src, af, NULL, (LPSOCKADDR) &sa, &address_length)
But it always returns the error code WSAEINVAL, indicating an invalid IP string. This makes no sense to me. I'm using VS2005 as my IDE.