File: example.py

package info (click to toggle)
nfqueue-bindings 0.3-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 212 kB
  • ctags: 80
  • sloc: ansic: 260; python: 220; perl: 195; makefile: 70; sh: 25
file content (69 lines) | stat: -rwxr-xr-x 1,183 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python

# need root privileges

import struct
import sys
import time

from socket import AF_INET, AF_INET6, inet_ntoa

sys.path.append('python')
sys.path.append('build/python')
import nfqueue

sys.path.append('dpkt-1.6')
from dpkt import ip

def cb(i,payload):
	print "python callback called !", i

	print "payload len ", payload.get_length()
	data = payload.get_data()
	pkt = ip.IP(data)
	print "proto:", pkt.p
	print "source: %s" % inet_ntoa(pkt.src)
	print "dest: %s" % inet_ntoa(pkt.dst)
	if pkt.p == ip.IP_PROTO_TCP:
	 	print "  sport: %s" % pkt.tcp.sport
	 	print "  dport: %s" % pkt.tcp.dport
        payload.set_verdict(nfqueue.NF_DROP)

	sys.stdout.flush()
	return 1

q = nfqueue.queue()

print "open"
q.open()

print "bind"
q.bind(AF_INET);

#print "setting callback (should fail, wrong arg type)"
#try:
#	q.set_callback("blah")
#except TypeError, e:
#	print "type failure (expected), continuing"

print "setting callback"
q.set_callback(cb)

print "creating queue"
q.create_queue(0)

q.set_queue_maxlen(5000)

print "trying to run"
try:
	q.try_run()
except KeyboardInterrupt, e:
	print "interrupted"


print "unbind"
q.unbind(AF_INET)

print "close"
q.close()