views:

240

answers:

1

I've been using getaddrinfo for looking up socket addresses for basic socket commands. Recently, though, the addresses it returns to me are for bogus IP addresses, which I have found using inet_ntop. I've tried my code, as well as that provided in Beej's Guide, and they both produce the same results. Here's the code:

struct addrinfo hints, *info;
int status;

memset(&hints, 0, sizeof hints);

hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;

if(status = getaddrinfo(address, port, &hints, &info)) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
}

char ip4[INET_ADDRSTRLEN];
inet_ntop(AF_INET, info->ai_addr, ip4, INET_ADDRSTRLEN);

std::cout<<ip4<<std::endl;

No matter what address I use, it always gives me an IP of the form

16.2.x.y

where 256*x + y is equal to the port number. Has anyone ever seen this happen, or can anyone guess why it's giving me this?

+2  A: 

Shouldn't you be passing

((sockaddr_in const *)info->ai_addr)->sin_addr

to inet_ntop?

avakar
Xymostech