xref: /freebsd/lib/libsys/socket.2 (revision 8269e7673cf033aba67dab8264fe719920c70f87)
1*8269e767SBrooks Davis.\" Copyright (c) 1983, 1991, 1993
2*8269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
3*8269e767SBrooks Davis.\"
4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
6*8269e767SBrooks Davis.\" are met:
7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
8*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
10*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
11*8269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
13*8269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
14*8269e767SBrooks Davis.\"    without specific prior written permission.
15*8269e767SBrooks Davis.\"
16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8269e767SBrooks Davis.\" SUCH DAMAGE.
27*8269e767SBrooks Davis.\"
28*8269e767SBrooks Davis.Dd January 15, 2023
29*8269e767SBrooks Davis.Dt SOCKET 2
30*8269e767SBrooks Davis.Os
31*8269e767SBrooks Davis.Sh NAME
32*8269e767SBrooks Davis.Nm socket
33*8269e767SBrooks Davis.Nd create an endpoint for communication
34*8269e767SBrooks Davis.Sh LIBRARY
35*8269e767SBrooks Davis.Lb libc
36*8269e767SBrooks Davis.Sh SYNOPSIS
37*8269e767SBrooks Davis.In sys/socket.h
38*8269e767SBrooks Davis.Ft int
39*8269e767SBrooks Davis.Fn socket "int domain" "int type" "int protocol"
40*8269e767SBrooks Davis.Sh DESCRIPTION
41*8269e767SBrooks DavisThe
42*8269e767SBrooks Davis.Fn socket
43*8269e767SBrooks Davissystem call
44*8269e767SBrooks Daviscreates an endpoint for communication and returns a descriptor.
45*8269e767SBrooks Davis.Pp
46*8269e767SBrooks DavisThe
47*8269e767SBrooks Davis.Fa domain
48*8269e767SBrooks Davisargument specifies a communications domain within which
49*8269e767SBrooks Daviscommunication will take place; this selects the protocol family
50*8269e767SBrooks Daviswhich should be used.
51*8269e767SBrooks DavisThese families are defined in the include file
52*8269e767SBrooks Davis.In sys/socket.h .
53*8269e767SBrooks DavisThe currently understood formats are:
54*8269e767SBrooks Davis.Pp
55*8269e767SBrooks Davis.Bd -literal -offset indent -compact
56*8269e767SBrooks DavisPF_LOCAL	Host-internal protocols (alias for PF_UNIX),
57*8269e767SBrooks DavisPF_UNIX		Host-internal protocols,
58*8269e767SBrooks DavisPF_INET		Internet version 4 protocols,
59*8269e767SBrooks DavisPF_INET6	Internet version 6 protocols,
60*8269e767SBrooks DavisPF_DIVERT	Firewall packet diversion/re-injection,
61*8269e767SBrooks DavisPF_ROUTE	Internal routing protocol,
62*8269e767SBrooks DavisPF_KEY		Internal key-management function,
63*8269e767SBrooks DavisPF_NETGRAPH	Netgraph sockets,
64*8269e767SBrooks DavisPF_NETLINK	Netlink protocols,
65*8269e767SBrooks DavisPF_BLUETOOTH	Bluetooth protocols,
66*8269e767SBrooks DavisPF_INET_SDP	OFED socket direct protocol (IPv4),
67*8269e767SBrooks DavisAF_HYPERV	HyperV sockets
68*8269e767SBrooks Davis.Ed
69*8269e767SBrooks Davis.Pp
70*8269e767SBrooks DavisEach protocol family is connected to an address family, which has the
71*8269e767SBrooks Davissame name except that the prefix is
72*8269e767SBrooks Davis.Dq Dv AF_
73*8269e767SBrooks Davisin place of
74*8269e767SBrooks Davis.Dq Dv PF_ .
75*8269e767SBrooks DavisOther protocol families may be also defined, beginning with
76*8269e767SBrooks Davis.Dq Dv PF_ ,
77*8269e767SBrooks Daviswith corresponding address families.
78*8269e767SBrooks Davis.Pp
79*8269e767SBrooks DavisThe socket has the indicated
80*8269e767SBrooks Davis.Fa type ,
81*8269e767SBrooks Daviswhich specifies the semantics of communication.
82*8269e767SBrooks DavisCurrently
83*8269e767SBrooks Davisdefined types are:
84*8269e767SBrooks Davis.Pp
85*8269e767SBrooks Davis.Bd -literal -offset indent -compact
86*8269e767SBrooks DavisSOCK_STREAM	Stream socket,
87*8269e767SBrooks DavisSOCK_DGRAM	Datagram socket,
88*8269e767SBrooks DavisSOCK_RAW	Raw-protocol interface,
89*8269e767SBrooks DavisSOCK_SEQPACKET	Sequenced packet stream
90*8269e767SBrooks Davis.Ed
91*8269e767SBrooks Davis.Pp
92*8269e767SBrooks DavisA
93*8269e767SBrooks Davis.Dv SOCK_STREAM
94*8269e767SBrooks Davistype provides sequenced, reliable,
95*8269e767SBrooks Davistwo-way connection based byte streams.
96*8269e767SBrooks DavisAn out-of-band data transmission mechanism may be supported.
97*8269e767SBrooks DavisA
98*8269e767SBrooks Davis.Dv SOCK_DGRAM
99*8269e767SBrooks Davissocket supports
100*8269e767SBrooks Davisdatagrams (connectionless, unreliable messages of
101*8269e767SBrooks Davisa fixed (typically small) maximum length).
102*8269e767SBrooks DavisA
103*8269e767SBrooks Davis.Dv SOCK_SEQPACKET
104*8269e767SBrooks Davissocket may provide a sequenced, reliable,
105*8269e767SBrooks Davistwo-way connection-based data transmission path for datagrams
106*8269e767SBrooks Davisof fixed maximum length; a consumer may be required to read
107*8269e767SBrooks Davisan entire packet with each read system call.
108*8269e767SBrooks DavisThis facility may have protocol-specific properties.
109*8269e767SBrooks Davis.Dv SOCK_RAW
110*8269e767SBrooks Davissockets provide access to internal network protocols and interfaces.
111*8269e767SBrooks DavisThe
112*8269e767SBrooks Davis.Dv SOCK_RAW
113*8269e767SBrooks Davistype is available only to the super-user and is described in
114*8269e767SBrooks Davis.Xr ip 4
115*8269e767SBrooks Davisand
116*8269e767SBrooks Davis.Xr ip6 4 .
117*8269e767SBrooks Davis.Pp
118*8269e767SBrooks DavisAdditionally, the following flags are allowed in the
119*8269e767SBrooks Davis.Fa type
120*8269e767SBrooks Davisargument:
121*8269e767SBrooks Davis.Pp
122*8269e767SBrooks Davis.Bd -literal -offset indent -compact
123*8269e767SBrooks DavisSOCK_CLOEXEC	Set close-on-exec on the new descriptor,
124*8269e767SBrooks DavisSOCK_NONBLOCK	Set non-blocking mode on the new socket
125*8269e767SBrooks Davis.Ed
126*8269e767SBrooks Davis.Pp
127*8269e767SBrooks DavisThe
128*8269e767SBrooks Davis.Fa protocol
129*8269e767SBrooks Davisargument
130*8269e767SBrooks Davisspecifies a particular protocol to be used with the socket.
131*8269e767SBrooks DavisNormally only a single protocol exists to support a particular
132*8269e767SBrooks Davissocket type within a given protocol family.
133*8269e767SBrooks DavisHowever, it is possible
134*8269e767SBrooks Davisthat many protocols may exist, in which case a particular protocol
135*8269e767SBrooks Davismust be specified in this manner.
136*8269e767SBrooks DavisThe protocol number to use is
137*8269e767SBrooks Davisparticular to the
138*8269e767SBrooks Davis.Dq "communication domain"
139*8269e767SBrooks Davisin which communication
140*8269e767SBrooks Davisis to take place; see
141*8269e767SBrooks Davis.Xr protocols 5 .
142*8269e767SBrooks Davis.Pp
143*8269e767SBrooks DavisThe
144*8269e767SBrooks Davis.Fa protocol
145*8269e767SBrooks Davisargument may be set to zero (0) to request the default
146*8269e767SBrooks Davisimplementation of a socket type for the protocol, if any.
147*8269e767SBrooks Davis.Pp
148*8269e767SBrooks DavisSockets of type
149*8269e767SBrooks Davis.Dv SOCK_STREAM
150*8269e767SBrooks Davisare full-duplex byte streams, similar
151*8269e767SBrooks Davisto pipes.
152*8269e767SBrooks DavisA stream socket must be in a
153*8269e767SBrooks Davis.Em connected
154*8269e767SBrooks Davisstate before any data may be sent or received
155*8269e767SBrooks Davison it.
156*8269e767SBrooks DavisA connection to another socket is created with a
157*8269e767SBrooks Davis.Xr connect 2
158*8269e767SBrooks Davissystem call.
159*8269e767SBrooks DavisOnce connected, data may be transferred using
160*8269e767SBrooks Davis.Xr read 2
161*8269e767SBrooks Davisand
162*8269e767SBrooks Davis.Xr write 2
163*8269e767SBrooks Daviscalls or some variant of the
164*8269e767SBrooks Davis.Xr send 2
165*8269e767SBrooks Davisand
166*8269e767SBrooks Davis.Xr recv 2
167*8269e767SBrooks Davisfunctions.
168*8269e767SBrooks Davis(Some protocol families, such as the Internet family,
169*8269e767SBrooks Davissupport the notion of an
170*8269e767SBrooks Davis.Dq implied connect ,
171*8269e767SBrooks Daviswhich permits data to be sent piggybacked onto a connect operation by
172*8269e767SBrooks Davisusing the
173*8269e767SBrooks Davis.Xr sendto 2
174*8269e767SBrooks Davissystem call.)
175*8269e767SBrooks DavisWhen a session has been completed a
176*8269e767SBrooks Davis.Xr close 2
177*8269e767SBrooks Davismay be performed.
178*8269e767SBrooks DavisOut-of-band data may also be transmitted as described in
179*8269e767SBrooks Davis.Xr send 2
180*8269e767SBrooks Davisand received as described in
181*8269e767SBrooks Davis.Xr recv 2 .
182*8269e767SBrooks Davis.Pp
183*8269e767SBrooks DavisThe communications protocols used to implement a
184*8269e767SBrooks Davis.Dv SOCK_STREAM
185*8269e767SBrooks Davisensure that data
186*8269e767SBrooks Davisis not lost or duplicated.
187*8269e767SBrooks DavisIf a piece of data for which the
188*8269e767SBrooks Davispeer protocol has buffer space cannot be successfully transmitted
189*8269e767SBrooks Daviswithin a reasonable length of time, then
190*8269e767SBrooks Davisthe connection is considered broken and calls
191*8269e767SBrooks Daviswill indicate an error with
192*8269e767SBrooks Davis-1 returns and with
193*8269e767SBrooks Davis.Er ETIMEDOUT
194*8269e767SBrooks Davisas the specific code
195*8269e767SBrooks Davisin the global variable
196*8269e767SBrooks Davis.Va errno .
197*8269e767SBrooks DavisThe protocols optionally keep sockets
198*8269e767SBrooks Davis.Dq warm
199*8269e767SBrooks Davisby forcing transmissions
200*8269e767SBrooks Davisroughly every minute in the absence of other activity.
201*8269e767SBrooks DavisAn error is then indicated if no response can be
202*8269e767SBrooks Daviselicited on an otherwise
203*8269e767SBrooks Davisidle connection for an extended period (e.g.\& 5 minutes).
204*8269e767SBrooks DavisBy default, a
205*8269e767SBrooks Davis.Dv SIGPIPE
206*8269e767SBrooks Davissignal is raised if a process sends
207*8269e767SBrooks Davison a broken stream, but this behavior may be inhibited via
208*8269e767SBrooks Davis.Xr setsockopt 2 .
209*8269e767SBrooks Davis.Pp
210*8269e767SBrooks Davis.Dv SOCK_SEQPACKET
211*8269e767SBrooks Davissockets employ the same system calls
212*8269e767SBrooks Davisas
213*8269e767SBrooks Davis.Dv SOCK_STREAM
214*8269e767SBrooks Davissockets.
215*8269e767SBrooks DavisThe only difference
216*8269e767SBrooks Davisis that
217*8269e767SBrooks Davis.Xr read 2
218*8269e767SBrooks Daviscalls will return only the amount of data requested,
219*8269e767SBrooks Davisand any remaining in the arriving packet will be discarded.
220*8269e767SBrooks Davis.Pp
221*8269e767SBrooks Davis.Dv SOCK_DGRAM
222*8269e767SBrooks Davisand
223*8269e767SBrooks Davis.Dv SOCK_RAW
224*8269e767SBrooks Davissockets allow sending of datagrams to correspondents
225*8269e767SBrooks Davisnamed in
226*8269e767SBrooks Davis.Xr send 2
227*8269e767SBrooks Daviscalls.
228*8269e767SBrooks DavisDatagrams are generally received with
229*8269e767SBrooks Davis.Xr recvfrom 2 ,
230*8269e767SBrooks Daviswhich returns the next datagram with its return address.
231*8269e767SBrooks Davis.Pp
232*8269e767SBrooks DavisAn
233*8269e767SBrooks Davis.Xr fcntl 2
234*8269e767SBrooks Davissystem call can be used to specify a process group to receive
235*8269e767SBrooks Davisa
236*8269e767SBrooks Davis.Dv SIGURG
237*8269e767SBrooks Davissignal when the out-of-band data arrives.
238*8269e767SBrooks DavisIt may also enable non-blocking I/O
239*8269e767SBrooks Davisand asynchronous notification of I/O events
240*8269e767SBrooks Davisvia
241*8269e767SBrooks Davis.Dv SIGIO .
242*8269e767SBrooks Davis.Pp
243*8269e767SBrooks DavisThe operation of sockets is controlled by socket level
244*8269e767SBrooks Davis.Em options .
245*8269e767SBrooks DavisThese options are defined in the file
246*8269e767SBrooks Davis.In sys/socket.h .
247*8269e767SBrooks DavisThe
248*8269e767SBrooks Davis.Xr setsockopt 2
249*8269e767SBrooks Davisand
250*8269e767SBrooks Davis.Xr getsockopt 2
251*8269e767SBrooks Davissystem calls are used to set and get options, respectively.
252*8269e767SBrooks Davis.Sh RETURN VALUES
253*8269e767SBrooks DavisA -1 is returned if an error occurs, otherwise the return
254*8269e767SBrooks Davisvalue is a descriptor referencing the socket.
255*8269e767SBrooks Davis.Sh ERRORS
256*8269e767SBrooks DavisThe
257*8269e767SBrooks Davis.Fn socket
258*8269e767SBrooks Davissystem call fails if:
259*8269e767SBrooks Davis.Bl -tag -width Er
260*8269e767SBrooks Davis.It Bq Er EACCES
261*8269e767SBrooks DavisPermission to create a socket of the specified type and/or protocol
262*8269e767SBrooks Davisis denied.
263*8269e767SBrooks Davis.It Bq Er EAFNOSUPPORT
264*8269e767SBrooks DavisThe address family (domain) is not supported or the
265*8269e767SBrooks Davisspecified domain is not supported by this protocol family.
266*8269e767SBrooks Davis.It Bq Er EMFILE
267*8269e767SBrooks DavisThe per-process descriptor table is full.
268*8269e767SBrooks Davis.It Bq Er ENFILE
269*8269e767SBrooks DavisThe system file table is full.
270*8269e767SBrooks Davis.It Bq Er ENOBUFS
271*8269e767SBrooks DavisInsufficient buffer space is available.
272*8269e767SBrooks DavisThe socket cannot be created until sufficient resources are freed.
273*8269e767SBrooks Davis.It Bq Er EPERM
274*8269e767SBrooks DavisUser has insufficient privileges to carry out the requested operation.
275*8269e767SBrooks Davis.It Bq Er EPROTONOSUPPORT
276*8269e767SBrooks DavisThe protocol type or the specified protocol is not supported
277*8269e767SBrooks Daviswithin this domain.
278*8269e767SBrooks Davis.It Bq Er EPROTOTYPE
279*8269e767SBrooks DavisThe socket type is not supported by the protocol.
280*8269e767SBrooks Davis.El
281*8269e767SBrooks Davis.Sh SEE ALSO
282*8269e767SBrooks Davis.Xr accept 2 ,
283*8269e767SBrooks Davis.Xr bind 2 ,
284*8269e767SBrooks Davis.Xr connect 2 ,
285*8269e767SBrooks Davis.Xr divert 4 ,
286*8269e767SBrooks Davis.Xr getpeername 2 ,
287*8269e767SBrooks Davis.Xr getsockname 2 ,
288*8269e767SBrooks Davis.Xr getsockopt 2 ,
289*8269e767SBrooks Davis.Xr ioctl 2 ,
290*8269e767SBrooks Davis.Xr ip 4 ,
291*8269e767SBrooks Davis.Xr ip6 4 ,
292*8269e767SBrooks Davis.Xr listen 2 ,
293*8269e767SBrooks Davis.Xr read 2 ,
294*8269e767SBrooks Davis.Xr recv 2 ,
295*8269e767SBrooks Davis.Xr select 2 ,
296*8269e767SBrooks Davis.Xr send 2 ,
297*8269e767SBrooks Davis.Xr shutdown 2 ,
298*8269e767SBrooks Davis.Xr socketpair 2 ,
299*8269e767SBrooks Davis.Xr write 2 ,
300*8269e767SBrooks Davis.Xr CMSG_DATA 3 ,
301*8269e767SBrooks Davis.Xr getprotoent 3 ,
302*8269e767SBrooks Davis.Xr netgraph 4 ,
303*8269e767SBrooks Davis.Xr protocols 5
304*8269e767SBrooks Davis.Rs
305*8269e767SBrooks Davis.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial"
306*8269e767SBrooks Davis.%B PS1
307*8269e767SBrooks Davis.%N 7
308*8269e767SBrooks Davis.Re
309*8269e767SBrooks Davis.Rs
310*8269e767SBrooks Davis.%T "BSD Interprocess Communication Tutorial"
311*8269e767SBrooks Davis.%B PS1
312*8269e767SBrooks Davis.%N 8
313*8269e767SBrooks Davis.Re
314*8269e767SBrooks Davis.Sh STANDARDS
315*8269e767SBrooks DavisThe
316*8269e767SBrooks Davis.Fn socket
317*8269e767SBrooks Davisfunction conforms to
318*8269e767SBrooks Davis.St -p1003.1-2008 .
319*8269e767SBrooks DavisThe
320*8269e767SBrooks Davis.Tn POSIX
321*8269e767SBrooks Davisstandard specifies only the
322*8269e767SBrooks Davis.Dv AF_INET ,
323*8269e767SBrooks Davis.Dv AF_INET6 ,
324*8269e767SBrooks Davisand
325*8269e767SBrooks Davis.Dv AF_UNIX
326*8269e767SBrooks Davisconstants for address families, and requires the use of
327*8269e767SBrooks Davis.Dv AF_*
328*8269e767SBrooks Davisconstants for the
329*8269e767SBrooks Davis.Fa domain
330*8269e767SBrooks Davisargument of
331*8269e767SBrooks Davis.Fn socket .
332*8269e767SBrooks DavisThe
333*8269e767SBrooks Davis.Dv SOCK_CLOEXEC
334*8269e767SBrooks Davisflag is expected to conform to the next revision of the
335*8269e767SBrooks Davis.Tn POSIX
336*8269e767SBrooks Davisstandard.
337*8269e767SBrooks DavisThe
338*8269e767SBrooks Davis.Dv SOCK_RDM
339*8269e767SBrooks Davis.Fa type ,
340*8269e767SBrooks Davisthe
341*8269e767SBrooks Davis.Dv PF_*
342*8269e767SBrooks Davisconstants, and other address families are
343*8269e767SBrooks Davis.Fx
344*8269e767SBrooks Davisextensions.
345*8269e767SBrooks Davis.Sh HISTORY
346*8269e767SBrooks DavisThe
347*8269e767SBrooks Davis.Fn socket
348*8269e767SBrooks Davissystem call appeared in
349*8269e767SBrooks Davis.Bx 4.2 .
350