-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbmp_filter.py
executable file
·191 lines (161 loc) · 5.13 KB
/
bmp_filter.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python
"""
Copyright (c) 2022 Marco Tollini <[email protected]>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""
import argparse
import mmap
message_type_map = {
0: "Route Monitoring",
1: "Statistics Report",
2: "Peer Down Notification",
3: "Peer Up Notification",
4: "Initiation Message",
5: "Termination Message",
6: "Route Mirroring Message",
}
class Statistics:
def __init__(self):
self.kept = {
0: 0,
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
6: 0,
}
self.discarded = {
0: 0,
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
6: 0,
}
def print(self):
print(f'### PACKETS KEPT ###')
print(f'Route Monitoring:\t\t{self.kept[0]}')
print(f'Statistics Report:\t\t{self.kept[1]}')
print(f'Peer Down Notification:\t\t{self.kept[2]}')
print(f'Peer Up Notification:\t\t{self.kept[3]}')
print(f'Initiation Message:\t\t{self.kept[4]}')
print(f'Termination Message:\t\t{self.kept[5]}')
print(f'Route Mirroring Message:\t{self.kept[6]}')
print(f'### PACKETS DISCARDED ###')
print(f'Route Monitoring:\t\t{self.discarded[0]}')
print(f'Statistics Report:\t\t{self.discarded[1]}')
print(f'Peer Down Notification:\t\t{self.discarded[2]}')
print(f'Peer Up Notification:\t\t{self.discarded[3]}')
print(f'Initiation Message:\t\t{self.discarded[4]}')
print(f'Termination Message:\t\t{self.discarded[5]}')
print(f'Route Mirroring Message:\t{self.discarded[6]}')
def parse_args():
parser = argparse.ArgumentParser(
description='Filter BMP message from record')
parser.add_argument(
'-i',
'--input',
dest="input",
type=str,
required=True,
help="Input file",
)
parser.add_argument(
'-o',
'--output',
dest="output",
type=str,
required=True,
help="Output file",
)
parser.add_argument(
'--route-monitoring',
dest="rm",
action='store_true',
help="Include router monitoring messages",
)
parser.add_argument(
'--statistics-report',
dest="stats",
action='store_true',
help="Include Statistics Report messages",
)
parser.add_argument(
'--peer-down',
dest="pd",
action='store_true',
help="Include Peer Down Notification messages",
)
parser.add_argument(
'--peer-up',
dest="pu",
action='store_true',
help="Include Peer Up Notification messages",
)
parser.add_argument(
'--initiation',
dest="init",
action='store_true',
help="Include Initiation Message messages",
)
parser.add_argument(
'--termination',
dest="term",
action='store_true',
help="Include Termination Message messages",
)
parser.add_argument(
'-m',
'--route-mirroring',
dest="rmirror",
action='store_true',
help="Include Route Mirroring messages",
)
args = parser.parse_args()
return args
def args_to_filter_map(args):
return {
0: not args.rm,
1: not args.stats,
2: not args.pd,
3: not args.pu,
4: not args.init,
5: not args.term,
6: not args.rmirror,
}
def decode_common_header(header):
version = int(header[0])
msg_length = int.from_bytes(header[1:5], byteorder='big', signed=False)
msg_type = header[5]
return version, msg_length, msg_type
def main():
args = parse_args()
filter_map = args_to_filter_map(args)
statistics = Statistics()
with open(args.input, 'rb') as inp:
with mmap.mmap(inp.fileno(), 0, access=mmap.ACCESS_READ) as inp_map:
with open(args.output, 'wb') as out:
head = 0
while head < len(inp_map):
version, msg_length, msg_type = decode_common_header(
inp_map[head:head+6])
if not filter_map[msg_type]:
statistics.kept[msg_type] += 1
out.write(inp_map[head:head+msg_length])
else:
statistics.discarded[msg_type] += 1
head += msg_length
statistics.print()
if __name__ == '__main__':
main()