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