This commit was manufactured by cvs2svn to create tag 'FreeBSD-release/1.0'.
[unix-history] / share / doc / ps1 / 08.ipc / 5.t
CommitLineData
15637ed4
RG
1.\" Copyright (c) 1986 The Regents of the University of California.
2.\" 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.\" @(#)5.t 5.1 (Berkeley) 4/17/91
33.\"
34.\".ds RH "Advanced Topics
35.bp
36.nr H1 5
37.nr H2 0
38.LG
39.B
40.ce
415. ADVANCED TOPICS
42.sp 2
43.R
44.NL
45.PP
46A number of facilities have yet to be discussed. For most users
47of the IPC the mechanisms already
48described will suffice in constructing distributed
49applications. However, others will find the need to utilize some
50of the features which we consider in this section.
51.NH 2
52Out of band data
53.PP
54The stream socket abstraction includes the notion of \*(lqout
55of band\*(rq data. Out of band data is a logically independent
56transmission channel associated with each pair of connected
57stream sockets. Out of band data is delivered to the user
58independently of normal data.
59The abstraction defines that the out of band data facilities
60must support the reliable delivery of at least one
61out of band message at a time. This message may contain at least one
62byte of data, and at least one message may be pending delivery
63to the user at any one time. For communications protocols which
64support only in-band signaling (i.e. the urgent data is
65delivered in sequence with the normal data), the system normally extracts
66the data from the normal data stream and stores it separately.
67This allows users to choose between receiving the urgent data
68in order and receiving it out of sequence without having to
69buffer all the intervening data. It is possible
70to ``peek'' (via MSG_PEEK) at out of band data.
71If the socket has a process group, a SIGURG signal is generated
72when the protocol is notified of its existence.
73A process can set the process group
74or process id to be informed by the SIGURG signal via the
75appropriate \fIfcntl\fP call, as described below for
76SIGIO.
77If multiple sockets may have out of band data awaiting
78delivery, a \fIselect\fP call for exceptional conditions
79may be used to determine those sockets with such data pending.
80Neither the signal nor the select indicate the actual arrival
81of the out-of-band data, but only notification that it is pending.
82.PP
83In addition to the information passed, a logical mark is placed in
84the data stream to indicate the point at which the out
85of band data was sent. The remote login and remote shell
86applications use this facility to propagate signals between
87client and server processes. When a signal
88flushs any pending output from the remote process(es), all
89data up to the mark in the data stream is discarded.
90.PP
91To send an out of band message the MSG_OOB flag is supplied to
92a \fIsend\fP or \fIsendto\fP calls,
93while to receive out of band data MSG_OOB should be indicated
94when performing a \fIrecvfrom\fP or \fIrecv\fP call.
95To find out if the read pointer is currently pointing at
96the mark in the data stream, the SIOCATMARK ioctl is provided:
97.DS
98ioctl(s, SIOCATMARK, &yes);
99.DE
100If \fIyes\fP is a 1 on return, the next read will return data
101after the mark. Otherwise (assuming out of band data has arrived),
102the next read will provide data sent by the client prior
103to transmission of the out of band signal. The routine used
104in the remote login process to flush output on receipt of an
105interrupt or quit signal is shown in Figure 5.
106It reads the normal data up to the mark (to discard it),
107then reads the out-of-band byte.
108.KF
109.DS
110#include <sys/ioctl.h>
111#include <sys/file.h>
112 ...
113oob()
114{
115 int out = FWRITE, mark;
116 char waste[BUFSIZ];
117
118 /* flush local terminal output */
119 ioctl(1, TIOCFLUSH, (char *)&out);
120 for (;;) {
121 if (ioctl(rem, SIOCATMARK, &mark) < 0) {
122 perror("ioctl");
123 break;
124 }
125 if (mark)
126 break;
127 (void) read(rem, waste, sizeof (waste));
128 }
129 if (recv(rem, &mark, 1, MSG_OOB) < 0) {
130 perror("recv");
131 ...
132 }
133 ...
134}
135.DE
136.ce
137Figure 5. Flushing terminal I/O on receipt of out of band data.
138.sp
139.KE
140.PP
141A process may also read or peek at the out-of-band data
142without first reading up to the mark.
143This is more difficult when the underlying protocol delivers
144the urgent data in-band with the normal data, and only sends
145notification of its presence ahead of time (e.g., the TCP protocol
146used to implement streams in the Internet domain).
147With such protocols, the out-of-band byte may not yet have arrived
148when a \fIrecv\fP is done with the MSG_OOB flag.
149In that case, the call will return an error of EWOULDBLOCK.
150Worse, there may be enough in-band data in the input buffer
151that normal flow control prevents the peer from sending the urgent data
152until the buffer is cleared.
153The process must then read enough of the queued data
154that the urgent data may be delivered.
155.PP
156Certain programs that use multiple bytes of urgent data and must
157handle multiple urgent signals (e.g., \fItelnet\fP\|(1C))
158need to retain the position of urgent data within the stream.
159This treatment is available as a socket-level option, SO_OOBINLINE;
160see \fIsetsockopt\fP\|(2) for usage.
161With this option, the position of urgent data (the \*(lqmark\*(rq)
162is retained, but the urgent data immediately follows the mark
163within the normal data stream returned without the MSG_OOB flag.
164Reception of multiple urgent indications causes the mark to move,
165but no out-of-band data are lost.
166.NH 2
167Non-Blocking Sockets
168.PP
169It is occasionally convenient to make use of sockets
170which do not block; that is, I/O requests which
171cannot complete immediately and
172would therefore cause the process to be suspended awaiting completion are
173not executed, and an error code is returned.
174Once a socket has been created via
175the \fIsocket\fP call, it may be marked as non-blocking
176by \fIfcntl\fP as follows:
177.DS
178#include <fcntl.h>
179 ...
180int s;
181 ...
182s = socket(AF_INET, SOCK_STREAM, 0);
183 ...
184if (fcntl(s, F_SETFL, FNDELAY) < 0)
185 perror("fcntl F_SETFL, FNDELAY");
186 exit(1);
187}
188 ...
189.DE
190.PP
191When performing non-blocking I/O on sockets, one must be
192careful to check for the error EWOULDBLOCK (stored in the
193global variable \fIerrno\fP), which occurs when
194an operation would normally block, but the socket it
195was performed on is marked as non-blocking.
196In particular, \fIaccept\fP, \fIconnect\fP, \fIsend\fP, \fIrecv\fP,
197\fIread\fP, and \fIwrite\fP can
198all return EWOULDBLOCK, and processes should be prepared
199to deal with such return codes.
200If an operation such as a \fIsend\fP cannot be done in its entirety,
201but partial writes are sensible (for example, when using a stream socket),
202the data that can be sent immediately will be processed,
203and the return value will indicate the amount actually sent.
204.NH 2
205Interrupt driven socket I/O
206.PP
207The SIGIO signal allows a process to be notified
208via a signal when a socket (or more generally, a file
209descriptor) has data waiting to be read. Use of
210the SIGIO facility requires three steps: First,
211the process must set up a SIGIO signal handler
212by use of the \fIsignal\fP or \fIsigvec\fP calls. Second,
213it must set the process id or process group id which is to receive
214notification of pending input to its own process id,
215or the process group id of its process group (note that
216the default process group of a socket is group zero).
217This is accomplished by use of an \fIfcntl\fP call.
218Third, it must enable asynchronous notification of pending I/O requests
219with another \fIfcntl\fP call. Sample code to
220allow a given process to receive information on
221pending I/O requests as they occur for a socket \fIs\fP
222is given in Figure 6. With the addition of a handler for SIGURG,
223this code can also be used to prepare for receipt of SIGURG signals.
224.KF
225.DS
226#include <fcntl.h>
227 ...
228int io_handler();
229 ...
230signal(SIGIO, io_handler);
231
232/* Set the process receiving SIGIO/SIGURG signals to us */
233
234if (fcntl(s, F_SETOWN, getpid()) < 0) {
235 perror("fcntl F_SETOWN");
236 exit(1);
237}
238
239/* Allow receipt of asynchronous I/O signals */
240
241if (fcntl(s, F_SETFL, FASYNC) < 0) {
242 perror("fcntl F_SETFL, FASYNC");
243 exit(1);
244}
245.DE
246.ce
247Figure 6. Use of asynchronous notification of I/O requests.
248.sp
249.KE
250.NH 2
251Signals and process groups
252.PP
253Due to the existence of the SIGURG and SIGIO signals each socket has an
254associated process number, just as is done for terminals.
255This value is initialized to zero,
256but may be redefined at a later time with the F_SETOWN
257\fIfcntl\fP, such as was done in the code above for SIGIO.
258To set the socket's process id for signals, positive arguments
259should be given to the \fIfcntl\fP call. To set the socket's
260process group for signals, negative arguments should be
261passed to \fIfcntl\fP. Note that the process number indicates
262either the associated process id or the associated process
263group; it is impossible to specify both at the same time.
264A similar \fIfcntl\fP, F_GETOWN, is available for determining the
265current process number of a socket.
266.PP
267Another signal which is useful when constructing server processes
268is SIGCHLD. This signal is delivered to a process when any
269child processes have changed state. Normally servers use
270the signal to \*(lqreap\*(rq child processes that have exited
271without explicitly awaiting their termination
272or periodic polling for exit status.
273For example, the remote login server loop shown in Figure 2
274may be augmented as shown in Figure 7.
275.KF
276.DS
277int reaper();
278 ...
279signal(SIGCHLD, reaper);
280listen(f, 5);
281for (;;) {
282 int g, len = sizeof (from);
283
284 g = accept(f, (struct sockaddr *)&from, &len,);
285 if (g < 0) {
286 if (errno != EINTR)
287 syslog(LOG_ERR, "rlogind: accept: %m");
288 continue;
289 }
290 ...
291}
292 ...
293#include <wait.h>
294reaper()
295{
296 union wait status;
297
298 while (wait3(&status, WNOHANG, 0) > 0)
299 ;
300}
301.DE
302.sp
303.ce
304Figure 7. Use of the SIGCHLD signal.
305.sp
306.KE
307.PP
308If the parent server process fails to reap its children,
309a large number of \*(lqzombie\*(rq processes may be created.
310.NH 2
311Pseudo terminals
312.PP
313Many programs will not function properly without a terminal
314for standard input and output. Since sockets do not provide
315the semantics of terminals,
316it is often necessary to have a process communicating over
317the network do so through a \fIpseudo-terminal\fP. A pseudo-
318terminal is actually a pair of devices, master and slave,
319which allow a process to serve as an active agent in communication
320between processes and users. Data written on the slave side
321of a pseudo-terminal is supplied as input to a process reading
322from the master side, while data written on the master side are
323processed as terminal input for the slave.
324In this way, the process manipulating
325the master side of the pseudo-terminal has control over the
326information read and written on the slave side
327as if it were manipulating the keyboard and reading the screen
328on a real terminal.
329The purpose of this abstraction is to
330preserve terminal semantics over a network connection\(em
331that is, the slave side appears as a normal terminal to
332any process reading from or writing to it.
333.PP
334For example, the remote
335login server uses pseudo-terminals for remote login sessions.
336A user logging in to a machine across the network is provided
337a shell with a slave pseudo-terminal as standard input, output,
338and error. The server process then handles the communication
339between the programs invoked by the remote shell and the user's
340local client process.
341When a user sends a character that generates an interrupt
342on the remote machine that flushes terminal output,
343the pseudo-terminal generates a control message for the server process.
344The server then sends an out of band message
345to the client process to signal a flush of data at the real terminal
346and on the intervening data buffered in the network.
347.PP
348Under 4.3BSD, the name of the slave side of a pseudo-terminal is of the form
349\fI/dev/ttyxy\fP, where \fIx\fP is a single letter
350starting at `p' and continuing to `t'.
351\fIy\fP is a hexadecimal digit (i.e., a single
352character in the range 0 through 9 or `a' through `f').
353The master side of a pseudo-terminal is \fI/dev/ptyxy\fP,
354where \fIx\fP and \fIy\fP correspond to the
355slave side of the pseudo-terminal.
356.PP
357In general, the method of obtaining a pair of master and
358slave pseudo-terminals is to
359find a pseudo-terminal which
360is not currently in use.
361The master half of a pseudo-terminal is a single-open device;
362thus, each master may be opened in turn until an open succeeds.
363The slave side of the pseudo-terminal is then opened,
364and is set to the proper terminal modes if necessary.
365The process then \fIfork\fPs; the child closes
366the master side of the pseudo-terminal, and \fIexec\fPs the
367appropriate program. Meanwhile, the parent closes the
368slave side of the pseudo-terminal and begins reading and
369writing from the master side. Sample code making use of
370pseudo-terminals is given in Figure 8; this code assumes
371that a connection on a socket \fIs\fP exists, connected
372to a peer who wants a service of some kind, and that the
373process has disassociated itself from any previous controlling terminal.
374.KF
375.DS
376gotpty = 0;
377for (c = 'p'; !gotpty && c <= 's'; c++) {
378 line = "/dev/ptyXX";
379 line[sizeof("/dev/pty")-1] = c;
380 line[sizeof("/dev/ptyp")-1] = '0';
381 if (stat(line, &statbuf) < 0)
382 break;
383 for (i = 0; i < 16; i++) {
384 line[sizeof("/dev/ptyp")-1] = "0123456789abcdef"[i];
385 master = open(line, O_RDWR);
386 if (master > 0) {
387 gotpty = 1;
388 break;
389 }
390 }
391}
392if (!gotpty) {
393 syslog(LOG_ERR, "All network ports in use");
394 exit(1);
395}
396
397line[sizeof("/dev/")-1] = 't';
398slave = open(line, O_RDWR); /* \fIslave\fP is now slave side */
399if (slave < 0) {
400 syslog(LOG_ERR, "Cannot open slave pty %s", line);
401 exit(1);
402}
403
404ioctl(slave, TIOCGETP, &b); /* Set slave tty modes */
405b.sg_flags = CRMOD|XTABS|ANYP;
406ioctl(slave, TIOCSETP, &b);
407
408i = fork();
409if (i < 0) {
410 syslog(LOG_ERR, "fork: %m");
411 exit(1);
412} else if (i) { /* Parent */
413 close(slave);
414 ...
415} else { /* Child */
416 (void) close(s);
417 (void) close(master);
418 dup2(slave, 0);
419 dup2(slave, 1);
420 dup2(slave, 2);
421 if (slave > 2)
422 (void) close(slave);
423 ...
424}
425.DE
426.ce
427Figure 8. Creation and use of a pseudo terminal
428.sp
429.KE
430.NH 2
431Selecting specific protocols
432.PP
433If the third argument to the \fIsocket\fP call is 0,
434\fIsocket\fP will select a default protocol to use with
435the returned socket of the type requested.
436The default protocol is usually correct, and alternate choices are not
437usually available.
438However, when using ``raw'' sockets to communicate directly with
439lower-level protocols or hardware interfaces,
440the protocol argument may be important for setting up demultiplexing.
441For example, raw sockets in the Internet family may be used to implement
442a new protocol above IP, and the socket will receive packets
443only for the protocol specified.
444To obtain a particular protocol one determines the protocol number
445as defined within the communication domain. For the Internet
446domain one may use one of the library routines
447discussed in section 3, such as \fIgetprotobyname\fP:
448.DS
449#include <sys/types.h>
450#include <sys/socket.h>
451#include <netinet/in.h>
452#include <netdb.h>
453 ...
454pp = getprotobyname("newtcp");
455s = socket(AF_INET, SOCK_STREAM, pp->p_proto);
456.DE
457This would result in a socket \fIs\fP using a stream
458based connection, but with protocol type of ``newtcp''
459instead of the default ``tcp.''
460.PP
461In the NS domain, the available socket protocols are defined in
462<\fInetns/ns.h\fP>. To create a raw socket for Xerox Error Protocol
463messages, one might use:
464.DS
465#include <sys/types.h>
466#include <sys/socket.h>
467#include <netns/ns.h>
468 ...
469s = socket(AF_NS, SOCK_RAW, NSPROTO_ERROR);
470.DE
471.NH 2
472Address binding
473.PP
474As was mentioned in section 2,
475binding addresses to sockets in the Internet and NS domains can be
476fairly complex. As a brief reminder, these associations
477are composed of local and foreign
478addresses, and local and foreign ports. Port numbers are
479allocated out of separate spaces, one for each system and one
480for each domain on that system.
481Through the \fIbind\fP system call, a
482process may specify half of an association, the
483<local address, local port> part, while the
484\fIconnect\fP
485and \fIaccept\fP
486primitives are used to complete a socket's association by
487specifying the <foreign address, foreign port> part.
488Since the association is created in two steps the association
489uniqueness requirement indicated previously could be violated unless
490care is taken. Further, it is unrealistic to expect user
491programs to always know proper values to use for the local address
492and local port since a host may reside on multiple networks and
493the set of allocated port numbers is not directly accessible
494to a user.
495.PP
496To simplify local address binding in the Internet domain the notion of a
497\*(lqwildcard\*(rq address has been provided. When an address
498is specified as INADDR_ANY (a manifest constant defined in
499<netinet/in.h>), the system interprets the address as
500\*(lqany valid address\*(rq. For example, to bind a specific
501port number to a socket, but leave the local address unspecified,
502the following code might be used:
503.DS
504#include <sys/types.h>
505#include <netinet/in.h>
506 ...
507struct sockaddr_in sin;
508 ...
509s = socket(AF_INET, SOCK_STREAM, 0);
510sin.sin_family = AF_INET;
511sin.sin_addr.s_addr = htonl(INADDR_ANY);
512sin.sin_port = htons(MYPORT);
513bind(s, (struct sockaddr *) &sin, sizeof (sin));
514.DE
515Sockets with wildcarded local addresses may receive messages
516directed to the specified port number, and sent to any
517of the possible addresses assigned to a host. For example,
518if a host has addresses 128.32.0.4 and 10.0.0.78, and a socket is bound as
519above, the process will be
520able to accept connection requests which are addressed to
521128.32.0.4 or 10.0.0.78.
522If a server process wished to only allow hosts on a
523given network connect to it, it would bind
524the address of the host on the appropriate network.
525.PP
526In a similar fashion, a local port may be left unspecified
527(specified as zero), in which case the system will select an
528appropriate port number for it. This shortcut will work
529both in the Internet and NS domains. For example, to
530bind a specific local address to a socket, but to leave the
531local port number unspecified:
532.DS
533hp = gethostbyname(hostname);
534if (hp == NULL) {
535 ...
536}
537bcopy(hp->h_addr, (char *) sin.sin_addr, hp->h_length);
538sin.sin_port = htons(0);
539bind(s, (struct sockaddr *) &sin, sizeof (sin));
540.DE
541The system selects the local port number based on two criteria.
542The first is that on 4BSD systems,
543Internet ports below IPPORT_RESERVED (1024) (for the Xerox domain,
5440 through 3000) are reserved
545for privileged users (i.e., the super user);
546Internet ports above IPPORT_USERRESERVED (50000) are reserved
547for non-privileged servers. The second is
548that the port number is not currently bound to some other
549socket. In order to find a free Internet port number in the privileged
550range the \fIrresvport\fP library routine may be used as follows
551to return a stream socket in with a privileged port number:
552.DS
553int lport = IPPORT_RESERVED \- 1;
554int s;
78ed81a3 555\&...
15637ed4
RG
556s = rresvport(&lport);
557if (s < 0) {
558 if (errno == EAGAIN)
559 fprintf(stderr, "socket: all ports in use\en");
560 else
561 perror("rresvport: socket");
562 ...
563}
564.DE
565The restriction on allocating ports was done to allow processes
566executing in a \*(lqsecure\*(rq environment to perform authentication
567based on the originating address and port number. For example,
568the \fIrlogin\fP(1) command allows users to log in across a network
569without being asked for a password, if two conditions hold:
570First, the name of the system the user
571is logging in from is in the file
572\fI/etc/hosts.equiv\fP on the system he is logging
573in to (or the system name and the user name are in
574the user's \fI.rhosts\fP file in the user's home
575directory), and second, that the user's rlogin
576process is coming from a privileged port on the machine from which he is
577logging. The port number and network address of the
578machine from which the user is logging in can be determined either
579by the \fIfrom\fP result of the \fIaccept\fP call, or
580from the \fIgetpeername\fP call.
581.PP
582In certain cases the algorithm used by the system in selecting
583port numbers is unsuitable for an application. This is because
584associations are created in a two step process. For example,
585the Internet file transfer protocol, FTP, specifies that data
586connections must always originate from the same local port. However,
587duplicate associations are avoided by connecting to different foreign
588ports. In this situation the system would disallow binding the
589same local address and port number to a socket if a previous data
590connection's socket still existed. To override the default port
591selection algorithm, an option call must be performed prior
592to address binding:
593.DS
594 ...
595int on = 1;
596 ...
597setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
598bind(s, (struct sockaddr *) &sin, sizeof (sin));
599.DE
600With the above call, local addresses may be bound which
601are already in use. This does not violate the uniqueness
602requirement as the system still checks at connect time to
603be sure any other sockets with the same local address and
604port do not have the same foreign address and port.
605If the association already exists, the error EADDRINUSE is returned.
606.NH 2
607Broadcasting and determining network configuration
608.PP
609By using a datagram socket, it is possible to send broadcast
610packets on many networks supported by the system.
611The network itself must support broadcast; the system
612provides no simulation of broadcast in software.
613Broadcast messages can place a high load on a network since they force
614every host on the network to service them. Consequently,
615the ability to send broadcast packets has been limited
616to sockets which are explicitly marked as allowing broadcasting.
617Broadcast is typically used for one of two reasons:
618it is desired to find a resource on a local network without prior
619knowledge of its address,
620or important functions such as routing require that information
621be sent to all accessible neighbors.
622.PP
623To send a broadcast message, a datagram socket
624should be created:
625.DS
626s = socket(AF_INET, SOCK_DGRAM, 0);
627.DE
628or
629.DS
630s = socket(AF_NS, SOCK_DGRAM, 0);
631.DE
632The socket is marked as allowing broadcasting,
633.DS
634int on = 1;
635
636setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on));
637.DE
638and at least a port number should be bound to the socket:
639.DS
640sin.sin_family = AF_INET;
641sin.sin_addr.s_addr = htonl(INADDR_ANY);
642sin.sin_port = htons(MYPORT);
643bind(s, (struct sockaddr *) &sin, sizeof (sin));
644.DE
645or, for the NS domain,
646.DS
647sns.sns_family = AF_NS;
648netnum = htonl(net);
649sns.sns_addr.x_net = *(union ns_net *) &netnum; /* insert net number */
650sns.sns_addr.x_port = htons(MYPORT);
651bind(s, (struct sockaddr *) &sns, sizeof (sns));
652.DE
653The destination address of the message to be broadcast
654depends on the network(s) on which the message is to be broadcast.
655The Internet domain supports a shorthand notation for broadcast
656on the local network, the address INADDR_BROADCAST (defined in
657<\fInetinet/in.h\fP>.
658To determine the list of addresses for all reachable neighbors
659requires knowledge of the networks to which the host is connected.
660Since this information should
661be obtained in a host-independent fashion and may be impossible
662to derive, 4.3BSD provides a method of
663retrieving this information from the system data structures.
664The SIOCGIFCONF \fIioctl\fP call returns the interface
665configuration of a host in the form of a
666single \fIifconf\fP structure; this structure contains
667a ``data area'' which is made up of an array of
668of \fIifreq\fP structures, one for each network interface
669to which the host is connected.
670These structures are defined in
671\fI<net/if.h>\fP as follows:
672.DS
673.if t .ta .5i 1.0i 1.5i 3.5i
674.if n .ta .7i 1.4i 2.1i 3.4i
675struct ifconf {
676 int ifc_len; /* size of associated buffer */
677 union {
678 caddr_t ifcu_buf;
679 struct ifreq *ifcu_req;
680 } ifc_ifcu;
681};
682
683#define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */
684#define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */
685
686#define IFNAMSIZ 16
687
688struct ifreq {
689 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
690 union {
691 struct sockaddr ifru_addr;
692 struct sockaddr ifru_dstaddr;
693 struct sockaddr ifru_broadaddr;
694 short ifru_flags;
695 caddr_t ifru_data;
696 } ifr_ifru;
697};
698
699.if t .ta \w' #define'u +\w' ifr_broadaddr'u +\w' ifr_ifru.ifru_broadaddr'u
700#define ifr_addr ifr_ifru.ifru_addr /* address */
701#define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */
702#define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
703#define ifr_flags ifr_ifru.ifru_flags /* flags */
704#define ifr_data ifr_ifru.ifru_data /* for use by interface */
705.DE
706The actual call which obtains the
707interface configuration is
708.DS
709struct ifconf ifc;
710char buf[BUFSIZ];
711
712ifc.ifc_len = sizeof (buf);
713ifc.ifc_buf = buf;
714if (ioctl(s, SIOCGIFCONF, (char *) &ifc) < 0) {
715 ...
716}
717.DE
718After this call \fIbuf\fP will contain one \fIifreq\fP structure for
719each network to which the host is connected, and
720\fIifc.ifc_len\fP will have been modified to reflect the number
721of bytes used by the \fIifreq\fP structures.
722.PP
723For each structure
724there exists a set of ``interface flags'' which tell
725whether the network corresponding to that interface is
726up or down, point to point or broadcast, etc. The
727SIOCGIFFLAGS \fIioctl\fP retrieves these
728flags for an interface specified by an \fIifreq\fP
729structure as follows:
730.DS
731struct ifreq *ifr;
732
733ifr = ifc.ifc_req;
734
735for (n = ifc.ifc_len / sizeof (struct ifreq); --n >= 0; ifr++) {
736 /*
737 * We must be careful that we don't use an interface
738 * devoted to an address family other than those intended;
739 * if we were interested in NS interfaces, the
740 * AF_INET would be AF_NS.
741 */
742 if (ifr->ifr_addr.sa_family != AF_INET)
743 continue;
744 if (ioctl(s, SIOCGIFFLAGS, (char *) ifr) < 0) {
745 ...
746 }
747 /*
748 * Skip boring cases.
749 */
750 if ((ifr->ifr_flags & IFF_UP) == 0 ||
751 (ifr->ifr_flags & IFF_LOOPBACK) ||
752 (ifr->ifr_flags & (IFF_BROADCAST | IFF_POINTTOPOINT)) == 0)
753 continue;
754.DE
755.PP
756Once the flags have been obtained, the broadcast address
757must be obtained. In the case of broadcast networks this is
758done via the SIOCGIFBRDADDR \fIioctl\fP, while for point-to-point networks
759the address of the destination host is obtained with SIOCGIFDSTADDR.
760.DS
761struct sockaddr dst;
762
763if (ifr->ifr_flags & IFF_POINTTOPOINT) {
764 if (ioctl(s, SIOCGIFDSTADDR, (char *) ifr) < 0) {
765 ...
766 }
767 bcopy((char *) ifr->ifr_dstaddr, (char *) &dst, sizeof (ifr->ifr_dstaddr));
768} else if (ifr->ifr_flags & IFF_BROADCAST) {
769 if (ioctl(s, SIOCGIFBRDADDR, (char *) ifr) < 0) {
770 ...
771 }
772 bcopy((char *) ifr->ifr_broadaddr, (char *) &dst, sizeof (ifr->ifr_broadaddr));
773}
774.DE
775.PP
776After the appropriate \fIioctl\fP's have obtained the broadcast
777or destination address (now in \fIdst\fP), the \fIsendto\fP call may be
778used:
779.DS
780 sendto(s, buf, buflen, 0, (struct sockaddr *)&dst, sizeof (dst));
781}
782.DE
783In the above loop one \fIsendto\fP occurs for every
784interface to which the host is connected that supports the notion of
785broadcast or point-to-point addressing.
786If a process only wished to send broadcast
787messages on a given network, code similar to that outlined above
788would be used, but the loop would need to find the
789correct destination address.
790.PP
791Received broadcast messages contain the senders address
792and port, as datagram sockets are bound before
793a message is allowed to go out.
794.NH 2
795Socket Options
796.PP
797It is possible to set and get a number of options on sockets
798via the \fIsetsockopt\fP and \fIgetsockopt\fP system calls.
799These options include such things as marking a socket for
800broadcasting, not to route, to linger on close, etc.
801The general forms of the calls are:
802.DS
803setsockopt(s, level, optname, optval, optlen);
804.DE
805and
806.DS
807getsockopt(s, level, optname, optval, optlen);
808.DE
809.PP
810The parameters to the calls are as follows: \fIs\fP
811is the socket on which the option is to be applied.
812\fILevel\fP specifies the protocol layer on which the
813option is to be applied; in most cases this is
814the ``socket level'', indicated by the symbolic constant
815SOL_SOCKET, defined in \fI<sys/socket.h>.\fP
816The actual option is specified in \fIoptname\fP, and is
817a symbolic constant also defined in \fI<sys/socket.h>\fP.
818\fIOptval\fP and \fIOptlen\fP point to the value of the
819option (in most cases, whether the option is to be turned
820on or off), and the length of the value of the option,
821respectively.
822For \fIgetsockopt\fP, \fIoptlen\fP is
823a value-result parameter, initially set to the size of
824the storage area pointed to by \fIoptval\fP, and modified
825upon return to indicate the actual amount of storage used.
826.PP
827An example should help clarify things. It is sometimes
828useful to determine the type (e.g., stream, datagram, etc.)
829of an existing socket; programs
830under \fIinetd\fP (described below) may need to perform this
831task. This can be accomplished as follows via the
832SO_TYPE socket option and the \fIgetsockopt\fP call:
833.DS
834#include <sys/types.h>
835#include <sys/socket.h>
836
837int type, size;
838
839size = sizeof (int);
840
841if (getsockopt(s, SOL_SOCKET, SO_TYPE, (char *) &type, &size) < 0) {
842 ...
843}
844.DE
845After the \fIgetsockopt\fP call, \fItype\fP will be set
846to the value of the socket type, as defined in
847\fI<sys/socket.h>\fP. If, for example, the socket were
848a datagram socket, \fItype\fP would have the value
849corresponding to SOCK_DGRAM.
850.NH 2
851NS Packet Sequences
852.PP
853The semantics of NS connections demand that
854the user both be able to look inside the network header associated
855with any incoming packet and be able to specify what should go
856in certain fields of an outgoing packet.
857Using different calls to \fIsetsockopt\fP, it is possible
858to indicate whether prototype headers will be associated by
859the user with each outgoing packet (SO_HEADERS_ON_OUTPUT),
860to indicate whether the headers received by the system should be
861delivered to the user (SO_HEADERS_ON_INPUT), or to indicate
862default information that should be associated with all
863outgoing packets on a given socket (SO_DEFAULT_HEADERS).
864.PP
865The contents of a SPP header (minus the IDP header) are:
866.DS
867.if t .ta \w" #define"u +\w" u_short"u +2.0i
868struct sphdr {
869 u_char sp_cc; /* connection control */
870#define SP_SP 0x80 /* system packet */
871#define SP_SA 0x40 /* send acknowledgement */
872#define SP_OB 0x20 /* attention (out of band data) */
873#define SP_EM 0x10 /* end of message */
874 u_char sp_dt; /* datastream type */
875 u_short sp_sid; /* source connection identifier */
876 u_short sp_did; /* destination connection identifier */
877 u_short sp_seq; /* sequence number */
878 u_short sp_ack; /* acknowledge number */
879 u_short sp_alo; /* allocation number */
880};
881.DE
882Here, the items of interest are the \fIdatastream type\fP and
883the \fIconnection control\fP fields. The semantics of the
884datastream type are defined by the application(s) in question;
885the value of this field is, by default, zero, but it can be
886used to indicate things such as Xerox's Bulk Data Transfer
887Protocol (in which case it is set to one). The connection control
888field is a mask of the flags defined just below it. The user may
889set or clear the end-of-message bit to indicate
890that a given message is the last of a given substream type,
891or may set/clear the attention bit as an alternate way to
892indicate that a packet should be sent out-of-band.
893As an example, to associate prototype headers with outgoing
894SPP packets, consider:
895.DS
896#include <sys/types.h>
897#include <sys/socket.h>
898#include <netns/ns.h>
899#include <netns/sp.h>
900 ...
901struct sockaddr_ns sns, to;
902int s, on = 1;
903struct databuf {
904 struct sphdr proto_spp; /* prototype header */
905 char buf[534]; /* max. possible data by Xerox std. */
906} buf;
907 ...
908s = socket(AF_NS, SOCK_SEQPACKET, 0);
909 ...
910bind(s, (struct sockaddr *) &sns, sizeof (sns));
911setsockopt(s, NSPROTO_SPP, SO_HEADERS_ON_OUTPUT, &on, sizeof(on));
912 ...
913buf.proto_spp.sp_dt = 1; /* bulk data */
914buf.proto_spp.sp_cc = SP_EM; /* end-of-message */
915strcpy(buf.buf, "hello world\en");
916sendto(s, (char *) &buf, sizeof(struct sphdr) + strlen("hello world\en"),
917 (struct sockaddr *) &to, sizeof(to));
918 ...
919.DE
920Note that one must be careful when writing headers; if the prototype
921header is not written with the data with which it is to be associated,
922the kernel will treat the first few bytes of the data as the
923header, with unpredictable results.
924To turn off the above association, and to indicate that packet
925headers received by the system should be passed up to the user,
926one might use:
927.DS
928#include <sys/types.h>
929#include <sys/socket.h>
930#include <netns/ns.h>
931#include <netns/sp.h>
932 ...
933struct sockaddr sns;
934int s, on = 1, off = 0;
935 ...
936s = socket(AF_NS, SOCK_SEQPACKET, 0);
937 ...
938bind(s, (struct sockaddr *) &sns, sizeof (sns));
939setsockopt(s, NSPROTO_SPP, SO_HEADERS_ON_OUTPUT, &off, sizeof(off));
940setsockopt(s, NSPROTO_SPP, SO_HEADERS_ON_INPUT, &on, sizeof(on));
941 ...
942.DE
943.PP
944Output is handled somewhat differently in the IDP world.
945The header of an IDP-level packet looks like:
946.DS
947.if t .ta \w'struct 'u +\w" struct ns_addr"u +2.0i
948struct idp {
949 u_short idp_sum; /* Checksum */
950 u_short idp_len; /* Length, in bytes, including header */
951 u_char idp_tc; /* Transport Control (i.e., hop count) */
952 u_char idp_pt; /* Packet Type (i.e., level 2 protocol) */
953 struct ns_addr idp_dna; /* Destination Network Address */
954 struct ns_addr idp_sna; /* Source Network Address */
955};
956.DE
957The primary field of interest in an IDP header is the \fIpacket type\fP
958field. The standard values for this field are (as defined
959in <\fInetns/ns.h\fP>):
960.DS
961.if t .ta \w" #define"u +\w" NSPROTO_ERROR"u +1.0i
962#define NSPROTO_RI 1 /* Routing Information */
963#define NSPROTO_ECHO 2 /* Echo Protocol */
964#define NSPROTO_ERROR 3 /* Error Protocol */
965#define NSPROTO_PE 4 /* Packet Exchange */
966#define NSPROTO_SPP 5 /* Sequenced Packet */
967.DE
968For SPP connections, the contents of this field are
969automatically set to NSPROTO_SPP; for IDP packets,
970this value defaults to zero, which means ``unknown''.
971.PP
972Setting the value of that field with SO_DEFAULT_HEADERS is
973easy:
974.DS
975#include <sys/types.h>
976#include <sys/socket.h>
977#include <netns/ns.h>
978#include <netns/idp.h>
979 ...
980struct sockaddr sns;
981struct idp proto_idp; /* prototype header */
982int s, on = 1;
983 ...
984s = socket(AF_NS, SOCK_DGRAM, 0);
985 ...
986bind(s, (struct sockaddr *) &sns, sizeof (sns));
987proto_idp.idp_pt = NSPROTO_PE; /* packet exchange */
988setsockopt(s, NSPROTO_IDP, SO_DEFAULT_HEADERS, (char *) &proto_idp,
989 sizeof(proto_idp));
990 ...
991.DE
992.PP
993Using SO_HEADERS_ON_OUTPUT is somewhat more difficult. When
994SO_HEADERS_ON_OUTPUT is turned on for an IDP socket, the socket
995becomes (for all intents and purposes) a raw socket. In this
996case, all the fields of the prototype header (except the
997length and checksum fields, which are computed by the kernel)
998must be filled in correctly in order for the socket to send and
999receive data in a sensible manner. To be more specific, the
1000source address must be set to that of the host sending the
1001data; the destination address must be set to that of the
1002host for whom the data is intended; the packet type must be
1003set to whatever value is desired; and the hopcount must be
1004set to some reasonable value (almost always zero). It should
1005also be noted that simply sending data using \fIwrite\fP
1006will not work unless a \fIconnect\fP or \fIsendto\fP call
1007is used, in spite of the fact that it is the destination
1008address in the prototype header that is used, not the one
1009given in either of those calls. For almost
1010all IDP applications , using SO_DEFAULT_HEADERS is easier and
1011more desirable than writing headers.
1012.NH 2
1013Three-way Handshake
1014.PP
1015The semantics of SPP connections indicates that a three-way
1016handshake, involving changes in the datastream type, should \(em
1017but is not absolutely required to \(em take place before a SPP
1018connection is closed. Almost all SPP connections are
1019``well-behaved'' in this manner; when communicating with
1020any process, it is best to assume that the three-way handshake
1021is required unless it is known for certain that it is not
1022required. In a three-way close, the closing process
1023indicates that it wishes to close the connection by sending
1024a zero-length packet with end-of-message set and with
1025datastream type 254. The other side of the connection
1026indicates that it is OK to close by sending a zero-length
1027packet with end-of-message set and datastream type 255. Finally,
1028the closing process replies with a zero-length packet with
1029substream type 255; at this point, the connection is considered
1030closed. The following code fragments are simplified examples
1031of how one might handle this three-way handshake at the user
1032level; in the future, support for this type of close will
1033probably be provided as part of the C library or as part of
1034the kernel. The first code fragment below illustrates how a process
1035might handle three-way handshake if it sees that the process it
1036is communicating with wants to close the connection:
1037.DS
1038#include <sys/types.h>
1039#include <sys/socket.h>
1040#include <netns/ns.h>
1041#include <netns/sp.h>
1042 ...
1043#ifndef SPPSST_END
1044#define SPPSST_END 254
1045#define SPPSST_ENDREPLY 255
1046#endif
1047struct sphdr proto_sp;
1048int s;
1049 ...
1050read(s, buf, BUFSIZE);
1051if (((struct sphdr *)buf)->sp_dt == SPPSST_END) {
1052 /*
1053 * SPPSST_END indicates that the other side wants to
1054 * close.
1055 */
1056 proto_sp.sp_dt = SPPSST_ENDREPLY;
1057 proto_sp.sp_cc = SP_EM;
1058 setsockopt(s, NSPROTO_SPP, SO_DEFAULT_HEADERS, (char *)&proto_sp,
1059 sizeof(proto_sp));
1060 write(s, buf, 0);
1061 /*
1062 * Write a zero-length packet with datastream type = SPPSST_ENDREPLY
1063 * to indicate that the close is OK with us. The packet that we
1064 * don't see (because we don't look for it) is another packet
1065 * from the other side of the connection, with SPPSST_ENDREPLY
1066 * on it it, too. Once that packet is sent, the connection is
1067 * considered closed; note that we really ought to retransmit
1068 * the close for some time if we do not get a reply.
1069 */
1070 close(s);
1071}
1072 ...
1073.DE
1074To indicate to another process that we would like to close the
1075connection, the following code would suffice:
1076.DS
1077#include <sys/types.h>
1078#include <sys/socket.h>
1079#include <netns/ns.h>
1080#include <netns/sp.h>
1081 ...
1082#ifndef SPPSST_END
1083#define SPPSST_END 254
1084#define SPPSST_ENDREPLY 255
1085#endif
1086struct sphdr proto_sp;
1087int s;
1088 ...
1089proto_sp.sp_dt = SPPSST_END;
1090proto_sp.sp_cc = SP_EM;
1091setsockopt(s, NSPROTO_SPP, SO_DEFAULT_HEADERS, (char *)&proto_sp,
1092 sizeof(proto_sp));
1093write(s, buf, 0); /* send the end request */
1094proto_sp.sp_dt = SPPSST_ENDREPLY;
1095setsockopt(s, NSPROTO_SPP, SO_DEFAULT_HEADERS, (char *)&proto_sp,
1096 sizeof(proto_sp));
1097/*
1098 * We assume (perhaps unwisely)
1099 * that the other side will send the
1100 * ENDREPLY, so we'll just send our final ENDREPLY
1101 * as if we'd seen theirs already.
1102 */
1103write(s, buf, 0);
1104close(s);
1105 ...
1106.DE
1107.NH 2
1108Packet Exchange
1109.PP
1110The Xerox standard protocols include a protocol that is both
1111reliable and datagram-oriented. This protocol is known as
1112Packet Exchange (PEX or PE) and, like SPP, is layered on top
1113of IDP. PEX is important for a number of things: Courier
1114remote procedure calls may be expedited through the use
1115of PEX, and many Xerox servers are located by doing a PEX
1116``BroadcastForServers'' operation. Although there is no
1117implementation of PEX in the kernel,
1118it may be simulated at the user level with some clever coding
1119and the use of one peculiar \fIgetsockopt\fP. A PEX packet
1120looks like:
1121.DS
1122.if t .ta \w'struct 'u +\w" struct idp"u +2.0i
1123/*
1124 * The packet-exchange header shown here is not defined
1125 * as part of any of the system include files.
1126 */
1127struct pex {
1128 struct idp p_idp; /* idp header */
1129 u_short ph_id[2]; /* unique transaction ID for pex */
1130 u_short ph_client; /* client type field for pex */
1131};
1132.DE
1133The \fIph_id\fP field is used to hold a ``unique id'' that
1134is used in duplicate suppression; the \fIph_client\fP
1135field indicates the PEX client type (similar to the packet
1136type field in the IDP header). PEX reliability stems from the
1137fact that it is an idempotent (``I send a packet to you, you
1138send a packet to me'') protocol. Processes on each side of
1139the connection may use the unique id to determine if they have
1140seen a given packet before (the unique id field differs on each
1141packet sent) so that duplicates may be detected, and to indicate
1142which message a given packet is in response to. If a packet with
1143a given unique id is sent and no response is received in a given
1144amount of time, the packet is retransmitted until it is decided
1145that no response will ever be received. To simulate PEX, one
1146must be able to generate unique ids -- something that is hard to
1147do at the user level with any real guarantee that the id is really
1148unique. Therefore, a means (via \fIgetsockopt\fP) has been provided
1149for getting unique ids from the kernel. The following code fragment
1150indicates how to get a unique id:
1151.DS
1152long uniqueid;
1153int s, idsize = sizeof(uniqueid);
1154 ...
1155s = socket(AF_NS, SOCK_DGRAM, 0);
1156 ...
1157/* get id from the kernel -- only on IDP sockets */
1158getsockopt(s, NSPROTO_PE, SO_SEQNO, (char *)&uniqueid, &idsize);
1159 ...
1160.DE
1161The retransmission and duplicate suppression code required to
1162simulate PEX fully is left as an exercise for the reader.
1163.NH 2
1164Inetd
1165.PP
1166One of the daemons provided with 4.3BSD is \fIinetd\fP, the
1167so called ``internet super-server.'' \fIInetd\fP is invoked at boot
1168time, and determines from the file \fI/etc/inetd.conf\fP the
1169servers for which it is to listen. Once this information has been
1170read and a pristine environment created, \fIinetd\fP proceeds
1171to create one socket for each service it is to listen for,
1172binding the appropriate port number to each socket.
1173.PP
1174\fIInetd\fP then performs a \fIselect\fP on all these
1175sockets for read availability, waiting for somebody wishing
1176a connection to the service corresponding to
1177that socket. \fIInetd\fP then performs an \fIaccept\fP on
1178the socket in question, \fIfork\fPs, \fIdup\fPs the new
1179socket to file descriptors 0 and 1 (stdin and
1180stdout), closes other open file
1181descriptors, and \fIexec\fPs the appropriate server.
1182.PP
1183Servers making use of \fIinetd\fP are considerably simplified,
1184as \fIinetd\fP takes care of the majority of the IPC work
1185required in establishing a connection. The server invoked
1186by \fIinetd\fP expects the socket connected to its client
1187on file descriptors 0 and 1, and may immediately perform
1188any operations such as \fIread\fP, \fIwrite\fP, \fIsend\fP,
1189or \fIrecv\fP. Indeed, servers may use
1190buffered I/O as provided by the ``stdio'' conventions, as
1191long as as they remember to use \fIfflush\fP when appropriate.
1192.PP
1193One call which may be of interest to individuals writing
1194servers under \fIinetd\fP is the \fIgetpeername\fP call,
1195which returns the address of the peer (process) connected
1196on the other end of the socket. For example, to log the
1197Internet address in ``dot notation'' (e.g., ``128.32.0.4'')
1198of a client connected to a server under
1199\fIinetd\fP, the following code might be used:
1200.DS
1201struct sockaddr_in name;
1202int namelen = sizeof (name);
1203 ...
1204if (getpeername(0, (struct sockaddr *)&name, &namelen) < 0) {
1205 syslog(LOG_ERR, "getpeername: %m");
1206 exit(1);
1207} else
1208 syslog(LOG_INFO, "Connection from %s", inet_ntoa(name.sin_addr));
1209 ...
1210.DE
1211While the \fIgetpeername\fP call is especially useful when
1212writing programs to run with \fIinetd\fP, it can be used
1213under other circumstances. Be warned, however, that \fIgetpeername\fP will
1214fail on UNIX domain sockets.