]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libc/sys/socket.2
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libc / sys / socket.2
1 .\" Copyright (c) 1983, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 4. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     From: @(#)socket.2      8.1 (Berkeley) 6/4/93
29 .\" $FreeBSD$
30 .\"
31 .Dd August 4, 2008
32 .Dt SOCKET 2
33 .Os
34 .Sh NAME
35 .Nm socket
36 .Nd create an endpoint for communication
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In sys/types.h
41 .In sys/socket.h
42 .Ft int
43 .Fn socket "int domain" "int type" "int protocol"
44 .Sh DESCRIPTION
45 The
46 .Fn socket
47 system call
48 creates an endpoint for communication and returns a descriptor.
49 .Pp
50 The
51 .Fa domain
52 argument specifies a communications domain within which
53 communication will take place; this selects the protocol family
54 which should be used.
55 These families are defined in the include file
56 .In sys/socket.h .
57 The currently understood formats are:
58 .Pp
59 .Bd -literal -offset indent -compact
60 PF_LOCAL        Host-internal protocols, formerly called PF_UNIX,
61 PF_UNIX         Host-internal protocols, deprecated, use PF_LOCAL,
62 PF_INET         Internet version 4 protocols,
63 PF_PUP          PUP protocols, like BSP,
64 PF_APPLETALK    AppleTalk protocols,
65 PF_ROUTE        Internal Routing protocol,
66 PF_LINK         Link layer interface,
67 PF_IPX          Novell Internet Packet eXchange protocol,
68 PF_RTIP         Help Identify RTIP packets,
69 PF_PIP          Help Identify PIP packets,
70 PF_ISDN         Integrated Services Digital Network,
71 PF_KEY          Internal key-management function,
72 PF_INET6        Internet version 6 protocols,
73 PF_NATM         Native ATM access,
74 PF_ATM          ATM,
75 PF_NETGRAPH     Netgraph sockets
76 .Ed
77 .Pp
78 The socket has the indicated
79 .Fa type ,
80 which specifies the semantics of communication.
81 Currently
82 defined types are:
83 .Pp
84 .Bd -literal -offset indent -compact
85 SOCK_STREAM     Stream socket,
86 SOCK_DGRAM      Datagram socket,
87 SOCK_RAW        Raw-protocol interface,
88 SOCK_RDM        Reliably-delivered packet,
89 SOCK_SEQPACKET  Sequenced packet stream
90 .Ed
91 .Pp
92 A
93 .Dv SOCK_STREAM
94 type provides sequenced, reliable,
95 two-way connection based byte streams.
96 An out-of-band data transmission mechanism may be supported.
97 A
98 .Dv SOCK_DGRAM
99 socket supports
100 datagrams (connectionless, unreliable messages of
101 a fixed (typically small) maximum length).
102 A
103 .Dv SOCK_SEQPACKET
104 socket may provide a sequenced, reliable,
105 two-way connection-based data transmission path for datagrams
106 of fixed maximum length; a consumer may be required to read
107 an entire packet with each read system call.
108 This facility is protocol specific, and presently unimplemented.
109 .Dv SOCK_RAW
110 sockets provide access to internal network protocols and interfaces.
111 The types
112 .Dv SOCK_RAW ,
113 which is available only to the super-user, and
114 .Dv SOCK_RDM ,
115 which is planned,
116 but not yet implemented, are not described here.
117 .Pp
118 The
119 .Fa protocol
120 argument
121 specifies a particular protocol to be used with the socket.
122 Normally only a single protocol exists to support a particular
123 socket type within a given protocol family.
124 However, it is possible
125 that many protocols may exist, in which case a particular protocol
126 must be specified in this manner.
127 The protocol number to use is
128 particular to the
129 .Dq "communication domain"
130 in which communication
131 is to take place; see
132 .Xr protocols 5 .
133 .Pp
134 Sockets of type
135 .Dv SOCK_STREAM
136 are full-duplex byte streams, similar
137 to pipes.
138 A stream socket must be in a
139 .Em connected
140 state before any data may be sent or received
141 on it.
142 A connection to another socket is created with a
143 .Xr connect 2
144 system call.
145 Once connected, data may be transferred using
146 .Xr read 2
147 and
148 .Xr write 2
149 calls or some variant of the
150 .Xr send 2
151 and
152 .Xr recv 2
153 functions.
154 (Some protocol families, such as the Internet family,
155 support the notion of an
156 .Dq implied connect ,
157 which permits data to be sent piggybacked onto a connect operation by
158 using the
159 .Xr sendto 2
160 system call.)
161 When a session has been completed a
162 .Xr close 2
163 may be performed.
164 Out-of-band data may also be transmitted as described in
165 .Xr send 2
166 and received as described in
167 .Xr recv 2 .
168 .Pp
169 The communications protocols used to implement a
170 .Dv SOCK_STREAM
171 insure that data
172 is not lost or duplicated.
173 If a piece of data for which the
174 peer protocol has buffer space cannot be successfully transmitted
175 within a reasonable length of time, then
176 the connection is considered broken and calls
177 will indicate an error with
178 -1 returns and with
179 .Er ETIMEDOUT
180 as the specific code
181 in the global variable
182 .Va errno .
183 The protocols optionally keep sockets
184 .Dq warm
185 by forcing transmissions
186 roughly every minute in the absence of other activity.
187 An error is then indicated if no response can be
188 elicited on an otherwise
189 idle connection for an extended period (e.g.\& 5 minutes).
190 A
191 .Dv SIGPIPE
192 signal is raised if a process sends
193 on a broken stream; this causes naive processes,
194 which do not handle the signal, to exit.
195 .Pp
196 .Dv SOCK_SEQPACKET
197 sockets employ the same system calls
198 as
199 .Dv SOCK_STREAM
200 sockets.
201 The only difference
202 is that
203 .Xr read 2
204 calls will return only the amount of data requested,
205 and any remaining in the arriving packet will be discarded.
206 .Pp
207 .Dv SOCK_DGRAM
208 and
209 .Dv SOCK_RAW
210 sockets allow sending of datagrams to correspondents
211 named in
212 .Xr send 2
213 calls.
214 Datagrams are generally received with
215 .Xr recvfrom 2 ,
216 which returns the next datagram with its return address.
217 .Pp
218 An
219 .Xr fcntl 2
220 system call can be used to specify a process group to receive
221 a
222 .Dv SIGURG
223 signal when the out-of-band data arrives.
224 It may also enable non-blocking I/O
225 and asynchronous notification of I/O events
226 via
227 .Dv SIGIO .
228 .Pp
229 The operation of sockets is controlled by socket level
230 .Em options .
231 These options are defined in the file
232 .In sys/socket.h .
233 The
234 .Xr setsockopt 2
235 and
236 .Xr getsockopt 2
237 system calls are used to set and get options, respectively.
238 .Sh RETURN VALUES
239 A -1 is returned if an error occurs, otherwise the return
240 value is a descriptor referencing the socket.
241 .Sh ERRORS
242 The
243 .Fn socket
244 system call fails if:
245 .Bl -tag -width Er
246 .It Bq Er EPROTONOSUPPORT
247 The protocol type or the specified protocol is not supported
248 within this domain.
249 .It Bq Er EMFILE
250 The per-process descriptor table is full.
251 .It Bq Er ENFILE
252 The system file table is full.
253 .It Bq Er EACCES
254 Permission to create a socket of the specified type and/or protocol
255 is denied.
256 .It Bq Er ENOBUFS
257 Insufficient buffer space is available.
258 The socket cannot be created until sufficient resources are freed.
259 .It Bq Er EPERM
260 User has insufficient privileges to carry out the requested operation.
261 .El
262 .Sh SEE ALSO
263 .Xr accept 2 ,
264 .Xr bind 2 ,
265 .Xr connect 2 ,
266 .Xr getpeername 2 ,
267 .Xr getsockname 2 ,
268 .Xr getsockopt 2 ,
269 .Xr ioctl 2 ,
270 .Xr listen 2 ,
271 .Xr read 2 ,
272 .Xr recv 2 ,
273 .Xr select 2 ,
274 .Xr send 2 ,
275 .Xr shutdown 2 ,
276 .Xr socketpair 2 ,
277 .Xr write 2 ,
278 .Xr getprotoent 3 ,
279 .Xr netgraph 4 ,
280 .Xr protocols 5
281 .Rs
282 .%T "An Introductory 4.3 BSD Interprocess Communication Tutorial"
283 .%B PS1
284 .%N 7
285 .Re
286 .Rs
287 .%T "BSD Interprocess Communication Tutorial"
288 .%B PS1
289 .%N 8
290 .Re
291 .Sh HISTORY
292 The
293 .Fn socket
294 system call appeared in
295 .Bx 4.2 .