Skip to content

Commit 1cbea51

Browse files
committed
Make netfilterqueue a package and add type hints
1 parent a935aad commit 1cbea51

File tree

12 files changed

+127
-49
lines changed

12 files changed

+127
-49
lines changed

CHANGES.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
v1.0.0, unreleased
22
Propagate exceptions raised by the user's packet callback
3-
Warn about exceptions raised by the packet callback during queue unbinding
3+
Avoid calls to the packet callback during queue unbinding
44
Raise an error if a packet verdict is set after its parent queue is closed
55
set_payload() now affects the result of later get_payload()
66
Handle signals received when run() is blocked in recv()
77
Accept packets in COPY_META mode, only failing on an attempt to access the payload
88
Add a parameter NetfilterQueue(sockfd=N) that uses an already-opened Netlink socket
9+
Add type hints
10+
Remove the Packet.payload attribute; it was never safe (treated as a char* but not NUL-terminated) nor documented, but was exposed in the API (perhaps inadvertently).
911

1012
v0.9.0, 12 Jan 2021
1113
Improve usability when Packet objects are retained past the callback

MANIFEST.in

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
include *.txt
2-
include *.rst
3-
include *.c
4-
include *.pyx
5-
include *.pxd
6-
recursive-include tests/ *.py
1+
include LICENSE.txt README.rst CHANGES.txt
2+
recursive-include netfilterqueue *.py *.pyx *.pxd *.c *.pyi py.typed
3+
recursive-include tests *.py

ci.sh

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,42 @@ python setup.py sdist --formats=zip
1111

