xref: /original-bsd/share/doc/psd/05.sysman/2.3.t (revision c3e32dec)
Copyright (c) 1983, 1993
The Regents of the University of California. All rights reserved.

%sccs.include.redist.roff%

@(#)2.3.t 8.1 (Berkeley) 06/08/93

.sh "Interprocess communications Interprocess communication primitives Communication domains

The system provides access to an extensible set of communication domains. A communication domain is identified by a manifest constant defined in the file <sys/socket.h>. Important standard domains supported by the system are the ``unix'' domain, AF_UNIX, for communication within the system, the ``Internet'' domain for communication in the DARPA Internet, AF_INET, and the ``NS'' domain, AF_NS, for communication using the Xerox Network Systems protocols. Other domains can be added to the system. Socket types and protocols

Within a domain, communication takes place between communication endpoints known as sockets. Each socket has the potential to exchange information with other sockets of an appropriate type within the domain.

Each socket has an associated abstract type, which describes the semantics of communication using that socket. Properties such as reliability, ordering, and prevention of duplication of messages are determined by the type. The basic set of socket types is defined in <sys/socket.h>: /* Standard socket types */ ._d #define SOCK_DGRAM 1 /* datagram */ #define SOCK_STREAM 2 /* virtual circuit */ #define SOCK_RAW 3 /* raw socket */ #define SOCK_RDM 4 /* reliably-delivered message */ #define SOCK_SEQPACKET 5 /* sequenced packets */ The SOCK_DGRAM type models the semantics of datagrams in network communication: messages may be lost or duplicated and may arrive out-of-order. A datagram socket may send messages to and receive messages from multiple peers. The SOCK_RDM type models the semantics of reliable datagrams: messages arrive unduplicated and in-order, the sender is notified if messages are lost. The send and receive operations (described below) generate reliable/unreliable datagrams. The SOCK_STREAM type models connection-based virtual circuits: two-way byte streams with no record boundaries. Connection setup is required before data communication may begin. The SOCK_SEQPACKET type models a connection-based, full-duplex, reliable, sequenced packet exchange; the sender is notified if messages are lost, and messages are never duplicated or presented out-of-order. Users of the last two abstractions may use the facilities for out-of-band transmission to send out-of-band data.

SOCK_RAW is used for unprocessed access to internal network layers and interfaces; it has no specific semantics.

Other socket types can be defined.

Each socket may have a specific protocol associated with it. This protocol is used within the domain to provide the semantics required by the socket type. Not all socket types are supported by each domain; support depends on the existence and the implementation of a suitable protocol within the domain. For example, within the ``Internet'' domain, the SOCK_DGRAM type may be implemented by the UDP user datagram protocol, and the SOCK_STREAM type may be implemented by the TCP transmission control protocol, while no standard protocols to provide SOCK_RDM or SOCK_SEQPACKET sockets exist. Socket creation, naming and service establishment

Sockets may be connected or unconnected. An unconnected socket descriptor is obtained by the socket call: s = socket(domain, type, protocol); result int s; int domain, type, protocol; The socket domain and type are as described above, and are specified using the definitions from <sys/socket.h>. The protocol may be given as 0, meaning any suitable protocol. One of several possible protocols may be selected using identifiers obtained from a library routine, getprotobyname.

An unconnected socket descriptor of a connection-oriented type may yield a connected socket descriptor in one of two ways: either by actively connecting to another socket, or by becoming associated with a name in the communications domain and accepting a connection from another socket. Datagram sockets need not establish connections before use.

To accept connections or to receive datagrams, a socket must first have a binding to a name (or address) within the communications domain. Such a binding may be established by a bind call: bind(s, name, namelen); int s; struct sockaddr *name; int namelen; Datagram sockets may have default bindings established when first sending data if not explicitly bound earlier. In either case, a socket's bound name may be retrieved with a getsockname call: getsockname(s, name, namelen); int s; result struct sockaddr *name; result int *namelen; while the peer's name can be retrieved with getpeername: getpeername(s, name, namelen); int s; result struct sockaddr *name; result int *namelen; Domains may support sockets with several names. Accepting connections

Once a binding is made to a connection-oriented socket, it is possible to listen for connections: listen(s, backlog); int s, backlog; The backlog specifies the maximum count of connections that can be simultaneously queued awaiting acceptance.

An accept call: t = accept(s, name, anamelen); result int t; int s; result struct sockaddr *name; result int *anamelen; returns a descriptor for a new, connected, socket from the queue of pending connections on s. If no new connections are queued for acceptance, the call will wait for a connection unless non-blocking I/O has been enabled. Making connections

An active connection to a named socket is made by the connect call: connect(s, name, namelen); int s; struct sockaddr *name; int namelen; Although datagram sockets do not establish connections, the connect call may be used with such sockets to create an association with the foreign address. The address is recorded for use in future send calls, which then need not supply destination addresses. Datagrams will be received only from that peer, and asynchronous error reports may be received.

It is also possible to create connected pairs of sockets without using the domain's name space to rendezvous; this is done with the socketpair call\(dg: .FS \(dg 4.3BSD supports socketpair creation only in the ``unix'' communication domain. .FE socketpair(domain, type, protocol, sv); int domain, type, protocol; result int sv[2]; Here the returned sv descriptors correspond to those obtained with accept and connect.

The call pipe(pv) result int pv[2]; creates a pair of SOCK_STREAM sockets in the UNIX domain, with pv[0] only writable and pv[1] only readable. Sending and receiving data

Messages may be sent from a socket by: cc = sendto(s, buf, len, flags, to, tolen); result int cc; int s; caddr_t buf; int len, flags; caddr_t to; int tolen; if the socket is not connected or: cc = send(s, buf, len, flags); result int cc; int s; caddr_t buf; int len, flags; if the socket is connected. The corresponding receive primitives are: msglen = recvfrom(s, buf, len, flags, from, fromlenaddr); result int msglen; int s; result caddr_t buf; int len, flags; result caddr_t from; result int *fromlenaddr; and msglen = recv(s, buf, len, flags); result int msglen; int s; result caddr_t buf; int len, flags;

In the unconnected case, the parameters to and tolen specify the destination or source of the message, while the from parameter stores the source of the message, and *fromlenaddr initially gives the size of the from buffer and is updated to reflect the true length of the from address.

All calls cause the message to be received in or sent from the message buffer of length len bytes, starting at address buf. The flags specify peeking at a message without reading it or sending or receiving high-priority out-of-band messages, as follows: ._d #define MSG_PEEK 0x1 /* peek at incoming message */ #define MSG_OOB 0x2 /* process out-of-band data */ Scatter/gather and exchanging access rights

It is possible scatter and gather data and to exchange access rights with messages. When either of these operations is involved, the number of parameters to the call becomes large. Thus the system defines a message header structure, in <sys/socket.h>, which can be used to conveniently contain the parameters to the calls: struct msghdr { caddr_t msg_name; /* optional address */ int msg_namelen; /* size of address */ struct iov *msg_iov; /* scatter/gather array */ int msg_iovlen; /* # elements in msg_iov */ caddr_t msg_accrights; /* access rights sent/received */ int msg_accrightslen; /* size of msg_accrights */ }; Here msg_name and msg_namelen specify the source or destination address if the socket is unconnected; msg_name may be given as a null pointer if no names are desired or required. The msg_iov and msg_iovlen describe the scatter/gather locations, as described in section 2.1.3. Access rights to be sent along with the message are specified in msg_accrights, which has length msg_accrightslen. In the ``unix'' domain these are an array of integer descriptors, taken from the sending process and duplicated in the receiver.

This structure is used in the operations sendmsg and recvmsg: sendmsg(s, msg, flags); int s; struct msghdr *msg; int flags; msglen = recvmsg(s, msg, flags); result int msglen; int s; result struct msghdr *msg; int flags; Using read and write with sockets

The normal UNIX read and write calls may be applied to connected sockets and translated into send and receive calls from or to a single area of memory and discarding any rights received. A process may operate on a virtual circuit socket, a terminal or a file with blocking or non-blocking input/output operations without distinguishing the descriptor type. Shutting down halves of full-duplex connections

A process that has a full-duplex socket such as a virtual circuit and no longer wishes to read from or write to this socket can give the call: shutdown(s, direction); int s, direction; where direction is 0 to not read further, 1 to not write further, or 2 to completely shut the connection down. If the underlying protocol supports unidirectional or bidirectional shutdown, this indication will be passed to the peer. For example, a shutdown for writing might produce an end-of-file condition at the remote end. Socket and protocol options

Sockets, and their underlying communication protocols, may support options. These options may be used to manipulate implementation- or protocol-specific facilities. The getsockopt and setsockopt calls are used to control options: getsockopt(s, level, optname, optval, optlen) int s, level, optname; result caddr_t optval; result int *optlen; setsockopt(s, level, optname, optval, optlen) int s, level, optname; caddr_t optval; int optlen; The option optname is interpreted at the indicated protocol level for socket s. If a value is specified with optval and optlen, it is interpreted by the software operating at the specified level. The level SOL_SOCKET is reserved to indicate options maintained by the socket facilities. Other level values indicate a particular protocol which is to act on the option request; these values are normally interpreted as a ``protocol number''. UNIX domain

This section describes briefly the properties of the UNIX communications domain. Types of sockets

In the UNIX domain, the SOCK_STREAM abstraction provides pipe-like facilities, while SOCK_DGRAM provides (usually) reliable message-style communications. Naming

Socket names are strings and may appear in the UNIX file system name space through portals\(dg. .FS \(dg The 4.3BSD implementation of the UNIX domain embeds bound sockets in the UNIX file system name space; this may change in future releases. .FE Access rights transmission

The ability to pass UNIX descriptors with messages in this domain allows migration of service within the system and allows user processes to be used in building system facilities. INTERNET domain

This section describes briefly how the Internet domain is mapped to the model described in this section. More information will be found in the document describing the network implementation in 4.3BSD. Socket types and protocols

SOCK_STREAM is supported by the Internet TCP protocol; SOCK_DGRAM by the UDP protocol. Each is layered atop the transport-level Internet Protocol (IP). The Internet Control Message Protocol is implemented atop/beside IP and is accessible via a raw socket. The SOCK_SEQPACKET has no direct Internet family analogue; a protocol based on one from the XEROX NS family and layered on top of IP could be implemented to fill this gap. Socket naming

Sockets in the Internet domain have names composed of the 32 bit Internet address, and a 16 bit port number. Options may be used to provide IP source routing or security options. The 32-bit address is composed of network and host parts; the network part is variable in size and is frequency encoded. The host part may optionally be interpreted as a subnet field plus the host on subnet; this is is enabled by setting a network address mask at boot time. Access rights transmission

No access rights transmission facilities are provided in the Internet domain. Raw access

The Internet domain allows the super-user access to the raw facilities of IP. These interfaces are modeled as SOCK_RAW sockets. Each raw socket is associated with one IP protocol number, and receives all traffic received for that protocol. This allows administrative and debugging functions to occur, and enables user-level implementations of special-purpose protocols such as inter-gateway routing protocols.