Network Interface Guide

Name-to-Address Mapping

Name-to-address mapping lets an application obtain the address of a service on a specified host, independent of the transport used. Name-to-address mapping consists of the following functions:

netdir_getbyname(3NSL) Maps the host and service name to a set of addresses
netdir_getbyaddr(3NSL) Maps addresses into host and service names
netdir_free(3NSL) Frees structures allocated by the name-to-address translation routines
taddr2uaddr(3NSL) Translates an address and returns a transport-independent character representation of the address
uaddr2taddr(3NSL)The universal address is translated into a netbuf structure
netdir_options(3NSL) Interfaces to transport-specific capabilities (such as the broadcast address and reserved port facilities of TCP and UDP)

The first argument of each routine points to a netconfig(4) structure that describes a transport. The routine uses the array of directory-lookup library paths in the netconfig(4) structure to call each path until the translation succeeds.

The libraries are described in Table 4-2. The routines described in the section, "Using the Name-to-Address Mapping Routines", are defined in the netdir(3NSL) man page.

Note -

The following libraries no longer exist in the Solaris 2 environment:,, and For more information on this change, see the nsswitch.conf(4) man page and the NOTES section of the gethostbyname(3NSL) man page.

Table 4-2 Name-to-Address Libraries


Transport Family 




For networks of the protocol family inet, its name-to-address mapping is provided by the name service switch based on the entries for hosts and services in the file nsswitch.conf(4). For networks of other families, the "-" indicates a nonfunctional name-to-address mapping.


Contains the name-to-address mapping routines of any protocol that accepts strings as addresses, such as the loopback transports. Library

Name-to-address translation files for the library are created and maintained by the system administrator. The files are /etc/net/transport-name/hosts and /etc/net/transport-name/services. transport-name is the local name of the transport that accepts string addresses (specified in the network ID field of the /etc/netconfig file). For example, the host file for ticlts would be /etc/net/ticlts/hosts, and the service file for ticlts would be /etc/net/ticlts/services.

Even though most string addresses do not distinguish between host and service, separating the string into a host part and a service part is consistent with other transports. The /etc/net/transport-name/hosts file contains a text string that is assumed to be the host address, followed by the host name. For example:

joyluckaddr	joyluck
carpediemaddr	carpediem
thehopaddr	thehop
pongoaddr	pongo

For loopback transports, it makes no sense to list other hosts because the service cannot go outside the containing host.

The /etc/net/transport-name/services file contains service names followed by strings identifying the service address. For example:

rpcbind	rpc
listen	serve

The routines create the full-string address by concatenating the host address, a period (.), and the service address. For example, the address of the listen service on pongo is pongoaddr.serve.

When an application requests the address of a service on a particular host on a transport that uses this library, the host name must be in /etc/net/transport/hosts, and the service name must be in /etc/net/transport/services. If either is missing, the name-to-address translation fails.

Using the Name-to-Address Mapping Routines

This section is an overview of what routines are available to use. The routines return or convert the network names to their respective network addresses. Note that netdir_getbyname(3NSL), netdir_getbyaddr(3NSL), and taddr2uaddr(3NSL) return pointers to data that must be freed by calls to netdir_free(3NSL).

	int netdir_getbyname(struct netconfig *nconf, 
			struct nd_hostserv *service, struct nd_addrlist **addrs);

netdir_getbyname(3NSL) maps the host and service name specified in service to a set of addresses consistent with the transport identified in nconf. The nd_hostserv and nd_addrlist structures are defined in the netdir(3NSL) man page. A pointer to the addresses is returned in addrs.

To find all addresses of a host and service (on all available transports), call netdir_getbyname(3NSL) with each netconfig(4) structure returned by either getnetpath(3NSL) or getnetconfig(3NSL).

int netdir_getbyaddr(struct netconfig *nconf,
		struct nd_hostservlist **service, struct netbuf *netaddr);

netdir_getbyaddr(3NSL) maps addresses into host and service names. The function is called with an address in netaddr and returns a list of host-name and service-name pairs in service. The nd_hostservlist structure is defined in netdir(3NSL).

