]>
Commit | Line | Data |
---|---|---|
7aa697dd BP |
1 | #! @PYTHON@ |
2 | # | |
e0edde6f | 3 | # Copyright (c) 2010 Nicira, Inc. |
7aa697dd BP |
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 | class PcapException(Exception): | |
23 | pass | |
24 | ||
25 | class PcapReader(object): | |
26 | def __init__(self, file_name): | |
27 | self.file = open(file_name, "rb") | |
28 | header = self.file.read(24) | |
29 | if len(header) != 24: | |
30 | raise PcapException("end of file reading pcap header") | |
31 | magic, version, thiszone, sigfigs, snaplen, network = \ | |
32 | struct.unpack(">6I", header) | |
33 | if magic == 0xa1b2c3d4: | |
34 | self.header_format = ">4I" | |
35 | elif magic == 0xd4c3b2a1: | |
36 | self.header_format = "<4I" | |
37 | else: | |
38 | raise PcapException("bad magic %u reading pcap file " | |
39 | "(expected 0xa1b2c3d4 or 0xd4c3b2a1)" % magic) | |
40 | ||
41 | def read(self): | |
42 | header = self.file.read(16) | |
43 | if len(header) == 0: | |
44 | return None | |
45 | elif len(header) != 16: | |
46 | raise PcapException("end of file within pcap record header") | |
47 | ||
48 | ts_sec, ts_usec, incl_len, orig_len = struct.unpack(self.header_format, | |
49 | header) | |
50 | packet = self.file.read(incl_len) | |
51 | if len(packet) != incl_len: | |
52 | raise PcapException("end of file reading pcap packet data") | |
53 | return packet | |
54 | argv0 = sys.argv[0] | |
55 | ||
56 | def usage(): | |
57 | print """\ | |
58 | %(argv0)s: print pcap file packet data as hex | |
59 | usage: %(argv0)s FILE | |
60 | where FILE is a PCAP file. | |
61 | ||
62 | The following options are also available: | |
63 | -h, --help display this help message | |
64 | -V, --version display version information\ | |
65 | """ % {'argv0': argv0} | |
66 | sys.exit(0) | |
67 | ||
68 | if __name__ == "__main__": | |
69 | try: | |
70 | try: | |
71 | options, args = getopt.gnu_getopt(sys.argv[1:], 'hV', | |
72 | ['help', 'version']) | |
73 | except getopt.GetoptException, geo: | |
74 | sys.stderr.write("%s: %s\n" % (argv0, geo.msg)) | |
75 | sys.exit(1) | |
76 | ||
77 | for key, value in options: | |
78 | if key in ['-h', '--help']: | |
79 | usage() | |
80 | elif key in ['-V', '--version']: | |
81 | print "ovs-pcap (Open vSwitch) @VERSION@" | |
82 | else: | |
83 | sys.exit(0) | |
84 | ||
85 | if len(args) != 1: | |
86 | sys.stderr.write("%s: exactly 1 non-option argument required " | |
87 | "(use --help for help)\n" % argv0) | |
88 | sys.exit(1) | |
89 | ||
90 | reader = PcapReader(args[0]) | |
91 | while True: | |
92 | packet = reader.read() | |
93 | if packet is None: | |
94 | break | |
95 | ||
96 | print binascii.hexlify(packet) | |
97 | ||
98 | except PcapException, e: | |
99 | sys.stderr.write("%s: %s\n" % (argv0, e)) | |
100 | sys.exit(1) | |
101 | ||
102 | # Local variables: | |
103 | # mode: python | |
104 | # End: |