-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOnlinePcapReaderThread.py
160 lines (132 loc) · 5.31 KB
/
OnlinePcapReaderThread.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from BufferedPackets import BufferedPackets
from impacket import ImpactDecoder, ImpactPacket
import pcapy
import threading
class OnlinePcapReaderThread(threading.Thread):
def __init__(self, protocol, port):
threading.Thread.__init__(self)
snaplen = 65535
promiscious = False
read_timeout = 0
self.pcap = pcapy.open_live("any", snaplen, promiscious, read_timeout)
self.connection_list = []
self.ready_connection_list = []
self.last_read_index = -1
self.done = False
self.port = port
self.protocol = protocol
self.lock = threading.Lock()
self.delete_read_connections = False
print "{} dst port {}".format(self.protocol, self.port)
self.pcap.setfilter("{} dst port {}".format(self.protocol, self.port))
#self.pcap.setfilter("{}".format(self.protocol))
def run(self):
#for i in range (0, 1000):
while not self.done:
(header, frame) = self.pcap.next()
if not header:
break
self.parse_packet(header, frame)
#self.clean_no_payload()
ready_indices = range(0, len(self.connection_list))
self.move_ready_packets(ready_indices)
#self.clean_no_payload()
print "Num of connections : " + str(len(self.connection_list))
self.done = True
def clean_no_payload(self):
try:
self.lock.acquire()
for i in range(len(self.connection_list) - 1, -1, -1):
if len(self.connection_list) == 0:
break
if self.connection_list[i].get_payload_length() == 0 and self.connection_list[i].ready:
del self.connection_list[i]
finally:
self.lock.release()
def parse_packet(self, header, frame):
decoder = ImpactDecoder.LinuxSLLDecoder()
ether = decoder.decode(frame)
ready_indices = []
if ether.get_ether_type() == ImpactPacket.IP.ethertype:
self.lock.acquire()
for i in range(0, len(self.connection_list)):
buffered_packets = self.connection_list[i]
if buffered_packets.add_frame(ether): #if there's an existing flow
self.lock.release()
if len(ready_indices) > 0:
self.move_ready_packets(ready_indices)
return
if buffered_packets.ready:
ready_indices.append(i)
buffered_packets = BufferedPackets(ether)
self.connection_list.append(buffered_packets)
self.lock.release()
if len(ready_indices) > 0:
self.move_ready_packets(ready_indices)
def move_ready_packets(self, ready_indices):
self.lock.acquire()
for i in range(len(ready_indices)-1, -1, -1):
if self.connection_list[i].get_payload_length() > 0:
self.ready_connection_list.append(self.connection_list[i])
del self.connection_list[i]
self.lock.release()
def has_ready_message(self):
self.lock.acquire()
if not self.delete_read_connections:
if len(self.ready_connection_list)-1 == self.last_read_index:
self.lock.release()
return False
else:
self.lock.release()
return True
else:
if len(self.ready_connection_list) > 0:
self.lock.release()
return True
else:
self.lock.release()
return False
def has_unread_message(self):
self.lock.acquire()
if len(self.ready_connection_list) > 0:
for buffered_packets in self.connection_list:
if not buffered_packets.read:
self.lock.release()
return True
self.lock.release()
return False
def pop_connection(self):
self.lock.acquire()
# for id in range(0, len(self.connection_list)):
# if self.connection_list[id].ready and not self.connection_list[id].read:
# bp = self.connection_list[id]
# # del self.connection_list[id]
# bp.read = True
# self.lock.release()
# return bp
if not self.delete_read_connections:
if len(self.ready_connection_list)-1 == self.last_read_index:
self.lock.release()
return None
else:
self.last_read_index += 1
buffered_packets = self.ready_connection_list[self.last_read_index]
buffered_packets.read = True
self.lock.release()
return buffered_packets
else:
if len(self.ready_connection_list) <= 0:
self.lock.release()
return None
else:
buffered_packets = self.ready_connection_list[0]
del self.ready_connection_list[0]
self.lock.release()
return buffered_packets
def reset_read_status(self):
for buffered_packets in self.connection_list:
buffered_packets.read = False
def forced_pop_connection(self):
bp = self.connection_list[0]
del self.connection_list[0]
return bp