Merge branch 'vendor/MDOCML'
[dragonfly.git] / lib / libc / sys / socket.2
1 .\" Copyright (c) 1983, 1991, 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 .\"     From: @(#)socket.2      8.1 (Berkeley) 6/4/93
29 .\" $FreeBSD: src/lib/libc/sys/socket.2,v 1.12.2.11 2002/12/29 16:35:34 schweikh Exp $
30 .\"
31 .Dd November 24, 1997
32 .Dt SOCKET 2
33 .Os
34 .Sh NAME
35 .Nm socket
36 .Nd create an endpoint for communication
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In sys/types.h
41 .In sys/socket.h
42 .Ft int
43 .Fn socket "int domain" "int type" "int protocol"
44 .Sh DESCRIPTION
45 .Fn Socket
46 creates an endpoint for communication and returns a descriptor.
47 .Pp
48 The
49 .Fa domain
50 parameter specifies a communications domain within which
51 communication will take place; this selects the protocol family
52 which should be used.
53 These families are defined in the include file
54 .In sys/socket.h .
55 The currently understood formats are:
56 .Pp
57 .Bd -literal -offset indent -compact
58 PF_LOCAL        Host-internal protocols, formerly called PF_UNIX,
59 PF_UNIX         Host-internal protocols, deprecated, use PF_LOCAL,
60 PF_INET         Internet version 4 protocols,
61 PF_IMPLINK      ARPAnet IMP addresses,
62 PF_PUP          PUP protocols, like BSP,
63 PF_CHAOS        MIT CHAOS protocols,
64 PF_NS           Xerox Network Systems protocols,
65 PF_ISO          ISO protocols,
66 PF_OSI          Open Systems Interconnection protocols,
67 PF_ECMA         European Computer Manufacturers,
68 PF_DATAKIT      Datakit protocols,
69 PF_CCITT        ITU-T protocols, like X.25,
70 PF_SNA          IBM SNA,
71 PF_DECnet       DECnet,
72 PF_DLI          DEC Direct Data Link Interface protocol,
73 PF_LAT          LAT protocol,
74 PF_HYLINK       NSC Hyperchannel,
75 PF_ROUTE        Internal Routing protocol,
76 PF_LINK         Link layer interface,
77 PF_XTP          eXpress Transfer Protocol,
78 PF_COIP         Connection-Oriented IP, aka ST II,
79 PF_CNT          Computer Network Technology,
80 PF_SIP          Simple Internet Protocol,
81 PF_IPX          Novell Internet Packet eXchange protocol,
82 PF_RTIP         Help Identify RTIP packets,
83 PF_PIP          Help Identify PIP packets,
84 PF_ISDN         Integrated Services Digital Network,
85 PF_KEY          Internal key-management function,
86 PF_INET6        Internet version 6 protocols,
87 PF_NATM         Native ATM access,
88 PF_ATM          ATM,
89 PF_NETGRAPH     Netgraph sockets
90 .Ed
91 .Pp
92 The socket has the indicated
93 .Fa type ,
94 which specifies the semantics of communication.  Currently
95 defined types are:
96 .Pp
97 .Bd -literal -offset indent -compact
98 SOCK_STREAM     Stream socket,
99 SOCK_DGRAM      Datagram socket,
100 SOCK_RAW        Raw-protocol interface,
101 SOCK_RDM        Reliably-delivered packet,
102 SOCK_SEQPACKET  Sequenced packet stream
103 .Ed
104 .Pp
105 A
106 .Dv SOCK_STREAM
107 type provides sequenced, reliable,
108 two-way connection based byte streams.
109 An out-of-band data transmission mechanism may be supported.
110 A
111 .Dv SOCK_DGRAM
112 socket supports
113 datagrams (connectionless, unreliable messages of
114 a fixed (typically small) maximum length).
115 A
116 .Dv SOCK_SEQPACKET
117 socket may provide a sequenced, reliable,
118 two-way connection-based data transmission path for datagrams
119 of fixed maximum length; a consumer may be required to read
120 an entire packet with each read system call.
121 This facility is protocol specific, and presently implemented
122 only for
123 .Dv PF_NS
124 and
125 .Dv PF_UNIX .
126 .Dv SOCK_RAW
127 sockets provide access to internal network protocols and interfaces.
128 The types
129 .Dv SOCK_RAW ,
130 which is available only to the super-user, and
131 .Dv SOCK_RDM ,
132 which is planned,
133 but not yet implemented, are not described here.
134 .Pp
135 The
136 .Fa protocol
137 specifies a particular protocol to be used with the socket.
138 Normally only a single protocol exists to support a particular
139 socket type within a given protocol family.  However, it is possible
140 that many protocols may exist, in which case a particular protocol
141 must be specified in this manner.  The protocol number to use is
142 particular to the
143 .Dq "communication domain"
144 in which communication
145 is to take place; see
146 .Xr protocols 5 .
147 .Pp
148 Sockets of type
149 .Dv SOCK_STREAM
150 are full-duplex byte streams, similar
151 to pipes.  A stream socket must be in a
152 .Em connected
153 state before any data may be sent or received
154 on it.  A connection to another socket is created with a
155 .Xr connect 2
156 call.
157 Once connected, data may be transferred using
158 .Xr read 2
159 and
160 .Xr write 2
161 calls or some variant of the
162 .Xr send 2
163 and
164 .Xr recv 2
165 calls.
166 (Some protocol families, such as the Internet family,
167 support the notion of an
168 .Dq implied connect ,
169 which permits data to be sent piggybacked onto a connect operation by
170 using the
171 .Xr sendto 2
172 call.)
173 When a session has been completed a
174 .Xr close 2
175 may be performed.
176 Out-of-band data may also be transmitted as described in
177 .Xr send 2
178 and received as described in
179 .Xr recv 2 .
180 .Pp
181 The communications protocols used to implement a
182 .Dv SOCK_STREAM
183 insure that data
184 is not lost or duplicated.  If a piece of data for which the
185 peer protocol has buffer space cannot be successfully transmitted
186 within a reasonable length of time, then
187 the connection is considered broken and calls
188 will indicate an error with
189 -1 returns and with
190 .Er ETIMEDOUT
191 as the specific code
192 in the global variable
193 .Va errno .
194 The protocols optionally keep sockets
195 .Dq warm
196 by forcing transmissions
197 roughly every minute in the absence of other activity.
198 An error is then indicated if no response can be
199 elicited on an otherwise
200 idle connection for an extended period (e.g. 5 minutes).
201 A
202 .Dv SIGPIPE
203 signal is raised if a process sends
204 on a broken stream; this causes naive processes,
205 which do not handle the signal, to exit.
206 .Pp
207 .Dv SOCK_SEQPACKET
208 sockets employ the same system calls
209 as
210 .Dv SOCK_STREAM
211 sockets.  The only difference
212 is that
213 .Xr read 2
214 calls will return only the amount of data requested,
215 and any remaining in the arriving packet will be discarded.
216 .Pp
217 .Dv SOCK_DGRAM
218 and
219 .Dv SOCK_RAW
220 sockets allow sending of datagrams to correspondents
221 named in
222 .Xr send 2
223 calls.  Datagrams are generally received with
224 .Xr recvfrom 2 ,
225 which returns the next datagram with its return address.
226 .Pp
227 An
228 .Xr fcntl 2
229 call can be used to specify a process group to receive
230 a
231 .Dv SIGURG
232 signal when the out-of-band data arrives.
233 It may also enable non-blocking I/O
234 and asynchronous notification of I/O events
235 via
236 .Dv SIGIO .
237 .Pp
238 The operation of sockets is controlled by socket level
239 .Em options .
240 These options are defined in the file
241 .In sys/socket.h .
242 .Xr Setsockopt 2
243 and
244 .Xr getsockopt 2
245 are used to set and get options, respectively.
246 .Sh RETURN VALUES
247 Upon successful completion
248 .Fn socket
249 returns a descriptor referencing the socket.
250 Otherwise, -1 is returned and the global variable
251 .Va errno
252 is set to indicate the error.
253 .Sh ERRORS
254 The
255 .Fn socket
256 call fails if:
257 .Bl -tag -width Er
258 .It Bq Er EPROTONOSUPPORT
259 The protocol type or the specified protocol is not supported
260 within this domain.
261 .It Bq Er EMFILE
262 The per-process descriptor table is full.
263 .It Bq Er ENFILE
264 The system file table is full.
265 .It Bq Er EACCES
266 Permission to create a socket of the specified type and/or protocol
267 is denied.
268 .It Bq Er ENOBUFS
269 Insufficient buffer space is available.
270 The socket cannot be created until sufficient resources are freed.
271 .El
272 .Sh SEE ALSO
273 .Xr accept 2 ,
274 .Xr bind 2 ,
275 .Xr connect 2 ,
276 .Xr getpeername 2 ,
277 .Xr getsockname 2 ,
278 .Xr getsockopt 2 ,
279 .Xr ioctl 2 ,
280 .Xr listen 2 ,
281 .Xr read 2 ,
282 .Xr recv 2 ,
283 .Xr select 2 ,
284 .Xr send 2 ,
285 .Xr shutdown 2 ,
286 .Xr socketpair 2 ,
287 .Xr write 2 ,
288 .Xr getprotoent 3 ,
289 .Xr netgraph 4 ,
290 .Xr protocols 5
291 .Rs
292 .%T "An Introductory 4.3 BSD Interprocess Communication Tutorial"
293 .%B PS1
294 .%N 7
295 .Re
296 .Rs
297 .%T "BSD Interprocess Communication Tutorial"
298 .%B PS1
299 .%N 8
300 .Re
301 .Sh HISTORY
302 The
303 .Fn socket
304 function call appeared in
305 .Bx 4.2 .