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
17 .\" The following are not yet documented:
22 .\" SO_PROTOCOL (2.6.32)
23 .\" SO_DOMAIN (2.6.32)
24 .\" SO_RXQ_OVFL (2.6.33)
26 .TH SOCKET 7 2008-12-03 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
210 .\" SO_ACCEPTCONN is in POSIX.1-2001, and its origin is explained in
211 .\" W R Stevens, UNPv1
214 Returns a value indicating whether or not this socket has been marked
215 to accept connections with
217 The value 0 indicates that this is not a listening socket,
218 the value 1 indicates that this is a listening socket.
224 Bind this socket to a particular device like \(lqeth0\(rq,
225 as specified in the passed interface name.
227 name is an empty string or the option length is zero, the socket device
229 The passed option is a variable-length null-terminated
230 interface name string with the maximum size of
232 If a socket is bound to an interface,
233 only packets received from that particular interface are processed by the
235 Note that this only works for some socket types, particularly
238 It is not supported for packet sockets (use normal
243 Set or get the broadcast flag.
244 When enabled, datagram sockets
245 receive packets sent to a broadcast address and they are allowed to send
246 packets to a broadcast address.
247 This option has no effect on stream-oriented sockets.
250 Enable BSD bug-to-bug compatibility.
251 This is used by the UDP protocol module in Linux 2.0 and 2.2.
252 If enabled ICMP errors received for a UDP socket will not be passed
254 In later kernel versions, support for this option has been phased out:
255 Linux 2.4 silently ignores it, and Linux 2.6 generates a kernel warning
256 (printk()) if a program uses this option.
257 Linux 2.0 also enabled BSD bug-to-bug compatibility
258 options (random header changing, skipping of the broadcast flag) for raw
259 sockets with this option, but that was removed in Linux 2.2.
262 Enable socket debugging.
263 Only allowed for processes with the
265 capability or an effective user ID of 0.
268 Get and clear the pending socket error.
274 Don't send via a gateway, only send to directly connected hosts.
275 The same effect can be achieved by setting the
280 Expects an integer boolean flag.
283 Enable sending of keep-alive messages on connection-oriented sockets.
284 Expects an integer boolean flag.
297 int l_onoff; /* linger active */
298 int l_linger; /* how many seconds to linger for */
307 will not return until all queued messages for the socket have been
308 successfully sent or the linger timeout has been reached.
310 the call returns immediately and the closing is done in the background.
311 When the socket is closed as part of
313 it always lingers in the background.
316 If this option is enabled,
317 out-of-band data is directly placed into the receive data stream.
318 Otherwise out-of-band data is only passed when the
320 flag is set during receiving.
321 .\" don't document it because it can do too much harm.
325 Enable or disable the receiving of the
328 For more information see
330 .\" FIXME Document SO_PASSSEC, added in 2.6.18; there is some info
331 .\" in the 2.6.18 ChangeLog
334 Return the credentials of the foreign process connected to this socket.
335 This is only possible for connected
339 stream and datagram socket pairs created using
343 The returned credentials are those that were in effect at the time
355 Set the protocol-defined priority for all packets to be sent on
357 Linux uses this value to order the networking queues:
358 packets with a higher priority may be processed first depending
359 on the selected device queueing discipline.
362 this also sets the IP type-of-service (TOS) field for outgoing packets.
363 Setting a priority outside the range 0 to 6 requires the
368 Sets or gets the maximum socket receive buffer in bytes.
369 The kernel doubles this value (to allow space for bookkeeping overhead)
371 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
373 and this doubled value is returned by
375 The default value is set by the
376 .I /proc/sys/net/core/rmem_default
377 file, and the maximum allowed value is set by the
378 .I /proc/sys/net/core/rmem_max
380 The minimum (doubled) value for this option is 256.
382 .BR SO_RCVBUFFORCE " (since Linux 2.6.14)"
383 Using this socket option, a privileged
384 .RB ( CAP_NET_ADMIN )
385 process can perform the same task as
389 limit can be overridden.
391 .BR SO_RCVLOWAT " and " SO_SNDLOWAT
392 Specify the minimum number of bytes in the buffer until the socket layer
393 will pass the data to the protocol
395 or the user on receiving
397 These two values are initialized to 1.
399 is not changeable on Linux
405 only since Linux 2.4.
410 system calls currently do not respect the
413 and mark a socket readable when even a single byte of data is available.
414 A subsequent read from the socket will block until
417 .\" See http://marc.theaimsgroup.com/?l=linux-kernel&m=111049368106984&w=2
418 .\" Tested on kernel 2.6.14 -- mtk, 30 Nov 05
420 .BR SO_RCVTIMEO " and " SO_SNDTIMEO
421 .\" Not implemented in 2.0.
422 .\" Implemented in 2.1.11 for getsockopt: always return a zero struct.
423 .\" Implemented in 2.3.41 for setsockopt, and actually used.
424 Specify the receiving or sending timeouts until reporting an error.
426 .IR "struct timeval" .
427 If an input or output function blocks for this period of time, and
428 data has been sent or received, the return value of that function
429 will be the amount of data transferred; if no data has been transferred
430 and the timeout has been reached then \-1 is returned with
436 .\" in fact to EAGAIN
437 just as if the socket was specified to be nonblocking.
438 If the timeout is set to zero (the default)
439 then the operation will never timeout.
440 Timeouts only have effect for system calls that perform socket I/O (e.g.,
445 timeouts have no effect for
452 Indicates that the rules used in validating addresses supplied in a
454 call should allow reuse of local addresses.
458 means that a socket may bind, except when there
459 is an active listening socket bound to the address.
460 When the listening socket is bound to
462 with a specific port then it is not possible
463 to bind to this port for any local address.
464 Argument is an integer boolean flag.
467 Sets or gets the maximum socket send buffer in bytes.
468 The kernel doubles this value (to allow space for bookkeeping overhead)
470 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
472 and this doubled value is returned by
474 The default value is set by the
475 .I /proc/sys/net/core/wmem_default
476 file and the maximum allowed value is set by the
477 .I /proc/sys/net/core/wmem_max
479 The minimum (doubled) value for this option is 2048.
481 .BR SO_SNDBUFFORCE " (since Linux 2.6.14)"
482 Using this socket option, a privileged
483 .RB ( CAP_NET_ADMIN )
484 process can perform the same task as
488 limit can be overridden.
491 Enable or disable the receiving of the
494 The timestamp control message is sent with level
501 reception time of the last packet passed to the user in this call.
504 for details on control messages.
507 Gets the socket type as an integer (like
513 When writing onto a connection-oriented socket that has been shut down
514 (by the local or the remote end)
516 is sent to the writing process and
519 The signal is not sent when the write call
524 When requested with the
531 is sent when an I/O event occurs.
532 It is possible to use
536 in the signal handler to find out which socket the event occurred on.
537 An alternative (in Linux 2.2) is to set a real-time signal using the
540 the handler of the real time signal will be called with
541 the file descriptor in the
547 for more information.
549 Under some circumstances (e.g., multiple processes accessing a
550 single socket), the condition that caused the
552 may have already disappeared when the process reacts to the signal.
553 If this happens, the process should wait again because Linux
554 will resend the signal later.
555 .\" .SS Ancillary Messages
557 The core socket networking parameters can be accessed
558 via files in the directory
559 .IR /proc/sys/net/core/ .
562 contains the default setting in bytes of the socket receive buffer.
565 contains the maximum socket receive buffer size in bytes which a user may
571 contains the default setting in bytes of the socket send buffer.
574 contains the maximum socket send buffer size in bytes which a user may
579 .IR message_cost " and " message_burst
580 configure the token bucket filter used to load limit warning messages
581 caused by external network events.
583 .I netdev_max_backlog
584 Maximum number of packets in the global input queue.
587 Maximum length of ancillary data and user control data like the iovecs
589 .\" netdev_fastroute is not documented because it is experimental
591 These operations can be accessed using
596 .IB error " = ioctl(" ip_socket ", " ioctl_type ", " &value_result ");"
603 with the receive timestamp of the last packet passed to the user.
604 This is useful for accurate round trip time measurements.
608 .IR "struct timeval" .
610 This ioctl should only be used if the socket option
612 is not set on the socket.
613 Otherwise, it returns the timestamp of the
614 last packet that was received while
616 was not set, or it fails if no such packet has been received,
625 Set the process or process group to send
631 asynchronous I/O operation has finished or urgent data is available.
632 The argument is a pointer to a
634 If the argument is positive, send the signals to that process.
636 argument is negative, send the signals to the process group with the ID
637 of the absolute value of the argument.
638 The process may only choose itself or its own process group to receive
639 signals unless it has the
641 capability or an effective UID of 0.
646 flag to enable or disable asynchronous I/O mode of the socket.
647 Asynchronous I/O mode means that the
649 signal or the signal set with
651 is raised when a new I/O event occurs.
653 Argument is an integer boolean flag.
654 (This operation is synonymous with the use of
662 Get the current process or process group that receives
685 was introduced in Linux 2.0.30.
690 interfaces was introduced in Linux 2.2.
694 are supported since Linux 2.3.41.
695 Earlier, timeouts were fixed to
696 a protocol-specific setting, and could not be read or written.
698 Linux assumes that half of the send/receive buffer is used for internal
699 kernel structures; thus the values in the corresponding
701 files are twice what can be observed on the wire.
703 Linux will only allow port reuse with the
706 when this option was set both in the previous program that performed a
708 to the port and in the program that wants to reuse the port.
709 This differs from some implementations (e.g., FreeBSD)
710 where only the later program needs to set the
713 Typically this difference is invisible, since, for example, a server
714 program is designed to always set this option.
722 .\" FIXME Document SO_ATTACH_FILTER and SO_DETACH_FILTER
724 The suggested interface to use them is via the libpcap
727 .\" This man page was written by Andi Kleen.
732 .BR capabilities (7),