xref: /netbsd-src/lib/libc/sys/socket.2 (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1.\"	$NetBSD: socket.2,v 1.31 2004/11/03 13:11:41 jmmv 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 May 15, 2003
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	ARPA 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
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
80A
81.Dv SOCK_STREAM
82type provides sequenced, reliable,
83two-way connection based byte streams.
84An out-of-band data transmission mechanism may be supported.
85A
86.Dv SOCK_DGRAM
87socket supports
88datagrams (connectionless, unreliable messages of
89a fixed (typically small) maximum length).
90A
91.Dv SOCK_SEQPACKET
92socket may provide a sequenced, reliable,
93two-way connection-based data transmission path for datagrams
94of fixed maximum length; a consumer may be required to read
95an entire packet with each read system call.
96This facility is protocol specific, and presently implemented
97only for
98.Dv PF_NS .
99.Dv SOCK_RAW
100sockets provide access to internal network protocols and interfaces.
101The types
102.Dv SOCK_RAW ,
103which is available only to the super-user, and
104.Dv SOCK_RDM ,
105which is planned,
106but not yet implemented, are not described here.
107.Pp
108The
109.Fa protocol
110specifies a particular protocol to be used with the socket.
111Normally only a single protocol exists to support a particular
112socket type within a given protocol family.
113However, it is possible that many protocols may exist, in which case
114a particular protocol must be specified in this manner.
115The protocol number to use is
116particular to the \*(lqcommunication domain\*(rq in which communication
117is to take place; see
118.Xr protocols 5 .
119.Pp
120Sockets of type
121.Dv SOCK_STREAM
122are full-duplex byte streams.
123A stream socket must be in a
124.Em connected
125state before any data may be sent or received
126on 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
150is not lost or duplicated.
151If a piece of data for which the
152peer protocol has buffer space cannot be successfully transmitted
153within a reasonable length of time, then
154the connection is considered broken and calls
155will indicate an error with
156\-1 returns and with
157.Er ETIMEDOUT
158as the specific code
159in the global variable
160.Va errno .
161The protocols optionally keep sockets
162.Dq warm
163by forcing transmissions
164roughly every minute in the absence of other activity.
165An error is then indicated if no response can be
166elicited on an otherwise
167idle connection for an extended period (e.g., 5 minutes).
168A
169.Dv SIGPIPE
170signal is raised if a process sends
171on a broken stream; this causes naive processes,
172which do not handle the signal, to exit.
173.Pp
174.Dv SOCK_SEQPACKET
175sockets employ the same system calls
176as
177.Dv SOCK_STREAM
178sockets.
179The only difference is that
180.Xr read 2
181calls will return only the amount of data requested,
182and any remaining in the arriving packet will be discarded.
183.Pp
184.Dv SOCK_DGRAM
185and
186.Dv SOCK_RAW
187sockets allow sending of datagrams to correspondents
188named in
189.Xr send 2
190calls.
191Datagrams are generally received with
192.Xr recvfrom 2 ,
193which returns the next datagram with its return address.
194.Pp
195An
196.Xr fcntl 2
197call can be used to specify a process group to receive
198a
199.Dv SIGURG
200signal when the out-of-band data arrives.
201It may also enable non-blocking I/O
202and asynchronous notification of I/O events
203via
204.Dv SIGIO .
205.Pp
206The operation of sockets is controlled by socket level
207.Em options .
208These options are defined in the file
209.Ao Pa sys/socket.h Ac .
210The
211.Xr setsockopt 2
212and
213.Xr getsockopt 2
214system calls are used to set and get options, respectively.
215.Sh RETURN VALUES
216A \-1 is returned if an error occurs, otherwise the return
217value is a descriptor referencing the socket.
218.Sh ERRORS
219The
220.Fn socket
221call fails if:
222.Bl -tag -width Er
223.It Bq Er EPROTONOSUPPORT
224The protocol type or the specified protocol is not supported
225within this domain.
226.It Bq Er EMFILE
227The per-process descriptor table is full.
228.It Bq Er ENFILE
229The system file table is full.
230.It Bq Er EACCES
231Permission to create a socket of the specified type and/or protocol
232is denied.
233.It Bq Er ENOBUFS
234Insufficient buffer space is available.
235The socket cannot be created until sufficient resources are freed.
236.El
237.Sh SEE ALSO
238.Xr accept 2 ,
239.Xr bind 2 ,
240.Xr connect 2 ,
241.Xr getsockname 2 ,
242.Xr getsockopt 2 ,
243.Xr ioctl 2 ,
244.Xr listen 2 ,
245.Xr poll 2 ,
246.Xr read 2 ,
247.Xr recv 2 ,
248.Xr select 2 ,
249.Xr send 2 ,
250.Xr setsockopt 2 ,
251.Xr shutdown 2 ,
252.Xr socketpair 2 ,
253.Xr write 2 ,
254.Xr getprotoent 3
255.Rs
256.%T "An Introductory 4.4BSD Interprocess Communication Tutorial"
257.%A Stuart Sechrest
258.Re
259.Pq see Pa /usr/share/doc/psd/20.ipctut
260.Rs
261.%T "Advanced 4.4BSD IPC Tutorial"
262.%A Samuel J. Leffler
263.%A Robert S. Fabry
264.%A William N. Joy
265.%A Phil Lapsley
266.%A Steve Miller
267.%A Chris Torek
268.Re
269.Pq see Pa /usr/share/doc/psd/21.ipc
270.Sh HISTORY
271The
272.Fn socket
273function call appeared in
274.Bx 4.2 .
275