この節では、使用できるマッピングルーチンについて簡単に説明します。ルーチンは、ネットワーク名を返すか、または対応するネットワークアドレスにネットワーク名を変換します。netdir_getbyname(3NSL)、netdir_getbyaddr(3NSL)、および taddr2uaddr(3NSL) はデータへのポインタを返しますが、これらのポインタは netdir_free(3NSL) 呼び出しで解放する必要があります。
int netdir_getbyname(struct netconfig *nconf, struct nd_hostserv *service, struct nd_addrlist **addrs);
netdir_getbyname(3NSL) は service に指定されたホスト名とサービス名を、nconf で指定されたトランスポートに一致するアドレスセットに対応づけます。nd_hostserv と nd_addrlist の各構造体は、netdir(3NSL) のマニュアルページに定義されています。アドレスへのポインタは、addrs に返されます。
使用可能なすべてのトランスポート上で、ホストおよびサービスのすべてのアドレスを取得するには、getnetpath(3NSL) または getnetconfig(3NSL) のいずれかで返される各 netconfig(4) 構造体を使用して netdir_getbyname(3NSL) を呼び出します。
int netdir_getbyaddr(struct netconfig *nconf, struct nd_hostservlist **service, struct netbuf *netaddr);
netdir_getbyaddr(3NSL) は、アドレスをホスト名とサービス名に対応づけます。このインタフェースは、netaddr に指定されたアドレスを使用して呼び出され、ホスト名とサービス名のペアのリストを service に返します。nd_hostservlist 構造体は、netdir(3NSL) に定義されています。
void netdir_free(void *ptr, int struct_type);
netdir_free(3NSL) ルーチンは、名前からアドレスへの変換ルーチンによって割り当てられた構造体を解放します。次の表に、パラメータに使用できる値を示します。
表 11–2 netdir_free(3NSL) ルーチン
struct_type |
ptr |
---|---|
ND_HOSTSERV |
nd_hostserv 構造体へのポインタ |
ND_HOSTSERVLIST |
nd_hostservlist 構造体へのポインタ |
ND_ADDR |
netbuf 構造体へのポインタ |
ND_ADDRLIST |
nd_addrlist 構造体へのポインタ |
char *taddr2uaddr(struct netconfig *nconf, struct netbuf *addr);
taddr2uaddr(3NSL) は、addr が指すアドレスを変換し、アドレスのトランスポートに依存しない文字列表現を返します。この文字列表現のことを「汎用アドレス」と呼びます。nconf には、アドレスが有効なトランスポートを指定します。汎用アドレスは、free(3C) で解放できます。
struct netbuf *uaddr2taddr(struct netconfig *nconf, char *uaddr);
uaddr が指す汎用アドレスは、netbuf 構造体に変換されます。nconf には、アドレスが有効なトランスポートを指定します。
int netdir_options(const struct netconfig *config, const int option, const int fildes, char *point_to_args);
netdir_options(3NSL) は、ブロードキャストアドレス、TCP や UDP の予約ポート機能など、トランスポート固有の機能へのインタフェースを提供します。nconf にはトランスポートを指定し、option にはトランスポート固有の動作を指定します。fd の値は option の値によって指定するかどうかが決まります。4 つ目の引数は、操作固有のデータを指します。
次の表に、option に使用できる値を示します。
表 11–3 netdir_options に指定できる値
オプション |
説明 |
---|---|
ND_SET_BROADCAST |
ブロードキャスト用のトランスポートを設定する (トランスポートがブロードキャスト機能をサポートしている場合) |
ND_SET_RESERVEDPORT |
アプリケーションが予約ポートにバインドできるようにする (トランスポートがそのようなバインドを許可している場合) |
ND_CHECK_RESERVEDPORT |
アドレスが予約ポートに対応しているかどうかを検証する (トランスポートが予約ポートをサポートしている場合) |
ND_MERGEADDR |
ローカルに意味のあるアドレスを、クライアントホストが接続できるアドレスに変換する |
netdir_perror(3NSL) ルーチンは、名前からアドレスにマッピングするルーチンの 1 つが失敗した理由を示すメッセージを stderr に表示します。
void netdir_perror(char *s);
netdir_sperror(3NSL) ルーチンは、名前からアドレスにマッピングするルーチンの 1 つが失敗した理由を示すエラーメッセージを含む文字列を返します。
char *netdir_sperror(void);
次の例に、ネットワーク選択および名前からアドレスへのマッピングを示します。
#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> 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(nconf,netbufp); printf("%s\n",uaddr); free(uaddr); } netdir_free( nd_addrlistp, ND_ADDRLIST ); } endnetconfig(handlep);