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