@@ -10,18 +10,22 @@ Libnetfilter_queue (the netfilter library, not this module) is part of the `Netf
1010Example
1111=======
1212
13- The following script prints a short description of each packet before accepting it::
13+ The following script prints a short description of each packet before accepting it. ::
1414
15- from netfilterqueue import NetfilterQueue
15+ from netfilterqueue import QueueHandler
1616
17- class PacketPrinter(NetfilterQueue ):
17+ class PacketPrinter(QueueHandler ):
1818 def handle(self, packet):
1919 print packet
2020 packet.accept()
2121
2222 p = PacketPrinter()
2323 p.bind(1)
24- p.run()
24+ try:
25+ p.run()
26+ except KeyboardInterrupt:
27+ print
28+
2529
2630To send packets destined for your LAN to the script, type something like::
2731
@@ -38,7 +42,7 @@ NetfilterQueue is a C extention module that links against libnetfilter_queue. Be
3842
39433. Libnetfilter_queue development files and associated dependencies
4044
41- On Debian or Ubuntu, these files are install with::
45+ On Debian or Ubuntu, install these files with::
4246
4347 sudo apt-get install build-essential python-dev libnetfilter-queue-dev
4448
@@ -54,17 +58,67 @@ From source
5458
5559To install from source::
5660
57- wget http://pypi.python.org/packages/source/N/NetfilterQueue/NetfilterQueue-0.1 .tar.gz
58- tar -xvzf NetfilterQueue-0.1 .tar.gz
59- cd NetfilterQueue-0.1
61+ wget http://pypi.python.org/packages/source/N/NetfilterQueue/NetfilterQueue-0.2 .tar.gz
62+ tar -xvzf NetfilterQueue-0.2 .tar.gz
63+ cd NetfilterQueue-0.2
6064 python setup.py install
6165
6266Setup will use Cython if it is installed, regenerating the .c source from the .pyx before compiling the .so.
6367
6468API
6569===
6670
67- Coming soon...
71+ ``NetfilterQueue.COPY_NONE ``
72+
73+ ``NetfilterQueue.COPY_META ``
74+
75+ ``NetfilterQueue.COPY_PACKET ``
76+ These constants specify how much of the packet should be given to the script- nothing, metadata, or the whole packet.
77+
78+ QueueHandler objects
79+ --------------------
80+
81+ You should define a class that inherits from QueueHandler and implenents the
82+ handle() method. Handle() is called for each packet that appears in the queue.
83+
84+ ``QueueHandler.bind(queue_num[, max_len[, mode[, range]]]) ``
85+ Create and bind to the queue. ``queue_num `` must match the number in your
86+ iptables rule. ``max_len `` sets the largest number of packets that can be
87+ in the queue; new packets are dropped if the size of the queue reaches this
88+ number. ``mode `` determines how much of the packet data is provided to
89+ your script. Use the constants above. ``range `` defines how many bytes of
90+ the packet you want to get. For example, if you only want the source and
91+ destination IPs of a IPv4 packet, ``range `` could be 20.
92+
93+ ``QueueHandler.unbind() ``
94+ Remove the queue. Packets matched by your iptables rule will be dropped.
95+
96+ ``QueueHandler.run() ``
97+ Begin accepting packets.
98+
99+ ``QueueHandler.handle(packet) ``
100+ Handle a single packet from the queue. You must call either
101+ ``packet.accept() `` or ``packet.drop() ``.
102+
103+ Packet objects
104+ --------------
105+
106+ Objects of this type are passed to your handle() method.
107+
108+ ``Packet.get_payload() ``
109+ Return the packet's payload as a string.
110+
111+ ``Packet.get_payload_len() ``
112+ Return the size of the payload.
113+
114+ ``Packet.set_mark(mark) ``
115+ Give the packet a kernel mark. ``mark `` is a 32-bit number.
116+
117+ ``Packet.accept() ``
118+ Accept the packet.
119+
120+ ``Packet.drop() ``
121+ Drop the packet.
68122
69123Usage
70124=====
@@ -107,11 +161,10 @@ The fields are:
107161
1081629. Libnetfilter_queue internal use
109163
110-
111164Limitations
112165===========
113166
114167TODO: fix this up
115168
116169* compiled to max 2048-byte packets, so won't work on LO?
117- * full API not implemented: omits set_payload(), interface methods, and what else?
170+ * full API not implemented: omits set_payload(), interface methods, and what else?
0 commit comments