Bogus IP address from getaddrinfo & inet_ntop

I use getaddrinfo to find socket addresses for basic socket commands. However, recently the addresses that he returns to me are for fictitious IP addresses that I found using inet_ntop. I tried my code, as well as the one presented in the Beej Guide , and both of them give the same results. Here is 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 IP forms

16.2.x.y

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

+3
source share
1 answer

If you do not pass by

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

inet_ntop?

+6

Source: https://habr.com/ru/post/1726668/


All Articles