1 .\" $NetBSD: 2.3.t,v 1.2 1998/01/09 06:54:49 perry Exp $
3 .\" Copyright (c) 1983, 1993, 1994
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)2.3.t 8.6 (Berkeley) 6/1/94
32 .Sh 2 "Interprocess communications
33 .Sh 3 "Interprocess communication primitives
34 .Sh 4 "Communication domains
36 The system provides access to an extensible set of
37 communication \fIdomains\fP. A communication domain (or protocol family)
38 is identified by a manifest constant defined in the
39 file \fI<sys/socket.h>\fP.
40 Important standard domains supported by the system are the local (``UNIX'')
41 domain (PF_LOCAL or PF_UNIX) for communication within the system,
42 the ``Internet'' domain (PF_INET) for communication in the DARPA Internet,
43 the ISO family of protocols (PF_ISO and PF_CCITT) for providing
44 a check-off box on the list of your system capabilities,
45 and the ``NS'' domain (PF_NS) for communication
46 using the Xerox Network Systems protocols.
47 Other domains can be added to the system.
48 .Sh 4 "Socket types and protocols
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.
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:
65 SOCK_DGRAM /* datagram */
66 SOCK_STREAM /* virtual circuit */
67 SOCK_RAW /* raw socket */
68 SOCK_RDM /* reliably-delivered message */
69 SOCK_SEQPACKET /* sequenced packets */
72 The SOCK_DGRAM type models the semantics of datagrams in network communication:
73 messages may be lost or duplicated and may arrive out-of-order.
74 A datagram socket may send messages to and receive messages from multiple
76 The SOCK_RDM type models the semantics of reliable datagrams: messages
77 arrive unduplicated and in-order, the sender is notified if
83 operations (described below)
84 generate reliable or unreliable datagrams.
85 The SOCK_STREAM type models connection-based virtual circuits: two-way
86 byte streams with no record boundaries.
87 Connection setup is required before data communication may begin.
88 The SOCK_SEQPACKET type models a connection-based,
89 full-duplex, reliable, exchange preserving message boundaries;
90 the sender is notified if messages are lost, and messages are never
91 duplicated or presented out-of-order.
92 Users of the last two abstractions may use the facilities for
93 out-of-band transmission to send out-of-band data.
95 SOCK_RAW is used for unprocessed access to internal network layers
96 and interfaces; it has no specific semantics.
97 Other socket types can be defined.
99 Each socket may have a specific \fIprotocol\fP associated with it.
100 This protocol is used within the domain to provide the semantics
101 required by the socket type.
102 Not all socket types are supported by each domain;
103 support depends on the existence and the implementation
104 of a suitable protocol within the domain.
105 For example, within the ``Internet'' domain, the SOCK_DGRAM type may be
106 implemented by the UDP user datagram protocol, and the SOCK_STREAM
107 type may be implemented by the TCP transmission control protocol, while
108 no standard protocols to provide SOCK_RDM or SOCK_SEQPACKET sockets exist.
109 .Sh 4 "Socket creation, naming and service establishment
111 Sockets may be \fIconnected\fP or \fIunconnected\fP. An unconnected
112 socket descriptor is obtained by the
116 .Fd socket 3 "create an endpoint for communication
117 s = socket(domain, type, protocol);
118 result int s; int domain, type, protocol;
120 The socket domain and type are as described above,
121 and are specified using the definitions from \fI<sys/socket.h>\fP.
122 The protocol may be given as 0, meaning any suitable protocol.
123 One of several possible protocols may be selected using identifiers
124 obtained from a library routine,
127 An unconnected socket descriptor of a connection-oriented type
128 may yield a connected socket descriptor
129 in one of two ways: either by actively connecting to another socket,
130 or by becoming associated with a name in the communications domain and
131 \fIaccepting\fP a connection from another socket.
132 Datagram sockets need not establish connections before use.
134 To accept connections or to receive datagrams,
135 a socket must first have a binding
136 to a name (or address) within the communications domain.
137 Such a binding may be established by a
141 .Fd bind 3 "bind a name to a socket
142 bind(s, name, namelen);
143 int s; struct sockaddr *name; int namelen;
145 Datagram sockets may have default bindings established when first
146 sending data if not explicitly bound earlier.
148 a socket's bound name may be retrieved with a
152 .Fd getsockname 3 "get socket name
153 getsockname(s, name, namelen);
154 int s; result struct sockaddr *name; result int *namelen;
156 while the peer's name can be retrieved with
159 .Fd getpeername 3 "get name of connected peer
160 getpeername(s, name, namelen);
161 int s; result struct sockaddr *name; result int *namelen;
163 Domains may support sockets with several names.
164 .Sh 4 "Accepting connections
166 Once a binding is made to a connection-oriented socket,
171 .Fd listen 2 "listen for connections on a socket
175 The \fIbacklog\fP specifies the maximum count of connections
176 that can be simultaneously queued awaiting acceptance.
182 .Fd accept 3 "accept a connection on a socket
183 t = accept(s, name, anamelen);
184 result int t; int s; result struct sockaddr *name; result int *anamelen;
186 returns a descriptor for a new, connected, socket
187 from the queue of pending connections on \fIs\fP.
188 If no new connections are queued for acceptance,
189 the call will wait for a connection unless
190 non-blocking I/O has been enabled (see section
192 .Sh 4 "Making connections
194 An active connection to a named socket is made by the
198 .Fd connect 3 "initiate a connection on a socket
199 connect(s, name, namelen);
200 int s; struct sockaddr *name; int namelen;
202 Although datagram sockets do not establish connections,
205 call may be used with such sockets
206 to create an \fIassociation\fP with the foreign address.
207 The address is recorded for use in future
209 calls, which then need not supply destination addresses.
210 Datagrams will be received only from that peer,
211 and asynchronous error reports may be received.
213 It is also possible to create connected pairs of sockets without
214 using the domain's name space to rendezvous; this is done with the
220 creation only in the PF_LOCAL communication domain.
223 .Fd socketpair 4 "create a pair of connected sockets
224 socketpair(domain, type, protocol, sv);
225 int domain, type, protocol; result int sv[2];
227 Here the returned \fIsv\fP descriptors correspond to those obtained with
234 .Fd pipe 1 "create descriptor pair for interprocess communication
238 creates a pair of SOCK_STREAM sockets in the PF_LOCAL domain,
239 with pv[0] only writable and pv[1] only readable.
240 .Sh 4 "Sending and receiving data
242 Messages may be sent from a socket by:
244 .Fd sendto 6 "send a message from a socket
245 cc = sendto(s, msg, len, flags, to, tolen);
246 result int cc; int s; void *msg; size_t len;
247 int flags; struct sockaddr *to; int tolen;
249 if the socket is not connected or:
251 .Fd send 4 "send a message from a socket
252 cc = send(s, msg, len, flags);
253 result int cc; int s; void *msg; size_t len; int flags;
255 if the socket is connected.
256 The corresponding receive primitives are:
258 .Fd recvfrom 6 "receive a message from a socket
259 msglen = recvfrom(s, buf, len, flags, from, fromlenaddr);
260 result int msglen; int s; result void *buf; size_t len; int flags;
261 result struct sockaddr *from; result int *fromlenaddr;
265 .Fd recv 4 "receive a message from a socket
266 msglen = recv(s, buf, len, flags);
267 result int msglen; int s; result void *buf; size_t len; int flags;
270 In the unconnected case,
271 the parameters \fIto\fP and \fItolen\fP
272 specify the destination or source of the message, while
273 the \fIfrom\fP parameter stores the source of the message,
274 and \fI*fromlenaddr\fP initially gives the size of the \fIfrom\fP
275 buffer and is updated to reflect the true length of the \fIfrom\fP
278 All calls cause the message to be received in or sent from
279 the message buffer of length \fIlen\fP bytes, starting at address \fIbuf\fP.
280 The \fIflags\fP specify
281 peeking at a message without reading it, sending or receiving
282 high-priority out-of-band messages, or other
283 special requests as follows:
287 MSG_OOB /* process out-of-band data */
288 MSG_PEEK /* peek at incoming message */
289 MSG_DONTROUTE /* send without using routing tables */
290 MSG_EOR /* data completes record */
291 MSG_TRUNC /* data discarded before delivery */
292 MSG_CTRUNC /* control data lost before delivery */
293 MSG_WAITALL /* wait for full request or error */
294 MSG_DONTWAIT /* this message should be nonblocking */
297 .Sh 4 "Scatter/gather and exchanging access rights
299 It is possible to scatter and gather data and to exchange access rights
300 with messages. When either of these operations is involved,
301 the number of parameters to the call becomes large.
302 Thus, the system defines a message header structure, in \fI<sys/socket.h>\fP,
304 used to conveniently contain the parameters to the calls:
310 caddr_t msg_name; /* optional address */
311 u_int msg_namelen; /* size of address */
312 struct iovec *msg_iov; /* scatter/gather array */
313 u_int msg_iovlen; /* # elements in msg_iov */
314 caddr_t msg_control; /* ancillary data */
315 u_int msg_controllen; /* ancillary data buffer len */
316 int msg_flags; /* flags on received message */
320 Here \fImsg_name\fP and \fImsg_namelen\fP specify the source or destination
321 address if the socket is unconnected; \fImsg_name\fP may be given as
322 a null pointer if no names are desired or required.
323 The \fImsg_iov\fP and \fImsg_iovlen\fP describe the scatter/gather
324 locations, as described in section
326 The data in the \fImsg_control\fP buffer is composed of
327 an array of variable length messages
328 used for additional information with or about a datagram
329 not expressible by flags. The format is a sequence
330 of message elements headed by \fIcmsghdr\fP structures:
336 u_int cmsg_len; /* data byte count, including hdr */
337 int cmsg_level; /* originating protocol */
338 int cmsg_type; /* protocol-specific type */
339 u_char cmsg_data[\|]; /* variable length type specific data */
343 The following macros are provided for use with the \fImsg_control\fP buffer:
347 CMSG_FIRSTHDR(mhdr) /* given msghdr, return first cmsghdr */
348 CMSG_NXTHDR(mhdr, cmsg) /* given msghdr and cmsghdr, return next cmsghdr */
349 CMSG_DATA(cmsg) /* given cmsghdr, return associated data pointer */
352 Access rights to be sent along with the message are specified
354 \fIcmsghdr\fP structures, with level SOL_SOCKET and type SCM_RIGHTS.
355 In the PF_LOCAL domain these are an array of integer descriptors,
356 copied from the sending process and duplicated in the receiver.
359 This structure is used in the operations
364 .Fd sendmsg 3 "send a message from a socket
365 sendmsg(s, msg, flags);
366 int s; struct msghdr *msg; int flags;
369 .Fd recvmsg 3 "receive a message from a socket
370 msglen = recvmsg(s, msg, flags);
371 result int msglen; int s; result struct msghdr *msg; int flags;
373 .Sh 4 "Using read and write with sockets
379 calls may be applied to connected sockets and translated into
383 calls from or to a single area of memory and discarding any rights
384 received. A process may operate on a virtual circuit socket, a terminal
385 or a file with blocking or non-blocking input/output
386 operations without distinguishing the descriptor type.
387 .Sh 4 "Shutting down halves of full-duplex connections
389 A process that has a full-duplex socket such as a virtual circuit
390 and no longer wishes to read from or write to this socket can
393 .Fd shutdown 2 "shut down part of a full-duplex connection
394 shutdown(s, direction);
397 where \fIdirection\fP is 0 to not read further, 1 to not
398 write further, or 2 to completely shut the connection down.
399 If the underlying protocol supports unidirectional or bidirectional shutdown,
400 this indication will be passed to the peer.
401 For example, a shutdown for writing might produce an end-of-file
402 condition at the remote end.
403 .Sh 4 "Socket and protocol options
405 Sockets, and their underlying communication protocols, may
406 support \fIoptions\fP. These options may be used to manipulate
407 implementation- or protocol-specific facilities.
412 calls are used to control options:
414 .Fd getsockopt 5 "get options on socket
415 getsockopt(s, level, optname, optval, optlen);
416 int s, level, optname; result void *optval; result int *optlen;
419 .Fd setsockopt 5 "set options on socket
420 setsockopt(s, level, optname, optval, optlen);
421 int s, level, optname; void *optval; int optlen;
423 The option \fIoptname\fP is interpreted at the indicated
424 protocol \fIlevel\fP for socket \fIs\fP. If a value is specified
425 with \fIoptval\fP and \fIoptlen\fP, it is interpreted by
426 the software operating at the specified \fIlevel\fP. The \fIlevel\fP
427 SOL_SOCKET is reserved to indicate options maintained
428 by the socket facilities. Other \fIlevel\fP values indicate
429 a particular protocol which is to act on the option request;
430 these values are normally interpreted as a ``protocol number''
431 within the protocol family.
432 .Sh 3 "PF_LOCAL domain
434 This section describes briefly the properties of the PF_LOCAL (``UNIX'')
435 communications domain.
436 .Sh 4 "Types of sockets
439 the SOCK_STREAM abstraction provides pipe-like
440 facilities, while SOCK_DGRAM provides (usually)
441 reliable message-style communications.
444 Socket names are strings and may appear in the filesystem
446 .Sh 4 "Access rights transmission
448 The ability to pass descriptors with messages in this domain
449 allows migration of service within the system and allows
450 user processes to be used in building system facilities.
451 .Sh 3 "INTERNET domain
453 This section describes briefly how the Internet domain is
454 mapped to the model described in this section. More
455 information will be found in the document describing the
456 network implementation in 4.4BSD (SMM:18).
457 .Sh 4 "Socket types and protocols
459 SOCK_STREAM is supported by the Internet TCP protocol;
460 SOCK_DGRAM by the UDP protocol.
461 Each is layered atop the transport-level Internet Protocol (IP).
462 The Internet Control Message Protocol is implemented atop/beside IP
463 and is accessible via a raw socket.
465 has no direct Internet family analogue; a protocol
466 based on one from the XEROX NS family and layered on
467 top of IP could be implemented to fill this gap.
470 Sockets in the Internet domain have names composed of a 32-bit
471 Internet address and a 16-bit port number.
472 Options may be used to
473 provide IP source routing or security options.
474 The 32-bit address is composed of network and host parts;
475 the network part is variable in size and is frequency encoded.
476 The host part may optionally be interpreted as a subnet field
477 plus the host on the subnet; this is is enabled by setting a network address
479 .Sh 4 "Access rights transmission
481 No access rights transmission facilities are provided in the Internet domain.
484 The Internet domain allows the super-user access to the raw facilities
486 These interfaces are modeled as SOCK_RAW sockets.
487 Each raw socket is associated with one IP protocol number,
488 and receives all traffic received for that protocol.
489 This approach allows administrative and debugging
491 and enables user-level implementations of special-purpose protocols
492 such as inter-gateway routing protocols.