Skip to content

Commit 80c08d1

Browse files
noamcohen97erlend-aaslandCAM-Gerlach
authoredNov 7, 2022
pythongh-95389: expose popular ETHERTYPE_* constants in the socket module (python#95390)
Co-authored-by: Erlend E. Aasland <[email protected]> Co-authored-by: CAM Gerlach <[email protected]>
1 parent 6168e71 commit 80c08d1

File tree

6 files changed

+65
-4
lines changed

6 files changed

+65
-4
lines changed
 

‎Doc/library/socket.rst

+34-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,11 @@ created. Socket addresses are represented as follows:
189189
``(ifname, proto[, pkttype[, hatype[, addr]]])`` where:
190190

191191
- *ifname* - String specifying the device name.
192-
- *proto* - An in network-byte-order integer specifying the Ethernet
193-
protocol number.
192+
- *proto* - The Ethernet protocol number.
193+
May be :data:`ETH_P_ALL` to capture all protocols,
194+
one of the :ref:`ETHERTYPE_* constants <socket-ethernet-types>`
195+
or any other Ethernet protocol number.
196+
Value must be in network-byte-order.
194197
- *pkttype* - Optional integer specifying the packet type:
195198

196199
- ``PACKET_HOST`` (the default) - Packet addressed to the local host.
@@ -508,6 +511,19 @@ Constants
508511
.. availability:: Linux >= 2.2.
509512

510513

514+
.. data:: ETH_P_ALL
515+
516+
:data:`!ETH_P_ALL` can be used in the :class:`~socket.socket`
517+
constructor as *proto* for the :const:`AF_PACKET` family in order to
518+
capture every packet, regardless of protocol.
519+
520+
For more information, see the :manpage:`packet(7)` manpage.
521+
522+
.. availability:: Linux.
523+
524+
.. versionadded:: 3.12
525+
526+
511527
.. data:: AF_RDS
512528
PF_RDS
513529
SOL_RDS
@@ -638,6 +654,22 @@ Constants
638654

639655
.. versionadded:: 3.12
640656

657+
.. _socket-ethernet-types:
658+
659+
.. data:: ETHERTYPE_ARP
660+
ETHERTYPE_IP
661+
ETHERTYPE_IPV6
662+
ETHERTYPE_VLAN
663+
664+
`IEEE 802.3 protocol number
665+
<https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.txt>`_.
666+
constants.
667+
668+
.. availability:: Linux, FreeBSD, macOS.
669+
670+
.. versionadded:: 3.12
671+
672+
641673
Functions
642674
^^^^^^^^^
643675

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Expose :data:`~socket.ETH_P_ALL` and some of the
2+
:ref:`ETHERTYPE_* constants <socket-ethernet-types>` in :mod:`socket`.
3+
Patch by Noam Cohen.

‎Modules/socketmodule.c

+23
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ shutdown(how) -- shut down traffic in one or both directions\n\
247247
#include <net/if.h>
248248
#endif
249249

250+
#ifdef HAVE_NET_ETHERNET_H
251+
#include <net/ethernet.h>
252+
#endif
253+
250254
/* Generic socket object definitions and includes */
251255
#define PySocket_BUILDING_SOCKET
252256
#include "socketmodule.h"
@@ -7711,6 +7715,25 @@ PyInit__socket(void)
77117715
PyModule_AddIntMacro(m, ALG_OP_VERIFY);
77127716
#endif
77137717

7718+
/* IEEE 802.3 protocol numbers required for a standard TCP/IP network stack */
7719+
#ifdef ETHERTYPE_ARP
7720+
PyModule_AddIntMacro(m, ETHERTYPE_ARP);
7721+
#endif
7722+
#ifdef ETHERTYPE_IP
7723+
PyModule_AddIntMacro(m, ETHERTYPE_IP);
7724+
#endif
7725+
#ifdef ETHERTYPE_IPV6
7726+
PyModule_AddIntMacro(m, ETHERTYPE_IPV6);
7727+
#endif
7728+
#ifdef ETHERTYPE_VLAN
7729+
PyModule_AddIntMacro(m, ETHERTYPE_VLAN);
7730+
#endif
7731+
7732+
/* Linux pseudo-protocol for sniffing every packet */
7733+
#ifdef ETH_P_ALL
7734+
PyModule_AddIntMacro(m, ETH_P_ALL);
7735+
#endif
7736+
77147737
/* Socket types */
77157738
PyModule_AddIntMacro(m, SOCK_STREAM);
77167739
PyModule_AddIntMacro(m, SOCK_DGRAM);

‎configure

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎configure.ac

+1-1
Original file line numberDiff line numberDiff line change
@@ -2708,7 +2708,7 @@ AC_CHECK_HEADERS([ \
27082708
alloca.h asm/types.h bluetooth.h conio.h crypt.h direct.h dlfcn.h endian.h errno.h fcntl.h grp.h \
27092709
ieeefp.h io.h langinfo.h libintl.h libutil.h linux/auxvec.h sys/auxv.h linux/fs.h linux/memfd.h \
27102710
linux/random.h linux/soundcard.h \
2711-
linux/tipc.h linux/wait.h netdb.h netinet/in.h netpacket/packet.h poll.h process.h pthread.h pty.h \
2711+
linux/tipc.h linux/wait.h netdb.h net/ethernet.h netinet/in.h netpacket/packet.h poll.h process.h pthread.h pty.h \
27122712
sched.h setjmp.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
27132713
sys/endian.h sys/epoll.h sys/event.h sys/eventfd.h sys/file.h sys/ioctl.h sys/kern_control.h \
27142714
sys/loadavg.h sys/lock.h sys/memfd.h sys/mkdev.h sys/mman.h sys/modem.h sys/param.h sys/poll.h \

‎pyconfig.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,9 @@
844844
/* Define to 1 if you have the <netpacket/packet.h> header file. */
845845
#undef HAVE_NETPACKET_PACKET_H
846846

847+
/* Define to 1 if you have the <net/ethernet.h> header file. */
848+
#undef HAVE_NET_ETHERNET_H
849+
847850
/* Define to 1 if you have the <net/if.h> header file. */
848851
#undef HAVE_NET_IF_H
849852

0 commit comments

Comments
 (0)
Please sign in to comment.