Skip to content
Merged
Changes from all 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
47 changes: 39 additions & 8 deletions scapy/libs/winpcapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Loading