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