@@ -10,18 +10,22 @@ Libnetfilter_queue (the netfilter library, not this module) is part of the `Netf
10
10
Example
11
11
=======
12
12
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. ::
14
14
15
- from netfilterqueue import NetfilterQueue
15
+ from netfilterqueue import QueueHandler
16
16
17
- class PacketPrinter(NetfilterQueue ):
17
+ class PacketPrinter(QueueHandler ):
18
18
def handle(self, packet):
19
19
print packet
20
20
packet.accept()
21
21
22
22
p = PacketPrinter()
23
23
p.bind(1)
24
- p.run()
24
+ try:
25
+ p.run()
26
+ except KeyboardInterrupt:
27
+ print
28
+
25
29
26
30
To send packets destined for your LAN to the script, type something like::
27
31
@@ -38,7 +42,7 @@ NetfilterQueue is a C extention module that links against libnetfilter_queue. Be
38
42
39
43
3. Libnetfilter_queue development files and associated dependencies
40
44
41
- On Debian or Ubuntu, these files are install with::
45
+ On Debian or Ubuntu, install these files with::
42
46
43
47
sudo apt-get install build-essential python-dev libnetfilter-queue-dev
44
48
@@ -54,17 +58,67 @@ From source
54
58
55
59
To install from source::
56
60
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
60
64
python setup.py install
61
65
62
66
Setup will use Cython if it is installed, regenerating the .c source from the .pyx before compiling the .so.
63
67
64
68
API
65
69
===
66
70
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.
68
122
69
123
Usage
70
124
=====
@@ -107,11 +161,10 @@ The fields are:
107
161
108
162
9. Libnetfilter_queue internal use
109
163
110
-
111
164
Limitations
112
165
===========
113
166
114
167
TODO: fix this up
115
168
116
169
* 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