1
1
"""
2
- Bind to a Linux netfilter queue. Send packets to a user-specified callback
2
+ Bind to a Linux netfilter queue. Send packets to a user-specified callback
3
3
function.
4
4
5
5
Copyright: (c) 2011, Kerkhoff Technologies Inc.
@@ -22,7 +22,7 @@ DEF MaxCopySize = BufferSize - MetadataSize
22
22
23
23
cimport cpython.version
24
24
25
- cdef int global_callback(nfq_q_handle * qh, nfgenmsg * nfmsg,
25
+ cdef int global_callback(nfq_q_handle * qh, nfgenmsg * nfmsg,
26
26
nfq_data * nfa, void * data) with gil:
27
27
""" Create a Packet and pass it to appropriate callback."""
28
28
cdef NetfilterQueue nfqueue = < NetfilterQueue> data
@@ -39,29 +39,29 @@ cdef class Packet:
39
39
self ._verdict_is_set = False
40
40
self ._mark_is_set = False
41
41
self ._given_payload = None
42
-
42
+
43
43
def __str__ (self ):
44
44
cdef iphdr * hdr = < iphdr* > self .payload
45
45
protocol = PROTOCOLS.get(hdr.protocol, " Unknown protocol" )
46
46
return " %s packet, %s bytes" % (protocol, self .payload_len)
47
-
47
+
48
48
cdef set_nfq_data(self , nfq_q_handle * qh, nfq_data * nfa):
49
49
"""
50
- Assign a packet from NFQ to this object. Parse the header and load
50
+ Assign a packet from NFQ to this object. Parse the header and load
51
51
local values.
52
52
"""
53
53
self ._qh = qh
54
54
self ._nfa = nfa
55
55
self ._hdr = nfq_get_msg_packet_hdr(nfa)
56
-
56
+
57
57
self .id = ntohl(self ._hdr.packet_id)
58
58
self .hw_protocol = ntohs(self ._hdr.hw_protocol)
59
59
self .hook = self ._hdr.hook
60
-
60
+
61
61
self .payload_len = nfq_get_payload(self ._nfa, & self .payload)
62
62
if self .payload_len < 0 :
63
63
raise OSError (" Failed to get payload of packet." )
64
-
64
+
65
65
nfq_get_timestamp(self ._nfa, & self .timestamp)
66
66
self .mark = nfq_get_nfmark(nfa)
67
67
@@ -71,7 +71,7 @@ cdef class Packet:
71
71
raise RuntimeWarning (" Verdict already given for this packet." )
72
72
73
73
cdef u_int32_t modified_payload_len = 0
74
- cdef unsigned char * modified_payload = NULL
74
+ cdef unsigned char * modified_payload = NULL
75
75
if self ._given_payload:
76
76
modified_payload_len = len (self ._given_payload)
77
77
modified_payload = self ._given_payload
@@ -92,7 +92,7 @@ cdef class Packet:
92
92
modified_payload)
93
93
94
94
self ._verdict_is_set = True
95
-
95
+
96
96
def get_payload (self ):
97
97
""" Return payload as Python string."""
98
98
cdef object py_string
@@ -103,17 +103,17 @@ cdef class Packet:
103
103
py_string = PyString_FromStringAndSize(
104
104
self .payload, self .payload_len)
105
105
return py_string
106
-
106
+
107
107
cpdef Py_ssize_t get_payload_len(self ):
108
108
return self .payload_len
109
-
109
+
110
110
cpdef double get_timestamp(self ):
111
111
return self .timestamp.tv_sec + (self .timestamp.tv_usec/ 1000000.0 )
112
-
112
+
113
113
cpdef set_payload(self , bytes payload):
114
114
""" Set the new payload of this packet."""
115
115
self ._given_payload = payload
116
-
116
+
117
117
cpdef set_mark(self , u_int32_t mark):
118
118
self ._given_mark = mark
119
119
self ._mark_is_set = True
@@ -122,11 +122,11 @@ cdef class Packet:
122
122
if self ._mark_is_set:
123
123
return self ._given_mark
124
124
return self .mark
125
-
125
+
126
126
cpdef accept(self ):
127
127
""" Accept the packet."""
128
128
self .verdict(NF_ACCEPT)
129
-
129
+
130
130
cpdef drop(self ):
131
131
""" Drop the packet."""
132
132
self .verdict(NF_DROP)
@@ -143,20 +143,20 @@ cdef class NetfilterQueue:
143
143
self .h = nfq_open()
144
144
if self .h == NULL :
145
145
raise OSError (" Failed to open NFQueue." )
146
- nfq_unbind_pf(self .h, self .af) # This does NOT kick out previous
146
+ nfq_unbind_pf(self .h, self .af) # This does NOT kick out previous
147
147
# running queues
148
148
if nfq_bind_pf(self .h, self .af) < 0 :
149
149
raise OSError (" Failed to bind family %s . Are you root?" % self .af)
150
-
150
+
151
151
def __dealloc__ (self ):
152
152
if self .qh != NULL :
153
153
nfq_destroy_queue(self .qh)
154
- # Don't call nfq_unbind_pf unless you want to disconnect any other
154
+ # Don't call nfq_unbind_pf unless you want to disconnect any other
155
155
# processes using this libnetfilter_queue on this protocol family!
156
156
nfq_close(self .h)
157
157
158
158
def bind (self , int queue_num , object user_callback ,
159
- u_int32_t max_len = DEFAULT_MAX_QUEUELEN,
159
+ u_int32_t max_len = DEFAULT_MAX_QUEUELEN,
160
160
u_int8_t mode = NFQNL_COPY_PACKET,
161
161
u_int32_t range = MaxPacketSize):
162
162
""" Create and bind to a new queue."""
@@ -165,20 +165,20 @@ cdef class NetfilterQueue:
165
165
< nfq_callback* > global_callback, < void * > self )
166
166
if self .qh == NULL :
167
167
raise OSError (" Failed to create queue %s ." % queue_num)
168
-
168
+
169
169
if range > MaxCopySize:
170
170
range = MaxCopySize
171
171
if nfq_set_mode(self .qh, mode, range ) < 0 :
172
172
raise OSError (" Failed to set packet copy mode." )
173
-
173
+
174
174
nfq_set_queue_maxlen(self .qh, max_len)
175
-
175
+
176
176
def unbind (self ):
177
177
""" Destroy the queue."""
178
178
if self .qh != NULL :
179
179
nfq_destroy_queue(self .qh)
180
180
# See warning about nfq_unbind_pf in __dealloc__ above.
181
-
181
+
182
182
def run (self ):
183
183
""" Begin accepting packets."""
184
184
cdef int fd = nfq_fd(self .h)
0 commit comments