OSDN Git Service

be4f8b45f4cd61e55bec4a57e93dcdef86c1fd7d
[linuxjm/LDP_man-pages.git] / original / man7 / socket.7
1 '\" t
2 .\" Don't change the first line, it tells man that we need tbl.
3 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
4 .\" and copyright (c) 1999 Matthew Wilcox.
5 .\" Permission is granted to distribute possibly modified copies
6 .\" of this page provided the header is included verbatim,
7 .\" and in case of nontrivial modification author and date
8 .\" of the modification is added to the header.
9 .\"
10 .\" 2002-10-30, Michael Kerrisk, <mtk.manpages@gmail.com>
11 .\"     Added description of SO_ACCEPTCONN
12 .\" 2004-05-20, aeb, added SO_RCVTIMEO/SO_SNDTIMEO text.
13 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
14 .\"     Added notes on capability requirements
15 .\"     A few small grammar fixes
16 .\" 2010-06-13 Jan Engelhardt <jengelh@medozas.de>
17 .\"     Documented SO_DOMAIN and SO_PROTOCOL.
18 .\" FIXME
19 .\" The following are not yet documented:
20 .\"     SO_PEERNAME
21 .\"     SO_TIMESTAMPNS
22 .\"     SO_MARK (see https://bugzilla.kernel.org/show_bug.cgi?id=16461)
23 .\"     SO_TIMESTAMPING (2.6.30)
24 .\"     SO_RXQ_OVFL (2.6.33)
25 .\"
26 .TH SOCKET 7 2010-06-13 Linux "Linux Programmer's Manual"
27 .SH NAME
28 socket \- Linux socket interface
29 .SH SYNOPSIS
30 .B #include <sys/socket.h>
31 .sp
32 .IB sockfd " = socket(int " socket_family ", int " socket_type ", int " protocol );
33 .SH DESCRIPTION
34 This manual page describes the Linux networking socket layer user
35 interface.
36 The BSD compatible sockets
37 are the uniform interface
38 between the user process and the network protocol stacks in the kernel.
39 The protocol modules are grouped into
40 .I protocol families
41 like
42 .BR AF_INET ", " AF_IPX ", " AF_PACKET
43 and
44 .I socket types
45 like
46 .B SOCK_STREAM
47 or
48 .BR SOCK_DGRAM .
49 See
50 .BR socket (2)
51 for more information on families and types.
52 .SS Socket Layer Functions
53 These functions are used by the user process to send or receive packets
54 and to do other socket operations.
55 For more information see their respective manual pages.
56
57 .BR socket (2)
58 creates a socket,
59 .BR connect (2)
60 connects a socket to a remote socket address,
61 the
62 .BR bind (2)
63 function binds a socket to a local socket address,
64 .BR listen (2)
65 tells the socket that new connections shall be accepted, and
66 .BR accept (2)
67 is used to get a new socket with a new incoming connection.
68 .BR socketpair (2)
69 returns two connected anonymous sockets (only implemented for a few
70 local families like
71 .BR AF_UNIX )
72 .PP
73 .BR send (2),
74 .BR sendto (2),
75 and
76 .BR sendmsg (2)
77 send data over a socket, and
78 .BR recv (2),
79 .BR recvfrom (2),
80 .BR recvmsg (2)
81 receive data from a socket.
82 .BR poll (2)
83 and
84 .BR select (2)
85 wait for arriving data or a readiness to send data.
86 In addition, the standard I/O operations like
87 .BR write (2),
88 .BR writev (2),
89 .BR sendfile (2),
90 .BR read (2),
91 and
92 .BR readv (2)
93 can be used to read and write data.
94 .PP
95 .BR getsockname (2)
96 returns the local socket address and
97 .BR getpeername (2)
98 returns the remote socket address.
99 .BR getsockopt (2)
100 and
101 .BR setsockopt (2)
102 are used to set or get socket layer or protocol options.
103 .BR ioctl (2)
104 can be used to set or read some other options.
105 .PP
106 .BR close (2)
107 is used to close a socket.
108 .BR shutdown (2)
109 closes parts of a full-duplex socket connection.
110 .PP
111 Seeking, or calling
112 .BR pread (2)
113 or
114 .BR pwrite (2)
115 with a nonzero position is not supported on sockets.
116 .PP
117 It is possible to do nonblocking I/O on sockets by setting the
118 .B O_NONBLOCK
119 flag on a socket file descriptor using
120 .BR fcntl (2).
121 Then all operations that would block will (usually)
122 return with
123 .B EAGAIN
124 (operation should be retried later);
125 .BR connect (2)
126 will return
127 .B EINPROGRESS
128 error.
129 The user can then wait for various events via
130 .BR poll (2)
131 or
132 .BR select (2).
133 .TS
134 tab(:) allbox;
135 c s s
136 l l l.
137 I/O events
138 Event:Poll flag:Occurrence
139 Read:POLLIN:T{
140 New data arrived.
141 T}
142 Read:POLLIN:T{
143 A connection setup has been completed
144 (for connection-oriented sockets)
145 T}
146 Read:POLLHUP:T{
147 A disconnection request has been initiated by the other end.
148 T}
149 Read:POLLHUP:T{
150 A connection is broken (only for connection-oriented protocols).
151 When the socket is written
152 .B SIGPIPE
153 is also sent.
154 T}
155 Write:POLLOUT:T{
156 Socket has enough send buffer space for writing new data.
157 T}
158 Read/Write:T{
159 POLLIN|
160 .br
161 POLLOUT
162 T}:T{
163 An outgoing
164 .BR connect (2)
165 finished.
166 T}
167 Read/Write:POLLERR:An asynchronous error occurred.
168 Read/Write:POLLHUP:The other end has shut down one direction.
169 Exception:POLLPRI:T{
170 Urgent data arrived.
171 .B SIGURG
172 is sent then.
173 T}
174 .\" FIXME . The following is not true currently:
175 .\" It is no I/O event when the connection
176 .\" is broken from the local end using
177 .\" .BR shutdown (2)
178 .\" or
179 .\" .BR close (2).
180 .TE
181
182 .PP
183 An alternative to
184 .BR poll (2)
185 and
186 .BR select (2)
187 is to let the kernel inform the application about events
188 via a
189 .B SIGIO
190 signal.
191 For that the
192 .B O_ASYNC
193 flag must be set on a socket file descriptor via
194 .BR fcntl (2)
195 and a valid signal handler for
196 .B SIGIO
197 must be installed via
198 .BR sigaction (2).
199 See the
200 .I Signals
201 discussion below.
202 .SS Socket Options
203 These socket options can be set by using
204 .BR setsockopt (2)
205 and read with
206 .BR getsockopt (2)
207 with the socket level set to
208 .B SOL_SOCKET
209 for all sockets:
210 .\" FIXME
211 .\" In the list below, the text used to describe argument types
212 .\" for each socket option should be more consistent
213 .\"
214 .\" SO_ACCEPTCONN is in POSIX.1-2001, and its origin is explained in
215 .\" W R Stevens, UNPv1
216 .TP
217 .B SO_ACCEPTCONN
218 Returns a value indicating whether or not this socket has been marked
219 to accept connections with
220 .BR listen (2).
221 The value 0 indicates that this is not a listening socket,
222 the value 1 indicates that this is a listening socket.
223 This socket option is read-only.
224 .TP
225 .B SO_BINDTODEVICE
226 Bind this socket to a particular device like \(lqeth0\(rq,
227 as specified in the passed interface name.
228 If the
229 name is an empty string or the option length is zero, the socket device
230 binding is removed.
231 The passed option is a variable-length null-terminated
232 interface name string with the maximum size of
233 .BR IFNAMSIZ .
234 If a socket is bound to an interface,
235 only packets received from that particular interface are processed by the
236 socket.
237 Note that this only works for some socket types, particularly
238 .B AF_INET
239 sockets.
240 It is not supported for packet sockets (use normal
241 .BR bind (8)
242 there).
243 .TP
244 .B SO_BROADCAST
245 Set or get the broadcast flag.
246 When enabled, datagram sockets
247 receive packets sent to a broadcast address and they are allowed to send
248 packets to a broadcast address.
249 This option has no effect on stream-oriented sockets.
250 .TP
251 .B SO_BSDCOMPAT
252 Enable BSD bug-to-bug compatibility.
253 This is used by the UDP protocol module in Linux 2.0 and 2.2.
254 If enabled ICMP errors received for a UDP socket will not be passed
255 to the user program.
256 In later kernel versions, support for this option has been phased out:
257 Linux 2.4 silently ignores it, and Linux 2.6 generates a kernel warning
258 (printk()) if a program uses this option.
259 Linux 2.0 also enabled BSD bug-to-bug compatibility
260 options (random header changing, skipping of the broadcast flag) for raw
261 sockets with this option, but that was removed in Linux 2.2.
262 .TP
263 .B SO_DEBUG
264 Enable socket debugging.
265 Only allowed for processes with the
266 .B CAP_NET_ADMIN
267 capability or an effective user ID of 0.
268 .TP
269 .BR SO_DOMAIN " (since Linux 2.6.32)"
270 Retrieves the socket domain as an integer, returning a value such as
271 .BR AF_INET6 .
272 See
273 .BR socket (2)
274 for details.
275 This socket option is read-only.
276 .TP
277 .B SO_ERROR
278 Get and clear the pending socket error.
279 This socket option is read-only.
280 Expects an integer.
281 .TP
282 .B SO_DONTROUTE
283 Don't send via a gateway, only send to directly connected hosts.
284 The same effect can be achieved by setting the
285 .B MSG_DONTROUTE
286 flag on a socket
287 .BR send (2)
288 operation.
289 Expects an integer boolean flag.
290 .TP
291 .B SO_KEEPALIVE
292 Enable sending of keep-alive messages on connection-oriented sockets.
293 Expects an integer boolean flag.
294 .TP
295 .B SO_LINGER
296 Sets or gets the
297 .B SO_LINGER
298 option.
299 The argument is a
300 .I linger
301 structure.
302 .sp
303 .in +4n
304 .nf
305 struct linger {
306     int l_onoff;    /* linger active */
307     int l_linger;   /* how many seconds to linger for */
308 };
309 .fi
310 .in
311 .IP
312 When enabled, a
313 .BR close (2)
314 or
315 .BR shutdown (2)
316 will not return until all queued messages for the socket have been
317 successfully sent or the linger timeout has been reached.
318 Otherwise,
319 the call returns immediately and the closing is done in the background.
320 When the socket is closed as part of
321 .BR exit (2),
322 it always lingers in the background.
323 .TP
324 .B SO_OOBINLINE
325 If this option is enabled,
326 out-of-band data is directly placed into the receive data stream.
327 Otherwise out-of-band data is only passed when the
328 .B MSG_OOB
329 flag is set during receiving.
330 .\" don't document it because it can do too much harm.
331 .\".B SO_NO_CHECK
332 .TP
333 .B SO_PASSCRED
334 Enable or disable the receiving of the
335 .B SCM_CREDENTIALS
336 control message.
337 For more information see
338 .BR unix (7).
339 .\" FIXME Document SO_PASSSEC, added in 2.6.18; there is some info
340 .\" in the 2.6.18 ChangeLog
341 .TP
342 .B SO_PEERCRED
343 Return the credentials of the foreign process connected to this socket.
344 This is only possible for connected
345 .B AF_UNIX
346 stream sockets and
347 .B AF_UNIX
348 stream and datagram socket pairs created using
349 .BR socketpair (2);
350 see
351 .BR unix (7).
352 The returned credentials are those that were in effect at the time
353 of the call to
354 .BR connect (2)
355 or
356 .BR socketpair (2).
357 Argument is a
358 .I ucred
359 structure.
360 This socket option is read-only.
361 .TP
362 .B SO_PRIORITY
363 Set the protocol-defined priority for all packets to be sent on
364 this socket.
365 Linux uses this value to order the networking queues:
366 packets with a higher priority may be processed first depending
367 on the selected device queueing discipline.
368 For
369 .BR ip (7),
370 this also sets the IP type-of-service (TOS) field for outgoing packets.
371 Setting a priority outside the range 0 to 6 requires the
372 .B CAP_NET_ADMIN
373 capability.
374 .TP
375 .BR SO_PROTOCOL " (since Linux 2.6.32)"
376 Retrieves the socket protocol as an integer, returning a value such as
377 .BR IPPROTO_SCTP .
378 See
379 .BR socket (2)
380 for details.
381 This socket option is read-only.
382 .TP
383 .B SO_RCVBUF
384 Sets or gets the maximum socket receive buffer in bytes.
385 The kernel doubles this value (to allow space for bookkeeping overhead)
386 when it is set using
387 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
388 .BR setsockopt (2),
389 and this doubled value is returned by
390 .BR getsockopt (2).
391 The default value is set by the
392 .I /proc/sys/net/core/rmem_default
393 file, and the maximum allowed value is set by the
394 .I /proc/sys/net/core/rmem_max
395 file.
396 The minimum (doubled) value for this option is 256.
397 .TP
398 .BR SO_RCVBUFFORCE " (since Linux 2.6.14)"
399 Using this socket option, a privileged
400 .RB ( CAP_NET_ADMIN )
401 process can perform the same task as
402 .BR SO_RCVBUF ,
403 but the
404 .I rmem_max
405 limit can be overridden.
406 .TP
407 .BR SO_RCVLOWAT " and " SO_SNDLOWAT
408 Specify the minimum number of bytes in the buffer until the socket layer
409 will pass the data to the protocol
410 .RB ( SO_SNDLOWAT )
411 or the user on receiving
412 .RB ( SO_RCVLOWAT ).
413 These two values are initialized to 1.
414 .B SO_SNDLOWAT
415 is not changeable on Linux
416 .RB ( setsockopt (2)
417 fails with the error
418 .BR ENOPROTOOPT ).
419 .B SO_RCVLOWAT
420 is changeable
421 only since Linux 2.4.
422 The
423 .BR select (2)
424 and
425 .BR poll (2)
426 system calls currently do not respect the
427 .B SO_RCVLOWAT
428 setting on Linux,
429 and mark a socket readable when even a single byte of data is available.
430 A subsequent read from the socket will block until
431 .B SO_RCVLOWAT
432 bytes are available.
433 .\" See http://marc.theaimsgroup.com/?l=linux-kernel&m=111049368106984&w=2
434 .\" Tested on kernel 2.6.14 -- mtk, 30 Nov 05
435 .TP
436 .BR SO_RCVTIMEO " and " SO_SNDTIMEO
437 .\" Not implemented in 2.0.
438 .\" Implemented in 2.1.11 for getsockopt: always return a zero struct.
439 .\" Implemented in 2.3.41 for setsockopt, and actually used.
440 Specify the receiving or sending timeouts until reporting an error.
441 The argument is a
442 .IR "struct timeval" .
443 If an input or output function blocks for this period of time, and
444 data has been sent or received, the return value of that function
445 will be the amount of data transferred; if no data has been transferred
446 and the timeout has been reached then \-1 is returned with
447 .I errno
448 set to
449 .B EAGAIN
450 or
451 .B EWOULDBLOCK
452 .\" in fact to EAGAIN
453 just as if the socket was specified to be nonblocking.
454 If the timeout is set to zero (the default)
455 then the operation will never timeout.
456 Timeouts only have effect for system calls that perform socket I/O (e.g.,
457 .BR read (2),
458 .BR recvmsg (2),
459 .BR send (2),
460 .BR sendmsg (2));
461 timeouts have no effect for
462 .BR select (2),
463 .BR poll (2),
464 .BR epoll_wait (2),
465 etc.
466 .TP
467 .B SO_REUSEADDR
468 Indicates that the rules used in validating addresses supplied in a
469 .BR bind (2)
470 call should allow reuse of local addresses.
471 For
472 .B AF_INET
473 sockets this
474 means that a socket may bind, except when there
475 is an active listening socket bound to the address.
476 When the listening socket is bound to
477 .B INADDR_ANY
478 with a specific port then it is not possible
479 to bind to this port for any local address.
480 Argument is an integer boolean flag.
481 .TP
482 .B SO_SNDBUF
483 Sets or gets the maximum socket send buffer in bytes.
484 The kernel doubles this value (to allow space for bookkeeping overhead)
485 when it is set using
486 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
487 .BR setsockopt (2),
488 and this doubled value is returned by
489 .BR getsockopt (2).
490 The default value is set by the
491 .I /proc/sys/net/core/wmem_default
492 file and the maximum allowed value is set by the
493 .I /proc/sys/net/core/wmem_max
494 file.
495 The minimum (doubled) value for this option is 2048.
496 .TP
497 .BR SO_SNDBUFFORCE " (since Linux 2.6.14)"
498 Using this socket option, a privileged
499 .RB ( CAP_NET_ADMIN )
500 process can perform the same task as
501 .BR SO_SNDBUF ,
502 but the
503 .I wmem_max
504 limit can be overridden.
505 .TP
506 .B SO_TIMESTAMP
507 Enable or disable the receiving of the
508 .B SO_TIMESTAMP
509 control message.
510 The timestamp control message is sent with level
511 .B SOL_SOCKET
512 and the
513 .I cmsg_data
514 field is a
515 .I "struct timeval"
516 indicating the
517 reception time of the last packet passed to the user in this call.
518 See
519 .BR cmsg (3)
520 for details on control messages.
521 .TP
522 .B SO_TYPE
523 Gets the socket type as an integer (e.g.,
524 .BR SOCK_STREAM ).
525 This socket option is read-only.
526 .SS Signals
527 When writing onto a connection-oriented socket that has been shut down
528 (by the local or the remote end)
529 .B SIGPIPE
530 is sent to the writing process and
531 .B EPIPE
532 is returned.
533 The signal is not sent when the write call
534 specified the
535 .B MSG_NOSIGNAL
536 flag.
537 .PP
538 When requested with the
539 .B FIOSETOWN
540 .BR fcntl (2)
541 or
542 .B SIOCSPGRP
543 .BR ioctl (2),
544 .B SIGIO
545 is sent when an I/O event occurs.
546 It is possible to use
547 .BR poll (2)
548 or
549 .BR select (2)
550 in the signal handler to find out which socket the event occurred on.
551 An alternative (in Linux 2.2) is to set a real-time signal using the
552 .B F_SETSIG
553 .BR fcntl (2);
554 the handler of the real time signal will be called with
555 the file descriptor in the
556 .I si_fd
557 field of its
558 .IR siginfo_t .
559 See
560 .BR fcntl (2)
561 for more information.
562 .PP
563 Under some circumstances (e.g., multiple processes accessing a
564 single socket), the condition that caused the
565 .B SIGIO
566 may have already disappeared when the process reacts to the signal.
567 If this happens, the process should wait again because Linux
568 will resend the signal later.
569 .\" .SS Ancillary Messages
570 .SS /proc interfaces
571 The core socket networking parameters can be accessed
572 via files in the directory
573 .IR /proc/sys/net/core/ .
574 .TP
575 .I rmem_default
576 contains the default setting in bytes of the socket receive buffer.
577 .TP
578 .I rmem_max
579 contains the maximum socket receive buffer size in bytes which a user may
580 set by using the
581 .B SO_RCVBUF
582 socket option.
583 .TP
584 .I wmem_default
585 contains the default setting in bytes of the socket send buffer.
586 .TP
587 .I wmem_max
588 contains the maximum socket send buffer size in bytes which a user may
589 set by using the
590 .B SO_SNDBUF
591 socket option.
592 .TP
593 .IR message_cost " and " message_burst
594 configure the token bucket filter used to load limit warning messages
595 caused by external network events.
596 .TP
597 .I netdev_max_backlog
598 Maximum number of packets in the global input queue.
599 .TP
600 .I optmem_max
601 Maximum length of ancillary data and user control data like the iovecs
602 per socket.
603 .\" netdev_fastroute is not documented because it is experimental
604 .SS Ioctls
605 These operations can be accessed using
606 .BR ioctl (2):
607
608 .in +4n
609 .nf
610 .IB error " = ioctl(" ip_socket ", " ioctl_type ", " &value_result ");"
611 .fi
612 .in
613 .TP
614 .B SIOCGSTAMP
615 Return a
616 .I struct timeval
617 with the receive timestamp of the last packet passed to the user.
618 This is useful for accurate round trip time measurements.
619 See
620 .BR setitimer (2)
621 for a description of
622 .IR "struct timeval" .
623 .\"
624 This ioctl should only be used if the socket option
625 .B SO_TIMESTAMP
626 is not set on the socket.
627 Otherwise, it returns the timestamp of the
628 last packet that was received while
629 .B SO_TIMESTAMP
630 was not set, or it fails if no such packet has been received,
631 (i.e.,
632 .BR ioctl (2)
633 returns \-1 with
634 .I errno
635 set to
636 .BR ENOENT ).
637 .TP
638 .B SIOCSPGRP
639 Set the process or process group to send
640 .B SIGIO
641 or
642 .B SIGURG
643 signals
644 to when an
645 asynchronous I/O operation has finished or urgent data is available.
646 The argument is a pointer to a
647 .IR pid_t .
648 If the argument is positive, send the signals to that process.
649 If the
650 argument is negative, send the signals to the process group with the ID
651 of the absolute value of the argument.
652 The process may only choose itself or its own process group to receive
653 signals unless it has the
654 .B CAP_KILL
655 capability or an effective UID of 0.
656 .TP
657 .B FIOASYNC
658 Change the
659 .B O_ASYNC
660 flag to enable or disable asynchronous I/O mode of the socket.
661 Asynchronous I/O mode means that the
662 .B SIGIO
663 signal or the signal set with
664 .B F_SETSIG
665 is raised when a new I/O event occurs.
666 .IP
667 Argument is an integer boolean flag.
668 (This operation is synonymous with the use of
669 .BR fcntl (2)
670 to set the
671 .B O_ASYNC
672 flag.)
673 .\"
674 .TP
675 .B SIOCGPGRP
676 Get the current process or process group that receives
677 .B SIGIO
678 or
679 .B SIGURG
680 signals,
681 or 0
682 when none is set.
683 .PP
684 Valid
685 .BR fcntl (2)
686 operations:
687 .TP
688 .B FIOGETOWN
689 The same as the
690 .B SIOCGPGRP
691 .BR ioctl (2).
692 .TP
693 .B FIOSETOWN
694 The same as the
695 .B SIOCSPGRP
696 .BR ioctl (2).
697 .SH VERSIONS
698 .B SO_BINDTODEVICE
699 was introduced in Linux 2.0.30.
700 .B SO_PASSCRED
701 is new in Linux 2.2.
702 The
703 .I /proc
704 interfaces was introduced in Linux 2.2.
705 .B SO_RCVTIMEO
706 and
707 .B SO_SNDTIMEO
708 are supported since Linux 2.3.41.
709 Earlier, timeouts were fixed to
710 a protocol-specific setting, and could not be read or written.
711 .SH NOTES
712 Linux assumes that half of the send/receive buffer is used for internal
713 kernel structures; thus the values in the corresponding
714 .I /proc
715 files are twice what can be observed on the wire.
716
717 Linux will only allow port reuse with the
718 .B SO_REUSEADDR
719 option
720 when this option was set both in the previous program that performed a
721 .BR bind (2)
722 to the port and in the program that wants to reuse the port.
723 This differs from some implementations (e.g., FreeBSD)
724 where only the later program needs to set the
725 .B SO_REUSEADDR
726 option.
727 Typically this difference is invisible, since, for example, a server
728 program is designed to always set this option.
729 .SH BUGS
730 The
731 .B CONFIG_FILTER
732 socket options
733 .B SO_ATTACH_FILTER
734 and
735 .B SO_DETACH_FILTER
736 .\" FIXME Document SO_ATTACH_FILTER and SO_DETACH_FILTER
737 are not documented.
738 The suggested interface to use them is via the libpcap
739 library.
740 .\" .SH AUTHORS
741 .\" This man page was written by Andi Kleen.
742 .SH "SEE ALSO"
743 .BR getsockopt (2),
744 .BR setsockopt (2),
745 .BR socket (2),
746 .BR capabilities (7),
747 .BR ddp (7),
748 .BR ip (7),
749 .BR packet (7),
750 .BR tcp (7),
751 .BR udp (7),
752 .BR unix (7)