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