Skip to content

Commit cadb66e

Browse files
benjaminpcsabella
andauthored
[3.7] closes bpo-25041: Document AF_PACKET socket address format. (GH-9207)
(cherry picked from commit 731ff68) Co-authored-by: Cheryl Sabella <[email protected]>
1 parent 01ce535 commit cadb66e

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

‎Doc/library/socket.rst‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,25 @@ created. Socket addresses are represented as follows:
173173

174174
.. versionadded:: 3.7
175175

176-
- Certain other address families (:const:`AF_PACKET`, :const:`AF_CAN`)
177-
support specific representations.
178-
179-
.. XXX document them!
176+
- :const:`AF_PACKET` is a low-level interface directly to network devices.
177+
The packets are represented by the tuple
178+
``(ifname, proto[, pkttype[, hatype[, addr]]])`` where:
179+
180+
- *ifname* - String specifying the device name.
181+
- *proto* - An in network-byte-order integer specifying the Ethernet
182+
protocol number.
183+
- *pkttype* - Optional integer specifying the packet type:
184+
185+
- ``PACKET_HOST`` (the default) - Packet addressed to the local host.
186+
- ``PACKET_BROADCAST`` - Physical-layer broadcast packet.
187+
- ``PACKET_MULTIHOST`` - Packet sent to a physical-layer multicast address.
188+
- ``PACKET_OTHERHOST`` - Packet to some other host that has been caught by
189+
a device driver in promiscuous mode.
190+
- ``PACKET_OUTGOING`` - Packet originating from the local host that is
191+
looped back to a packet socket.
192+
- *hatype* - Optional integer specifying the ARP hardware address type.
193+
- *addr* - Optional bytes-like object specifying the hardware physical
194+
address, whose interpretation depends on the device.
180195

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

378393

394+
.. data:: AF_PACKET
395+
PF_PACKET
396+
PACKET_*
397+
398+
Many constants of these forms, documented in the Linux documentation, are
399+
also defined in the socket module.
400+
401+
Availability: Linux >= 2.2.
402+
403+
379404
.. data:: AF_RDS
380405
PF_RDS
381406
SOL_RDS
@@ -469,12 +494,12 @@ The following functions all create :ref:`socket objects <socket-objects>`.
469494

470495
Create a new socket using the given address family, socket type and protocol
471496
number. The address family should be :const:`AF_INET` (the default),
472-
:const:`AF_INET6`, :const:`AF_UNIX`, :const:`AF_CAN` or :const:`AF_RDS`. The
473-
socket type should be :const:`SOCK_STREAM` (the default),
474-
:const:`SOCK_DGRAM`, :const:`SOCK_RAW` or perhaps one of the other ``SOCK_``
475-
constants. The protocol number is usually zero and may be omitted or in the
476-
case where the address family is :const:`AF_CAN` the protocol should be one
477-
of :const:`CAN_RAW`, :const:`CAN_BCM` or :const:`CAN_ISOTP`
497+
:const:`AF_INET6`, :const:`AF_UNIX`, :const:`AF_CAN`, :const:`AF_PACKET`,
498+
or :const:`AF_RDS`. The socket type should be :const:`SOCK_STREAM` (the
499+
default), :const:`SOCK_DGRAM`, :const:`SOCK_RAW` or perhaps one of the other
500+
``SOCK_`` constants. The protocol number is usually zero and may be omitted
501+
or in the case where the address family is :const:`AF_CAN` the protocol
502+
should be one of :const:`CAN_RAW`, :const:`CAN_BCM` or :const:`CAN_ISOTP`.
478503

479504
If *fileno* is specified, the values for *family*, *type*, and *proto* are
480505
auto-detected from the specified file descriptor. Auto-detection can be
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Document ``AF_PACKET`` in the :mod:`socket` module.

‎Modules/socketmodule.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
18811881
const char *interfaceName;
18821882
int protoNumber;
18831883
int hatype = 0;
1884-
int pkttype = 0;
1884+
int pkttype = PACKET_HOST;
18851885
Py_buffer haddr = {NULL, NULL};
18861886

18871887
if (!PyTuple_Check(args)) {
@@ -1912,7 +1912,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
19121912
if (protoNumber < 0 || protoNumber > 0xffff) {
19131913
PyErr_SetString(
19141914
PyExc_OverflowError,
1915-
"getsockaddrarg: protoNumber must be 0-65535.");
1915+
"getsockaddrarg: proto must be 0-65535.");
19161916
PyBuffer_Release(&haddr);
19171917
return 0;
19181918
}
@@ -2925,7 +2925,7 @@ PyDoc_STRVAR(bind_doc,
29252925
\n\
29262926
Bind the socket to a local address. For IP sockets, the address is a\n\
29272927
pair (host, port); the host must refer to the local host. For raw packet\n\
2928-
sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
2928+
sockets the address is a tuple (ifname, proto [,pkttype [,hatype [,addr]]])");
29292929

29302930

29312931
/* s.close() method.

0 commit comments

Comments
 (0)