getaddrinfo() function returns the wrong IP address
11,101
google.com can resolve to different IP addresses depending on your own location. It's kind of load balancing trick.
Author by
Ashwin
Updated on June 23, 2022Comments
-
Ashwin about 2 years
I am trying to resolve a URL's IP address using getaddrinfo(), but it always returns the wrong IP address, I have tried with several URL's and the result is same. any help wold be greatly appreciated.
The program returnes the ip address : 209.85.175.99 insted of returning the real IP which is 74.125.39.147
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> int main() { char *hostname = "www.google.lk"; struct addrinfo hints, *res; struct in_addr addr; int err; memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; hints.ai_family = AF_INET; if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0) { printf("error %d\n", err); return 1; } addr.s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr; printf("ip address : %s\n", inet_ntoa(addr)); freeaddrinfo(res); return 0; }