File: example.py

package info (click to toggle)
nfqueue-bindings 0.4-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 232 kB
  • sloc: ansic: 257; python: 211; perl: 183; makefile: 28; sh: 25
file content (61 lines) | stat: -rwxr-xr-x 1,085 bytes parent folder | download | duplicates (2)
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
#!/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

count = 0

def cb(i, payload):
	global count

	print "python callback called !"
	count += 1

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

	payload.set_verdict(nfqueue.NF_ACCEPT)

	sys.stdout.flush()
	return 1

q = nfqueue.queue()

print "setting callback"
q.set_callback(cb)

print "open"
q.fast_open(0, AF_INET)

q.set_queue_maxlen(50000)

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

print "%d packets handled" % count

print "unbind"
q.unbind(AF_INET)

print "close"
q.close()