E5F5 bpo-25041: Document AF_PACKET socket address format by csabella · Pull Request #4092 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions Doc/library/socket.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,25 @@ created. Socket addresses are represented as follows:

.. versionadded:: 3.7

- Certain other address families (:const:`AF_PACKET`, :const:`AF_CAN`)
support specific representations.

.. XXX document them!
- :const:`AF_PACKET` is a low-level interface directly to network devices.
The packets are represented by the tuple
``(ifname, proto[, pkttype[, hatype[, addr]]])`` where:

- *ifname* - String specifying the device name.
- *proto* - An in network-byte-order integer specifying the Ethernet
protocol number.
- *pkttype* - Optional integer specifying the packet type:

- ``PACKET_HOST`` (the default) - Packet addressed to the local host.
- ``PACKET_BROADCAST`` - Physical-layer broadcast packet.
- ``PACKET_MULTIHOST`` - Packet sent to a physical-layer multicast address.
- ``PACKET_OTHERHOST`` - Packet to some other host that has been caught by
a device driver in promiscuous mode.
- ``PACKET_OUTGOING`` - Packet originating from the local host that is
looped back to a packet socket.
- *hatype* - Optional integer specifying the ARP hardware address type.
- *addr* - Optional bytes-like object specifying the hardware physical
address, whose interpretation depends on the device.

If you use a hostname in the *host* portion of IPv4/v6 socket address, the
program may show a nondeterministic behavior, as Python uses the first address
Expand Down Expand Up @@ -376,6 +391,16 @@ Constants
.. versionadded:: 3.7


.. data:: AF_PACKET
PF_PACKET
PACKET_*

Many constants of these forms, documented in the Linux documentation, are
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Linux documentation" -> "man pages"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Linux documentation" is used in the other definitions within this section, so I had used it for consistency. Should I change them all?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, I didn't realize that term was already in use here. I suppose this is fine to leave as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at git blame, it seems that one person started it and then all the others continued it, presumably for consistency. If man pages is better, then maybe it would be best to change it now instead of propagating Linux documentation every time?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make sense to change that, but that should probably be another change.

also defined in the socket module.

Availability: Linux >= 2.2.


.. data:: AF_RDS
PF_RDS
SOL_RDS
Expand Down Expand Up @@ -469,12 +494,12 @@ The following functions all create :ref:`socket objects <socket-objects>`.

Create a new socket using the given address family, socket type and protocol
number. The address family should be :const:`AF_INET` (the default),
:const:`AF_INET6`, :const:`AF_UNIX`, :const:`AF_CAN` or :const:`AF_RDS`. The
socket type should be :const:`SOCK_STREAM` (the default),
:const:`SOCK_DGRAM`, :const:`SOCK_RAW` or perhaps one of the other ``SOCK_``
constants. The protocol number is usually zero and may be omitted or in the
case where the address family is :const:`AF_CAN` the protocol should be one
of :const:`CAN_RAW`, :const:`CAN_BCM` or :const:`CAN_ISOTP`
:const:`AF_INET6`, :const:`AF_UNIX`, :const:`AF_CAN`, :const:`AF_PACKET`,
or :const:`AF_RDS`. The socket type should be :const:`SOCK_STREAM` (the
default), :const:`SOCK_DGRAM`, :const:`SOCK_RAW` or perhaps one of the other
``SOCK_`` constants. The protocol number is usually zero and may be omitted
or in the case where the address family is :const:`AF_CAN` the protocol
should be one of :const:`CAN_RAW`, :const:`CAN_BCM` or :const:`CAN_ISOTP`.

If *fileno* is specified, the values for *family*, *type*, and *proto* are
auto-detected from the specified file descriptor. Auto-detection can be
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Document ``AF_PACKET`` in the :mod:`socket` module.
6 changes: 3 additions & 3 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1901,7 +1901,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
const char *interfaceName;
int protoNumber;
int hatype = 0;
int pkttype = 0;
int pkttype = PACKET_HOST;
Py_buffer haddr = {NULL, NULL};

if (!PyTuple_Check(args)) {
Expand Down Expand Up @@ -1943,7 +1943,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
if (protoNumber < 0 || protoNumber > 0xffff) {
PyErr_Format(
PyExc_OverflowError,
"%s(): protoNumber must be 0-65535.", caller);
"%s(): proto must be 0-65535.", caller);
PyBuffer_Release(&haddr);
return 0;
}
Expand Down Expand Up @@ -2979,7 +2979,7 @@ PyDoc_STRVAR(bind_doc,
\n\
Bind the socket to a local address. For IP sockets, the address is a\n\
pair (host, port); the host must refer to the local host. For raw packet\n\
sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
sockets the address is a tuple (ifname, proto [,pkttype [,hatype [,addr]]])");


/* s.close() method.
Expand Down
0