-
Notifications
You must be signed in to change notification settings - Fork 10
/
common.py
288 lines (250 loc) · 9.92 KB
/
common.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
#!/usr/bin/python
import json
import config
from typing import List, Tuple
def read_jsondata(filename):
'''open file to load json data'''
try:
data = json.load(open(filename))
except IOError:
print ("Incorrect JSON file specification")
exit(0)
return data
def merge_padding(data):
'''merges padding field with the next field'''
for header_type in data["header_types"]:
# try except added to prevent falling into error when scalars_0 has 0 fields
try:
temp_list = [header_type["fields"][0]]
for i in range(1, len(header_type["fields"])):
if (temp_list[-1][0][:4] == "_pad"):
temp_list = temp_list[:-1]
temp_list.append(
[header_type["fields"][i][0], header_type["fields"][i-1][1]+header_type["fields"][i][1]])
else:
temp_list.append(header_type["fields"][i])
header_type["fields"] = temp_list
except:
pass
return data
def valid_state_name(state):
'''assign valid name to state depending on which header it extracts'''
if len(state["parser_ops"]) > 0:
if type(state["parser_ops"][0]["parameters"][0]["value"]) is list:
return state["parser_ops"][0]["parameters"][0]["value"][0]
else:
return state["parser_ops"][0]["parameters"][0]["value"]
else:
return state["name"]
def search_state(parser, name):
'''search for valid state name in the parse states'''
for state in parser["parse_states"]:
if (state["name"] == name):
return valid_state_name(state)
def search_header_type(header_types, name):
'''search for header type given the header_type_name specified in header definition'''
for header_type in header_types:
if (header_type["name"] == name):
return header_type
def make_control_graph(parsers: List[dict]) -> List[List]:
'''make a control graph for all possible state transitions
returns the list of edges in graph'''
graph = []
for parser in parsers:
for state in parser["parse_states"]:
name = valid_state_name(state)
if len(state["transition_key"]) > 0:
for transition in state["transitions"]:
if transition["next_state"] != None:
graph.append([name,
state["transition_key"][0]["value"][1],
transition["value"],
search_state(
parser, transition["next_state"])
])
else:
graph.append([name, None, None, "final"])
else:
if state["transitions"][0]["next_state"] != None:
graph.append([name, None, None, search_state(
parser, state["transitions"][0]["next_state"])])
else:
graph.append([name, None, None, "final"])
if (config.DEBUG):
print("\nEdges in the control_graph\n")
for i in graph:
print(i)
return graph
# TODO (pro-panda): define elements of list
def make_control_graph_multi(parsers: List[dict]) -> List[List]:
graph = []
for parser in parsers:
for state in parser["parse_states"]:
name = valid_state_name(state)
if len(state["transition_key"]) > 0:
for transition in state["transitions"]:
if transition["next_state"] != None:
# extract the headers which the transition is based on one of their fields
originHdr = [d["value"][0]
for d in state["transition_key"]]
if(len(set(originHdr)) != 1):
print(
"Error: Header transitions based on multiple fields from different headers are not supported.")
exit(1)
# extract the fields which the transition is based on
transition_fields = [d["value"][1]
for d in state["transition_key"]]
graph.append([name,
transition_fields,
transition["value"],
search_state(
parser, transition["next_state"])
])
else:
graph.append([name, None, None, "final"])
else:
if state["transitions"][0]["next_state"] != None:
graph.append([name, None, None, search_state(
parser, state["transitions"][0]["next_state"])])
else:
graph.append([name, None, None, "final"])
if (config.DEBUG):
print("\nEdges in the control_graph\n")
for i in graph:
print(i)
return graph
def spaces(count):
return (" " * count)
def gen_hex_mask(rbit_num, length):
return hex(int('0b' + '1'*length + '0'*rbit_num, 2))
def sanitize_headers(headers: List[dict]) -> Tuple[List[str], List[dict]]:
unique_headers = {}
for header in headers:
if header['metadata']:
continue
name = header['name']
if name.find('[') != (-1):
header['name'] = name[:name.find('[')]
unique_headers[header['name']] = header
return list(unique_headers.keys()), list(unique_headers.values())
def detect_builtin_hdr(headers: List[dict]) -> None:
for header_id in range(len(headers)):
if (headers[header_id]['name'] == 'ethernet'):
temp = input(
"\nEthernet header detected, would you like the standard ethernet header to be used(y/n) : ").strip()
if (temp == 'y'):
config.ETHER_DETECT = True
elif (headers[header_id]['name'] == 'ipv4'):
temp = input(
"\nIPv4 header detected, would you like the standard IPv4 header to be used(y/n) : ").strip()
if (temp == 'y'):
config.IPv4_DETECT = True
elif (headers[header_id]['name'] == 'ipv6'):
temp = input(
"\nIPv6 header detected, would you like the standard IPv6 header to be used(y/n) : ").strip()
if (temp == 'y'):
config.IPv6_DETECT = True
elif (headers[header_id]['name'] == 'tcp'):
temp = input(
"\nTCP header detected, would you like the standard TCP header to be used(y/n) : ").strip()
if (temp == 'y'):
config.TCP_DETECT = True
elif (headers[header_id]['name'] == 'udp'):
temp = input(
"\nUDP header detected, would you like the standard UDP header to be used(y/n) : ").strip()
if (temp == 'y'):
config.UDP_DETECT = True
return
def nibble(size):
if (size <= 8):
return 2
if (size <= 16):
return 4
if (size <= 24):
return 6
if (size <= 32):
return 8
if (size <= 40):
return 10
if (size <= 48):
return 12
if (size <= 56):
return 14
if (size <= 64):
return 16
raise ValueError("Size exceeds max limit for converting to nibble")
class State:
def __init__(self, name):
self.name = name
self.children = []
def print_state(self):
print("node's name: ", self.name)
print("node's children: ", [
self.children[i].name for i in range(len(self.children))])
def delete_obj(del_list, orig_list):
for item in del_list:
orig_list.remove(item)
def find_children(root, nodes):
if len(nodes) == 0:
return
else:
children = []
del_list = []
for node in nodes:
if node[0] == root.name:
children.append(node[-1])
del_list.append(node)
delete_obj(del_list, nodes)
children = set(children)
for child in children:
state = State(child)
find_children(state, nodes)
root.children.append(state)
return
def make_tree(graph):
paths = []
state_names = [edge[0] for edge in graph]
state_names = set(state_names)
non_roots = [edge[-1] for edge in graph]
non_roots = set(non_roots)
for name in state_names:
if name not in non_roots:
root = State(name)
find_children(root, graph)
paths.append(root)
root.print_state()
return paths
def find_ethernet(node, rmv_headers, sub_headers):
if (node.name == "ethernet" or node.name == "Ether") and config.ETHER_DETECT == True:
find_eth_subhdr(node, sub_headers)
return
elif len(node.children) == 0:
if node.name != "final":
rmv_headers.append(node.name)
return
else:
if node.name != "scalars" and node.name != "final":
rmv_headers.append(node.name)
for child in node.children:
find_ethernet(child, rmv_headers, sub_headers)
return
def find_eth_subhdr(node, sub_headers):
if len(node.children) == 0:
if node.name != "final":
sub_headers.append(node.name)
return
else:
for child in node.children:
if child.name != "final":
sub_headers.append(child.name)
find_eth_subhdr(child, sub_headers)
return
def is_builtin_header(header_port):
if ((config.ETHER_DETECT and header_port == 'ethernet')
or (config.IPv4_DETECT and header_port == 'ipv4')
or (config.IPv6_DETECT and header_port == 'ipv6')
or (config.TCP_DETECT and header_port == 'tcp')
or (config.UDP_DETECT and header_port == 'udp')):
return True
else:
return False