For C/C++, you're looking for functions in the gethostbyname()
family (see man gethostbyname
) and inet_ntoa
. The gethostbyname()
query DNS and return a list of IP addresses for the host name, which you could then print with inet_ntoa
.
Here's an example program that will lookup the IP addresses of the specified host name and print them out. Note: I've not put in any error checking, so be careful!
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char** argv)
{
struct hostent* host = gethostbyname(argv[1]);
int count = 0;
char** current_addr = host->h_addr_list;
while (*current_addr != NULL)
{
struct in_addr* addr = (struct in_addr*)(*current_addr);
printf("address[%d]: %s\n", count, inet_ntoa(*addr));
++current_addr;
++count;
}
}
An example from my Kubuntu 10.04 machine:
mcc@fatback:~/sandbox/c$ ./gethostbyaddr_ex www.yahoo.com
address[0]: 69.147.125.65
address[1]: 67.195.160.76