Skip to content

Commit f6ef533

Browse files
author
Matt
committed
Add API to README.
1 parent a3676c4 commit f6ef533

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

README.rst

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@ Libnetfilter_queue (the netfilter library, not this module) is part of the `Netf
1010
Example
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

2630
To 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

3943
3. 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

5559
To 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

6266
Setup will use Cython if it is installed, regenerating the .c source from the .pyx before compiling the .so.
6367

6468
API
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

69123
Usage
70124
=====
@@ -107,11 +161,10 @@ The fields are:
107161

108162
9. Libnetfilter_queue internal use
109163

110-
111164
Limitations
112165
===========
113166

114167
TODO: 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?

netfilterqueue.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ cdef class Packet:
183183
cpdef accept(self)
184184
cpdef drop(self)
185185

186-
cdef class NetfilterQueue:
186+
cdef class QueueHandler:
187187
cdef nfq_handle *h # Handle to NFQueue library
188188
cdef nfq_q_handle *qh # A handle to the queue
189189
cdef u_int16_t af # Address family

netfilterqueue.pyx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ cdef int global_callback(nfq_q_handle *qh, nfgenmsg *nfmsg,
1717
"""Create an Packet and pass it to appropriate Python/Cython callback."""
1818
packet = Packet()
1919
packet.set_nfq_data(qh, nfa)
20-
(<NetfilterQueue>data).handle(packet)
20+
(<QueueHandler>data).handle(packet)
2121
return 1
2222

2323
cdef class Packet:
24-
"""A packet received from NetfilterQueue."""
24+
"""A packet received from QueueHandler."""
2525
def __cinit__(self):
2626
self._verdict_is_set = False
2727
self._mark_is_set = False
@@ -104,7 +104,7 @@ cdef class Packet:
104104
"""Drop the packet."""
105105
self.verdict(NF_DROP)
106106

107-
cdef class NetfilterQueue:
107+
cdef class QueueHandler:
108108
"""Handle a single numbered queue."""
109109
def __cinit__(self, *args, **kwargs):
110110
self.af = kwargs.get("af", PF_INET)
@@ -124,7 +124,7 @@ cdef class NetfilterQueue:
124124
# processes using this libnetfilter_queue on this protocol family!
125125
nfq_close(self.h)
126126

127-
def bind(self, int queue_num, u_int32_t maxlen=DEFAULT_MAX_QUEUELEN, u_int8_t mode=NFQNL_COPY_PACKET, u_int32_t range=MaxPacketSize):
127+
def bind(self, int queue_num, u_int32_t max_len=DEFAULT_MAX_QUEUELEN, u_int8_t mode=NFQNL_COPY_PACKET, u_int32_t range=MaxPacketSize):
128128
"""Create and bind to a new queue."""
129129
self.qh = nfq_create_queue(self.h, queue_num, <nfq_callback*>global_callback, <void*>self)
130130
if self.qh == NULL:
@@ -135,7 +135,7 @@ cdef class NetfilterQueue:
135135
if nfq_set_mode(self.qh, mode, range) < 0:
136136
raise OSError("Failed to set packet copy mode.")
137137

138-
nfq_set_queue_maxlen(self.qh, maxlen)
138+
nfq_set_queue_maxlen(self.qh, max_len)
139139

140140
def unbind(self):
141141
"""Destroy the queue."""

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