Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-95389: socketmodule: expose popular ETHERTYPE_* constants #95390

Merged
merged 19 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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
32 changes: 30 additions & 2 deletions Doc/library/socket.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ created. Socket addresses are represented as follows:
``(ifname, proto[, pkttype[, hatype[, addr]]])`` where:

- *ifname* - String specifying the device name.
- *proto* - An in network-byte-order integer specifying the Ethernet
protocol number.
- *proto* - The Ethernet protocol number. May be one of
:ref:`ETHERTYPE_* constants <socket-ethernet-types>`
noamcohen97 marked this conversation as resolved.
Show resolved Hide resolved
or any other Ethernet protocol number.
In both cases, values must be in network-byte-order.
noamcohen97 marked this conversation as resolved.
Show resolved Hide resolved
- *pkttype* - Optional integer specifying the packet type:

- ``PACKET_HOST`` (the default) - Packet addressed to the local host.
Expand Down Expand Up @@ -501,6 +503,20 @@ Constants
.. availability:: Linux >= 2.2.


.. data:: ETH_P_ALL

``ETH_P_ALL`` can be used in :class:`~socket.socket`
constructor as *proto* for the :const:`AF_PACKET` family in order to
capture every packet.
noamcohen97 marked this conversation as resolved.
Show resolved Hide resolved

For more information you can consult the Linux documentation
:manpage:`packet(7)`.
noamcohen97 marked this conversation as resolved.
Show resolved Hide resolved

.. availability:: Linux.

.. versionadded:: 3.12
noamcohen97 marked this conversation as resolved.
Show resolved Hide resolved


.. data:: AF_RDS
PF_RDS
SOL_RDS
Expand Down Expand Up @@ -631,6 +647,18 @@ Constants

.. versionadded:: 3.12

.. _socket-ethernet-types:

.. data:: ETHERTYPE_ARP
ETHERTYPE_IP
ETHERTYPE_IPV6
ETHERTYPE_VLAN

IEEE 802.3 protocol numbers.
noamcohen97 marked this conversation as resolved.
Show resolved Hide resolved

noamcohen97 marked this conversation as resolved.
Show resolved Hide resolved
noamcohen97 marked this conversation as resolved.
Show resolved Hide resolved
.. versionadded:: 3.12


Functions
^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Expose some of ``ETHERTYPE_*`` and ``ETH_P_ALL`` constants in :mod:`socket`. Patch by Noam Cohen.
noamcohen97 marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ shutdown(how) -- shut down traffic in one or both directions\n\
#include <net/if.h>
#endif

#ifdef HAVE_NET_ETHERNET_H
#include <net/ethernet.h>
#endif

/* Generic socket object definitions and includes */
#define PySocket_BUILDING_SOCKET
#include "socketmodule.h"
Expand Down Expand Up @@ -7711,6 +7715,25 @@ PyInit__socket(void)
PyModule_AddIntMacro(m, ALG_OP_VERIFY);
#endif

/* IEEE 802.3 protocol numbers required for a standard TCP/IP network stack */
#ifdef ETHERTYPE_ARP
PyModule_AddIntMacro(m, ETHERTYPE_ARP);
#endif
#ifdef ETHERTYPE_IP
PyModule_AddIntMacro(m, ETHERTYPE_IP);
#endif
#ifdef ETHERTYPE_IPV6
PyModule_AddIntMacro(m, ETHERTYPE_IPV6);
#endif
#ifdef ETHERTYPE_VLAN
PyModule_AddIntMacro(m, ETHERTYPE_VLAN);
#endif

/* Linux pseudo-protocol for sniffing every packet */
#ifdef ETH_P_ALL
PyModule_AddIntMacro(m, ETH_P_ALL);
#endif

/* Socket types */
PyModule_AddIntMacro(m, SOCK_STREAM);
PyModule_AddIntMacro(m, SOCK_DGRAM);
Expand Down
2 changes: 1 addition & 1 deletion configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2643,7 +2643,7 @@ AC_CHECK_HEADERS([ \
alloca.h asm/types.h bluetooth.h conio.h crypt.h direct.h dlfcn.h endian.h errno.h fcntl.h grp.h \
ieeefp.h io.h langinfo.h libintl.h libutil.h linux/auxvec.h sys/auxv.h linux/fs.h linux/memfd.h \
linux/random.h linux/soundcard.h \
linux/tipc.h linux/wait.h netdb.h netinet/in.h netpacket/packet.h poll.h process.h pthread.h pty.h \
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 \
sched.h setjmp.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
sys/endian.h sys/epoll.h sys/event.h sys/eventfd.h sys/file.h sys/ioctl.h sys/kern_control.h \
sys/loadavg.h sys/lock.h sys/memfd.h sys/mkdev.h sys/mman.h sys/modem.h sys/param.h sys/poll.h \
Expand Down
3 changes: 3 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,9 @@
/* Define to 1 if you have the <netpacket/packet.h> header file. */
#undef HAVE_NETPACKET_PACKET_H

/* Define to 1 if you have the <net/ethernet.h> header file. */
#undef HAVE_NET_ETHERNET_H

/* Define to 1 if you have the <net/if.h> header file. */
#undef HAVE_NET_IF_H

Expand Down