diff --git a/scapy/libs/winpcapy.py b/scapy/libs/winpcapy.py index 42bfa850921..17a0474a7fc 100644 --- a/scapy/libs/winpcapy.py +++ b/scapy/libs/winpcapy.py @@ -12,9 +12,10 @@ from ctypes import * from ctypes.util import find_library import os +import time from scapy.libs.structures import bpf_program -from scapy.consts import WINDOWS, BSD +from scapy.consts import WINDOWS, BSD, DARWIN, LINUX, NETBSD, OPENBSD if WINDOWS: # Try to load Npcap, or Winpcap @@ -54,20 +55,50 @@ FILE = c_void_p STRING = c_char_p +try: + c_time_t # Python 3.12+ +except NameError: + # Older Pythons: ask whether this build's time_t can hold 2**31 + try: + time.gmtime(2 ** 31) + c_time_t = c_int64 + except (OverflowError, OSError, ValueError): + c_time_t = c_int32 + class bpf_version(Structure): _fields_ = [("bv_major", c_ushort), ("bv_minor", c_ushort)] -class timeval(Structure): - _fields_ = [('tv_sec', c_long), - ('tv_usec', c_long)] - +# timeval has a different structure depending on the OS +# - Windows uses two longs +# - OpenBSD its own struct bpf_timeval of two u_int32_t +# - Elsewhere its seconds are a time_t +if WINDOWS: + # https://learn.microsoft.com/fr-fr/windows/win32/api/winsock/ns-winsock-timeval + class timeval(Structure): + _fields_ = [('tv_sec', c_long), + ('tv_usec', c_long)] +elif OPENBSD: + # https://github.com/openbsd/src/blob/a5d3ee8e660b7269f17fcd507a49e1e10ce52d58/sys/net/bpf.h#L143 + class timeval(Structure): + _fields_ = [('tv_sec', c_uint32), + ('tv_usec', c_uint32)] +elif NETBSD or DARWIN: + class timeval(Structure): + _fields_ = [('tv_sec', c_time_t), + ('tv_usec', c_int)] +elif LINUX: + # As wide as tv_sec in glibc and musl, including glibc's 64-bit time + class timeval(Structure): + _fields_ = [('tv_sec', c_time_t), + ('tv_usec', c_time_t)] +else: + class timeval(Structure): + _fields_ = [('tv_sec', c_time_t), + ('tv_usec', c_long)] -# sockaddr is used by pcap_addr. -# For example if sa_family==socket.AF_INET then we need cast -# with sockaddr_in # sockaddr has a different structure depending on the OS if BSD: