开发者

Bogus IP Address from getaddrinfo & inet_ntop

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?


Shouldn't you be passing

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

to inet_ntop?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