Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Converting host names into network addresses and vice versa

&pagelevel(3)&pagelevel

There are special socket functions for converting host names to network addresses and vice versa in the AF_INET and AF_INET6 address families.

Socket functions for converting addresses in the AF_INET and AF_INET6 address families

The getaddrinfo() function returns address and port information on the host name and the service name specified in the call.

The getnameinfo() function returns the host name and service name of the IP address and port number specified in the call.

The getipnodebyname() function converts a host name to an IPv4 or IPv6 address.

The getipnodebyaddr() function converts an IPv4 or IPv6 address to a host name.

The inet_ntop() function converts an Internet host name to a character string. This character string is returned as follows:

  • in hexadecimal colon notation for AF_INET6

  • in decimal dotted notation for AF_INET

The inet_pton() function converts an Internet host address in printable representation

  • from a character string in decimal dotted notation to a binary IPv4 address (AF_INET).

  • from a character string in hexadecimal colon notation to a binary IPv6 address (AF_INET6).

Socket functions address conversion which are only supported in AF_INET

The gethostbyname() function converts a host name to an IPv4 address.

The gethostbyaddr() function converts an IPv4 address to a host name.

Functions gethostbyname() and gethostbyaddr() return a pointer to an object of data type struct hostent as their result.

The hostent structure is declared in <netdb.h> as follows:

struct hostent {
     char *h_name;          /* official host name */
     char **h_aliases;      /* alias list */
     int  h_addrtype;       /* address type */
     int  h_length;         /* length of the address (in bytes) */
     char **h_addr_list;    /* list of addresses for the host, */
                            /* terminated with the null pointer*/
};
#define h_addr h_addr_list[0]  /* first address, network byte order */

The hostent object returned by gethostbyname() and gethostbyaddr() always contains the following information:

  • the name of the host

  • a list of the host aliases

  • address type (domain)

  • a list of IPv4 addresses, terminated with the null pointer

The address list is required because a computer may have multiple addresses which are all assigned to the same host name.

The inet_ntoa() function converts an IPv4 host address to a character string in accordance with the normal Internet dotted notation.