-
Notifications
You must be signed in to change notification settings - Fork 0
/
routegrammar.py
309 lines (246 loc) · 8.38 KB
/
routegrammar.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#
# $Id, routegrammar.py 14 2012-06-07 02:46:19Z nickw $
#
# NAME, routegrammar.py
#
# AUTHOR, Nick Whalen <[email protected]>
# COPYRIGHT, 2012 by Nick Whalen
# LICENSE:
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# DESCRIPTION:
#
#
import sys
import parsenode
import cidrize
# -------- NODE_SPEC --------
class NODE_SPEC_Error(Exception):
pass
class NODE_SPEC(parsenode.ParseNode):
"""
Defines the 'NODE_SPEC' segment of the iproute2 routing grammar.
"""
# Defined by iproute2's grammar
types = ('unicast', 'local', 'broadcast', 'multicast', 'throw', 'unreachable', 'prohibit', 'blackhole', 'nat')
options = ('tos','table','proto','scope','metric')
# NODE_SPEC variables/options
TYPE = None
PREFIX = None
tos = None
table = None
proto = None
scope = None
metric = None
def __init__(self, tokens):
"""
Constructor. Just calls the parent constructor (where all the interesting stuff lives).
:param tokens:
"""
super(NODE_SPEC,self).__init__(tokens)
#---
def parse(self, tokens):
"""
Parses the NODE_SPEC part of a route (as defined by iproute2)
:param tokens:
:return, Array of tokens that were not used by the parser.
"""
# Type is optional
if tokens[0] in self.types:
self.TYPE = tokens[0]
self._addRawSegment(self.TYPE) # Make sure we have the string segment stored
tokens.remove(tokens[0])
# PREFIX validation
error_txt = self.validatePrefix(tokens[0])
if not error_txt:
self.PREFIX = tokens[0]
self._addRawSegment(self.PREFIX) # Make sure we have the string segment stored
tokens.remove(tokens[0])
else:
raise NODE_SPEC_Error("Prefix (%s) did not pass validation, %s" %(tokens[0], error_txt))
# Option parsing
new_token_list = list(tokens)
matched_option = False
for token in tokens:
# If we matched a token, it had a parameter we need to ignore
if matched_option:
matched_option = False
continue
# If the token is matched, store it
if token in self.options:
self[token] = tokens[tokens.index(token)+1]
self._addRawSegment(token)
self._addRawSegment(self[token])
new_token_list.pop(new_token_list.index(token)+1) # remove option parameter
new_token_list.remove(token) # remove the option from the list
matched_option = True
# Clean up raw_data
self.raw_data = self.raw_data.strip()
return new_token_list
#---
def validatePrefix(self, prefix):
"""
Validates an Internet network or ip address (/32).
:param prefix, The network in CIDR notation.
:return, Text of error from cidrize on error, otherwise None.
"""
try:
cidrize.cidrize(prefix)
except cidrize.CidrizeError:
return sys.exc_value
else:
return None
#---
#---
# -------- NH --------
class NH(parsenode.ParseNode):
"""
Defines the 'NH' segment of the iproute2 routing grammar.
"""
options = ('via', 'dev', 'weight')
flags = ('onlink', 'pervasive')
# NH variables/options
NHFLAGS = None
via = None
dev = None
weight = None
def __init__(self, tokens):
"""
"""
super(NH,self).__init__(tokens)
#---
def parse(self, tokens):
"""
Parses the NH part of a route (as defined by iproute2)
:param tokens:
:return, Array of tokens that were not used by the parser.
"""
# NHFLAGS is optional
if tokens[0] in self.flags:
self.NHFLAGS = tokens[0]
self._addRawSegment(self.NHFLAGS) # Make sure we have the string segment stored
tokens.remove(tokens[0])
# Option parsing
new_token_list = list(tokens)
matched_option = False
for token in tokens:
# If we matched a token, it had a parameter we need to ignore
if matched_option:
matched_option = False
continue
# If the token is matched, store it
if token in self.options:
self[token] = tokens[tokens.index(token)+1]
self._addRawSegment(token)
self._addRawSegment(self[token])
new_token_list.pop(new_token_list.index(token)+1) # remove option parameter
new_token_list.remove(token) # remove the option from the list
matched_option = True
# Clean up raw_data
self.raw_data = self.raw_data.strip()
return new_token_list
#---
#----
# -------- OPTIONS --------
class OPTIONS(parsenode.ParseNode):
"""
Defines the 'OPTIONS' segment of the iproute2 routing grammar.
"""
options = ('mtu', 'advmss','rtt','rttvar','reordering','window','cwnd','initcwnd','ssthresh','realms','src',
'rto_min','hoplimit','initrwnd')
# OPTIONS variables/options
mtu = None
advmss = None
rtt = None
rttvar = None
reordering = None
window = None
cwnd = None
initcwnd = None
ssthresh = None
realms = None
src = None
rto_min = None
hoplimit = None
initrwnd = None
def __init__(self, tokens):
"""
"""
super(OPTIONS,self).__init__(tokens)
#---
def parse(self, tokens):
"""
Parses the OPTIONS part of a route (as defined by iproute2)
:param tokens:
:return, Array of tokens that were not used by the parser.
"""
# Option parsing
new_token_list = list(tokens)
matched_option = False
for token in tokens:
# If we matched a token, it had a parameter we need to ignore
if matched_option:
matched_option = False
continue
# If the token is matched, store it
if token in self.options:
self[token] = tokens[tokens.index(token)+1]
self._addRawSegment(token)
self._addRawSegment(self[token])
new_token_list.pop(new_token_list.index(token)+1) # remove option parameter
new_token_list.remove(token) # remove the option from the list
matched_option = True
# Clean up raw_data
self.raw_data = self.raw_data.strip()
return new_token_list
#---
#----
# -------- INFO_SPEC --------
class INFO_SPEC_Error(Exception):
pass
class INFO_SPEC(parsenode.ParseNode):
"""
Defines the 'INFO_SPEC' segment of the iproute2 routing grammar.
"""
#TODO: This is reference to NH according to the grammar, and there can be multiples. Fix it to support this.
nexthop = None
def __init__(self, tokens):
super(INFO_SPEC,self).__init__(tokens, [NH, OPTIONS])
#---
def parse(self, tokens):
return tokens
#---
#---
# -------- ROUTE --------
class ROUTE(parsenode.ParseNode):
"""
Defines the 'ROUTE' segment of the the iproute2 routing grammar.
"""
actions = ('add', 'del', 'change', 'append', 'replace', 'monitor')
action = None
def __init__(self, tokens):
super(ROUTE,self).__init__(tokens, [NODE_SPEC, INFO_SPEC])
#---
def parse(self, tokens):
"""
Parses the ROUTE part of an iproute2 routing entry.
:param tokens:
:return, Array of tokens that were not used by the parser.
"""
if tokens[0] in self.actions:
self.action = tokens[0]
self._addRawSegment(self.action) # Make sure we have the string segment stored
tokens.remove(tokens[0])
return tokens
#---
#---