updates for 4.3 from lapsley
[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.\"
5.\" @(#)3.t 1.2 (Berkeley) %G%
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.
189 */
190netnum = htonl(2266);
191dst.sns_addr.x_net = *(union ns_net *) &netnum;
192dst.sns_family = AF_NS;
193
194/*
195 * host 2.7.1.0.2a.18 == "gyre:Computer Science:UofMaryland"
196 */
197dst.sns_addr.x_host.c_host[0] = 0x02;
198dst.sns_addr.x_host.c_host[1] = 0x07;
199dst.sns_addr.x_host.c_host[2] = 0x01;
200dst.sns_addr.x_host.c_host[3] = 0x00;
201dst.sns_addr.x_host.c_host[4] = 0x2a;
202dst.sns_addr.x_host.c_host[5] = 0x18;
203dst.sns_addr.x_port = htons(75);
204.DE
200989e9
MK
205.NH 2
206Network names
207.PP
208As for host names, routines for mapping network names to numbers,
209and back, are provided. These routines return a \fInetent\fP
210structure:
211.DS
212.DT
213/*
214 * Assumption here is that a network number
215 * fits in 32 bits -- probably a poor one.
216 */
217struct netent {
218 char *n_name; /* official name of net */
219 char **n_aliases; /* alias list */
220 int n_addrtype; /* net address type */
d60e8dff 221 int n_net; /* network number, host byte order */
200989e9
MK
222};
223.DE
224The routines \fIgetnetbyname\fP(3N), \fIgetnetbynumber\fP(3N),
225and \fIgetnetent\fP(3N) are the network counterparts to the
d60e8dff
MK
226host routines described above. The routines extract their
227information from \fI/etc/networks\fP.
228.PP
229NS network numbers are determined either by asking your local
230Xerox Network Administrator (and hardcoding the information
231into your code), or by querying the Clearinghouse for addresses.
232The internetwork router is the only process
233that needs to manipulate network numbers on a regular basis; if
234a process wishes to communicate with a machine, it should ask the
235Clearinghouse for that machine's address (which will include
236the net number).
200989e9
MK
237.NH 2
238Protocol names
239.PP
d60e8dff
MK
240For protocols, which are defined in \fI/etc/protocols\fP,
241the \fIprotoent\fP structure defines the
200989e9
MK
242protocol-name mapping
243used with the routines \fIgetprotobyname\fP(3N),
244\fIgetprotobynumber\fP(3N),
245and \fIgetprotoent\fP(3N):
246.DS
247.DT
248struct protoent {
249 char *p_name; /* official protocol name */
250 char **p_aliases; /* alias list */
d60e8dff 251 int p_proto; /* protocol number */
200989e9
MK
252};
253.DE
d60e8dff
MK
254.PP
255In the NS domain, protocols are indicated by the "client type"
256field of a IDP header. No protocol database exists; see section
2575 for more information.
200989e9
MK
258.NH 2
259Service names
260.PP
261Information regarding services is a bit more complicated. A service
262is expected to reside at a specific \*(lqport\*(rq and employ
263a particular communication protocol. This view is consistent with
264the Internet domain, but inconsistent with other network architectures.
d60e8dff
MK
265Further, a service may reside on multiple ports.
266If this occurs, the higher level library routines
200989e9
MK
267will have to be bypassed in favor of homegrown routines similar in
268spirit to the \*(lqgethostbynameandnet\*(rq routine described above.
d60e8dff 269Services available are contained in the file \fI/etc/services\fP.
200989e9
MK
270A service mapping is described by the \fIservent\fP structure,
271.DS
272.DT
273struct servent {
274 char *s_name; /* official service name */
275 char **s_aliases; /* alias list */
d60e8dff 276 int s_port; /* port number, network byte order */
200989e9
MK
277 char *s_proto; /* protocol to use */
278};
279.DE
280The routine \fIgetservbyname\fP(3N) maps service
281names to a servent structure by specifying a service name and,
282optionally, a qualifying protocol. Thus the call
283.DS
d60e8dff 284sp = getservbyname("telnet", (char *) 0);
200989e9
MK
285.DE
286returns the service specification for a telnet server using
287any protocol, while the call
288.DS
289sp = getservbyname("telnet", "tcp");
290.DE
291returns only that telnet server which uses the TCP protocol.
292The routines \fIgetservbyport\fP(3N) and \fIgetservent\fP(3N) are
293also provided. The \fIgetservbyport\fP routine has an interface similar
294to that provided by \fIgetservbyname\fP; an optional protocol name may
295be specified to qualify lookups.
d60e8dff
MK
296.PP
297In the NS domain, services are handled by a central dispatcher
298provided as part of the Courier remote procedure call facilities.
299Again, the reader is referred to the Courier compiler documentation
300and to the Xerox standard*
301.FS
302* \fICourier: The Remote Procedure Call Protocol\fP, XSIS 038112.
303.FE
304for further details.
200989e9
MK
305.NH 2
306Miscellaneous
307.PP
d60e8dff 308With the support routines described above, an Internet application program
200989e9
MK
309should rarely have to deal directly
310with addresses. This allows
311services to be developed as much as possible in a network independent
312fashion. It is clear, however, that purging all network dependencies
313is very difficult. So long as the user is required to supply network
314addresses when naming services and sockets there will always some
315network dependency in a program. For example, the normal
316code included in client programs, such as the remote login program,
317is of the form shown in Figure 1.
200989e9
MK
318(This example will be considered in more detail in section 4.)
319.PP
320If we wanted to make the remote login program independent of the
321Internet protocols and addressing scheme we would be forced to add
322a layer of routines which masked the network dependent aspects from
323the mainstream login code. For the current facilities available in
d60e8dff 324the system this does not appear to be worthwhile.
200989e9
MK
325.PP
326Aside from the address-related data base routines, there are several
327other routines available in the run-time library which are of interest
328to users. These are intended mostly to simplify manipulation of
329names and addresses. Table 1 summarizes the routines
330for manipulating variable length byte strings and handling byte
331swapping of network addresses and values.
332.KF
333.DS B
334.TS
335box;
336l | l
337l | l.
338Call Synopsis
339_
340bcmp(s1, s2, n) compare byte-strings; 0 if same, not 0 otherwise
341bcopy(s1, s2, n) copy n bytes from s1 to s2
342bzero(base, n) zero-fill n bytes starting at base
343htonl(val) convert 32-bit quantity from host to network byte order
344htons(val) convert 16-bit quantity from host to network byte order
345ntohl(val) convert 32-bit quantity from network to host byte order
346ntohs(val) convert 16-bit quantity from network to host byte order
347.TE
348.DE
349.ce
350Table 1. C run-time routines.
351.KE
352.PP
353The byte swapping routines are provided because the operating
d60e8dff
MK
354system expects addresses to be supplied in network order. On
355some architectures, such as the VAX,
356host byte ordering is different than
357network byte ordering. Consequently,
200989e9
MK
358programs are sometimes required to byte swap quantities. The
359library routines which return network addresses provide them
360in network order so that they may simply be copied into the structures
361provided to the system. This implies users should encounter the
362byte swapping problem only when \fIinterpreting\fP network addresses.
363For example, if an Internet port is to be printed out the following
364code would be required:
365.DS
366printf("port number %d\en", ntohs(sp->s_port));
367.DE
d60e8dff 368On machines where unneeded these routines are defined as null
200989e9 369macros.
d60e8dff
MK
370.DS
371.if t .ta .5i 1.0i 1.5i 2.0i
372.if n .ta .7i 1.4i 2.1i 2.8i
373#include <sys/types.h>
374#include <sys/socket.h>
375#include <netinet/in.h>
376#include <stdio.h>
377#include <netdb.h>
378 ...
379main(argc, argv)
380 int argc;
381 char *argv[];
382{
383 struct sockaddr_in server;
384 struct servent *sp;
385 struct hostent *hp;
386 int s;
387 ...
388 sp = getservbyname("login", "tcp");
389 if (sp == NULL) {
390 fprintf(stderr, "rlogin: tcp/login: unknown service\en");
391 exit(1);
392 }
393 hp = gethostbyname(argv[1]);
394 if (hp == NULL) {
395 fprintf(stderr, "rlogin: %s: unknown host\en", argv[1]);
396 exit(2);
397 }
398 bzero((char *)&server, sizeof (server));
399 bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length);
400 server.sin_family = hp->h_addrtype;
401 server.sin_port = sp->s_port;
402 s = socket(AF_INET, SOCK_STREAM, 0);
403 if (s < 0) {
404 perror("rlogin: socket");
405 exit(3);
406 }
407 ...
408 /* Connect does the bind() for us */
409
410 if (connect(s, (char *)&server, sizeof (server)) < 0) {
411 perror("rlogin: connect");
412 exit(5);
413 }
414 ...
415}
416.DE
417.ce
418Figure 1. Remote login client code.