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