1212
# ... but not to install it
1313
pip uninstall -y cython
14+
python setup.py build_ext
1415
pip install dist/*.zip
1516

1617
pip install -Ur test-requirements.txt
1718

1819
if [ "$CHECK_LINT" = "1" ]; then
1920
error=0
20-
if ! black --check setup.py tests; then
21+
black_files="setup.py tests netfilterqueue"
22+
if ! black --check $black_files; then
23+
error=$?
24+
black --diff $black_files
25+
fi
26+
mypy --strict -p netfilterqueue || error=$?
27+
( mkdir empty; cd empty; python -m mypy.stubtest netfilterqueue ) || error=$?
28+
29+
if [ $error -ne 0 ]; then
2130
cat <<EOF
2231
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2332
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2433
25-
Formatting problems were found (listed above). To fix them, run
34+
Problems were found by static analysis (listed above).
35+
To fix formatting and see remaining errors, run:
2636
2737
pip install -r test-requirements.txt
28-
black setup.py tests
38+
black $black_files
39+
mypy --strict -p netfilterqueue
40+
( mkdir empty; cd empty; python -m mypy.stubtest netfilterqueue )
2941
3042
in your local checkout.
3143
32-
EOF
33-
error=1
34-
fi
35-
if [ "$error" = "1" ]; then
36-
cat <<EOF
3744
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3845
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3946
EOF
47+
exit 1
4048
fi
41-
exit $error
49+
exit 0
4250
fi
4351

4452
cd tests

netfilterqueue.pxd renamed to netfilterqueue/__init__.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ cdef class Packet:
201201

202202
# Packet details:
203203
cdef Py_ssize_t payload_len
204-
cdef readonly unsigned char *payload
204+
cdef unsigned char *payload
205205
cdef timeval timestamp
206206
cdef u_int8_t hw_addr[8]
207207

netfilterqueue/__init__.pyi

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import socket
2+
from enum import IntEnum
3+
from typing import Callable, Dict, Optional, Tuple
4+
5+
__version__: str
6+
VERSION: Tuple[int, ...]
7+
8+
COPY_NONE: int
9+
COPY_META: int
10+
COPY_PACKET: int
11+
12+
class Packet:
13+
hook: int
14+
hw_protocol: int
15+
id: int
16+
mark: int
17+
def get_hw(self) -> Optional[bytes]: ...
18+
def get_payload(self) -> bytes: ...
19+
def get_payload_len(self) -> int: ...
20+
def get_timestamp(self) -> float: ...
21+
def get_mark(self) -> int: ...
22+
def set_payload(self, payload: bytes) -> None: ...
23+
def set_mark(self, mark: int) -> None: ...
24+
def retain(self) -> None: ...
25+
def accept(self) -> None: ...
26+
def drop(self) -> None: ...
27+
def repeat(self) -> None: ...
28+
29+
class NetfilterQueue:
30+
def __new__(self, *, af: int = ..., sockfd: int = ...) -> NetfilterQueue: ...
31+
def bind(
32+
self,
33+
queue_num: int,
34+
user_callback: Callable[[Packet], None],
35+
max_len: int = ...,
36+
mode: int = COPY_PACKET,
37+
range: int = ...,
38+
sock_len: int = ...,
39+
) -> None: ...
40+
def unbind(self) -> None: ...
41+
def get_fd(self) -> int: ...
42+
def run(self, block: bool = ...) -> None: ...
43+
def run_socket(self, s: socket.socket) -> None: ...
44+
45+
PROTOCOLS: Dict[int, str]

netfilterqueue.pyx renamed to netfilterqueue/__init__.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ function.
55
Copyright: (c) 2011, Kerkhoff Technologies Inc.
66
License: MIT; see LICENSE.txt
77
"""
8-
VERSION = (0, 9, 0)
98

109
# Constants for module users
1110
COPY_NONE = 0
@@ -24,6 +23,10 @@ DEF SockCopySize = MaxCopySize + SockOverhead
2423
# Socket queue should hold max number of packets of copysize bytes
2524
DEF SockRcvSize = DEFAULT_MAX_QUEUELEN * SockCopySize // 2
2625

26+
__package__ = "netfilterqueue"
27+
28+
from ._version import __version__, VERSION
29+
2730
from cpython.exc cimport PyErr_CheckSignals
2831

2932
# A negative return value from this callback will stop processing and

netfilterqueue/_version.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This file is imported from __init__.py and exec'd from setup.py
2+
3+
__version__ = "0.9.0+dev"
4+
VERSION = (0, 9, 0)

netfilterqueue/py.typed

Whitespace-only changes.

setup.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os, sys
22
from setuptools import setup, Extension
33

4-
VERSION = "0.9.0" # Remember to change CHANGES.txt and netfilterqueue.pyx when version changes.
4+
exec(open("netfilterqueue/_version.py", encoding="utf-8").read())
55

66
setup_requires = []
77
try:
@@ -10,7 +10,9 @@
1010

1111
ext_modules = cythonize(
1212
Extension(
13-
"netfilterqueue", ["netfilterqueue.pyx"], libraries=["netfilter_queue"]
13+
"netfilterqueue.__init__",
14+
["netfilterqueue/__init__.pyx"],
15+
libraries=["netfilter_queue"],
1416
),
1517
compiler_directives={"language_level": "3str"},
1618
)
@@ -21,7 +23,7 @@
2123
# setup_requires below.
2224
setup_requires = ["cython"]
2325
elif not os.path.exists(
24-
os.path.join(os.path.dirname(__file__), "netfilterqueue.c")
26+
os.path.join(os.path.dirname(__file__), "netfilterqueue/__init__.c")
2527
):
2628
sys.stderr.write(
2729
"You must have Cython installed (`pip install cython`) to build this "
@@ -31,21 +33,27 @@
3133
)
3234
sys.exit(1)
3335
ext_modules = [
34-
Extension("netfilterqueue", ["netfilterqueue.c"], libraries=["netfilter_queue"])
36+
Extension(
37+
"netfilterqueue.__init__",
38+
["netfilterqueue/__init__.c"],
39+
libraries=["netfilter_queue"],
40+
)
3541
]
3642

3743
setup(
38-
ext_modules=ext_modules,
39-
setup_requires=setup_requires,
40-
python_requires=">=3.6",
4144
name="NetfilterQueue",
42-
version=VERSION,
45+
version=__version__,
4346
license="MIT",
4447
author="Matthew Fox",
4548
author_email="matt@tansen.ca",
4649
url="https://github.com/oremanj/python-netfilterqueue",
4750
description="Python bindings for libnetfilter_queue",
48-
long_description=open("README.rst").read(),
51+
long_description=open("README.rst", encoding="utf-8").read(),
52+
packages=["netfilterqueue"],
53+
ext_modules=ext_modules,
54+
include_package_data=True,
55+
setup_requires=setup_requires,
56+
python_requires=">=3.6",
4957
classifiers=[
5058
"Development Status :: 5 - Production/Stable",
5159
"License :: OSI Approved :: MIT License",

test-requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pytest-trio
55
async_generator
66
black
77
platformdirs <= 2.4.0 # needed by black; 2.4.1+ don't support py3.6
8+
mypy; implementation_name == "cpython"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy