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:
20 .\" SO_PEERNAME (2.4?)
22 .\" Seems to do something similar to getpeernam(), but then
23 .\" why is it necessary / how does it differ?
24 .\" SO_TIMESTAMPNS (2.6.22)
25 .\" Documentation/networking/timestamping.txt
26 .\" commit 92f37fd2ee805aa77925c1e64fd56088b46094fc
27 .\" Author: Eric Dumazet <dada1@cosmosbay.com>
28 .\" SO_TIMESTAMPING (2.6.30)
29 .\" Documentation/networking/timestamping.txt
30 .\" commit cb9eff097831007afb30d64373f29d99825d0068
31 .\" Author: Patrick Ohly <patrick.ohly@intel.com>
32 .\" SO_RXQ_OVFL (2.6.33)
33 .\" commit 3b885787ea4112eaa80945999ea0901bf742707f
34 .\" Author: Neil Horman <nhorman@tuxdriver.com>
35 .\" SO_WIFI_STATUS (3.3)
36 .\" commit 6e3e939f3b1bf8534b32ad09ff199d88800835a0
37 .\" Author: Johannes Berg <johannes.berg@intel.com>
38 .\" Also: SCM_WIFI_STATUS
40 .\" commit ef64a54f6e558155b4f149bb10666b9e914b6c54
41 .\" Author: Pavel Emelyanov <xemul@parallels.com>
43 .\" commit 3bdc0eba0b8b47797f4a76e377dd8360f317450f
44 .\" Author: Ben Greear <greearb@candelatech.com>
46 .TH SOCKET 7 2012-12-15 Linux "Linux Programmer's Manual"
48 socket \- Linux socket interface
50 .B #include <sys/socket.h>
52 .IB sockfd " = socket(int " socket_family ", int " socket_type ", int " protocol );
54 This manual page describes the Linux networking socket layer user
56 The BSD compatible sockets
57 are the uniform interface
58 between the user process and the network protocol stacks in the kernel.
59 The protocol modules are grouped into
62 .BR AF_INET ", " AF_IPX ", " AF_PACKET
71 for more information on families and types.
72 .SS Socket-layer functions
73 These functions are used by the user process to send or receive packets
74 and to do other socket operations.
75 For more information see their respective manual pages.
80 connects a socket to a remote socket address,
83 function binds a socket to a local socket address,
85 tells the socket that new connections shall be accepted, and
87 is used to get a new socket with a new incoming connection.
89 returns two connected anonymous sockets (only implemented for a few
97 send data over a socket, and
101 receive data from a socket.
105 wait for arriving data or a readiness to send data.
106 In addition, the standard I/O operations like
113 can be used to read and write data.
116 returns the local socket address and
118 returns the remote socket address.
122 are used to set or get socket layer or protocol options.
124 can be used to set or read some other options.
127 is used to close a socket.
129 closes parts of a full-duplex socket connection.
135 with a nonzero position is not supported on sockets.
137 It is possible to do nonblocking I/O on sockets by setting the
139 flag on a socket file descriptor using
141 Then all operations that would block will (usually)
144 (operation should be retried later);
149 The user can then wait for various events via
158 Event:Poll flag:Occurrence
163 A connection setup has been completed
164 (for connection-oriented sockets)
167 A disconnection request has been initiated by the other end.
170 A connection is broken (only for connection-oriented protocols).
171 When the socket is written
176 Socket has enough send buffer space for writing new data.
187 Read/Write:POLLERR:An asynchronous error occurred.
188 Read/Write:POLLHUP:The other end has shut down one direction.
194 .\" FIXME . The following is not true currently:
195 .\" It is no I/O event when the connection
196 .\" is broken from the local end using
206 is to let the kernel inform the application about events
212 flag must be set on a socket file descriptor via
214 and a valid signal handler for
216 must be installed via
221 .SS Socket address structures
222 Each socket domain has its own format for socket addresses,
223 with a domain-specific address structure.
224 Each of these structures begins with an
225 integer "family" field (typed as
227 that indicates the type of the address structure.
229 the various system calls (e.g.,
234 .BR getpeername (2)),
235 which are generic to all socket domains,
236 to determine the domain of a particular socket address.
238 To allow any type of socket address to be passed to
239 interfaces in the sockets API,
241 .IR "struct sockaddr"
243 The purpose of this type is purely to allow casting of
244 domain-specific socket address types to a "generic" type,
245 so as to avoid compiler warnings about type mismatches in
246 calls to the sockets API.
248 In addition, the sockets API provides the data type
249 .IR "struct sockaddr_storage".
251 is suitable to accommodate all supported domain-specific socket
252 address structures; it is large enough and is aligned properly.
253 (In particular, it is large enough to hold
254 IPv6 socket addresses.)
255 The structure includes the following field, which can be used to identify
256 the type of socket address actually stored in the structure:
260 sa_family_t ss_family;
266 structure is useful in programs that must handle socket addresses
268 (e.g., programs that must deal with both IPv4 and IPv6 socket addresses).
270 These socket options can be set by using
274 with the socket level set to
278 .\" In the list below, the text used to describe argument types
279 .\" for each socket option should be more consistent
281 .\" SO_ACCEPTCONN is in POSIX.1-2001, and its origin is explained in
282 .\" W R Stevens, UNPv1
285 Returns a value indicating whether or not this socket has been marked
286 to accept connections with
288 The value 0 indicates that this is not a listening socket,
289 the value 1 indicates that this is a listening socket.
290 This socket option is read-only.
293 Bind this socket to a particular device like \(lqeth0\(rq,
294 as specified in the passed interface name.
296 name is an empty string or the option length is zero, the socket device
298 The passed option is a variable-length null-terminated
299 interface name string with the maximum size of
301 If a socket is bound to an interface,
302 only packets received from that particular interface are processed by the
304 Note that this only works for some socket types, particularly
307 It is not supported for packet sockets (use normal
311 This option is available for getting since Linux 3.8. The option
312 length should contain the buffer size to receive the device name
313 and is recommended to be
315 bytes. The real device name length is reported back in the option
319 Set or get the broadcast flag.
320 When enabled, datagram sockets are allowed to send
321 packets to a broadcast address.
322 This option has no effect on stream-oriented sockets.
325 Enable BSD bug-to-bug compatibility.
326 This is used by the UDP protocol module in Linux 2.0 and 2.2.
327 If enabled ICMP errors received for a UDP socket will not be passed
329 In later kernel versions, support for this option has been phased out:
330 Linux 2.4 silently ignores it, and Linux 2.6 generates a kernel warning
331 (printk()) if a program uses this option.
332 Linux 2.0 also enabled BSD bug-to-bug compatibility
333 options (random header changing, skipping of the broadcast flag) for raw
334 sockets with this option, but that was removed in Linux 2.2.
337 Enable socket debugging.
338 Only allowed for processes with the
340 capability or an effective user ID of 0.
342 .BR SO_DOMAIN " (since Linux 2.6.32)"
343 Retrieves the socket domain as an integer, returning a value such as
348 This socket option is read-only.
351 Get and clear the pending socket error.
352 This socket option is read-only.
356 Don't send via a gateway, only send to directly connected hosts.
357 The same effect can be achieved by setting the
362 Expects an integer boolean flag.
365 Enable sending of keep-alive messages on connection-oriented sockets.
366 Expects an integer boolean flag.
379 int l_onoff; /* linger active */
380 int l_linger; /* how many seconds to linger for */
389 will not return until all queued messages for the socket have been
390 successfully sent or the linger timeout has been reached.
392 the call returns immediately and the closing is done in the background.
393 When the socket is closed as part of
395 it always lingers in the background.
397 .BR SO_MARK " (since Linux 2.6.25)"
398 .\" commit 4a19ec5800fc3bb64e2d87c4d9fdd9e636086fe0
399 .\" and 914a9ab386a288d0f22252fc268ecbc048cdcbd5
400 Set the mark for each packet sent through this socket
401 (similar to the netfilter MARK target but socket-based).
402 Changing the mark can be used for mark-based
403 routing without netfilter or for packet filtering.
404 Setting this option requires the
409 If this option is enabled,
410 out-of-band data is directly placed into the receive data stream.
411 Otherwise out-of-band data is only passed when the
413 flag is set during receiving.
414 .\" don't document it because it can do too much harm.
418 Enable or disable the receiving of the
421 For more information see
423 .\" FIXME Document SO_PASSSEC, added in 2.6.18; there is some info
424 .\" in the 2.6.18 ChangeLog
427 Return the credentials of the foreign process connected to this socket.
428 This is only possible for connected
432 stream and datagram socket pairs created using
436 The returned credentials are those that were in effect at the time
444 This socket option is read-only.
447 Set the protocol-defined priority for all packets to be sent on
449 Linux uses this value to order the networking queues:
450 packets with a higher priority may be processed first depending
451 on the selected device queueing discipline.
454 this also sets the IP type-of-service (TOS) field for outgoing packets.
455 Setting a priority outside the range 0 to 6 requires the
459 .BR SO_PROTOCOL " (since Linux 2.6.32)"
460 Retrieves the socket protocol as an integer, returning a value such as
465 This socket option is read-only.
468 Sets or gets the maximum socket receive buffer in bytes.
469 The kernel doubles this value (to allow space for bookkeeping overhead)
471 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
473 and this doubled value is returned by
475 .\" The following thread on LMKL is quite informative:
476 .\" getsockopt/setsockopt with SO_RCVBUF and SO_SNDBUF "non-standard" behaviour
478 .\" http://thread.gmane.org/gmane.linux.kernel/1328935
479 The default value is set by the
480 .I /proc/sys/net/core/rmem_default
481 file, and the maximum allowed value is set by the
482 .I /proc/sys/net/core/rmem_max
484 The minimum (doubled) value for this option is 256.
486 .BR SO_RCVBUFFORCE " (since Linux 2.6.14)"
487 Using this socket option, a privileged
488 .RB ( CAP_NET_ADMIN )
489 process can perform the same task as
493 limit can be overridden.
495 .BR SO_RCVLOWAT " and " SO_SNDLOWAT
496 Specify the minimum number of bytes in the buffer until the socket layer
497 will pass the data to the protocol
499 or the user on receiving
501 These two values are initialized to 1.
503 is not changeable on Linux
509 only since Linux 2.4.
514 system calls currently do not respect the
517 and mark a socket readable when even a single byte of data is available.
518 A subsequent read from the socket will block until
521 .\" See http://marc.theaimsgroup.com/?l=linux-kernel&m=111049368106984&w=2
522 .\" Tested on kernel 2.6.14 -- mtk, 30 Nov 05
524 .BR SO_RCVTIMEO " and " SO_SNDTIMEO
525 .\" Not implemented in 2.0.
526 .\" Implemented in 2.1.11 for getsockopt: always return a zero struct.
527 .\" Implemented in 2.3.41 for setsockopt, and actually used.
528 Specify the receiving or sending timeouts until reporting an error.
530 .IR "struct timeval" .
531 If an input or output function blocks for this period of time, and
532 data has been sent or received, the return value of that function
533 will be the amount of data transferred; if no data has been transferred
534 and the timeout has been reached then \-1 is returned with
540 .\" in fact to EAGAIN
545 just as if the socket was specified to be nonblocking.
546 If the timeout is set to zero (the default)
547 then the operation will never timeout.
548 Timeouts only have effect for system calls that perform socket I/O (e.g.,
553 timeouts have no effect for
560 Indicates that the rules used in validating addresses supplied in a
562 call should allow reuse of local addresses.
566 means that a socket may bind, except when there
567 is an active listening socket bound to the address.
568 When the listening socket is bound to
570 with a specific port then it is not possible
571 to bind to this port for any local address.
572 Argument is an integer boolean flag.
575 Sets or gets the maximum socket send buffer in bytes.
576 The kernel doubles this value (to allow space for bookkeeping overhead)
578 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
579 .\" See also the comment to SO_RCVBUF (17 Jul 2012 LKML mail)
581 and this doubled value is returned by
583 The default value is set by the
584 .I /proc/sys/net/core/wmem_default
585 file and the maximum allowed value is set by the
586 .I /proc/sys/net/core/wmem_max
588 The minimum (doubled) value for this option is 2048.
590 .BR SO_SNDBUFFORCE " (since Linux 2.6.14)"
591 Using this socket option, a privileged
592 .RB ( CAP_NET_ADMIN )
593 process can perform the same task as
597 limit can be overridden.
600 Enable or disable the receiving of the
603 The timestamp control message is sent with level
610 reception time of the last packet passed to the user in this call.
613 for details on control messages.
616 Gets the socket type as an integer (e.g.,
618 This socket option is read-only.
620 When writing onto a connection-oriented socket that has been shut down
621 (by the local or the remote end)
623 is sent to the writing process and
626 The signal is not sent when the write call
631 When requested with the
638 is sent when an I/O event occurs.
639 It is possible to use
643 in the signal handler to find out which socket the event occurred on.
644 An alternative (in Linux 2.2) is to set a real-time signal using the
647 the handler of the real time signal will be called with
648 the file descriptor in the
654 for more information.
656 Under some circumstances (e.g., multiple processes accessing a
657 single socket), the condition that caused the
659 may have already disappeared when the process reacts to the signal.
660 If this happens, the process should wait again because Linux
661 will resend the signal later.
662 .\" .SS Ancillary messages
664 The core socket networking parameters can be accessed
665 via files in the directory
666 .IR /proc/sys/net/core/ .
669 contains the default setting in bytes of the socket receive buffer.
672 contains the maximum socket receive buffer size in bytes which a user may
678 contains the default setting in bytes of the socket send buffer.
681 contains the maximum socket send buffer size in bytes which a user may
686 .IR message_cost " and " message_burst
687 configure the token bucket filter used to load limit warning messages
688 caused by external network events.
690 .I netdev_max_backlog
691 Maximum number of packets in the global input queue.
694 Maximum length of ancillary data and user control data like the iovecs
696 .\" netdev_fastroute is not documented because it is experimental
698 These operations can be accessed using
703 .IB error " = ioctl(" ip_socket ", " ioctl_type ", " &value_result ");"
710 with the receive timestamp of the last packet passed to the user.
711 This is useful for accurate round trip time measurements.
715 .IR "struct timeval" .
717 This ioctl should only be used if the socket option
719 is not set on the socket.
720 Otherwise, it returns the timestamp of the
721 last packet that was received while
723 was not set, or it fails if no such packet has been received,
732 Set the process or process group to send
738 asynchronous I/O operation has finished or urgent data is available.
739 The argument is a pointer to a
741 If the argument is positive, send the signals to that process.
743 argument is negative, send the signals to the process group with the ID
744 of the absolute value of the argument.
745 The process may only choose itself or its own process group to receive
746 signals unless it has the
748 capability or an effective UID of 0.
753 flag to enable or disable asynchronous I/O mode of the socket.
754 Asynchronous I/O mode means that the
756 signal or the signal set with
758 is raised when a new I/O event occurs.
760 Argument is an integer boolean flag.
761 (This operation is synonymous with the use of
769 Get the current process or process group that receives
792 was introduced in Linux 2.0.30.
797 interfaces was introduced in Linux 2.2.
801 are supported since Linux 2.3.41.
802 Earlier, timeouts were fixed to
803 a protocol-specific setting, and could not be read or written.
805 Linux assumes that half of the send/receive buffer is used for internal
806 kernel structures; thus the values in the corresponding
808 files are twice what can be observed on the wire.
810 Linux will only allow port reuse with the
813 when this option was set both in the previous program that performed a
815 to the port and in the program that wants to reuse the port.
816 This differs from some implementations (e.g., FreeBSD)
817 where only the later program needs to set the
820 Typically this difference is invisible, since, for example, a server
821 program is designed to always set this option.
829 .\" FIXME Document SO_ATTACH_FILTER and SO_DETACH_FILTER
831 The suggested interface to use them is via the libpcap
834 .\" This man page was written by Andi Kleen.
840 .BR capabilities (7),