xref: /netbsd-src/lib/libc/sys/socket.2 (revision f9cde7647aa8cc9bbe3cc293d5a87d5c711126cd)
1.\"	$NetBSD: socket.2,v 1.51 2023/07/28 23:41:02 wiz 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 June 28, 2022
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.Bl -column -offset indent ".Dv PF_APPLETALK"
57.It Dv PF_LOCAL Ta local domain Po previously Tn UNIX domain Pc protocols
58.It Dv PF_INET Ta Tn ARPA Internet protocols
59.It Dv PF_INET6 Ta Tn IPv6 (Internet Protocol version 6) protocols
60.It Dv PF_NS Ta Xerox Network Systems protocols
61.It Dv PF_APPLETALK Ta AppleTalk protocols
62.It Dv PF_BLUETOOTH Ta Bluetooth protocols
63.It Dv PF_CAN Ta Tn CAN bus protocols
64.El
65.Pp
66The socket has the indicated
67.Fa type ,
68which specifies the semantics of communication.
69Currently defined types are:
70.Bl -tag -offset indent -width Dv
71.It Dv SOCK_STREAM
72Provides sequenced, reliable, two-way connection based byte streams.
73An out-of-band data transmission mechanism may be supported.
74.It Dv SOCK_DGRAM
75Supports datagrams: connectionless, unreliable messages of a
76fixed\(emtypically small\(emmaximum length.
77.It Dv SOCK_RAW
78Provides access to internal network protocols and interfaces.
79Available only to the super-user.
80Not described here.
81.It Dv SOCK_SEQPACKET
82Provides a sequenced, reliable, two-way connection-based data
83transmission path for datagrams of fixed maximum length.
84A consumer may be required to read an entire packet with each read
85system call.
86.It Dv SOCK_RDM
87Not implemented.
88.El
89.Pp
90The following flags can be or'ed to the socket type to add conditions to
91the returned file descriptor:
92.Bl -tag -offset indent -width Dv
93.It Dv SOCK_CLOEXEC
94Set the close on exec property.
95.It Dv SOCK_NONBLOCK
96Set non-blocking I/O.
97.It Dv SOCK_NOSIGPIPE
98Return
99.Er EPIPE
100instead of raising
101.Dv SIGPIPE .
102.El
103.Pp
104The
105.Fa protocol
106specifies a particular protocol to be used with the socket.
107Normally only a single protocol exists to support a particular
108socket type within a given protocol family.
109However, it is possible that many protocols may exist, in which case
110a particular protocol must be specified in this manner.
111The protocol number to use is
112particular to the \*(lqcommunication domain\*(rq in which communication
113is to take place; see
114.Xr protocols 5 .
115.Pp
116Sockets of type
117.Dv SOCK_STREAM
118are full-duplex byte streams.
119A stream socket must be in a
120.Em connected
121state before any data may be sent or received
122on it.
123A connection to another socket is created with a
124.Xr connect 2
125call.
126Once connected, data may be transferred using
127.Xr read 2
128and
129.Xr write 2
130calls or some variant of the
131.Xr send 2
132and
133.Xr recv 2
134calls.
135When a session has been completed a
136.Xr close 2
137may be performed.
138Out-of-band data may also be transmitted as described in
139.Xr send 2
140and received as described in
141.Xr recv 2 .
142.Pp
143The communications protocols used to implement a
144.Dv SOCK_STREAM
145ensure that data
146is not lost or duplicated.
147If a piece of data for which the
148peer protocol has buffer space cannot be successfully transmitted
149within a reasonable length of time, then
150the connection is considered broken and calls
151will indicate an error with
152\-1 returns and with
153.Er ETIMEDOUT
154as the specific code
155in the global variable
156.Va errno .
157The protocols optionally keep sockets
158.Dq warm
159by forcing transmissions
160roughly every minute in the absence of other activity.
161An error is then indicated if no response can be
162elicited on an otherwise
163idle connection for an extended period (e.g., 5 minutes).
164A
165.Dv SIGPIPE
166signal is raised if a process sends
167on a broken stream; this causes naive processes,
168which do not handle the signal, to exit.
169.Pp
170.Dv SOCK_SEQPACKET
171sockets employ the same system calls
172as
173.Dv SOCK_STREAM
174sockets.
175The only difference is that
176.Xr read 2
177calls will return only the amount of data requested,
178and any remaining in the arriving packet will be discarded.
179.Pp
180.Dv SOCK_DGRAM
181and
182.Dv SOCK_RAW
183sockets allow sending of datagrams to correspondents
184named in
185.Xr send 2
186calls.
187Datagrams are generally received with
188.Xr recvfrom 2 ,
189which returns the next datagram with its return address.
190.Pp
191An
192.Xr fcntl 2
193call can be used to specify a process group to receive
194a
195.Dv SIGURG
196signal when the out-of-band data arrives.
197It may also enable non-blocking I/O
198and asynchronous notification of I/O events
199via
200.Dv SIGIO .
201.Pp
202The operation of sockets is controlled by socket level
203.Em options .
204These options are defined in the file
205.Ao Pa sys/socket.h Ac .
206The
207.Xr setsockopt 2
208and
209.Xr getsockopt 2
210system calls are used to set and get options, respectively.
211.Sh RETURN VALUES
212A \-1 is returned if an error occurs, otherwise the return
213value is a descriptor referencing the socket.
214.Sh ERRORS
215The
216.Fn socket
217call fails if:
218.Bl -tag -width Er
219.It Bq Er EACCES
220Permission to create a socket of the specified type and/or protocol
221is denied.
222.It Bq Er EAFNOSUPPORT
223The address family (domain) is not supported or
224the specified domain is not supported by this protocol family.
225.It Bq Er EMFILE
226The per-process descriptor table is full.
227.It Bq Er ENFILE
228The system file table is full.
229.It Bq Er ENOBUFS
230Insufficient buffer space is available.
231The socket cannot be created until sufficient resources are freed.
232.It Bq Er EPROTONOSUPPORT
233The protocol family is not supported or
234the specified protocol is not supported within this domain.
235.It Bq Er EPROTOTYPE
236The socket type is not supported by the protocol.
237.El
238.Sh SEE ALSO
239.Xr accept 2 ,
240.Xr bind 2 ,
241.Xr connect 2 ,
242.Xr getsockname 2 ,
243.Xr getsockopt 2 ,
244.Xr ioctl 2 ,
245.Xr listen 2 ,
246.Xr poll 2 ,
247.Xr read 2 ,
248.Xr recv 2 ,
249.Xr select 2 ,
250.Xr send 2 ,
251.Xr setsockopt 2 ,
252.Xr shutdown 2 ,
253.Xr socketpair 2 ,
254.Xr write 2 ,
255.Xr getprotoent 3
256.Rs
257.%T "An Introductory 4.4BSD Interprocess Communication Tutorial"
258.%A Stuart Sechrest
259.Re
260.Pq see Pa /usr/share/doc/reference/ref3/sockets
261.Rs
262.%T "Advanced 4.4BSD IPC Tutorial"
263.%A Samuel J. Leffler
264.%A Robert S. Fabry
265.%A William N. Joy
266.%A Phil Lapsley
267.%A Steve Miller
268.%A Chris Torek
269.Re
270.Pq see Pa /usr/share/doc/reference/ref3/sockets-advanced
271.Sh HISTORY
272The
273.Fn socket
274function call appeared in
275.Bx 4.2 .
276