void netdir_free(void *ptr, int struct_type);

The netdir_free(3NSL) routine frees structures allocated by the name-to-address translation routines. The parameters can take the values shown in Table 4-3.

Table 4-3 netdir_free(3NSL) Routines




Pointer to an nd_hostserv structure


Pointer to an nd_hostservlist structure


Pointer to a netbuf structure


Pointer to an nd_addrlist structure

char *taddr2uaddr(struct netconfig *nconf, struct netbuf *addr);

taddr2uaddr(3NSL) translates the address pointed to by addr and returns a transport-independent character representation of the address ("universal address"). nconf specifies the transport for which the address is valid. The universal address can be freed by free(3C).

struct netbuf *uaddr2taddr(struct netconfig *nconf, char *uaddr);

The "universal address" pointed to by uaddr is translated into a netbuf structure; nconf specifies the transport for which the address is valid.

int netdir_options(struct netconfig *nconf,
		 int dt, int dt,
		 char *point_to_args);

netdir_options(3NSL) interfaces to transport-specific capabilities (such as the broadcast address and reserved port facilities of TCP and UDP). nconf specifies a transport. option specifies the transport-specific action to take. fd might or might not be used depending upon the value of option. The fourth argument points to operation-specific data.

Table 4-4 shows the values used for option:

Table 4-4 Values for netdir_options




Sets the transport for broadcast (if the transport supports broadcast) 


Lets the application bind to a reserved port (if allowed by the transport) 


Verifies that an address corresponds to a reserved port (if the transport supports reserved ports) 


Transforms a locally meaningful address into an address to which client hosts can connect 

netdir_perror(3NSL) displays the message stating why one of the name-to-address mapping routines failed on stderr.

void netdir_perror(char *s);

netdir_sperror(3NSL) returns a string containing the error message stating why one of the name-to-address mapping routines failed.

char *netdir_sperror(void);

Example 4-7 shows network selection and name-to-address mapping.

Example 4-7 Network Selection and Name-to-Address Mapping

#include <netconfig.h>
#include <netdir.h>
#include <sys/tiuser.h>

struct nd_hostserv nd_hostserv;   /* host and service information */
struct nd_addrlist *nd_addrlistp; /* addresses for the service */
struct netbuf *netbufp;           /* the address of the service */
struct netconfig *nconf;          /* transport information*/
int i;                            /* the number of addresses */
char *uaddr;                      /* service universal address */
void *handlep;                    /* a handle into network selection */
 * Set the host structure to reference the "date"
 * service on host "gandalf"
nd_hostserv.h_host = "gandalf";
nd_hostserv.h_serv = "date";
 * Initialize the network selection mechanism.
if ((handlep = setnetpath()) == (void *)NULL) {
 * Loop through the transport providers.
while ((nconf = getnetpath(handlep)) != (struct netconfig *)NULL)
    * Print out the information associated with the
    * transport provider described in the "netconfig"
    * structure.
   printf("Transport provider name: %s\n", nconf->nc_netid);
   printf("Transport protocol family: %s\n", nconf->nc_protofmly);
   printf("The transport device file: %s\n", nconf->nc_device);
   printf("Transport provider semantics: ");
	   switch (nconf->nc_semantics) {
   case NC_TPI_COTS:
      printf("virtual circuit\n");
   case NC_TPI_COTS_ORD:
      printf("virtual circuit with orderly release\n");

   case NC_TPI_CLTS:
    * Get the address for service "date" on the host
    * named "gandalf" over the transport provider
    * specified in the netconfig structure.
   if (netdir_getbyname(nconf, &nd_hostserv, &nd_addrlistp) != ND_OK) {
      printf("Cannot determine address for service\n");
   printf("<%d> addresses of date service on gandalf:\n",
    * Print out all addresses for service "date" on
    * host "gandalf" on current transport provider.
   netbufp = nd_addrlistp->n_addrs;
   for (i = 0; i < nd_addrlistp->n_cnt; i++, netbufp++) {
      uaddr = taddr2uaddr(nconf,netbufp);
   	netdir_free( nd_addrlistp, ND_ADDRLIST );