I am trying to resolve a URL using getaddrinfo (), but it always returns the wrong IP, I tried with multiple URLs and the result is the same. Any help would be appreciated.
The program returns an IP address: 209.85.175.99 insted to return a real IP address, 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; }
source share