xref: /freebsd-14.2/lib/libc/sys/socket.2 (revision 0cbc7cc2)
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.\" $FreeBSD$
34.\"
35.Dd November 24, 1997
36.Dt SOCKET 2
37.Os
38.Sh NAME
39.Nm socket
40.Nd create an endpoint for communication
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In sys/types.h
45.In sys/socket.h
46.Ft int
47.Fn socket "int domain" "int type" "int protocol"
48.Sh DESCRIPTION
49The
50.Fn socket
51system call
52creates an endpoint for communication and returns a descriptor.
53.Pp
54The
55.Fa domain
56argument specifies a communications domain within which
57communication will take place; this selects the protocol family
58which should be used.
59These families are defined in the include file
60.In sys/socket.h .
61The currently understood formats are:
62.Pp
63.Bd -literal -offset indent -compact
64PF_LOCAL	Host-internal protocols, formerly called PF_UNIX,
65PF_UNIX		Host-internal protocols, deprecated, use PF_LOCAL,
66PF_INET		Internet version 4 protocols,
67PF_PUP		PUP protocols, like BSP,
68PF_APPLETALK	AppleTalk protocols,
69PF_ROUTE	Internal Routing protocol,
70PF_LINK		Link layer interface,
71PF_IPX		Novell Internet Packet eXchange protocol,
72PF_RTIP		Help Identify RTIP packets,
73PF_PIP		Help Identify PIP packets,
74PF_ISDN		Integrated Services Digital Network,
75PF_KEY		Internal key-management function,
76PF_INET6	Internet version 6 protocols,
77PF_NATM		Native ATM access,
78PF_ATM		ATM,
79PF_NETGRAPH	Netgraph sockets
80.Ed
81.Pp
82The socket has the indicated
83.Fa type ,
84which specifies the semantics of communication.  Currently
85defined types are:
86.Pp
87.Bd -literal -offset indent -compact
88SOCK_STREAM	Stream socket,
89SOCK_DGRAM	Datagram socket,
90SOCK_RAW	Raw-protocol interface,
91SOCK_RDM	Reliably-delivered packet,
92SOCK_SEQPACKET	Sequenced packet stream
93.Ed
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 unimplemented.
112.Dv SOCK_RAW
113sockets provide access to internal network protocols and interfaces.
114The types
115.Dv SOCK_RAW ,
116which is available only to the super-user, and
117.Dv SOCK_RDM ,
118which is planned,
119but not yet implemented, are not described here.
120.Pp
121The
122.Fa protocol
123argument
124specifies a particular protocol to be used with the socket.
125Normally only a single protocol exists to support a particular
126socket type within a given protocol family.  However, it is possible
127that many protocols may exist, in which case a particular protocol
128must be specified in this manner.  The protocol number to use is
129particular to the
130.Dq "communication domain"
131in 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, similar
138to pipes.  A stream socket must be in a
139.Em connected
140state before any data may be sent or received
141on it.  A connection to another socket is created with a
142.Xr connect 2
143system call.
144Once connected, data may be transferred using
145.Xr read 2
146and
147.Xr write 2
148calls or some variant of the
149.Xr send 2
150and
151.Xr recv 2
152functions.
153(Some protocol families, such as the Internet family,
154support the notion of an
155.Dq implied connect ,
156which permits data to be sent piggybacked onto a connect operation by
157using the
158.Xr sendto 2
159system call.)
160When a session has been completed a
161.Xr close 2
162may be performed.
163Out-of-band data may also be transmitted as described in
164.Xr send 2
165and received as described in
166.Xr recv 2 .
167.Pp
168The communications protocols used to implement a
169.Dv SOCK_STREAM
170insure that data
171is not lost or duplicated.  If a piece of data for which the
172peer protocol has buffer space cannot be successfully transmitted
173within a reasonable length of time, then
174the connection is considered broken and calls
175will indicate an error with
176-1 returns and with
177.Er ETIMEDOUT
178as the specific code
179in the global variable
180.Va errno .
181The protocols optionally keep sockets
182.Dq warm
183by forcing transmissions
184roughly every minute in the absence of other activity.
185An error is then indicated if no response can be
186elicited on an otherwise
187idle connection for an extended period (e.g. 5 minutes).
188A
189.Dv SIGPIPE
190signal is raised if a process sends
191on a broken stream; this causes naive processes,
192which do not handle the signal, to exit.
193.Pp
194.Dv SOCK_SEQPACKET
195sockets employ the same system calls
196as
197.Dv SOCK_STREAM
198sockets.  The only difference
199is that
200.Xr read 2
201calls will return only the amount of data requested,
202and any remaining in the arriving packet will be discarded.
203.Pp
204.Dv SOCK_DGRAM
205and
206.Dv SOCK_RAW
207sockets allow sending of datagrams to correspondents
208named in
209.Xr send 2
210calls.  Datagrams are generally received with
211.Xr recvfrom 2 ,
212which returns the next datagram with its return address.
213.Pp
214An
215.Xr fcntl 2
216system call can be used to specify a process group to receive
217a
218.Dv SIGURG
219signal when the out-of-band data arrives.
220It may also enable non-blocking I/O
221and asynchronous notification of I/O events
222via
223.Dv SIGIO .
224.Pp
225The operation of sockets is controlled by socket level
226.Em options .
227These options are defined in the file
228.In sys/socket.h .
229The
230.Xr setsockopt 2
231and
232.Xr getsockopt 2
233system calls are used to set and get options, respectively.
234.Sh RETURN VALUES
235A -1 is returned if an error occurs, otherwise the return
236value is a descriptor referencing the socket.
237.Sh ERRORS
238The
239.Fn socket
240system call fails if:
241.Bl -tag -width Er
242.It Bq Er EPROTONOSUPPORT
243The protocol type or the specified protocol is not supported
244within this domain.
245.It Bq Er EMFILE
246The per-process descriptor table is full.
247.It Bq Er ENFILE
248The system file table is full.
249.It Bq Er EACCES
250Permission to create a socket of the specified type and/or protocol
251is denied.
252.It Bq Er ENOBUFS
253Insufficient buffer space is available.
254The socket cannot be created until sufficient resources are freed.
255.El
256.Sh SEE ALSO
257.Xr accept 2 ,
258.Xr bind 2 ,
259.Xr connect 2 ,
260.Xr getpeername 2 ,
261.Xr getsockname 2 ,
262.Xr getsockopt 2 ,
263.Xr ioctl 2 ,
264.Xr listen 2 ,
265.Xr read 2 ,
266.Xr recv 2 ,
267.Xr select 2 ,
268.Xr send 2 ,
269.Xr shutdown 2 ,
270.Xr socketpair 2 ,
271.Xr write 2 ,
272.Xr getprotoent 3 ,
273.Xr netgraph 4 ,
274.Xr protocols 5
275.Rs
276.%T "An Introductory 4.3 BSD Interprocess Communication Tutorial"
277.%B PS1
278.%N 7
279.Re
280.Rs
281.%T "BSD Interprocess Communication Tutorial"
282.%B PS1
283.%N 8
284.Re
285.Sh HISTORY
286The
287.Fn socket
288system call appeared in
289.Bx 4.2 .
290