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.
@@ -20,7 +20,7 @@ DEF BufferSize = 4096
20
20
DEF MetadataSize = 80
21
21
DEF MaxCopySize = BufferSize - MetadataSize
22
22
23
- cdef int global_callback(nfq_q_handle * qh, nfgenmsg * nfmsg,
23
+ cdef int global_callback(nfq_q_handle * qh, nfgenmsg * nfmsg,
24
24
nfq_data * nfa, void * data) with gil:
25
25
""" Create a Packet and pass it to appropriate callback."""
26
26
cdef NetfilterQueue nfqueue = < NetfilterQueue> data
@@ -37,29 +37,29 @@ cdef class Packet:
37
37
self ._verdict_is_set = False
38
38
self ._mark_is_set = False
39
39
self ._given_payload = None
40
-
40
+
41
41
def __str__ (self ):
42
42
cdef iphdr * hdr = < iphdr* > self .payload
43
43
protocol = PROTOCOLS.get(hdr.protocol, " Unknown protocol" )
44
44
return " %s packet, %s bytes" % (protocol, self .payload_len)
45
-
45
+
46
46
cdef set_nfq_data(self , nfq_q_handle * qh, nfq_data * nfa):
47
47
"""
48
- Assign a packet from NFQ to this object. Parse the header and load
48
+ Assign a packet from NFQ to this object. Parse the header and load
49
49
local values.
50
50
"""
51
51
self ._qh = qh
52
52
self ._nfa = nfa
53
53
self ._hdr = nfq_get_msg_packet_hdr(nfa)
54
-
54
+
55
55
self .id = ntohl(self ._hdr.packet_id)
56
56
self .hw_protocol = ntohs(self ._hdr.hw_protocol)
57
57
self .hook = self ._hdr.hook
58
-
58
+
59
59
self .payload_len = nfq_get_payload(self ._nfa, & self .payload)
60
60
if self .payload_len < 0 :
61
61
raise OSError (" Failed to get payload of packet." )
62
-
62
+
63
63
nfq_get_timestamp(self ._nfa, & self .timestamp)
64
64
self .mark = nfq_get_nfmark(nfa)
65
65
@@ -69,7 +69,7 @@ cdef class Packet:
69
69
raise RuntimeWarning (" Verdict already given for this packet." )
70
70
71
71
cdef u_int32_t modified_payload_len = 0
72
- cdef unsigned char * modified_payload = NULL
72
+ cdef unsigned char * modified_payload = NULL
73
73
if self ._given_payload:
74
74
modified_payload_len = len (self ._given_payload)
75
75
modified_payload = self ._given_payload
@@ -90,21 +90,21 @@ cdef class Packet:
90
90
modified_payload)
91
91
92
92
self ._verdict_is_set = True
93
-
93
+
94
94
def get_payload (self ):
95
95
""" Return payload as Python string."""
96
96
return self .payload[:self .payload_len]
97
97
98
98
cpdef Py_ssize_t get_payload_len(self ):
99
99
return self .payload_len
100
-
100
+
101
101
cpdef double get_timestamp(self ):
102
102
return self .timestamp.tv_sec + (self .timestamp.tv_usec/ 1000000.0 )
103
-
103
+
104
104
cpdef set_payload(self , bytes payload):
105
105
""" Set the new payload of this packet."""
106
106
self ._given_payload = payload
107
-
107
+
108
108
cpdef set_mark(self , u_int32_t mark):
109
109
self ._given_mark = mark
110
110
self ._mark_is_set = True
@@ -113,11 +113,11 @@ cdef class Packet:
113
113
if self ._mark_is_set:
114
114
return self ._given_mark
115
115
return self .mark
116
-
116
+
117
117
cpdef accept(self ):
118
118
""" Accept the packet."""
119
119
self .verdict(NF_ACCEPT)
120
-
120
+
121
121
cpdef drop(self ):
122
122
""" Drop the packet."""
123
123
self .verdict(NF_DROP)
@@ -134,21 +134,21 @@ cdef class NetfilterQueue:
134
134
self .h = nfq_open()
135
135
if self .h == NULL :
136
136
raise OSError (" Failed to open NFQueue." )
137
- nfq_unbind_pf(self .h, self .af) # This does NOT kick out previous
137
+ nfq_unbind_pf(self .h, self .af) # This does NOT kick out previous
138
138
# running queues
139
139
if nfq_bind_pf(self .h, self .af) < 0 :
140
140
raise OSError (" Failed to bind family %s . Are you root?" % self .af)
141
-
141
+
142
142
def __dealloc__ (self ):
143
143
if self .qh != NULL :
144
144
nfq_destroy_queue(self .qh)
145
145
self .qh = NULL
146
- # Don't call nfq_unbind_pf unless you want to disconnect any other
146
+ # Don't call nfq_unbind_pf unless you want to disconnect any other
147
147
# processes using this libnetfilter_queue on this protocol family!
148
148
nfq_close(self .h)
149
149
150
150
def bind (self , int queue_num , object user_callback ,
151
- u_int32_t max_len = DEFAULT_MAX_QUEUELEN,
151
+ u_int32_t max_len = DEFAULT_MAX_QUEUELEN,
152
152
u_int8_t mode = NFQNL_COPY_PACKET,
153
153
u_int32_t range = MaxPacketSize):
154
154
""" Create and bind to a new queue."""
@@ -157,21 +157,21 @@ cdef class NetfilterQueue:
157
157
< nfq_callback* > global_callback, < void * > self )
158
158
if self .qh == NULL :
159
159
raise OSError (" Failed to create queue %s ." % queue_num)
160
-
160
+
161
161
if range > MaxCopySize:
162
162
range = MaxCopySize
163
163
if nfq_set_mode(self .qh, mode, range ) < 0 :
164
164
raise OSError (" Failed to set packet copy mode." )
165
-
165
+
166
166
nfq_set_queue_maxlen(self .qh, max_len)
167
-
167
+
168
168
def unbind (self ):
169
169
""" Destroy the queue."""
170
170
if self .qh != NULL :
171
171
nfq_destroy_queue(self .qh)
172
172
self .qh = NULL
173
173
# See warning about nfq_unbind_pf in __dealloc__ above.
174
-
174
+
175
175
def run (self ):
176
176
""" Begin accepting packets."""
177
177
cdef int fd = nfq_fd(self .h)
0 commit comments