Skip to content

Commit ccb3530

Browse files
committed
Add run_socket, which uses socket.recv so that gevent can monkeypatch it. Fixes oremanj#5
1 parent 2960dc3 commit ccb3530

File tree

4 files changed

+970
-245
lines changed

4 files changed

+970
-245
lines changed

README.rst

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ it. ::
2929
3030
nfqueue.unbind()
3131

32+
You can also make your own socket so that it can be used with gevent, for example. ::
33+
34+
from netfilterqueue import NetfilterQueue
35+
import socket
36+
37+
def print_and_accept(pkt):
38+
print(pkt)
39+
pkt.accept()
40+
41+
nfqueue = NetfilterQueue()
42+
nfqueue.bind(1, print_and_accept)
43+
s = socket.fromfd(nfqueue.get_fd(), socket.AF_UNIX, socket.SOCK_STREAM)
44+
try:
45+
nfqueue.run_socket(s)
46+
except KeyboardInterrupt:
47+
print('')
48+
49+
s.close()
50+
nfqueue.unbind()
51+
3252
To send packets destined for your LAN to the script, type something like::
3353

3454
iptables -I INPUT -d 192.168.0.0/24 -j NFQUEUE --queue-num 1
@@ -107,6 +127,12 @@ a call to ``bind``, then start receiving packets with a call to ``run``.
107127
block=False to let your thread continue. You can get the file descriptor
108128
of the socket with the ``get_fd`` method.
109129

130+
``QueueHandler.run_socket(socket)``
131+
Send packets to your callback, but use the supplied socket instead of
132+
recv, so that, for example, gevent can monkeypatch it. You can make a
133+
socket with ``socket.fromfd(nfqueue.get_fd(), socket.AF_UNIX, socket.SOCK_STREAM)``
134+
and optionally make it non-blocking with ``socket.setblocking(False)``.
135+
110136
Packet objects
111137
--------------
112138

@@ -191,8 +217,6 @@ The fields are:
191217
Limitations
192218
===========
193219

194-
More details coming soon...
195-
196220
* Compiled with a 4096-byte buffer for packets, so it probably won't work on
197221
loopback or Ethernet with jumbo packets. If this is a problem, either lower
198222
MTU on your loopback, disable jumbo packets, or get Cython,

0 commit comments

Comments
 (0)