Skip to content

Commit 52fef8f

Browse files
committed
bpo-25041: Document AF_PACKET socket address format
1 parent ae3087c commit 52fef8f

2 files changed

Lines changed: 42 additions & 17 deletions

File tree

Doc/library/socket.rst

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,25 @@ created. Socket addresses are represented as follows:
161161

162162
.. versionadded:: 3.7
163163

164-
- Certain other address families (:const:`AF_PACKET`, :const:`AF_CAN`)
165-
support specific representations.
166-
167-
.. XXX document them!
164+
- The :const:`AF_PACKET` address family is a Linux-only packet address protocol
165+
represented by the tuple ``(ifname, proto[, pkttype[, hatype[, addr]]])``
166+
where:
167+
168+
- *ifname* - String specifying the device name.
169+
- *proto* - An endian-corrected integer specifying the Ethernet protocol
170+
number.
171+
- *pkttype* - Optional constant integer specifying the packet type:
172+
173+
- ``PACKET_HOST`` (the default) - Packet addressed to the local host.
174+
- ``PACKET_BROADCAST`` - Physical-layer broadcast packet.
175+
- ``PACKET_MULTIHOST`` - Packet sent to a physical-layer multicast address.
176+
- ``PACKET_OTHERHOST`` - Packet to some other host that has been caught by
177+
a device driver in promiscuous mode.
178+
- ``PACKET_OUTGOING`` - Packet originating from the local host that is
179+
looped back to a packet socket.
180+
- *hatype* - Optional integer specifying the ARP hardware address type.
181+
- *addr* - Optional bytes-like object specifying the hardware physical
182+
address, whose interpretation depends on the device.
168183

169184
For IPv4 addresses, two special forms are accepted instead of a host address:
170185
the empty string represents :const:`INADDR_ANY`, and the string
@@ -363,6 +378,16 @@ Constants
363378
.. versionadded:: 3.7
364379

365380

381+
.. data:: AF_PACKET
382+
PF_PACKET
383+
PACKET_*
384+
385+
Many constants of these forms, documented in the Linux documentation, are
386+
also defined in the socket module.
387+
388+
Availability: Linux >= 2.2.
389+
390+
366391
.. data:: AF_RDS
367392
PF_RDS
368393
SOL_RDS
@@ -456,15 +481,16 @@ The following functions all create :ref:`socket objects <socket-objects>`.
456481

457482
Create a new socket using the given address family, socket type and protocol
458483
number. The address family should be :const:`AF_INET` (the default),
459-
:const:`AF_INET6`, :const:`AF_UNIX`, :const:`AF_CAN` or :const:`AF_RDS`. The
460-
socket type should be :const:`SOCK_STREAM` (the default),
461-
:const:`SOCK_DGRAM`, :const:`SOCK_RAW` or perhaps one of the other ``SOCK_``
462-
constants. The protocol number is usually zero and may be omitted or in the
463-
case where the address family is :const:`AF_CAN` the protocol should be one
464-
of :const:`CAN_RAW`, :const:`CAN_BCM` or :const:`CAN_ISOTP`. If *fileno* is specified, the other
465-
arguments are ignored, causing the socket with the specified file descriptor
466-
to return. Unlike :func:`socket.fromfd`, *fileno* will return the same
467-
socket and not a duplicate. This may help close a detached socket using
484+
:const:`AF_INET6`, :const:`AF_UNIX`, :const:`AF_CAN`, :const:`AF_PACKET`,
485+
or :const:`AF_RDS`. The socket type should be :const:`SOCK_STREAM` (the
486+
default), :const:`SOCK_DGRAM`, :const:`SOCK_RAW` or perhaps one of the other
487+
``SOCK_`` constants. The protocol number is usually zero and may be omitted
488+
or in the case where the address family is :const:`AF_CAN` the protocol
489+
should be one of :const:`CAN_RAW`, :const:`CAN_BCM` or :const:`CAN_ISOTP`.
490+
If *fileno* is specified, the other arguments are ignored, causing the
491+
socket with the specified file descriptor to return. Unlike
492+
:func:`socket.fromfd`, *fileno* will return the same socket and not a
493+
duplicate. This may help close a detached socket using
468494
:meth:`socket.close()`.
469495

470496
The newly created socket is :ref:`non-inheritable <fd_inheritance>`.

Modules/socketmodule.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
18031803
char *interfaceName;
18041804
int protoNumber;
18051805
int hatype = 0;
1806-
int pkttype = 0;
1806+
int pkttype = PACKET_HOST;
18071807
Py_buffer haddr = {NULL, NULL};
18081808

18091809
if (!PyTuple_Check(args)) {
@@ -1834,7 +1834,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
18341834
if (protoNumber < 0 || protoNumber > 0xffff) {
18351835
PyErr_SetString(
18361836
PyExc_OverflowError,
1837-
"getsockaddrarg: protoNumber must be 0-65535.");
1837+
"getsockaddrarg: proto must be 0-65535.");
18381838
PyBuffer_Release(&haddr);
18391839
return 0;
18401840
}
@@ -2787,7 +2787,6 @@ sock_bind(PySocketSockObject *s, PyObject *addro)
27872787

27882788
if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
27892789
return NULL;
2790-
Py_BEGIN_ALLOW_THREADS
27912790
res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
27922791
Py_END_ALLOW_THREADS
27932792
if (res < 0)
@@ -2800,7 +2799,7 @@ PyDoc_STRVAR(bind_doc,
28002799
\n\
28012800
Bind the socket to a local address. For IP sockets, the address is a\n\
28022801
pair (host, port); the host must refer to the local host. For raw packet\n\
2803-
sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
2802+
sockets the address is a tuple (ifname, proto [,pkttype [,hatype [,addr]]])");
28042803

28052804

28062805
/* s.close() method.

0 commit comments

Comments
 (0)