Skip to content

Commit 2efa2c7

Browse files
committed
gh-45048: Document the availability and uses of the SO_ constants.
Original Author: ammaraskar <[email protected]> Signed-off-by: ukarroum <[email protected]>
1 parent 5d8e432 commit 2efa2c7

File tree

1 file changed

+282
-0
lines changed

1 file changed

+282
-0
lines changed

Doc/library/socket.rst

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,288 @@ Constants
411411

412412
.. _socket-unix-constants:
413413

414+
Socket level options
415+
''''''''''''''''''''
416+
417+
These constants are passed to :meth:`socket.setsockopt` and :meth:`socket.getsockopt`
418+
with a level of :const:`SOL_SOCKET` to set and get socket settings. Their availability
419+
and behavior is dependent upon the underlying OS. Basic descriptions of each option are
420+
documented below but your OS's documentation will be far more reliable. The following
421+
table serves a basic compatibility guide.
422+
423+
Unless otherwise noted, these options are available on Linux versions >= 2.3,
424+
FreeBSD >= 2 and Windows >= 2000.
425+
426+
======================= =========== =========== ================
427+
Socket Option Linux FreeBSD Windows
428+
======================= =========== =========== ================
429+
**SO_DEBUG** ✓ ✓ ✓ [1]_
430+
**SO_ACCEPTCONN** ✓ ✓ ✓
431+
**SO_REUSEADDR** ✓ ✓ ✓
432+
**SO_EXCLUSIVEADDRUSE** ✓
433+
**SO_KEEPALIVE** ✓ ✓ ✓
434+
**SO_DONTROUTE** ✓ ✓ ✓ [1]_
435+
**SO_BROADCAST** ✓ ✓ ✓ >= Vista
436+
**SO_USELOOPBACK** ✓ ✓ >= Vista [1]_
437+
**SO_LINGER** ✓ ✓ ✓
438+
**SO_OOBINLINE** ✓ ✓ ✓
439+
**SO_REUSEPORT** ✓ >= 3.9 ✓
440+
**SO_SNDBUF** ✓ ✓ ✓
441+
**SO_RCVBUF** ✓ ✓ ✓
442+
**SO_SNDLOWAT** ✓ ✓ ✓ [1]_
443+
**SO_RCVLOWAT** ✓ ✓ ✓ [1]_
444+
**SO_SNDTIMEO** ✓ ✓ ✓
445+
**SO_RCVTIMEO** ✓ ✓ ✓
446+
**SO_ERROR** ✓ ✓ ✓
447+
**SO_TYPE** ✓ ✓ ✓
448+
**SO_SETFIB** ✓ >= 7.1
449+
**SO_PASSCRED** ✓
450+
**SO_PEERCRED** ✓
451+
**SO_PASSSEC** ✓ >= 2.6.2
452+
**SO_PEERSEC** ✓ >= 2.6.2
453+
**SO_BINDTODEVICE** ✓
454+
**SO_PRIORITY** ✓
455+
**SO_MARK** ✓ >= 2.6.25
456+
**SO_DOMAIN** ✓ >= 2.6.32
457+
**SO_PROTOCOL** ✓ >= 2.6.32 ✓ >= 8.3
458+
======================= =========== =========== ================
459+
460+
.. [1] On Windows these options exist purely for compatibility
461+
and are `not functional <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740532(v=vs.85).aspx>`_.
462+
463+
.. data:: SOL_SOCKET
464+
465+
This is the level parameter passed to :meth:`socket.setsockopt`
466+
and :meth:`socket.getsockopt` to change socket level parameters.
467+
468+
For example, this is how to change a socket's sending buffer size. ::
469+
470+
>>> s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
471+
16384
472+
>>> s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048)
473+
>>> s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
474+
2048
475+
476+
.. data:: SO_DEBUG
477+
478+
Setting this option to 1 on a socket causes it to print out debugging
479+
information into the kernel log. (Viewable with dmesg)
480+
481+
.. Availability: Linux, Windows `(non functional) <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740532(v=vs.85).aspx>`_
482+
483+
.. data:: SO_ACCEPTCONN
484+
485+
Returns 1 if the socket is marked to accept connections with
486+
:meth:`socket.listen`. This option is read only.
487+
488+
.. Availability: Linux, Windows
489+
490+
.. data:: SO_REUSEADDR
491+
492+
Allows a socket to be able to bind to an address that was previously
493+
bound to. When a socket shuts down, whether by virtue of the program
494+
exiting or an explicit call to shut it down, it takes the OS some time
495+
to terminate existing connections and perform the proper shut down
496+
procedure. ``SO_REUSEADDR`` allows bypassing of this behavior and permits
497+
another socket to be able to bind to the address.
498+
499+
.. Availability: Linux, Windows
500+
501+
.. data:: SO_EXCLUSIVEADDRUSE
502+
503+
Allows overriding ``SO_REUSEADDR`` behavior for exclusive access to an
504+
address and port for high availability services.
505+
506+
.. seealso::
507+
508+
`Microsoft's documentation <https://msdn.microsoft.com/en-us/library/windows/desktop/cc150667(v=vs.85).aspx>`_
509+
on this option.
510+
511+
.. Availability: Windows
512+
513+
.. data:: SO_KEEPALIVE
514+
515+
Set to 1 to enable sending of keep-alive packets on certain types of
516+
sockets.
517+
518+
.. Availability: Linux, Windows
519+
520+
.. data:: SO_DONTROUTE
521+
522+
Indicates that packets sent through this socket should be routed
523+
through the interface it is bound to.
524+
525+
.. Availability: Linux, Windows `(non functional) <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740532(v=vs.85).aspx>`_
526+
527+
.. data:: SO_BROADCAST
528+
529+
This option may be set to 1 to enable broadcasting messages, if
530+
supported by the protocol. Note that IPV6 (:const:`AF_INET6`) does
531+
not support broadcasting.
532+
533+
.. Availability: Linux, Windows >= Vista.
534+
535+
.. data:: SO_USELOOPBACK
536+
537+
.. Availability: FreeBSD, Windows >= Vista `(non functional) <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740532(v=vs.85).aspx>`_
538+
539+
.. data:: SO_LINGER
540+
541+
When a socket is set to linger, a call to :meth:`~socket.close` or
542+
:meth:`~socket.shutdown` will not return until all queued messages
543+
for the socket have been successfully sent or the linger timeout
544+
is reached. If the socket is closed due to the program exiting, it
545+
will linger in the background.
546+
547+
The value for this option is a struct on most operating systems.
548+
Consult your OS documentation for the struct's details.
549+
550+
.. Availability: Linux, Windows
551+
552+
.. data:: SO_OOBINLINE
553+
554+
Allows receiving of out of band data in the normal stream.
555+
556+
.. seealso::
557+
558+
`Out-of-band data <https://www.ibm.com/support/knowledgecenter/en/ssw_i5_54/rzab6/coobd.htm>`_
559+
for an in depth explanation.
560+
561+
.. Availability: Linux, Windows
562+
563+
.. data:: SO_REUSEPORT
564+
565+
.. Availability: Linux >= 3.9 (for TCP/UDP)
566+
567+
.. data:: SO_SNDBUF
568+
SO_RCVBUF
569+
570+
Sets the size of the sending and receiving buffers for this socket in
571+
bytes. Most operating systems impose an upper limit on the size of
572+
these buffers.
573+
574+
.. Availability: Linux, Windows
575+
576+
.. data::
577+
SO_RCVLOWAT
578+
SO_SNDLOWAT
579+
580+
``SO_RCVLOWAT`` sets the minimum number of bytes that must be present in
581+
the socket's internal receive buffer before they are returned by a
582+
read call. ``SO_SNDLOWAT`` similiary sets the minimum bytes before data is
583+
sent from the send buffer to the socket protocol.
584+
585+
SO_SNDLOWAT is read-only on Linux and SO_RCVLOWAT is read-only on
586+
Linux versions below 2.4.
587+
588+
.. Availability: Linux, Windows `(non functional) <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740532(v=vs.85).aspx>`_
589+
590+
.. data:: SO_RCVTIMEO
591+
SO_SNDTIMEO
592+
593+
Specifies the amount of time send and receive calls for this socket will
594+
block before timing out. The default timeout of zero means that operations
595+
will never time out.
596+
597+
This is independent of :meth:`~socket.settimeout`.
598+
599+
On Linux this is a `struct timeval`, on Windows this is an integer.
600+
601+
.. Availability: Linux, Windows
602+
603+
.. data:: SO_ERROR
604+
605+
Gets and resets the pending socket error.
606+
607+
.. Availability: Linux, Windows
608+
609+
.. data:: SO_TYPE
610+
611+
Gets the type of the socket. This type corresponds to the type as
612+
defined in the :func:`socket.socket` function. Examples include
613+
:const:`SOCK_STREAM` and :const:`SOCK_DGRAM`.
614+
615+
.. Availability: Linux, Windows
616+
617+
.. data:: SO_SETFIB
618+
619+
Sets the FIB (routing table) for the socket.
620+
621+
Availability: FreeBSD >= 7.1
622+
623+
.. versionadded:: 3.1
624+
625+
.. data:: SO_PASSCRED
626+
SO_PEERCRED
627+
628+
Allows for the passing of SCM credentials over unix sockets. These
629+
are passed as ancillary messages which can be received using :func:`~socket.recvmsg`
630+
631+
.. versionadded:: 3.3
632+
633+
.. Availability: Linux
634+
635+
.. data:: SO_PASSSEC
636+
SO_PEERSEC
637+
638+
Allows for the passing of security contexts over unix sockets.
639+
640+
.. versionadded:: 3.6
641+
642+
.. Availability: Linux >= 2.6.2
643+
644+
.. data:: SO_BINDTODEVICE
645+
646+
Binds a socket to a particular network interface device like "eth0",
647+
When bound, only packets received from that particular device are
648+
processsed by the socket.
649+
650+
.. versionadded:: 3.3
651+
652+
.. Availability: Linux
653+
654+
.. data:: SO_PRIORITY
655+
656+
Sets the protocol-defined priority for each packet sent on this socket.
657+
Packets with higher priority may be processed first depending on the
658+
queueing mechanism of the network interface.
659+
660+
.. versionadded:: 3.4
661+
662+
.. Availability: Linux
663+
664+
.. data:: SO_MARK
665+
666+
Sets the mark on each packet sent through this socket. This may be used
667+
for routing or packet filtering.
668+
669+
.. versionadded:: 3.5
670+
671+
.. Availability: Linux >= 2.6.25
672+
673+
.. data:: SO_DOMAIN
674+
SO_PROTOCOL
675+
676+
Passing ``SO_DOMAIN`` to :meth:`~socket.getsockopt` allows for the retrival
677+
of the ``family`` value as defined in the :func:`socket.socket` function.
678+
``SO_PROTOCOL`` returns the ``proto`` value. The protocol value can be the
679+
exact protocol used such as ``IPPROTO_TCP`` even if 0 was passed in to specify
680+
the default protocol.
681+
682+
Both these options are read only.
683+
684+
The value returned for the ``family`` is an integer which is the value of
685+
the constants above like :const:`AF_INET`. In order to get the const name
686+
value back you can use the AddressFamily enum. ::
687+
688+
>>> family = s.getsockopt(socket.SOL_SOCKET, socket.SO_DOMAIN)
689+
>>> socket.AddressFamily(family)
690+
<AddressFamily.AF_INET: 2>
691+
692+
Availability: Linux >= 2.6.32.
693+
694+
.. versionadded:: 3.6
695+
414696
.. data:: SO_*
415697
SOMAXCONN
416698
MSG_*

0 commit comments

Comments
 (0)