<< Prev | Beej's Guide to Network Programming | Next >> |
Get an IP address for a hostname, or vice-versa
#include <sys/socket.h> #include <netdb.h> struct hostent *gethostbyname(const char *name); // DEPRECATED! struct hostent *gethostbyaddr(const char *addr, int len, int type);
PLEASE NOTE: these two functions are superseded by getaddrinfo() and getnameinfo()! In particular, gethostbyname() doesn't work well with IPv6.
These functions map back and forth between host names and IP
addresses. For instance, if you have "www.example.com", you can use
gethostbyname() to get its IP address and store it in a
Conversely, if you have a
(If you have a string containing an IP address in dots-and-numbers format that you want to look up the hostname of, you'd be better off using getaddrinfo() with the AI_CANONNAME flag.)
gethostbyname() takes a string like "www.yahoo.com", and
returns a
gethostbyaddr() takes a
So what is this
char *h_name |
The real canonical host name. |
char **h_aliases |
A list of aliases that can be accessed with arrays—the last element is NULL |
int h_addrtype |
The result's address type, which really should be AF_INET for our purposes. |
int length |
The length of the addresses in bytes, which is 4 for IP (version 4) addresses. |
char **h_addr_list |
A list of IP addresses for this host. Although this
is a |
h_addr |
A commonly defined alias for h_addr_list[0]. If you just want any old IP address for this host (yeah, they can have more than one) just use this field. |
Returns a pointer to a resultant
Instead of the normal perror() and all that stuff you'd normally use for error reporting, these functions have parallel results in the variable h_errno, which can be printed using the functions herror() or hstrerror(). These work just like the classic errno, perror(), and strerror() functions you're used to.
// THIS IS A DEPRECATED METHOD OF GETTING HOST NAMES // use getaddrinfo() instead! #include <stdio.h> #include <errno.h> #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int main(int argc, char *argv[]) { int i; struct hostent *he; struct in_addr **addr_list; if (argc != 2) { fprintf(stderr,"usage: ghbn hostname\n"); return 1; } if ((he = gethostbyname(argv[1])) == NULL) { // get the host info herror("gethostbyname"); return 2; } // print information about this host: printf("Official name is: %s\n", he->h_name); printf(" IP addresses: "); addr_list = (struct in_addr **)he->h_addr_list; for(i = 0; addr_list[i] != NULL; i++) { printf("%s ", inet_ntoa(*addr_list[i])); } printf("\n"); return 0; }
// THIS HAS BEEN SUPERCEDED // use getnameinfo() instead! struct hostent *he; struct in_addr ipv4addr; struct in6_addr ipv6addr; inet_pton(AF_INET, "192.0.2.34", &ipv4addr); he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET); printf("Host name: %s\n", he->h_name); inet_pton(AF_INET6, "2001:db8:63b3:1::beef", &ipv6addr); he = gethostbyaddr(&ipv6addr, sizeof ipv6addr, AF_INET6); printf("Host name: %s\n", he->h_name);
getaddrinfo(),
getnameinfo(),
gethostname(),
errno,
perror(),
strerror(),
<< Prev | Beej's Guide to Network Programming | Next >> |