xref: /netbsd-src/lib/libc/sys/socket.2 (revision eb7c1594f145c931049e1fd9eb056a5987e87e59)
1.\"	$NetBSD: socket.2,v 1.28 2003/08/07 16:44:09 agc 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, similar
123to pipes.
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 EPROTONOSUPPORT
225The protocol type or the specified protocol is not supported
226within this domain.
227.It Bq Er EMFILE
228The per-process descriptor table is full.
229.It Bq Er ENFILE
230The system file table is full.
231.It Bq Er EACCES
232Permission to create a socket of the specified type and/or protocol
233is denied.
234.It Bq Er ENOBUFS
235Insufficient buffer space is available.
236The socket cannot be created until sufficient resources are freed.
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/psd/20.ipctut
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/psd/21.ipc
271.Sh HISTORY
272The
273.Fn socket
274function call appeared in
275.Bx 4.2 .
276