xref: /freebsd-12.1/lib/libc/sys/socket.2 (revision abbcaa0a)
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. 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.\"     From: @(#)socket.2	8.1 (Berkeley) 6/4/93
33.\"	$Id: socket.2,v 1.8 1997/11/23 17:58:46 bde Exp $
34.\"
35.Dd November 24, 1997
36.Dt SOCKET 2
37.Os BSD 4.2
38.Sh NAME
39.Nm socket
40.Nd create an endpoint for communication
41.Sh SYNOPSIS
42.Fd #include <sys/types.h>
43.Fd #include <sys/socket.h>
44.Ft int
45.Fn socket "int domain" "int type" "int protocol"
46.Sh DESCRIPTION
47.Fn Socket
48creates an endpoint for communication and returns a descriptor.
49.Pp
50The
51.Fa domain
52parameter specifies a communications domain within which
53communication will take place; this selects the protocol family
54which should be used.
55These families are defined in the include file
56.Ao Pa sys/socket.h Ac .
57The currently understood formats are
58.Pp
59.Bd -literal -offset indent -compact
60PF_LOCAL	(Host-internal protocols, formerly called PF_UNIX),
61PF_INET		(ARPA Internet protocols),
62PF_ISO		(ISO protocols),
63PF_CCITT	(ITU-T protocols, like X.25),
64PF_NS		(Xerox Network Systems protocols), and
65.\"PF_IMPLINK	(IMP \*(lqhost at IMP\*(rq link layer).
66.Ed
67.Pp
68The socket has the indicated
69.Fa type ,
70which specifies the semantics of communication.  Currently
71defined types are:
72.Pp
73.Bd -literal -offset indent -compact
74SOCK_STREAM
75SOCK_DGRAM
76SOCK_RAW
77SOCK_SEQPACKET
78SOCK_RDM
79.Ed
80.Pp
81A
82.Dv SOCK_STREAM
83type provides sequenced, reliable,
84two-way connection based byte streams.
85An out-of-band data transmission mechanism may be supported.
86A
87.Dv SOCK_DGRAM
88socket supports
89datagrams (connectionless, unreliable messages of
90a fixed (typically small) maximum length).
91A
92.Dv SOCK_SEQPACKET
93socket may provide a sequenced, reliable,
94two-way connection-based data transmission path for datagrams
95of fixed maximum length; a consumer may be required to read
96an entire packet with each read system call.
97This facility is protocol specific, and presently implemented
98only for
99.Dv PF_NS .
100.Dv SOCK_RAW
101sockets provide access to internal network protocols and interfaces.
102The types
103.Dv SOCK_RAW ,
104which is available only to the super-user, and
105.Dv SOCK_RDM ,
106which is planned,
107but not yet implemented, are not described here.
108.Pp
109The
110.Fa protocol
111specifies a particular protocol to be used with the socket.
112Normally only a single protocol exists to support a particular
113socket type within a given protocol family.  However, it is possible
114that many protocols may exist, in which case a particular protocol
115must be specified in this manner.  The protocol number to use is
116particular to the \*(lqcommunication domain\*(rq in which communication
117is to take place; see
118.Xr protocols 5 .
119.Pp
120Sockets of type
121.Dv SOCK_STREAM
122are full-duplex byte streams, similar
123to pipes.  A stream socket must be in a
124.Em connected
125state before any data may be sent or received
126on it.  A connection to another socket is created with a
127.Xr connect 2
128call.
129Once connected, data may be transferred using
130.Xr read 2
131and
132.Xr write 2
133calls or some variant of the
134.Xr send 2
135and
136.Xr recv 2
137calls.
138(Some protocol families, such as the Internet family,
139support the notion of an
140.Dq implied connect,
141which permits data to be sent piggybacked onto a connect operation by
142using the
143.Xr sendto 2
144call.)
145When a session has been completed a
146.Xr close 2
147may be performed.
148Out-of-band data may also be transmitted as described in
149.Xr send 2
150and received as described in
151.Xr recv 2 .
152.Pp
153The communications protocols used to implement a
154.Dv SOCK_STREAM
155insure that data
156is not lost or duplicated.  If a piece of data for which the
157peer protocol has buffer space cannot be successfully transmitted
158within a reasonable length of time, then
159the connection is considered broken and calls
160will indicate an error with
161-1 returns and with
162.Dv ETIMEDOUT
163as the specific code
164in the global variable
165.Va errno .
166The protocols optionally keep sockets
167.Dq warm
168by forcing transmissions
169roughly every minute in the absence of other activity.
170An error is then indicated if no response can be
171elicited on an otherwise
172idle connection for a extended period (e.g. 5 minutes).
173A
174.Dv SIGPIPE
175signal is raised if a process sends
176on a broken stream; this causes naive processes,
177which do not handle the signal, to exit.
178.Pp
179.Dv SOCK_SEQPACKET
180sockets employ the same system calls
181as
182.Dv SOCK_STREAM
183sockets.  The only difference
184is that
185.Xr read 2
186calls will return only the amount of data requested,
187and any remaining in the arriving packet will be discarded.
188.Pp
189.Dv SOCK_DGRAM
190and
191.Dv SOCK_RAW
192sockets allow sending of datagrams to correspondents
193named in
194.Xr send 2
195calls.  Datagrams are generally received with
196.Xr recvfrom 2 ,
197which returns the next datagram with its return address.
198.Pp
199An
200.Xr fcntl 2
201call can be used to specify a process group to receive
202a
203.Dv SIGURG
204signal when the out-of-band data arrives.
205It may also enable non-blocking I/O
206and asynchronous notification of I/O events
207via
208.Dv SIGIO .
209.Pp
210The operation of sockets is controlled by socket level
211.Em options .
212These options are defined in the file
213.Ao Pa sys/socket.h Ac .
214.Xr Setsockopt 2
215and
216.Xr getsockopt 2
217are used to set and get options, respectively.
218.Sh RETURN VALUES
219A -1 is returned if an error occurs, otherwise the return
220value is a descriptor referencing the socket.
221.Sh ERRORS
222The
223.Fn socket
224call fails if:
225.Bl -tag -width EPROTONOPSUPPORTA
226.It Bq Er EPROTONOSUPPORT
227The protocol type or the specified protocol is not supported
228within this domain.
229.It Bq Er EMFILE
230The per-process descriptor table is full.
231.It Bq Er ENFILE
232The system file table is full.
233.It Bq Er EACCES
234Permission to create a socket of the specified type and/or protocol
235is denied.
236.It Bq Er ENOBUFS
237Insufficient buffer space is available.
238The socket cannot be created until sufficient resources are freed.
239.El
240.Sh SEE ALSO
241.Xr accept 2 ,
242.Xr bind 2 ,
243.Xr connect 2 ,
244.Xr getpeername 2 ,
245.Xr getsockname 2 ,
246.Xr getsockopt 2 ,
247.Xr ioctl 2 ,
248.Xr listen 2 ,
249.Xr read 2 ,
250.Xr recv 2 ,
251.Xr select 2 ,
252.Xr send 2 ,
253.Xr shutdown 2 ,
254.Xr socketpair 2 ,
255.Xr write 2 ,
256.Xr getprotoent 3 ,
257.Xr protocols 5
258.Rs
259.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial"
260.%B PS1
261.%N 7
262.Re
263.Rs
264.%T "BSD Interprocess Communication Tutorial"
265.%B PS1
266.%N 8
267.Re
268.Sh HISTORY
269The
270.Fn socket
271function call appeared in
272.Bx 4.2 .
273