Fix build by #if 0 out unused de_sgsap_tmsi() function.
[obnox/wireshark/wip.git] / tools / netscreen2dump.py
1 #!/usr/bin/env python
2 """
3 Converts netscreen snoop hex-dumps to a hex-dump that text2pcap can read.
4
5 $Id$
6
7 Copyright (c) 2004 by Gilbert Ramirez <gram@alumni.rice.edu>
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 """
23
24 import sys
25 import re
26 import os
27 import stat
28 import time
29
30 class OutputFile:
31     TIMER_MAX = 99999.9
32
33     def __init__(self, name, base_time):
34         try:
35             self.fh = open(name, "w")
36         except IOError, err:
37             sys.exit(err)
38
39         self.base_time = base_time
40         self.prev_timestamp = 0.0
41
42     def PrintPacket(self, timestamp, datalines):
43         # What do to with the timestamp? I need more data about what
44         # the netscreen timestamp is, then I can generate one for the text file.
45 #        print "TS:", timestamp.group("time")
46         try:
47             timestamp = float(timestamp.group("time"))
48         except ValueError:
49             sys.exit("Unable to convert '%s' to floating point." % \
50                     (timestamp,))
51
52         # Did we wrap around the timeer max?
53         if timestamp < self.prev_timestamp:
54             self.base_time += self.TIMER_MAX
55
56         self.prev_timestamp = timestamp
57
58         packet_timestamp = self.base_time + timestamp
59
60         # Determine the time string to print
61         gmtime = time.gmtime(packet_timestamp)
62         subsecs = packet_timestamp - int(packet_timestamp)
63         assert subsecs <= 0
64         subsecs = int(subsecs * 10)
65
66         print >> self.fh, "%s.%d" % (time.strftime("%Y-%m-%d %H:%M:%S", gmtime), \
67                 subsecs)
68
69         # Print the packet data
70         offset = 0
71         for lineno, hexgroup in datalines:
72             hexline = hexgroup.group("hex")
73             hexpairs = hexline.split()
74             print >> self.fh, "%08x   %s" % (offset, hexline)
75             offset += len(hexpairs)
76
77         # Blank line
78         print >> self.fh
79
80 # Find a timestamp line
81 re_timestamp = re.compile(r"^(?P<time>\d+\.\d): [\w/]+\((?P<io>.)\)(:| len=)")
82
83 # Find a hex dump line
84 re_hex_line = re.compile(r"(?P<hex>([0-9a-f]{2} ){1,16})\s+(?P<ascii>.){1,16}")
85
86 def run(input_filename, output_filename):
87     try:
88         ifh = open(input_filename, "r")
89     except IOError, err:
90         sys.exit(err)
91
92     # Get the file's creation time.
93     try:
94         ctime = os.stat(input_filename)[stat.ST_CTIME]
95     except OSError, err:
96         sys.exit(err)
97
98     output_file = OutputFile(output_filename, ctime)
99
100     timestamp = None
101     datalines = []
102     lineno = 0
103
104     for line in ifh.xreadlines():
105         lineno += 1
106         # If we have no timestamp yet, look for one
107         if not timestamp:
108             m = re_timestamp.search(line)
109             if m:
110                 timestamp = m
111
112         # Otherwise, look for hex dump lines
113         else:
114             m = re_hex_line.search(line)
115             if m:
116                 datalines.append((lineno, m))
117             else:
118                 # If we have been gathering hex dump lines,
119                 # and this line is not a hex dump line, then the hex dump
120                 # has finished, and so has the packet. So print the packet
121                 # and reset our variables so we can look for the next packet.
122                 if datalines:
123                     output_file.PrintPacket(timestamp, datalines)
124                     timestamp = None
125                     datalines = []
126
127     # At the end of the file we may still have hex dump data in memory.
128     # If so, print the packet
129     if datalines:
130         output_file.PrintPacket(timestamp, datalines)
131         timestamp = None
132         datalines = []
133
134
135 def usage():
136     print >> sys.stderr, "Usage: netscreen2dump.py netscreen-dump-file new-dump-file"
137     sys.exit(1)
138
139 def main():
140     if len(sys.argv) != 3:
141         usage()
142
143     run(sys.argv[1], sys.argv[2])
144
145 if __name__ == "__main__":
146     main()