1 import binascii
2 import time
3
6 '''
7 Writes to the specified file in Daintree SNA packet capture file format.
8 @type savefile: String
9 @param savefile: Output Daintree SNA packet capture file.
10 @rtype: None
11 '''
12 ltime = time.localtime()
13 timeymd = ''.join([str(ltime[0]),str(ltime[1]),str(ltime[2])])
14 DSNA_HEADER1 = '#Format=4\r\n'
15 DSNA_HEADER2 = '# SNA v3.0.0.7 SUS:%s ACT:067341\r\n'%timeymd
16 self._pcount = 0
17 self._fh = open(savefile, "w")
18 self._fh.write(DSNA_HEADER1)
19 self._fh.write(DSNA_HEADER2)
20
21 - def pcap_dump(self, packet, ts_sec=None, ts_usec=None, orig_len=None):
22 '''
23 This method is a wrapper around the pwrite() method for compatibility
24 with the PcapDumper.pcap_dump method.
25 '''
26 self.pwrite(packet)
27
28 - def pwrite(self, packet, channel=26, rssi=0):
29 '''
30 Appends a new packet to the daintree capture file.
31 @type packet: String
32 @param packet: Packet contents
33 @type channel: Int
34 @param channel: Capture file reported channel number (optional, def=26)
35 @type rssi: Int
36 @param rssi: Capture file repored RSSI (optional, def=0)
37 @rtype: None
38 '''
39 self._pcount += 1
40 record = ''.join([
41 str(self._pcount), " ",
42 "%6f"%time.time(), " ",
43 str(len(packet)), " ",
44 binascii.hexlify(packet), " ",
45 "255 ",
46 "1 ",
47 str(rssi), " ",
48 str(channel), " ",
49 str(self._pcount), " ",
50 "0 1 32767\r\n"])
51 self._fh.write(record)
52
54 '''
55 Close the input packet capture file.
56 @rtype: None
57 '''
58 del(self._fh)
59
60
63 '''
64 Reads from a specified Daintree SNA packet capture file.
65 @type savefile: String
66 @param savefile: Daintree SNA packet capture filename to read from.
67 @rtype: None. An exception is raised if the capture file is not in Daintree SNA format.
68 '''
69 DSNA_HEADER1 = '#Format=4\r\n'
70 self._fh = open(savefile, "r")
71 header = self._fh.readline()
72
73 if header != DSNA_HEADER1:
74 raise Exception('Invalid or unsupported Daintree SNA file specified')
75
77 '''
78 Close the output packet capture.
79 @rtype: None
80 '''
81 del(self._fh)
82
84 '''
85 Retrieves the next packet from the capture file. Returns a list of
86 [Hdr, packet] where Hdr is a list of [timestamp, snaplen, plen] and
87 packet is a string of the payload content. Returns None at the end
88 of the packet capture.
89 @rtype: List
90 '''
91 try:
92 while(1):
93 record = self._fh.readline().split(' ')
94 if record[0][0] == "#":
95 continue
96 else:
97 break
98
99 if record == None:
100 return None
101
102
103 return [[float(record[1]),len(record[3]),len(record[3])], binascii.unhexlify(record[3])]
104 except IndexError:
105 return [None, None]
106