-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.py
169 lines (135 loc) · 4.96 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from sys import stdout
from socket import ntohl
from math import floor, ceil, log
ether_type_description = { 0x0800 : 'IP',
0x0806 : 'ARP',
0x8100 : '802.1Q(VLAN)',
0x86DD : 'IPv6' }
def ether_type_to_string(ether_type):
if ether_type in ether_type_description:
return ether_type_description[ether_type]
else:
return 'unknown(%04X)' % ether_type
def mac_to_string(mac):
"""Returns an Ethernet MAC address in the form
XX:XX:XX:XX:XX:XX."""
return ('%02X:%02X:%02X:%02X:%02X:%02X' %
(mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]))
def ip_to_string(ip):
"""Returns ip as a string in dotted quad notation."""
# ip = ntohl(ip) # network byte order is big-endian
return '%d.%d.%d.%d' % (ip & 0xff,
(ip >> 8) & 0xff,
(ip >> 16) & 0xff,
(ip >> 24) & 0xff)
ip_proto_name = { 0 : 'ip',
1 : 'icmp',
2 : 'igmp',
3 : 'ggp',
4 : 'ipencap',
5 : 'st',
6 : 'tcp',
8 : 'egp',
9 : 'igp',
12 : 'pup',
17 : 'udp',
20 : 'hmp',
22 : 'xns-idp',
27 : 'rdp',
29 : 'iso-tp4',
36 : 'xtp',
37 : 'ddp',
38 : 'idpr-cmtp',
41 : 'ipv6',
43 : 'ipv6-route',
44 : 'ipv6-frag',
45 : 'idrp',
46 : 'rsvp',
47 : 'gre',
50 : 'esp',
51 : 'ah',
57 : 'skip',
58 : 'ipv6-icmp',
59 : 'ipv6-nonxt',
60 : 'ipv6-opts',
73 : 'rspf',
81 : 'vmtp',
88 : 'eigrp',
89 : 'ospf',
93 : 'ax.25',
94 : 'ipip',
97 : 'etherip',
98 : 'encap',
103 : 'pim',
108 : 'ipcomp',
112 : 'vrrp',
115 : 'l2tp',
124 : 'isis',
132 : 'sctp',
133 : 'fc',
136 : 'udplite' }
def ip_proto_to_string(proto):
if proto in ip_proto_name:
return ip_proto_name[proto]
else:
return 'unknown(%d)' % proto
def speed_to_string(speed):
speed_name = { 10000000 : '10Mb',
100000000 : '100Mb',
1000000000 : '1Gb',
10000000000 : '10Gb' }
if speed in speed_name:
return speed_name[speed]
else:
return str(speed)
def hexdump_escape(c):
"""Returns c if its ASCII code is in [32,126]."""
if 32 <= ord(c) <= 126:
return c
else:
return '.'
def hexdump_bytes(buf, stream=stdout):
"""Prints a 'classic' hexdump, ie two blocks of 8 bytes per line,
to stream."""
# Various values that determine the formatting of the hexdump.
# - col_fmt is the format used for an individual value
# - col_width gives the width of an individual value
# - off_fmt determines the formatting of the byte offset displayed
# on the left.
# - sep1_width determines how much whitespaces is inserted between
# columns 8 and 9.
# - sep2_width determines the amount of whitespace between column
# 16 and the ASCII column on the right
col_fmt = '%02X '
col_width = 3
off_fmt = '%%0%dX ' % int(ceil(log(len(buf), 16)))
sep1_width = 3
sep2_width = 5
# Print all complete 16-byte chunks.
for blk_idx in range(len(buf) // 16):
stream.write(off_fmt % (blk_idx * 16))
for offset in range(8):
stream.write(col_fmt % buf[blk_idx * 16 + offset])
stream.write(' ' * sep1_width)
for offset in range(8, 16):
stream.write(col_fmt % buf[blk_idx * 16 + offset])
stream.write(' ' * sep2_width)
for offset in range(16):
c = chr(buf[blk_idx * 16 + offset])
stream.write('%c' % hexdump_escape(c))
stream.write('\n')
# Print the remaining bytes.
if len(buf) % 16 > 0:
stream.write(off_fmt % (len(buf) - (len(buf) % 16)))
blk_off = len(buf) - len(buf) % 16
for offset in range(min(len(buf) % 16, 8)):
stream.write(col_fmt % buf[blk_off + offset])
stream.write(' ' * sep1_width)
for offset in range(8, len(buf) % 16):
stream.write(col_fmt % buf[blk_off + offset])
stream.write(' ' * ((16 - len(buf) % 16) * col_width))
stream.write(' ' * sep2_width)
for offset in range(len(buf) % 16):
c = chr(buf[len(buf) - (len(buf) // 16) * 16 + offset])
stream.write('%c' % hexdump_escape(c))
stream.write('\n')