@@ -12,17 +12,16 @@ Example
12
12
13
13
The following script prints a short description of each packet before accepting it. ::
14
14
15
- from netfilterqueue import QueueHandler
15
+ from netfilterqueue import NetfilterQueue
16
16
17
- class PacketPrinter(QueueHandler):
18
- def handle(self, packet):
19
- print packet
20
- packet.accept()
17
+ def print_and_accept(pkt):
18
+ print pkt
19
+ pkt.accept()
21
20
22
- p = PacketPrinter ()
23
- p .bind(1)
21
+ nfqueue = NetfilterQueue ()
22
+ nfqueue .bind(1, print_and_accept )
24
23
try:
25
- p .run()
24
+ nfqueue .run()
26
25
except KeyboardInterrupt:
27
26
print
28
27
@@ -43,7 +42,7 @@ NetfilterQueue is a C extention module that links against libnetfilter_queue. Be
43
42
44
43
On Debian or Ubuntu, install these files with::
45
44
46
- sudo apt-get install build-essential python-dev libnetfilter-queue-dev
45
+ apt-get install build-essential python-dev libnetfilter-queue-dev
47
46
48
47
From PyPI
49
48
---------
@@ -57,12 +56,12 @@ From source
57
56
58
57
To install from source::
59
58
60
- wget http://pypi.python.org/packages/source/N/NetfilterQueue/NetfilterQueue-0.2 .tar.gz
61
- tar -xvzf NetfilterQueue-0.2 .tar.gz
62
- cd NetfilterQueue-0.2
59
+ wget http://pypi.python.org/packages/source/N/NetfilterQueue/NetfilterQueue-0.3 .tar.gz
60
+ tar -xvzf NetfilterQueue-0.3 .tar.gz
61
+ cd NetfilterQueue-0.3
63
62
python setup.py install
64
63
65
- Setup will use Cython if it is installed, regenerating the .c source from the .pyx before compiling the .so.
64
+ If Cython is installed, Distutils will use it to regenerate the .c source from the .pyx. It will then compile the .c into a .so.
66
65
67
66
API
68
67
===
74
73
``NetfilterQueue.COPY_PACKET ``
75
74
These constants specify how much of the packet should be given to the script- nothing, metadata, or the whole packet.
76
75
77
- QueueHandler objects
78
- --------------------
76
+ NetfilterQueue objects
77
+ ----------------------
79
78
80
- You should define a class that inherits from QueueHandler and implenents the
81
- handle() method. Handle() is called for each packet that appears in the queue .
79
+ A NetfilterQueue object represents a single queue. Configure your queue with
80
+ a call to `` bind ``, then start receiving packets with a call to `` run `` .
82
81
83
- ``QueueHandler.bind(queue_num[, max_len[, mode[, range]]]) ``
82
+ ``QueueHandler.bind(queue_num, callback [, max_len[, mode[, range]]]) ``
84
83
Create and bind to the queue. ``queue_num `` must match the number in your
85
- iptables rule. ``max_len `` sets the largest number of packets that can be
86
- in the queue; new packets are dropped if the size of the queue reaches this
87
- number. ``mode `` determines how much of the packet data is provided to
88
- your script. Use the constants above. ``range `` defines how many bytes of
89
- the packet you want to get. For example, if you only want the source and
90
- destination IPs of a IPv4 packet, ``range `` could be 20.
84
+ iptables rule. ``callback `` is a function or method that takes one
85
+ argument, a Packet object (see below). ``max_len `` sets the largest number
86
+ of packets that can be in the queue; new packets are dropped if the size of
87
+ the queue reaches this number. ``mode `` determines how much of the packet
88
+ data is provided to your script. Use the constants above. ``range `` defines
89
+ how many bytes of the packet you want to get. For example, if you only want
90
+ the source and destination IPs of a IPv4 packet, ``range `` could be 20.
91
91
92
92
``QueueHandler.unbind() ``
93
93
Remove the queue. Packets matched by your iptables rule will be dropped.
94
94
95
95
``QueueHandler.run() ``
96
- Begin accepting packets.
97
-
98
- ``QueueHandler.handle(packet) ``
99
- Handle a single packet from the queue. You must call either
100
- ``packet.accept() `` or ``packet.drop() ``.
96
+ Send packets to your callback. This method blocks.
101
97
102
98
Packet objects
103
99
--------------
104
100
105
- Objects of this type are passed to your handle() method .
101
+ Objects of this type are passed to your callback .
106
102
107
103
``Packet.get_payload() ``
108
104
Return the packet's payload as a string.
@@ -118,11 +114,22 @@ Objects of this type are passed to your handle() method.
118
114
119
115
``Packet.drop() ``
120
116
Drop the packet.
117
+
118
+ Callback objects
119
+ ----------------
120
+
121
+ Your callback can be function or a method and must accept one argument, a
122
+ Packet object. You must call either Packet.accept() or Packet.drop() before
123
+ returning.
124
+
125
+ ``callback(packet) `` or ``callback(self, packet) ``
126
+ Handle a single packet from the queue. You must call either
127
+ ``packet.accept() `` or ``packet.drop() ``.
121
128
122
129
Usage
123
130
=====
124
131
125
- To route packets to the queue::
132
+ To send packets to the queue::
126
133
127
134
iptables -I <table or chain> <match specification> -j NFQUEUE --queue-num <queue number>
128
135
@@ -133,7 +140,7 @@ For example::
133
140
The only special part of the rule is the target. Rules can have any match and
134
141
can be added to any table or chain.
135
142
136
- Valid queue numbers are integers from 0 to 65,536 inclusive.
143
+ Valid queue numbers are integers from 0 to 65,535 inclusive.
137
144
138
145
To view libnetfilter_queue stats, refer to /proc/net/netfilter/nfnetlink_queue::
139
146
@@ -158,7 +165,7 @@ The fields are:
158
165
159
166
8. Total number of packets sent to queue
160
167
161
- 9. Libnetfilter_queue internal use
168
+ 9. Something for libnetfilter_queue's internal use
162
169
163
170
Limitations
164
171
===========
@@ -190,6 +197,5 @@ License
190
197
=======
191
198
192
199
Copyright (c) 2011, Kerkhoff Technologies, Inc.
193
- All rights reserved.
194
200
195
- Licensed under ` BSD <https://github.com/kti/python-netfilterqueue/blob/master/LICENSE.txt >`_
201
+ ` MIT licensed <https://github.com/kti/python-netfilterqueue/blob/master/LICENSE.txt >`_
0 commit comments