-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilereader.py
94 lines (80 loc) · 2.48 KB
/
filereader.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
# Copyright (C) 2010 Sara Dar
# Released under GNU LGPL 2.1
# See LICENSE.txt for more information
#import sys
from socket import inet_ntoa
#import string
import pcap
import dpkt
#import binascii
#from StringIO import StringIO
import pymdht.core.message as message
#import os
#import stat
#import pymdht.identifier as identifier
#import keyword
class FileReader:
fname=''
def __init__(self):
#self.dataobj=data()
pass
def file_reader(self,filename):
msglist=[]
f=open(filename)
pcap=dpkt.pcap.Reader(f)
srclist=[]
tidlist=[]
dstlist=[]
tslist=[]
c=0
for (ts, raw_packet) in pcap:
try:
ip_packet=dpkt.ethernet.Ethernet(raw_packet).ip
src_addr=(inet_ntoa(ip_packet.src), ip_packet.udp.sport)
except (AttributeError):
# This is not an IP packet
c=c+1
continue
try:
msg = message.IncomingMsg(ip_packet.data.data,src_addr)
srclist.append(src_addr)
dstlist.append((inet_ntoa(ip_packet.dst), ip_packet.udp.dport))
tslist.append(ts)
msglist.append(msg)
tidlist.append(msg.tid)
#print msg.version
except(message.MsgError):
#print 'ERROR:', ip_packet.data.data
continue
#msglist.append(msg)
#transid.append(ord(msg.tid[0]))
print c
assert len(dstlist) == len(srclist)
assert len(dstlist) == len(tslist)
assert len(dstlist) == len(msglist)
assert len(dstlist) == len(tidlist)
f.close()
return msglist,tidlist,tslist,srclist,dstlist
class Data:
def __init__(self):
messages=[]
transIDs=[]
timestamps=[]
sources=[]
destinations=[]
def get_packet(self,packetlist):
self.messages=packetlist.messages
self.transIDs=packetlist.transIDs
self.timestamps=packetlist.timestamps
self.sources=packetlist.sources
self.destinations=packetlist.destinations
return self
def get_data(self,filename):
fobj=FileReader()
mlist, tidls, tsls, srcls,dstls=fobj.file_reader(filename)
self.messages=mlist
self.transIDs=tidls
self.timestamps=tsls
self.sources=srcls
self.destinations=dstls
return self