]> git.proxmox.com Git - mirror_ovs.git/blame - utilities/ovs-pcap.in
Require Python 3 and remove support for Python 2.
[mirror_ovs.git] / utilities / ovs-pcap.in
CommitLineData
1ca0323e 1#! @PYTHON3@
7aa697dd 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
8ea171ab
RB
17from __future__ import print_function
18
7aa697dd
BP
19import binascii
20import getopt
21import struct
22import sys
23
bdca6c4b 24
7aa697dd
BP
25class PcapException(Exception):
26 pass
27
bdca6c4b 28
7aa697dd
BP
29class PcapReader(object):
30 def __init__(self, file_name):
31 self.file = open(file_name, "rb")
32 header = self.file.read(24)
33 if len(header) != 24:
34 raise PcapException("end of file reading pcap header")
35 magic, version, thiszone, sigfigs, snaplen, network = \
36 struct.unpack(">6I", header)
2b2532dd 37 if magic == 0xa1b2c3d4 or magic == 0xa1b23c4d:
7aa697dd 38 self.header_format = ">4I"
2b2532dd 39 elif magic == 0xd4c3b2a1 or magic == 0x4d3cb2a1:
7aa697dd
BP
40 self.header_format = "<4I"
41 else:
42 raise PcapException("bad magic %u reading pcap file "
2b2532dd
MM
43 "(expected 0xa1b2c3d4, 0xa1b23c4d, 0x4d3cb2a1 or "
44 "0xd4c3b2a1)" % magic)
7aa697dd
BP
45
46 def read(self):
47 header = self.file.read(16)
48 if len(header) == 0:
49 return None
50 elif len(header) != 16:
51 raise PcapException("end of file within pcap record header")
52
53 ts_sec, ts_usec, incl_len, orig_len = struct.unpack(self.header_format,
54 header)
55 packet = self.file.read(incl_len)
56 if len(packet) != incl_len:
57 raise PcapException("end of file reading pcap packet data")
58 return packet
81a86b9a
JS
59
60
7aa697dd
BP
61argv0 = sys.argv[0]
62
bdca6c4b 63
7aa697dd 64def usage():
8ea171ab 65 print("""\
7aa697dd
BP
66%(argv0)s: print pcap file packet data as hex
67usage: %(argv0)s FILE
68where FILE is a PCAP file.
69
70The following options are also available:
71 -h, --help display this help message
72 -V, --version display version information\
8ea171ab 73""" % {'argv0': argv0})
7aa697dd
BP
74 sys.exit(0)
75
81a86b9a 76
7aa697dd
BP
77if __name__ == "__main__":
78 try:
79 try:
80 options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
81 ['help', 'version'])
f3068bff 82 except getopt.GetoptException as geo:
7aa697dd
BP
83 sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
84 sys.exit(1)
85
86 for key, value in options:
87 if key in ['-h', '--help']:
88 usage()
89 elif key in ['-V', '--version']:
8ea171ab 90 print("ovs-pcap (Open vSwitch) @VERSION@")
7aa697dd
BP
91 else:
92 sys.exit(0)
93
94 if len(args) != 1:
95 sys.stderr.write("%s: exactly 1 non-option argument required "
96 "(use --help for help)\n" % argv0)
97 sys.exit(1)
98
99 reader = PcapReader(args[0])
100 while True:
101 packet = reader.read()
102 if packet is None:
103 break
104
7d8eadce 105 print(binascii.hexlify(packet).decode().strip())
7aa697dd 106
f3068bff 107 except PcapException as e:
7aa697dd
BP
108 sys.stderr.write("%s: %s\n" % (argv0, e))
109 sys.exit(1)
110
111# Local variables:
112# mode: python
113# End: