Go to main content

Oracle® Solaris 11.3 Programming Interfaces Guide

Exit Print View

Updated: April 2019
 
 

Name-to-Address Mapping

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

netdir_getbyname()

Maps the host and service name to a set of addresses. For more information, see the netdir_getbyname(3NSL) man page.

netdir_getbyaddr()

Maps addresses into host and service names. For more information, see the netdir_getbyaddr(3NSL) man page.

netdir_free()

Frees structures allocated by the name-to-address translation routines. For more information, see the netdir_free(3NSL) man page.

taddr2uaddr()

Translates an address and returns a transport-independent character representation of the address. For more information, see the taddr2uaddr(3NSL) man page.

uaddr2taddr()

The universal address is translated into a netbuf structure. For more information, see the uaddr2taddr(3NSL) man page.

netdir_options()

Interfaces to transport-specific capabilities such as the broadcast address and reserved port facilities of TCP and UDP. For more information, see the netdir_options(3NSL) man page.

netdir_perror()

Displays a message stating why one of the routines that map name-to-address failed on stderr. For more information, see the netdir_perror(3NSL) man page.

netdir_sperror()

Returns a string containing the error message stating why one of the routines that map name-to-address failed. For more information, see the netdir_sperror(3NSL) man page.

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

The name-to-address libraries are described in Figure 19, Table 19, Name-to-Address Libraries. The routines that are described in Using the Name-to-Address Mapping Routines are defined in the netdir(3NSL) man page.


Note -  The following libraries no longer exist in the Oracle Solaris environment: tcpip.so, switch.so, and nis.so. For more information about this change, see the nsswitch.conf(4) man page and the NOTES section of the gethostbyname(3NSL) man page.
Table 19  Name-to-Address Libraries
Library
Transport Family
Description
-
inet
The name-to-address mapping for networks of the protocol family inet 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 dash indicates a nonfunctional name-to-address mapping.

straddr.so Library

Name-to-address translation files for the straddr.so library are created by the system administrator. The system administrator also maintains these translation files. The straddr.so 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, which is 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.

Most string addresses do not distinguish between host and service. However, 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:

joyluckaddr

joyluck

carpediemaddr

carpediem

thehopaddr

thehop

pongoaddr

pongo

The /etc/net/transport-name/services file contains service names followed by strings that identify the service address:

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. The service name must be in /etc/net/transport/services. If either name is missing, the name-to-address translation fails.

Using the Name-to-Address Mapping Routines

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

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

netdir_getbyname() maps the host and service name specified in service to a set of addresses that are consistent with the transport identified in config. For more information about the nd_hostserv and nd_addrlist structures, see 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() with each netconfig structure returned by either getnetpath() or getnetconfig(). For more information, see the netdir_getbyname(3NSL), netconfig(4), getnetpath(3NSL), and getnetconfig(3NSL) man pages.

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

netdir_getbyaddr() maps addresses into host and service names. The interface is called with an address in netaddr and returns a list of host-name and service-name pairs in service. For information about the nd_hostservlist structure, see the netdir(3NSL) man page.

void netdir_free(void *ptr, int struct_type);

The netdir_free() routine frees structures allocated by the name-to-address translation routines. For more information, see the netdir_free(3NSL) man page. The parameters can take the values that are shown in the following table.

Table 20  netdir_free() Routines
struct_type
ptr
–ND_HOSTSERV
Pointer to an nd_hostserv structure
–ND_HOSTSERVLIST
Pointer to an nd_hostservlist structure
–ND_ADDR
Pointer to a netbuf structure
–ND_ADDRLIST
Pointer to an nd_addrlist structure
char *taddr2uaddr(struct netconfig *config, struct netbuf *addr);

taddr2uaddr() translates the address pointed to by addr and returns a transport-independent character representation of the address. This character representation is called a universal address. The value that is given in config specifies the transport for which the address is valid. The universal address can be freed by free(). For more information, see the taddr2uaddr(3NSL) and free(3C) man pages.

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

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

int netdir_options(struct netconfig *config, int option, int fildes,
            char *point_to_args);

netdir_options() provides interfaces to transport-specific capabilities, such as the broadcast address and reserved port facilities of TCP and UDP. The value of config specifies a transport, while option specifies the transport-specific action to take. The value in option might disable consideration of the value in fd. The fourth argument points to operation-specific data. For more information, see the netdir_options(3NSL) man page.

The following table shows the values used for option.

Table 21  Values for netdir_options()
Option
Description
–ND_SET_BROADCAST
Sets the transport for broadcast if the transport supports broadcast
–ND_SET_RESERVEDPORT
Enables application binding to reserved ports if allowed by the transport
–ND_CHECK_RESERVEDPORT
Verifies that an address corresponds to a reserved port if the transport supports reserved ports
–ND_MERGEADDR
Transforms a locally meaningful address into an address to which client hosts can connect

The netdir_perror() routine displays a message stating why one of the routines that map name-to-address failed on stderr. For more information, see the netdir_perror(3NSL) man page.

void netdir_perror(char *s);

The netdir_sperror() routine returns a string containing the error message stating why one of the routines that map name-to-address failed. For more information, see the netdir_sperror(3NSL) man page.

char *netdir_sperror(void);

The following example shows network selection and name-to-address mapping.

Example 48  Showing 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 *config;         /* 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) {
   nc_perror(argv[0]);
   exit(1);
}
/*
 * Loop through the transport providers.
 */
while ((config = 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", config->nc_netid);
   printf("Transport protocol family: %s\n", config->nc_protofmly);
   printf("The transport device file: %s\n", config->nc_device);
   printf("Transport provider semantics: ");
	   switch (config->nc_semantics) {
   case NC_TPI_COTS:
      printf("virtual circuit\n");
      break;
   case NC_TPI_COTS_ORD:
      printf("virtual circuit with orderly release\n");
      break;

   case NC_TPI_CLTS:
      printf("datagram\n");
      break;
   }
   /*
    * Get the address for service "date" on the host
    * named "gandalf" over the transport provider
    * specified in the netconfig structure.
    */
   if (netdir_getbyname(config, &nd_hostserv, &nd_addrlistp) != ND_OK) {
      printf("Cannot determine address for service\n");
      netdir_perror(argv[0]);
      continue;
   }
   printf("<%d> addresses of date service on gandalf:\n",
      nd_addrlistp->n_cnt);
   /*
    * 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(config,netbufp);
      printf("%s\n",uaddr);
      free(uaddr);
   }
   	netdir_free( nd_addrlistp, ND_ADDRLIST );

}
endnetconfig(handlep);