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