Go to main content

Oracle® Solaris 11.4 Programming Interfaces Guide

Exit Print View

Updated: November 2020

Standard Routines

This section describes the routines that you can use to locate and construct network addresses. Unless otherwise stated, interfaces presented in this section apply only to the Internet family.

Locating a service on a remote host requires many levels of mapping before the client and server communicate. A service has a name for human use. The service and host names must translate to network addresses. Finally, the network address must be usable to locate and route to the host. The specifics of the mappings can vary between network architectures.

Standard routines map host names to network addresses, network names to network numbers, protocol names to protocol numbers, and service names to port numbers. The standard routines also indicate the appropriate protocol to use in communicating with the server process. The file netdb.h must be included when using any of these routines.

Host and Service Names

The interfaces getaddrinfo(), getnameinfo(), gai_strerror(), and freeaddrinfo() provide a simplified way to translate between the names and addresses of a service on a host. These interfaces are more recent than the getipnodebyname(), gethostbyname(), and getservbyname() APIs. Both IPv6 and IPv4 addresses are handled transparently. For more information, see the getaddrinfo(3C), getnameinfo(3C), gai_strerror(3C), freeaddrinfo(3C), getipnodebyname(3C), gethostbyname(3C), and getservbyname(3C) man pages.

The getaddrinfo() routine returns the combined address and port number of the specified host and service names. Because the information returned by getaddrinfo() is dynamically allocated, the information must be freed by freeaddrinfo() to prevent memory leaks. getnameinfo() returns the host and services names associated with a specified address and port number. Call gai_strerror() to print error messages based on the EAI_xxx codes returned by getaddrinfo() and getnameinfo().

An example of using getaddrinfo() follows.

struct addrinfo        *res, *aip;
struct addrinfo        hints;
int                    error;

/* Get host address.  Any type of address will do. */
bzero(&hints, sizeof (hints));
hints.ai_flags = AI_ALL|AI_ADDRCONFIG;
hints.ai_socktype = SOCK_STREAM;

error = getaddrinfo(hostname, servicename, &hints, &res);
if (error != 0) {
    (void) fprintf(stderr, "getaddrinfo: %s for host %s service %s\n",
    gai_strerror(error), hostname, servicename);
    return (-1);

After processing the information returned by getaddrinfo() in the structure pointed to by res, the storage should be released by freeaddrinfo(res).

The getnameinfo() routine is particularly useful in identifying the cause of an error, as in the following example:

struct sockaddr_storage faddr;
int                     sock, new_sock, sock_opt;
socklen_t               faddrlen;
int                     error;
char                    hname[NI_MAXHOST];
char                    sname[NI_MAXSERV];
faddrlen = sizeof (faddr);
new_sock = accept(sock, (struct sockaddr *)&faddr, &faddrlen);
if (new_sock == -1) {
    if (errno != EINTR && errno != ECONNABORTED) {
error = getnameinfo((struct sockaddr *)&faddr, faddrlen, hname, 
    sizeof (hname), sname, sizeof (sname), 0);
if (error) {
    (void) fprintf(stderr, "getnameinfo: %s\n",
} else {
    (void) printf("Connection from %s/%s\n", hname, sname);

Host Names – hostent

An Internet host-name-to-address mapping is represented by the hostent structure as defined in gethostent(3C):

struct hostent {
    char  *h_name;            /* official name of host */
    char  **h_aliases;        /* alias list */
    int   h_addrtype;         /* hostaddrtype(e.g.,AF_INET6) */
    int   h_length;           /* length of address */
    char  **h_addr_list;      /* list of addrs, null terminated */
/*1st addr, net byte order*/
#define h_addr h_addr_list[0]

Maps an Internet host name to a hostent structure


Maps an Internet host address to a hostent structure


Frees the memory of a hostent structure


Maps an Internet host address to a string

The routines return a hostent structure that contains the name of the host, its aliases, the address type, and a NULL-terminated list of variable length addresses. The list of addresses is required because a host can have many addresses. The h_addr definition is for backward compatibility, and is the first address in the list of addresses in the hostent structure.

Network Names – netent

The routines to map network names to numbers and the reverse return a netent structure:

 * Assumes that a network number fits in 32 bits.
struct netent {
   char     *n_name;      /* official name of net */
   char     **n_aliases;  /* alias list */
   int      n_addrtype;   /* net address type */
   int      n_net;        /* net number, host byte order */

getnetbyname(3C), getnetbyaddr_r(3C), and getnetent(3C) are the network counterparts to the host routines previously described.

Protocol Names – protoent

The protoent structure defines the protocol-name mapping used with getprotobyname(), getprotobynumber(), and getprotoent() and are defined in getprotoent():

struct protoent {
    char    *p_name;       /* official protocol name */
    char    **p_aliases    /* alias list */
    int     p_proto;       /* protocol number */

For more information, see the getprotobyname(3C), getprotobynumber(3C), and getprotoent(3C) man pages.

Service Names – servent

An Internet family service resides at a specific, well-known port, and uses a particular protocol. A service-name-to-port-number mapping is described by the servent structure that is defined in getprotoent():

struct servent {
    char    *s_name;        /* official service name */
    char    **s_aliases;    /* alias list */
    int     s_port;         /* port number, network byte order */
    char    *s_proto;       /* protocol to use */

getservbyname(3C) maps service names and, optionally, a qualifying protocol to a servent structure. The following call returns the service specification of an SSH server that is using any protocol.

sp = getservbyname("ssh", (char *) 0);

The following call returns the SSH server that uses the TCP protocol.

sp = getservbyname("ssh", "tcp");

getservbyport() and getservent() are also provided. getservbyport() has an interface that is similar to the interface used by getservbyname(). You can specify an optional protocol name to qualify lookups For more information, see the getservbyname(3C), getservbyport(3C), and getservent(3C) man pages.

Other Socket Routines

Several other routines that simplify manipulating names and addresses are available. The following table summarizes the routines for manipulating variable-length byte strings and byte-swapping network addresses and values.

Table 9  Runtime Library Routines
Compares byte-strings; 0 if same, not 0 otherwise. For more information, see the memcmp(3C) man page.
Copies n bytes from s2 to s1. For more information, see the memcpy(3C) man page.
Sets n bytes to value starting at base. For more information, see the memset(3C) man page.
32-bit quantity from host into network byte order. For more information, see the htonl(3C) man page.
16-bit quantity from host into network byte order. For more information, see the htons(3C) man pages.
32-bit quantity from network into host byte order. For more information, see the ntohl(3C) man page.
16-bit quantity from network into host byte order. For more information, see the ntohs(3C) man page.

The byte-swapping routines are provided because the operating system expects addresses to be supplied in network order. On some architectures, the host byte ordering is different from network byte order, so programs must sometimes byte-swap values. Routines that return network addresses do so in network order. Byte-swapping problems occur only when interpreting network addresses. For example, the following code formats a TCP or UDP port:

printf("port number %d\n", ntohs(sp->s_port));

On systems that do not need these routines, the routines are defined as null macros.