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.
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.
19 .\" The following are not yet documented:
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)
26 .TH SOCKET 7 2012-04-23 Linux "Linux Programmer's Manual"
28 socket \- Linux socket interface
30 .B #include <sys/socket.h>
32 .IB sockfd " = socket(int " socket_family ", int " socket_type ", int " protocol );
34 This manual page describes the Linux networking socket layer user
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
42 .BR AF_INET ", " AF_IPX ", " AF_PACKET
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.
60 connects a socket to a remote socket address,
63 function binds a socket to a local socket address,
65 tells the socket that new connections shall be accepted, and
67 is used to get a new socket with a new incoming connection.
69 returns two connected anonymous sockets (only implemented for a few
77 send data over a socket, and
81 receive data from a socket.
85 wait for arriving data or a readiness to send data.
86 In addition, the standard I/O operations like
93 can be used to read and write data.
96 returns the local socket address and
98 returns the remote socket address.
102 are used to set or get socket layer or protocol options.
104 can be used to set or read some other options.
107 is used to close a socket.
109 closes parts of a full-duplex socket connection.
115 with a nonzero position is not supported on sockets.
117 It is possible to do nonblocking I/O on sockets by setting the
119 flag on a socket file descriptor using
121 Then all operations that would block will (usually)
124 (operation should be retried later);
129 The user can then wait for various events via
138 Event:Poll flag:Occurrence
143 A connection setup has been completed
144 (for connection-oriented sockets)
147 A disconnection request has been initiated by the other end.
150 A connection is broken (only for connection-oriented protocols).
151 When the socket is written
156 Socket has enough send buffer space for writing new data.
167 Read/Write:POLLERR:An asynchronous error occurred.
168 Read/Write:POLLHUP:The other end has shut down one direction.
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
187 is to let the kernel inform the application about events
193 flag must be set on a socket file descriptor via
195 and a valid signal handler for
197 must be installed via
203 These socket options can be set by using
207 with the socket level set to
211 .\" In the list below, the text used to describe argument types
212 .\" for each socket option should be more consistent
214 .\" SO_ACCEPTCONN is in POSIX.1-2001, and its origin is explained in
215 .\" W R Stevens, UNPv1
218 Returns a value indicating whether or not this socket has been marked
219 to accept connections with
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.
226 Bind this socket to a particular device like \(lqeth0\(rq,
227 as specified in the passed interface name.
229 name is an empty string or the option length is zero, the socket device
231 The passed option is a variable-length null-terminated
232 interface name string with the maximum size of
234 If a socket is bound to an interface,
235 only packets received from that particular interface are processed by the
237 Note that this only works for some socket types, particularly
240 It is not supported for packet sockets (use normal
245 Set or get the broadcast flag.
246 When enabled, datagram sockets are allowed to send
247 packets to a broadcast address.
248 This option has no effect on stream-oriented sockets.
251 Enable BSD bug-to-bug compatibility.
252 This is used by the UDP protocol module in Linux 2.0 and 2.2.
253 If enabled ICMP errors received for a UDP socket will not be passed
255 In later kernel versions, support for this option has been phased out:
256 Linux 2.4 silently ignores it, and Linux 2.6 generates a kernel warning
257 (printk()) if a program uses this option.
258 Linux 2.0 also enabled BSD bug-to-bug compatibility
259 options (random header changing, skipping of the broadcast flag) for raw
260 sockets with this option, but that was removed in Linux 2.2.
263 Enable socket debugging.
264 Only allowed for processes with the
266 capability or an effective user ID of 0.
268 .BR SO_DOMAIN " (since Linux 2.6.32)"
269 Retrieves the socket domain as an integer, returning a value such as
274 This socket option is read-only.
277 Get and clear the pending socket error.
278 This socket option is read-only.
282 Don't send via a gateway, only send to directly connected hosts.
283 The same effect can be achieved by setting the
288 Expects an integer boolean flag.
291 Enable sending of keep-alive messages on connection-oriented sockets.
292 Expects an integer boolean flag.
305 int l_onoff; /* linger active */
306 int l_linger; /* how many seconds to linger for */
315 will not return until all queued messages for the socket have been
316 successfully sent or the linger timeout has been reached.
318 the call returns immediately and the closing is done in the background.
319 When the socket is closed as part of
321 it always lingers in the background.
324 If this option is enabled,
325 out-of-band data is directly placed into the receive data stream.
326 Otherwise out-of-band data is only passed when the
328 flag is set during receiving.
329 .\" don't document it because it can do too much harm.
333 Enable or disable the receiving of the
336 For more information see
338 .\" FIXME Document SO_PASSSEC, added in 2.6.18; there is some info
339 .\" in the 2.6.18 ChangeLog
342 Return the credentials of the foreign process connected to this socket.
343 This is only possible for connected
347 stream and datagram socket pairs created using
351 The returned credentials are those that were in effect at the time
359 This socket option is read-only.
362 Set the protocol-defined priority for all packets to be sent on
364 Linux uses this value to order the networking queues:
365 packets with a higher priority may be processed first depending
366 on the selected device queueing discipline.
369 this also sets the IP type-of-service (TOS) field for outgoing packets.
370 Setting a priority outside the range 0 to 6 requires the
374 .BR SO_PROTOCOL " (since Linux 2.6.32)"
375 Retrieves the socket protocol as an integer, returning a value such as
380 This socket option is read-only.
383 Sets or gets the maximum socket receive buffer in bytes.
384 The kernel doubles this value (to allow space for bookkeeping overhead)
386 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
388 and this doubled value is returned by
390 The default value is set by the
391 .I /proc/sys/net/core/rmem_default
392 file, and the maximum allowed value is set by the
393 .I /proc/sys/net/core/rmem_max
395 The minimum (doubled) value for this option is 256.
397 .BR SO_RCVBUFFORCE " (since Linux 2.6.14)"
398 Using this socket option, a privileged
399 .RB ( CAP_NET_ADMIN )
400 process can perform the same task as
404 limit can be overridden.
406 .BR SO_RCVLOWAT " and " SO_SNDLOWAT
407 Specify the minimum number of bytes in the buffer until the socket layer
408 will pass the data to the protocol
410 or the user on receiving
412 These two values are initialized to 1.
414 is not changeable on Linux
420 only since Linux 2.4.
425 system calls currently do not respect the
428 and mark a socket readable when even a single byte of data is available.
429 A subsequent read from the socket will block until
432 .\" See http://marc.theaimsgroup.com/?l=linux-kernel&m=111049368106984&w=2
433 .\" Tested on kernel 2.6.14 -- mtk, 30 Nov 05
435 .BR SO_RCVTIMEO " and " SO_SNDTIMEO
436 .\" Not implemented in 2.0.
437 .\" Implemented in 2.1.11 for getsockopt: always return a zero struct.
438 .\" Implemented in 2.3.41 for setsockopt, and actually used.
439 Specify the receiving or sending timeouts until reporting an error.
441 .IR "struct timeval" .
442 If an input or output function blocks for this period of time, and
443 data has been sent or received, the return value of that function
444 will be the amount of data transferred; if no data has been transferred
445 and the timeout has been reached then \-1 is returned with
451 .\" in fact to EAGAIN
452 just as if the socket was specified to be nonblocking.
453 If the timeout is set to zero (the default)
454 then the operation will never timeout.
455 Timeouts only have effect for system calls that perform socket I/O (e.g.,
460 timeouts have no effect for
467 Indicates that the rules used in validating addresses supplied in a
469 call should allow reuse of local addresses.
473 means that a socket may bind, except when there
474 is an active listening socket bound to the address.
475 When the listening socket is bound to
477 with a specific port then it is not possible
478 to bind to this port for any local address.
479 Argument is an integer boolean flag.
482 Sets or gets the maximum socket send buffer in bytes.
483 The kernel doubles this value (to allow space for bookkeeping overhead)
485 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
487 and this doubled value is returned by
489 The default value is set by the
490 .I /proc/sys/net/core/wmem_default
491 file and the maximum allowed value is set by the
492 .I /proc/sys/net/core/wmem_max
494 The minimum (doubled) value for this option is 2048.
496 .BR SO_SNDBUFFORCE " (since Linux 2.6.14)"
497 Using this socket option, a privileged
498 .RB ( CAP_NET_ADMIN )
499 process can perform the same task as
503 limit can be overridden.
506 Enable or disable the receiving of the
509 The timestamp control message is sent with level
516 reception time of the last packet passed to the user in this call.
519 for details on control messages.
522 Gets the socket type as an integer (e.g.,
524 This socket option is read-only.
526 When writing onto a connection-oriented socket that has been shut down
527 (by the local or the remote end)
529 is sent to the writing process and
532 The signal is not sent when the write call
537 When requested with the
544 is sent when an I/O event occurs.
545 It is possible to use
549 in the signal handler to find out which socket the event occurred on.
550 An alternative (in Linux 2.2) is to set a real-time signal using the
553 the handler of the real time signal will be called with
554 the file descriptor in the
560 for more information.
562 Under some circumstances (e.g., multiple processes accessing a
563 single socket), the condition that caused the
565 may have already disappeared when the process reacts to the signal.
566 If this happens, the process should wait again because Linux
567 will resend the signal later.
568 .\" .SS Ancillary Messages
570 The core socket networking parameters can be accessed
571 via files in the directory
572 .IR /proc/sys/net/core/ .
575 contains the default setting in bytes of the socket receive buffer.
578 contains the maximum socket receive buffer size in bytes which a user may
584 contains the default setting in bytes of the socket send buffer.
587 contains the maximum socket send buffer size in bytes which a user may
592 .IR message_cost " and " message_burst
593 configure the token bucket filter used to load limit warning messages
594 caused by external network events.
596 .I netdev_max_backlog
597 Maximum number of packets in the global input queue.
600 Maximum length of ancillary data and user control data like the iovecs
602 .\" netdev_fastroute is not documented because it is experimental
604 These operations can be accessed using
609 .IB error " = ioctl(" ip_socket ", " ioctl_type ", " &value_result ");"
616 with the receive timestamp of the last packet passed to the user.
617 This is useful for accurate round trip time measurements.
621 .IR "struct timeval" .
623 This ioctl should only be used if the socket option
625 is not set on the socket.
626 Otherwise, it returns the timestamp of the
627 last packet that was received while
629 was not set, or it fails if no such packet has been received,
638 Set the process or process group to send
644 asynchronous I/O operation has finished or urgent data is available.
645 The argument is a pointer to a
647 If the argument is positive, send the signals to that process.
649 argument is negative, send the signals to the process group with the ID
650 of the absolute value of the argument.
651 The process may only choose itself or its own process group to receive
652 signals unless it has the
654 capability or an effective UID of 0.
659 flag to enable or disable asynchronous I/O mode of the socket.
660 Asynchronous I/O mode means that the
662 signal or the signal set with
664 is raised when a new I/O event occurs.
666 Argument is an integer boolean flag.
667 (This operation is synonymous with the use of
675 Get the current process or process group that receives
698 was introduced in Linux 2.0.30.
703 interfaces was introduced in Linux 2.2.
707 are supported since Linux 2.3.41.
708 Earlier, timeouts were fixed to
709 a protocol-specific setting, and could not be read or written.
711 Linux assumes that half of the send/receive buffer is used for internal
712 kernel structures; thus the values in the corresponding
714 files are twice what can be observed on the wire.
716 Linux will only allow port reuse with the
719 when this option was set both in the previous program that performed a
721 to the port and in the program that wants to reuse the port.
722 This differs from some implementations (e.g., FreeBSD)
723 where only the later program needs to set the
726 Typically this difference is invisible, since, for example, a server
727 program is designed to always set this option.
735 .\" FIXME Document SO_ATTACH_FILTER and SO_DETACH_FILTER
737 The suggested interface to use them is via the libpcap
740 .\" This man page was written by Andi Kleen.
745 .BR capabilities (7),