python: Resolve pep8 blank line errors.
[cascardo/ovs.git] / utilities / ovs-pcap.in
1 #! @PYTHON@
2 #
3 # Copyright (c) 2010 Nicira, Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import binascii
18 import getopt
19 import struct
20 import sys
21
22
23 class PcapException(Exception):
24     pass
25
26
27 class PcapReader(object):
28     def __init__(self, file_name):
29         self.file = open(file_name, "rb")
30         header = self.file.read(24)
31         if len(header) != 24:
32             raise PcapException("end of file reading pcap header")
33         magic, version, thiszone, sigfigs, snaplen, network = \
34             struct.unpack(">6I", header)
35         if magic == 0xa1b2c3d4:
36             self.header_format = ">4I"
37         elif magic == 0xd4c3b2a1:
38             self.header_format = "<4I"
39         else:
40             raise PcapException("bad magic %u reading pcap file "
41                         "(expected 0xa1b2c3d4 or 0xd4c3b2a1)" % magic)
42
43     def read(self):
44         header = self.file.read(16)
45         if len(header) == 0:
46             return None
47         elif len(header) != 16:
48             raise PcapException("end of file within pcap record header")
49
50         ts_sec, ts_usec, incl_len, orig_len = struct.unpack(self.header_format,
51                                                             header)
52         packet = self.file.read(incl_len)
53         if len(packet) != incl_len:
54             raise PcapException("end of file reading pcap packet data")
55         return packet
56 argv0 = sys.argv[0]
57
58
59 def usage():
60     print """\
61 %(argv0)s: print pcap file packet data as hex
62 usage: %(argv0)s FILE
63 where FILE is a PCAP file.
64
65 The following options are also available:
66   -h, --help                  display this help message
67   -V, --version               display version information\
68 """ % {'argv0': argv0}
69     sys.exit(0)
70
71 if __name__ == "__main__":
72     try:
73         try:
74             options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
75                                               ['help', 'version'])
76         except getopt.GetoptException, geo:
77             sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
78             sys.exit(1)
79
80         for key, value in options:
81             if key in ['-h', '--help']:
82                 usage()
83             elif key in ['-V', '--version']:
84                 print "ovs-pcap (Open vSwitch) @VERSION@"
85             else:
86                 sys.exit(0)
87
88         if len(args) != 1:
89             sys.stderr.write("%s: exactly 1 non-option argument required "
90                              "(use --help for help)\n" % argv0)
91             sys.exit(1)
92
93         reader = PcapReader(args[0])
94         while True:
95             packet = reader.read()
96             if packet is None:
97                 break
98
99             print binascii.hexlify(packet)
100
101     except PcapException, e:
102         sys.stderr.write("%s: %s\n" % (argv0, e))
103         sys.exit(1)
104
105 # Local variables:
106 # mode: python
107 # End: