home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / share / doc / ps1 / 06.sysman / 2.3.t < prev    next >
Encoding:
Text File  |  1991-04-17  |  15.6 KB  |  413 lines

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