xref: /netbsd/lib/libc/sys/socket.2 (revision 6550d01e)
1.\"	$NetBSD: socket.2,v 1.36 2007/09/06 09:32:52 jnemeth 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 September 6, 2007
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_ISO		ISO protocols
62PF_NS		Xerox Network Systems protocols
63PF_IMPLINK	IMP \*(lqhost at IMP\*(rq link layer
64PF_APPLETALK	AppleTalk protocols
65PF_BLUETOOTH	Bluetooth protocols
66.Ed
67.Pp
68The socket has the indicated
69.Fa type ,
70which specifies the semantics of communication.
71Currently defined 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.
114However, it is possible that many protocols may exist, in which case
115a particular protocol must be specified in this manner.
116The protocol number to use is
117particular to the \*(lqcommunication domain\*(rq in which communication
118is to take place; see
119.Xr protocols 5 .
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
127on it.
128A connection to another socket is created with a
129.Xr connect 2
130call.
131Once connected, data may be transferred using
132.Xr read 2
133and
134.Xr write 2
135calls or some variant of the
136.Xr send 2
137and
138.Xr recv 2
139calls.
140When a session has been completed a
141.Xr close 2
142may be performed.
143Out-of-band data may also be transmitted as described in
144.Xr send 2
145and received as described in
146.Xr recv 2 .
147.Pp
148The communications protocols used to implement a
149.Dv SOCK_STREAM
150ensure that data
151is not lost or duplicated.
152If a piece of data for which the
153peer protocol has buffer space cannot be successfully transmitted
154within a reasonable length of time, then
155the connection is considered broken and calls
156will indicate an error with
157\-1 returns and with
158.Er ETIMEDOUT
159as the specific code
160in the global variable
161.Va errno .
162The protocols optionally keep sockets
163.Dq warm
164by forcing transmissions
165roughly every minute in the absence of other activity.
166An error is then indicated if no response can be
167elicited on an otherwise
168idle connection for an extended period (e.g., 5 minutes).
169A
170.Dv SIGPIPE
171signal is raised if a process sends
172on a broken stream; this causes naive processes,
173which do not handle the signal, to exit.
174.Pp
175.Dv SOCK_SEQPACKET
176sockets employ the same system calls
177as
178.Dv SOCK_STREAM
179sockets.
180The only difference is that
181.Xr read 2
182calls will return only the amount of data requested,
183and any remaining in the arriving packet will be discarded.
184.Pp
185.Dv SOCK_DGRAM
186and
187.Dv SOCK_RAW
188sockets allow sending of datagrams to correspondents
189named in
190.Xr send 2
191calls.
192Datagrams are generally received with
193.Xr recvfrom 2 ,
194which returns the next datagram with its return address.
195.Pp
196An
197.Xr fcntl 2
198call can be used to specify a process group to receive
199a
200.Dv SIGURG
201signal when the out-of-band data arrives.
202It may also enable non-blocking I/O
203and asynchronous notification of I/O events
204via
205.Dv SIGIO .
206.Pp
207The operation of sockets is controlled by socket level
208.Em options .
209These options are defined in the file
210.Ao Pa sys/socket.h Ac .
211The
212.Xr setsockopt 2
213and
214.Xr getsockopt 2
215system calls are used to set and get options, respectively.
216.Sh RETURN VALUES
217A \-1 is returned if an error occurs, otherwise the return
218value is a descriptor referencing the socket.
219.Sh ERRORS
220The
221.Fn socket
222call fails if:
223.Bl -tag -width Er
224.It Bq Er EACCES
225Permission to create a socket of the specified type and/or protocol
226is denied.
227.It Bq Er EAFNOSUPPORT
228The address family (domain) is not supported or
229the specified domain is not supported by this protocol family.
230.It Bq Er EMFILE
231The per-process descriptor table is full.
232.It Bq Er ENFILE
233The system file table is full.
234.It Bq Er ENOBUFS
235Insufficient buffer space is available.
236The socket cannot be created until sufficient resources are freed.
237.It Bq Er EPROTONOSUPPORT
238The protocol family is not supported or
239the specified protocol is not supported within this domain.
240.It Bq Er EPROTOTYPE
241The socket type is not supported by the protocol.
242.El
243.Sh SEE ALSO
244.Xr accept 2 ,
245.Xr bind 2 ,
246.Xr connect 2 ,
247.Xr getsockname 2 ,
248.Xr getsockopt 2 ,
249.Xr ioctl 2 ,
250.Xr listen 2 ,
251.Xr poll 2 ,
252.Xr read 2 ,
253.Xr recv 2 ,
254.Xr select 2 ,
255.Xr send 2 ,
256.Xr setsockopt 2 ,
257.Xr shutdown 2 ,
258.Xr socketpair 2 ,
259.Xr write 2 ,
260.Xr getprotoent 3
261.Rs
262.%T "An Introductory 4.4BSD Interprocess Communication Tutorial"
263.%A Stuart Sechrest
264.Re
265.Pq see Pa /usr/share/doc/psd/20.ipctut
266.Rs
267.%T "Advanced 4.4BSD IPC Tutorial"
268.%A Samuel J. Leffler
269.%A Robert S. Fabry
270.%A William N. Joy
271.%A Phil Lapsley
272.%A Steve Miller
273.%A Chris Torek
274.Re
275.Pq see Pa /usr/share/doc/psd/21.ipc
276.Sh HISTORY
277The
278.Fn socket
279function call appeared in
280.Bx 4.2 .
281