]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/doc/psd/05.sysman/2.3.t
Import libxo-0.7.2; add xo_options.7.
[FreeBSD/FreeBSD.git] / share / doc / psd / 05.sysman / 2.3.t
1 .\" Copyright (c) 1983, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)2.3.t       8.1 (Berkeley) 6/8/93
29 .\" $FreeBSD$
30 .\"
31 .sh "Interprocess communications
32 .NH 3
33 Interprocess communication primitives
34 .NH 4
35 Communication domains
36 .PP
37 The system provides access to an extensible set of 
38 communication \fIdomains\fP.  A communication domain
39 is identified by a manifest constant defined in the
40 file \fI<sys/socket.h>\fP.
41 Important standard domains supported by the system are the ``unix''
42 domain, AF_UNIX, for communication within the system, the ``Internet''
43 domain for communication in the DARPA Internet, AF_INET,
44 and the ``NS'' domain, AF_NS, for communication
45 using the Xerox Network Systems protocols.
46 Other domains can be added to the system.
47 .NH 4
48 Socket types and protocols
49 .PP
50 Within a domain, communication takes place between communication endpoints
51 known as \fIsockets\fP.  Each socket has the potential to exchange
52 information with other sockets of an appropriate type within the domain.
53 .PP
54 Each socket has an associated
55 abstract type, which describes the semantics of communication using that
56 socket.  Properties such as reliability, ordering, and prevention
57 of duplication of messages are determined by the type.
58 The basic set of socket types is defined in \fI<sys/socket.h>\fP:
59 .DS
60 /* Standard socket types */
61 ._d
62 #define SOCK_DGRAM      1       /* datagram */
63 #define SOCK_STREAM     2       /* virtual circuit */
64 #define SOCK_RAW        3       /* raw socket */
65 #define SOCK_RDM        4       /* reliably-delivered message */
66 #define SOCK_SEQPACKET  5       /* sequenced packets */
67 .DE
68 The SOCK_DGRAM type models the semantics of datagrams in network communication:
69 messages may be lost or duplicated and may arrive out-of-order.
70 A datagram socket may send messages to and receive messages from multiple
71 peers.
72 The SOCK_RDM type models the semantics of reliable datagrams: messages
73 arrive unduplicated and in-order, the sender is notified if
74 messages are lost.
75 The \fIsend\fP and \fIreceive\fP operations (described below)
76 generate reliable/unreliable datagrams.
77 The SOCK_STREAM type models connection-based virtual circuits: two-way
78 byte streams with no record boundaries.
79 Connection setup is required before data communication may begin.
80 The SOCK_SEQPACKET type models a connection-based,
81 full-duplex, reliable, sequenced packet exchange;
82 the sender is notified if messages are lost, and messages are never
83 duplicated or presented out-of-order.
84 Users of the last two abstractions may use the facilities for
85 out-of-band transmission to send out-of-band data.
86 .PP
87 SOCK_RAW is used for unprocessed access to internal network layers
88 and interfaces; it has no specific semantics.
89 .PP
90 Other socket types can be defined.
91 .PP
92 Each socket may have a specific \fIprotocol\fP associated with it.
93 This protocol is used within the domain to provide the semantics
94 required by the socket type.
95 Not all socket types are supported by each domain;
96 support depends on the existence and the implementation
97 of a suitable protocol within the domain.
98 For example, within the ``Internet'' domain, the SOCK_DGRAM type may be
99 implemented by the UDP user datagram protocol, and the SOCK_STREAM
100 type may be implemented by the TCP transmission control protocol, while
101 no standard protocols to provide SOCK_RDM or SOCK_SEQPACKET sockets exist.
102 .NH 4
103 Socket creation, naming and service establishment
104 .PP
105 Sockets may be \fIconnected\fP or \fIunconnected\fP.  An unconnected
106 socket descriptor is obtained by the \fIsocket\fP call:
107 .DS
108 s = socket(domain, type, protocol);
109 result int s; int domain, type, protocol;
110 .DE
111 The socket domain and type are as described above,
112 and are specified using the definitions from \fI<sys/socket.h>\fP.
113 The protocol may be given as 0, meaning any suitable protocol.
114 One of several possible protocols may be selected using identifiers
115 obtained from a library routine, \fIgetprotobyname\fP.
116 .PP
117 An unconnected socket descriptor of a connection-oriented type
118 may yield a connected socket descriptor
119 in one of two ways: either by actively connecting to another socket,
120 or by becoming associated with a name in the communications domain and
121 \fIaccepting\fP a connection from another socket.
122 Datagram sockets need not establish connections before use.
123 .PP
124 To accept connections or to receive datagrams,
125 a socket must first have a binding
126 to a name (or address) within the communications domain.
127 Such a binding may be established by a \fIbind\fP call:
128 .DS
129 bind(s, name, namelen);
130 int s; struct sockaddr *name; int namelen;
131 .DE
132 Datagram sockets may have default bindings established when first
133 sending data if not explicitly bound earlier.
134 In either case,
135 a socket's bound name may be retrieved with a \fIgetsockname\fP call:
136 .DS
137 getsockname(s, name, namelen);
138 int s; result struct sockaddr *name; result int *namelen;
139 .DE
140 while the peer's name can be retrieved with \fIgetpeername\fP:
141 .DS
142 getpeername(s, name, namelen);
143 int s; result struct sockaddr *name; result int *namelen;
144 .DE
145 Domains may support sockets with several names.
146 .NH 4
147 Accepting connections
148 .PP
149 Once a binding is made to a connection-oriented socket,
150 it is possible to \fIlisten\fP for connections:
151 .DS
152 listen(s, backlog);
153 int s, backlog;
154 .DE
155 The \fIbacklog\fP specifies the maximum count of connections
156 that can be simultaneously queued awaiting acceptance.
157 .PP
158 An \fIaccept\fP call:
159 .DS
160 t = accept(s, name, anamelen);
161 result int t; int s; result struct sockaddr *name; result int *anamelen;
162 .DE
163 returns a descriptor for a new, connected, socket
164 from the queue of pending connections on \fIs\fP.
165 If no new connections are queued for acceptance,
166 the call will wait for a connection unless non-blocking I/O has been enabled.
167 .NH 4
168 Making connections
169 .PP
170 An active connection to a named socket is made by the \fIconnect\fP call:
171 .DS
172 connect(s, name, namelen);
173 int s; struct sockaddr *name; int namelen;
174 .DE
175 Although datagram sockets do not establish connections,
176 the \fIconnect\fP call may be used with such sockets
177 to create an \fIassociation\fP with the foreign address.
178 The address is recorded for use in future \fIsend\fP calls,
179 which then need not supply destination addresses.
180 Datagrams will be received only from that peer,
181 and asynchronous error reports may be received.
182 .PP
183 It is also possible to create connected pairs of sockets without
184 using the domain's name space to rendezvous; this is done with the
185 \fIsocketpair\fP call\(dg:
186 .FS
187 \(dg 4.3BSD supports \fIsocketpair\fP creation only in the ``unix''
188 communication domain.
189 .FE
190 .DS
191 socketpair(domain, type, protocol, sv);
192 int domain, type, protocol; result int sv[2];
193 .DE
194 Here the returned \fIsv\fP descriptors correspond to those obtained with
195 \fIaccept\fP and \fIconnect\fP.
196 .PP
197 The call
198 .DS
199 pipe(pv)
200 result int pv[2];
201 .DE
202 creates a pair of SOCK_STREAM sockets in the UNIX domain,
203 with pv[0] only writable and pv[1] only readable.
204 .NH 4
205 Sending and receiving data
206 .PP
207 Messages may be sent from a socket by:
208 .DS
209 cc = sendto(s, buf, len, flags, to, tolen);
210 result int cc; int s; caddr_t buf; int len, flags; caddr_t to; int tolen;
211 .DE
212 if the socket is not connected or:
213 .DS
214 cc = send(s, buf, len, flags);
215 result int cc; int s; caddr_t buf; int len, flags;
216 .DE
217 if the socket is connected.
218 The corresponding receive primitives are:
219 .DS
220 msglen = recvfrom(s, buf, len, flags, from, fromlenaddr);
221 result int msglen; int s; result caddr_t buf; int len, flags;
222 result caddr_t from; result int *fromlenaddr;
223 .DE
224 and
225 .DS
226 msglen = recv(s, buf, len, flags);
227 result int msglen; int s; result caddr_t buf; int len, flags;
228 .DE
229 .PP
230 In the unconnected case,
231 the parameters \fIto\fP and \fItolen\fP
232 specify the destination or source of the message, while
233 the \fIfrom\fP parameter stores the source of the message,
234 and \fI*fromlenaddr\fP initially gives the size of the \fIfrom\fP
235 buffer and is updated to reflect the true length of the \fIfrom\fP
236 address.
237 .PP
238 All calls cause the message to be received in or sent from
239 the message buffer of length \fIlen\fP bytes, starting at address \fIbuf\fP.
240 The \fIflags\fP specify
241 peeking at a message without reading it or sending or receiving
242 high-priority out-of-band messages, as follows:
243 .DS
244 ._d
245 #define MSG_PEEK        0x1     /* peek at incoming message */
246 #define MSG_OOB 0x2     /* process out-of-band data */
247 .DE
248 .NH 4
249 Scatter/gather and exchanging access rights
250 .PP
251 It is possible scatter and gather data and to exchange access rights
252 with messages.  When either of these operations is involved,
253 the number of parameters to the call becomes large.
254 Thus the system defines a message header structure, in \fI<sys/socket.h>\fP,
255 which can be
256 used to conveniently contain the parameters to the calls:
257 .DS
258 .if t .ta .5i 1.25i 2i 2.7i
259 .if n ._f
260 struct msghdr {
261         caddr_t msg_name;               /* optional address */
262         int     msg_namelen;    /* size of address */
263         struct  iov *msg_iov;   /* scatter/gather array */
264         int     msg_iovlen;             /* # elements in msg_iov */
265         caddr_t msg_accrights;  /* access rights sent/received */
266         int     msg_accrightslen;       /* size of msg_accrights */
267 };
268 .DE
269 Here \fImsg_name\fP and \fImsg_namelen\fP specify the source or destination
270 address if the socket is unconnected; \fImsg_name\fP may be given as
271 a null pointer if no names are desired or required.
272 The \fImsg_iov\fP and \fImsg_iovlen\fP describe the scatter/gather
273 locations, as described in section 2.1.3.
274 Access rights to be sent along with the message are specified
275 in \fImsg_accrights\fP, which has length \fImsg_accrightslen\fP.
276 In the ``unix'' domain these are an array of integer descriptors,
277 taken from the sending process and duplicated in the receiver.
278 .PP
279 This structure is used in the operations \fIsendmsg\fP and \fIrecvmsg\fP:
280 .DS
281 sendmsg(s, msg, flags);
282 int s; struct msghdr *msg; int flags;
283
284 msglen = recvmsg(s, msg, flags);
285 result int msglen; int s; result struct msghdr *msg; int flags;
286 .DE
287 .NH 4
288 Using read and write with sockets
289 .PP
290 The normal UNIX \fIread\fP and \fIwrite\fP calls may be
291 applied to connected sockets and translated into \fIsend\fP and \fIreceive\fP
292 calls from or to a single area of memory and discarding any rights
293 received.  A process may operate on a virtual circuit socket, a terminal
294 or a file with blocking or non-blocking input/output
295 operations without distinguishing the descriptor type.
296 .NH 4
297 Shutting down halves of full-duplex connections
298 .PP
299 A process that has a full-duplex socket such as a virtual circuit
300 and no longer wishes to read from or write to this socket can
301 give the call:
302 .DS
303 shutdown(s, direction);
304 int s, direction;
305 .DE
306 where \fIdirection\fP is 0 to not read further, 1 to not
307 write further, or 2 to completely shut the connection down.
308 If the underlying protocol supports unidirectional or bidirectional shutdown,
309 this indication will be passed to the peer.
310 For example, a shutdown for writing might produce an end-of-file
311 condition at the remote end.
312 .NH 4
313 Socket and protocol options
314 .PP
315 Sockets, and their underlying communication protocols, may
316 support \fIoptions\fP.  These options may be used to manipulate
317 implementation- or protocol-specific facilities. 
318 The \fIgetsockopt\fP
319 and \fIsetsockopt\fP calls are used to control options:
320 .DS
321 getsockopt(s, level, optname, optval, optlen)
322 int s, level, optname; result caddr_t optval; result int *optlen;
323
324 setsockopt(s, level, optname, optval, optlen)
325 int s, level, optname; caddr_t optval; int optlen;
326 .DE
327 The option \fIoptname\fP is interpreted at the indicated
328 protocol \fIlevel\fP for socket \fIs\fP.  If a value is specified
329 with \fIoptval\fP and \fIoptlen\fP, it is interpreted by
330 the software operating at the specified \fIlevel\fP.  The \fIlevel\fP
331 SOL_SOCKET is reserved to indicate options maintained
332 by the socket facilities.  Other \fIlevel\fP values indicate
333 a particular protocol which is to act on the option request;
334 these values are normally interpreted as a ``protocol number''.
335 .NH 3
336 UNIX domain
337 .PP
338 This section describes briefly the properties of the UNIX communications
339 domain.
340 .NH 4
341 Types of sockets
342 .PP
343 In the UNIX domain,
344 the SOCK_STREAM abstraction provides pipe-like
345 facilities, while SOCK_DGRAM provides (usually)
346 reliable message-style communications.
347 .NH 4
348 Naming
349 .PP
350 Socket names are strings and may appear in the UNIX file
351 system name space through portals\(dg.
352 .FS
353 \(dg The 4.3BSD implementation of the UNIX domain embeds
354 bound sockets in the UNIX file system name space;
355 this may change in future releases.
356 .FE
357 .NH 4
358 Access rights transmission
359 .PP
360 The ability to pass UNIX descriptors with messages in this domain
361 allows migration of service within the system and allows
362 user processes to be used in building system facilities.
363 .NH 3
364 INTERNET domain
365 .PP
366 This section describes briefly how the Internet domain is
367 mapped to the model described in this section.  More
368 information will be found in the document describing the
369 network implementation in 4.3BSD.
370 .NH 4
371 Socket types and protocols
372 .PP
373 SOCK_STREAM is supported by the Internet TCP protocol;
374 SOCK_DGRAM by the UDP protocol.
375 Each is layered atop the transport-level Internet Protocol (IP).
376 The Internet Control Message Protocol is implemented atop/beside IP
377 and is accessible via a raw socket.
378 The SOCK_SEQPACKET
379 has no direct Internet family analogue; a protocol
380 based on one from the XEROX NS family and layered on
381 top of IP could be implemented to fill this gap.
382 .NH 4
383 Socket naming
384 .PP
385 Sockets in the Internet domain have names composed of the 32 bit
386 Internet address, and a 16 bit port number.
387 Options may be used to
388 provide IP source routing or security options.
389 The 32-bit address is composed of network and host parts;
390 the network part is variable in size and is frequency encoded.
391 The host part may optionally be interpreted as a subnet field
392 plus the host on subnet; this is enabled by setting a network address
393 mask at boot time.
394 .NH 4
395 Access rights transmission
396 .PP
397 No access rights transmission facilities are provided in the Internet domain.
398 .NH 4
399 Raw access
400 .PP
401 The Internet domain allows the super-user access to the raw facilities
402 of IP.
403 These interfaces are modeled as SOCK_RAW sockets.
404 Each raw socket is associated with one IP protocol number,
405 and receives all traffic received for that protocol.
406 This allows administrative and debugging
407 functions to occur,
408 and enables user-level implementations of special-purpose protocols
409 such as inter-gateway routing protocols.