This section provides 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(), netdir_getbyaddr(), and taddr2uaddr() return pointers to data that must be freed by calls to netdir_free().
int netdir_getbyname(struct netconfig *nconf, struct nd_hostserv *service, struct nd_addrlist **addrs); |
netdir_getbyname() 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(3N) 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().
int netdir_getbyaddr(struct netconfig *nconf, struct nd_hostservlist **service, struct netbuf *netaddr); |
netdir_getbyaddr() 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(3N).
void netdir_free(void *ptr, int struct_type); |
The netdir_free() 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() 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 *nconf, struct netbuf *addr); |
taddr2uaddr() 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().
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 option, int fd, char *point_to_args); |
netdir_options() 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|
Option |
Description |
|---|---|
|
ND_SET_BROADCAST |
Sets the transport for broadcast (if the transport supports broadcast) |
|
ND_SET_RESERVEDPORT |
Lets the application bind to a reserved port (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 |
netdir_perror() displays the message stating why one of the name-to-address mapping routines failed on stderr.
void netdir_perror(char *s); |
netdir_sperror() 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.
#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) {
nc_perror(argv[0]);
exit(1);
}
/*
* 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");
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(nconf, &nd_hostserv, &nd_addrlistp) != ND_OK) {
printf("Cannot determine address for service\n");
netdir_perror(argv[0]);
continue;
}
printf("<%d> address 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(nconf,netbufp);
printf("%s\n",uaddr);
free(uaddr);
}
netdir_free( nd_addrlistp, ND_ADDRLIST );
}
endnetconfig(handlep);
|