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