xref: /netbsd-src/lib/libc/sys/socket.2 (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1.\"	$NetBSD: socket.2,v 1.42 2017/05/27 21:02:55 bouyer Exp $
2.\"
3.\" Copyright (c) 1983, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)socket.2	8.1 (Berkeley) 6/4/93
31.\"
32.Dd April 27, 2017
33.Dt SOCKET 2
34.Os
35.Sh NAME
36.Nm socket
37.Nd create an endpoint for communication
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In sys/socket.h
42.Ft int
43.Fn socket "int domain" "int type" "int protocol"
44.Sh DESCRIPTION
45.Fn socket
46creates an endpoint for communication and returns a descriptor.
47.Pp
48The
49.Fa domain
50parameter specifies a communications domain within which
51communication will take place; this selects the protocol family
52which should be used.
53These families are defined in the include file
54.Ao Pa sys/socket.h Ac .
55The currently understood formats are:
56.Pp
57.Bd -literal -offset indent -compact
58PF_LOCAL	local (previously UNIX) domain protocols
59PF_INET		ARPA Internet protocols
60PF_INET6	IPv6 (Internet Protocol version 6) protocols
61PF_NS		Xerox Network Systems protocols
62PF_APPLETALK	AppleTalk protocols
63PF_BLUETOOTH	Bluetooth protocols
64PF_CAN		CAN bus protocols
65.Ed
66.Pp
67The socket has the indicated
68.Fa type ,
69which specifies the semantics of communication.
70Currently defined types are:
71.Pp
72.Bd -literal -offset indent -compact
73SOCK_STREAM
74SOCK_DGRAM
75SOCK_RAW
76SOCK_SEQPACKET
77SOCK_RDM
78.Ed
79.Pp
80The following flags can be or'ed to the type to condition the returned
81file descriptor:
82The following flags are valid:
83.Bl -column SOCK_NONBLOCK -offset indent
84.It Dv SOCK_CLOEXEC
85Set the close on exec property.
86.It Dv SOCK_NONBLOCK
87Sets non-blocking I/O.
88.It Dv SOCK_NOSIGPIPE
89Return
90.Er EPIPE
91instead of raising
92.Dv SIGPIPE .
93.El
94.Pp
95A
96.Dv SOCK_STREAM
97type provides sequenced, reliable,
98two-way connection based byte streams.
99An out-of-band data transmission mechanism may be supported.
100A
101.Dv SOCK_DGRAM
102socket supports
103datagrams (connectionless, unreliable messages of
104a fixed (typically small) maximum length).
105A
106.Dv SOCK_SEQPACKET
107socket may provide a sequenced, reliable,
108two-way connection-based data transmission path for datagrams
109of fixed maximum length; a consumer may be required to read
110an entire packet with each read system call.
111This facility is protocol specific, and presently implemented
112only for
113.Dv PF_NS .
114.Dv SOCK_RAW
115sockets provide access to internal network protocols and interfaces.
116The types
117.Dv SOCK_RAW ,
118which is available only to the super-user, and
119.Dv SOCK_RDM ,
120which is planned,
121but not yet implemented, are not described here.
122.Pp
123The
124.Fa protocol
125specifies a particular protocol to be used with the socket.
126Normally only a single protocol exists to support a particular
127socket type within a given protocol family.
128However, it is possible that many protocols may exist, in which case
129a particular protocol must be specified in this manner.
130The protocol number to use is
131particular to the \*(lqcommunication domain\*(rq in which communication
132is to take place; see
133.Xr protocols 5 .
134.Pp
135Sockets of type
136.Dv SOCK_STREAM
137are full-duplex byte streams.
138A stream socket must be in a
139.Em connected
140state before any data may be sent or received
141on it.
142A connection to another socket is created with a
143.Xr connect 2
144call.
145Once connected, data may be transferred using
146.Xr read 2
147and
148.Xr write 2
149calls or some variant of the
150.Xr send 2
151and
152.Xr recv 2
153calls.
154When a session has been completed a
155.Xr close 2
156may be performed.
157Out-of-band data may also be transmitted as described in
158.Xr send 2
159and received as described in
160.Xr recv 2 .
161.Pp
162The communications protocols used to implement a
163.Dv SOCK_STREAM
164ensure that data
165is not lost or duplicated.
166If a piece of data for which the
167peer protocol has buffer space cannot be successfully transmitted
168within a reasonable length of time, then
169the connection is considered broken and calls
170will indicate an error with
171\-1 returns and with
172.Er ETIMEDOUT
173as the specific code
174in the global variable
175.Va errno .
176The protocols optionally keep sockets
177.Dq warm
178by forcing transmissions
179roughly every minute in the absence of other activity.
180An error is then indicated if no response can be
181elicited on an otherwise
182idle connection for an extended period (e.g., 5 minutes).
183A
184.Dv SIGPIPE
185signal is raised if a process sends
186on a broken stream; this causes naive processes,
187which do not handle the signal, to exit.
188.Pp
189.Dv SOCK_SEQPACKET
190sockets employ the same system calls
191as
192.Dv SOCK_STREAM
193sockets.
194The only difference is that
195.Xr read 2
196calls will return only the amount of data requested,
197and any remaining in the arriving packet will be discarded.
198.Pp
199.Dv SOCK_DGRAM
200and
201.Dv SOCK_RAW
202sockets allow sending of datagrams to correspondents
203named in
204.Xr send 2
205calls.
206Datagrams are generally received with
207.Xr recvfrom 2 ,
208which returns the next datagram with its return address.
209.Pp
210An
211.Xr fcntl 2
212call can be used to specify a process group to receive
213a
214.Dv SIGURG
215signal when the out-of-band data arrives.
216It may also enable non-blocking I/O
217and asynchronous notification of I/O events
218via
219.Dv SIGIO .
220.Pp
221The operation of sockets is controlled by socket level
222.Em options .
223These options are defined in the file
224.Ao Pa sys/socket.h Ac .
225The
226.Xr setsockopt 2
227and
228.Xr getsockopt 2
229system calls are used to set and get options, respectively.
230.Sh RETURN VALUES
231A \-1 is returned if an error occurs, otherwise the return
232value is a descriptor referencing the socket.
233.Sh ERRORS
234The
235.Fn socket
236call fails if:
237.Bl -tag -width Er
238.It Bq Er EACCES
239Permission to create a socket of the specified type and/or protocol
240is denied.
241.It Bq Er EAFNOSUPPORT
242The address family (domain) is not supported or
243the specified domain is not supported by this protocol family.
244.It Bq Er EMFILE
245The per-process descriptor table is full.
246.It Bq Er ENFILE
247The system file table is full.
248.It Bq Er ENOBUFS
249Insufficient buffer space is available.
250The socket cannot be created until sufficient resources are freed.
251.It Bq Er EPROTONOSUPPORT
252The protocol family is not supported or
253the specified protocol is not supported within this domain.
254.It Bq Er EPROTOTYPE
255The socket type is not supported by the protocol.
256.El
257.Sh SEE ALSO
258.Xr accept 2 ,
259.Xr bind 2 ,
260.Xr connect 2 ,
261.Xr getsockname 2 ,
262.Xr getsockopt 2 ,
263.Xr ioctl 2 ,
264.Xr listen 2 ,
265.Xr poll 2 ,
266.Xr read 2 ,
267.Xr recv 2 ,
268.Xr select 2 ,
269.Xr send 2 ,
270.Xr setsockopt 2 ,
271.Xr shutdown 2 ,
272.Xr socketpair 2 ,
273.Xr write 2 ,
274.Xr getprotoent 3
275.Rs
276.%T "An Introductory 4.4BSD Interprocess Communication Tutorial"
277.%A Stuart Sechrest
278.Re
279.Pq see Pa /usr/share/doc/psd/20.ipctut
280.Rs
281.%T "Advanced 4.4BSD IPC Tutorial"
282.%A Samuel J. Leffler
283.%A Robert S. Fabry
284.%A William N. Joy
285.%A Phil Lapsley
286.%A Steve Miller
287.%A Chris Torek
288.Re
289.Pq see Pa /usr/share/doc/psd/21.ipc
290.Sh HISTORY
291The
292.Fn socket
293function call appeared in
294.Bx 4.2 .
295