Removed definition "LIB= rpc". We want libc.a to contain librpc.a, not
[unix-history] / .ref-386BSD-0.1-patchkit / usr / othersrc / share / doc / ps1 / 08.ipc / 3.t
CommitLineData
ca1f21c5
WJ
1.\" Copyright (c) 1986 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\" must display the following acknowledgement:
14.\" This product includes software developed by the University of
15.\" California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)3.t 5.1 (Berkeley) 4/17/91
33.\"
34.\".ds RH "Network Library Routines
35.bp
36.nr H1 3
37.nr H2 0
38.bp
39.LG
40.B
41.ce
423. NETWORK LIBRARY ROUTINES
43.sp 2
44.R
45.NL
46.PP
47The discussion in section 2 indicated the possible need to
48locate and construct network addresses when using the
49interprocess communication facilities in a distributed
50environment. To aid in this task a number of routines
51have been added to the standard C run-time library.
52In this section we will consider the new routines provided
53to manipulate network addresses. While the 4.3BSD networking
54facilities support both the DARPA standard Internet protocols
55and the Xerox NS protocols, most of the routines presented
56in this section do not apply to the NS domain. Unless otherwise
57stated, it should be assumed that the routines presented in this
58section do not apply to the NS domain.
59.PP
60Locating a service on a remote host requires many levels of
61mapping before client and server may
62communicate. A service is assigned a name which is intended
63for human consumption; e.g. \*(lqthe \fIlogin server\fP on host
64monet\*(rq.
65This name, and the name of the peer host, must then be translated
66into network \fIaddresses\fP which are not necessarily suitable
67for human consumption. Finally, the address must then used in locating
68a physical \fIlocation\fP and \fIroute\fP to the service. The
69specifics of these three mappings are likely to vary between
70network architectures. For instance, it is desirable for a network
71to not require hosts to
72be named in such a way that their physical location is known by
73the client host. Instead, underlying services in the network
74may discover the actual location of the host at the time a client
75host wishes to communicate. This ability to have hosts named in
76a location independent manner may induce overhead in connection
77establishment, as a discovery process must take place,
78but allows a host to be physically mobile without requiring it to
79notify its clientele of its current location.
80.PP
81Standard routines are provided for: mapping host names
82to network addresses, network names to network numbers,
83protocol names to protocol numbers, and service names
84to port numbers and the appropriate protocol to
85use in communicating with the server process. The
86file <\fInetdb.h\fP> must be included when using any of these
87routines.
88.NH 2
89Host names
90.PP
91An Internet host name to address mapping is represented by
92the \fIhostent\fP structure:
93.DS
94.if t .ta 0.6i 1.1i 2.6i
95struct hostent {
96 char *h_name; /* official name of host */
97 char **h_aliases; /* alias list */
98 int h_addrtype; /* host address type (e.g., AF_INET) */
99 int h_length; /* length of address */
100 char **h_addr_list; /* list of addresses, null terminated */
101};
102
103#define h_addr h_addr_list[0] /* first address, network byte order */
104.DE
105The routine \fIgethostbyname\fP(3N) takes an Internet host name
106and returns a \fIhostent\fP structure,
107while the routine \fIgethostbyaddr\fP(3N)
108maps Internet host addresses into a \fIhostent\fP structure.
109.PP
110The official name of the host and its public aliases are
111returned by these routines,
112along with the address type (family) and a null terminated list of
113variable length address. This list of addresses is
114required because it is possible
115for a host to have many addresses, all having the same name.
116The \fIh_addr\fP definition is provided for backward compatibility,
117and is defined to be the first address in the list of addresses
118in the \fIhostent\fP structure.
119.PP
120The database for these calls is provided either by the
121file \fI/etc/hosts\fP (\fIhosts\fP\|(5)),
122or by use of a nameserver, \fInamed\fP\|(8).
123Because of the differences in these databases and their access protocols,
124the information returned may differ.
125When using the host table version of \fIgethostbyname\fP,
126only one address will be returned, but all listed aliases will be included.
127The nameserver version may return alternate addresses,
128but will not provide any aliases other than one given as argument.
129.PP
130Unlike Internet names, NS names are always mapped into host
131addresses by the use of a standard NS \fIClearinghouse service\fP,
132a distributed name and authentication server. The algorithms
133for mapping NS names to addresses via a Clearinghouse are
134rather complicated, and the routines are not part of the
135standard libraries. The user-contributed Courier (Xerox
136remote procedure call protocol) compiler contains routines
137to accomplish this mapping; see the documentation and
138examples provided therein for more information. It is
139expected that almost all software that has to communicate
140using NS will need to use the facilities of
141the Courier compiler.
142.PP
143An NS host address is represented by the following:
144.DS
145union ns_host {
146 u_char c_host[6];
147 u_short s_host[3];
148};
149
150union ns_net {
151 u_char c_net[4];
152 u_short s_net[2];
153};
154
155struct ns_addr {
156 union ns_net x_net;
157 union ns_host x_host;
158 u_short x_port;
159};
160.DE
161The following code fragment inserts a known NS address into
162a \fIns_addr\fP:
163.DS
164#include <sys/types.h>
165#include <sys/socket.h>
166#include <netns/ns.h>
167 ...
168u_long netnum;
169struct sockaddr_ns dst;
170 ...
171bzero((char *)&dst, sizeof(dst));
172
173/*
174 * There is no convenient way to assign a long
175 * integer to a ``union ns_net'' at present; in
176 * the future, something will hopefully be provided,
177 * but this is the portable way to go for now.
178 * The network number below is the one for the NS net
179 * that the desired host (gyre) is on.
180 */
181netnum = htonl(2266);
182dst.sns_addr.x_net = *(union ns_net *) &netnum;
183dst.sns_family = AF_NS;
184
185/*
186 * host 2.7.1.0.2a.18 == "gyre:Computer Science:UofMaryland"
187 */
188dst.sns_addr.x_host.c_host[0] = 0x02;
189dst.sns_addr.x_host.c_host[1] = 0x07;
190dst.sns_addr.x_host.c_host[2] = 0x01;
191dst.sns_addr.x_host.c_host[3] = 0x00;
192dst.sns_addr.x_host.c_host[4] = 0x2a;
193dst.sns_addr.x_host.c_host[5] = 0x18;
194dst.sns_addr.x_port = htons(75);
195.DE
196.NH 2
197Network names
198.PP
199As for host names, routines for mapping network names to numbers,
200and back, are provided. These routines return a \fInetent\fP
201structure:
202.DS
203.DT
204/*
205 * Assumption here is that a network number
206 * fits in 32 bits -- probably a poor one.
207 */
208struct netent {
209 char *n_name; /* official name of net */
210 char **n_aliases; /* alias list */
211 int n_addrtype; /* net address type */
212 int n_net; /* network number, host byte order */
213};
214.DE
215The routines \fIgetnetbyname\fP(3N), \fIgetnetbynumber\fP(3N),
216and \fIgetnetent\fP(3N) are the network counterparts to the
217host routines described above. The routines extract their
218information from \fI/etc/networks\fP.
219.PP
220NS network numbers are determined either by asking your local
221Xerox Network Administrator (and hardcoding the information
222into your code), or by querying the Clearinghouse for addresses.
223The internetwork router is the only process
224that needs to manipulate network numbers on a regular basis; if
225a process wishes to communicate with a machine, it should ask the
226Clearinghouse for that machine's address (which will include
227the net number).
228.NH 2
229Protocol names
230.PP
231For protocols, which are defined in \fI/etc/protocols\fP,
232the \fIprotoent\fP structure defines the
233protocol-name mapping
234used with the routines \fIgetprotobyname\fP(3N),
235\fIgetprotobynumber\fP(3N),
236and \fIgetprotoent\fP(3N):
237.DS
238.DT
239struct protoent {
240 char *p_name; /* official protocol name */
241 char **p_aliases; /* alias list */
242 int p_proto; /* protocol number */
243};
244.DE
245.PP
246In the NS domain, protocols are indicated by the "client type"
247field of a IDP header. No protocol database exists; see section
2485 for more information.
249.NH 2
250Service names
251.PP
252Information regarding services is a bit more complicated. A service
253is expected to reside at a specific \*(lqport\*(rq and employ
254a particular communication protocol. This view is consistent with
255the Internet domain, but inconsistent with other network architectures.
256Further, a service may reside on multiple ports.
257If this occurs, the higher level library routines
258will have to be bypassed or extended.
259Services available are contained in the file \fI/etc/services\fP.
260A service mapping is described by the \fIservent\fP structure,
261.DS
262.DT
263struct servent {
264 char *s_name; /* official service name */
265 char **s_aliases; /* alias list */
266 int s_port; /* port number, network byte order */
267 char *s_proto; /* protocol to use */
268};
269.DE
270The routine \fIgetservbyname\fP(3N) maps service
271names to a servent structure by specifying a service name and,
272optionally, a qualifying protocol. Thus the call
273.DS
274sp = getservbyname("telnet", (char *) 0);
275.DE
276returns the service specification for a telnet server using
277any protocol, while the call
278.DS
279sp = getservbyname("telnet", "tcp");
280.DE
281returns only that telnet server which uses the TCP protocol.
282The routines \fIgetservbyport\fP(3N) and \fIgetservent\fP(3N) are
283also provided. The \fIgetservbyport\fP routine has an interface similar
284to that provided by \fIgetservbyname\fP; an optional protocol name may
285be specified to qualify lookups.
286.PP
287In the NS domain, services are handled by a central dispatcher
288provided as part of the Courier remote procedure call facilities.
289Again, the reader is referred to the Courier compiler documentation
290and to the Xerox standard*
291.FS
292* \fICourier: The Remote Procedure Call Protocol\fP, XSIS 038112.
293.FE
294for further details.
295.NH 2
296Miscellaneous
297.PP
298With the support routines described above, an Internet application program
299should rarely have to deal directly
300with addresses. This allows
301services to be developed as much as possible in a network independent
302fashion. It is clear, however, that purging all network dependencies
303is very difficult. So long as the user is required to supply network
304addresses when naming services and sockets there will always some
305network dependency in a program. For example, the normal
306code included in client programs, such as the remote login program,
307is of the form shown in Figure 1.
308(This example will be considered in more detail in section 4.)
309.PP
310If we wanted to make the remote login program independent of the
311Internet protocols and addressing scheme we would be forced to add
312a layer of routines which masked the network dependent aspects from
313the mainstream login code. For the current facilities available in
314the system this does not appear to be worthwhile.
315.PP
316Aside from the address-related data base routines, there are several
317other routines available in the run-time library which are of interest
318to users. These are intended mostly to simplify manipulation of
319names and addresses. Table 1 summarizes the routines
320for manipulating variable length byte strings and handling byte
321swapping of network addresses and values.
322.KF
323.DS B
324.TS
325box;
326l | l
327l | l.
328Call Synopsis
329_
330bcmp(s1, s2, n) compare byte-strings; 0 if same, not 0 otherwise
331bcopy(s1, s2, n) copy n bytes from s1 to s2
332bzero(base, n) zero-fill n bytes starting at base
333htonl(val) convert 32-bit quantity from host to network byte order
334htons(val) convert 16-bit quantity from host to network byte order
335ntohl(val) convert 32-bit quantity from network to host byte order
336ntohs(val) convert 16-bit quantity from network to host byte order
337.TE
338.DE
339.ce
340Table 1. C run-time routines.
341.KE
342.PP
343The byte swapping routines are provided because the operating
344system expects addresses to be supplied in network order. On
345some architectures, such as the VAX,
346host byte ordering is different than
347network byte ordering. Consequently,
348programs are sometimes required to byte swap quantities. The
349library routines which return network addresses provide them
350in network order so that they may simply be copied into the structures
351provided to the system. This implies users should encounter the
352byte swapping problem only when \fIinterpreting\fP network addresses.
353For example, if an Internet port is to be printed out the following
354code would be required:
355.DS
356printf("port number %d\en", ntohs(sp->s_port));
357.DE
358On machines where unneeded these routines are defined as null
359macros.
360.DS
361.if t .ta .5i 1.0i 1.5i 2.0i
362.if n .ta .7i 1.4i 2.1i 2.8i
363#include <sys/types.h>
364#include <sys/socket.h>
365#include <netinet/in.h>
366#include <stdio.h>
367#include <netdb.h>
368 ...
369main(argc, argv)
370 int argc;
371 char *argv[];
372{
373 struct sockaddr_in server;
374 struct servent *sp;
375 struct hostent *hp;
376 int s;
377 ...
378 sp = getservbyname("login", "tcp");
379 if (sp == NULL) {
380 fprintf(stderr, "rlogin: tcp/login: unknown service\en");
381 exit(1);
382 }
383 hp = gethostbyname(argv[1]);
384 if (hp == NULL) {
385 fprintf(stderr, "rlogin: %s: unknown host\en", argv[1]);
386 exit(2);
387 }
388 bzero((char *)&server, sizeof (server));
389 bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length);
390 server.sin_family = hp->h_addrtype;
391 server.sin_port = sp->s_port;
392 s = socket(AF_INET, SOCK_STREAM, 0);
393 if (s < 0) {
394 perror("rlogin: socket");
395 exit(3);
396 }
397 ...
398 /* Connect does the bind() for us */
399
400 if (connect(s, (char *)&server, sizeof (server)) < 0) {
401 perror("rlogin: connect");
402 exit(5);
403 }
404 ...
405}
406.DE
407.ce
408Figure 1. Remote login client code.