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 to 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 getipnodebyname() function converts a host name to an IPv4 or IPv6 address. A host name is passed when getipnodebyname() is called.

The getipnodebyaddr() function converts an IPv4 or IPv6 address to a host name. An IPv4 or IPv6 address is passed when getipnodebyaddr() is called.

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).

Abbreviated notation using two consecutive colons “::” is not supported for AF_INET6.

Socket functions address conversion which are only supported in AF_INET

The gethostbyname() function converts a host name to an IPv4 address. A host name is passed when gethostbyname() is called.

The gethostbyaddr() function converts an IPv4 address to a host name. An IPv4 address is passed when gethostbyaddr() is called.

gethostbyname() and gethostbyaddr(), as well as getipnodebyname() and getipnodebyaddr(), 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() and by getipnodebyname() and getipnodebyaddr() always contains the following information, if this is made available by the database:

  • the official name of the host

  • a list of the host aliases

  • address type (domain)

  • a list of addresses of variable length, terminated with the null pointer

The address list is required because a host normally has several addresses which are all assigned to the same host name. h_addr ensures backward compatibility and is defined as the first address in the address list of the hostent structure.

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