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