-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmeshnet-lab-bridge.py
More file actions
executable file
·354 lines (271 loc) · 8.48 KB
/
meshnet-lab-bridge.py
File metadata and controls
executable file
·354 lines (271 loc) · 8.48 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/python
import socket
import os, os.path
import time
import sys
import json
import re
import subprocess
from collections import deque
if len(sys.argv) != 4:
print(f"Usage: {sys.argv[0]} <unix-socket-path> <meshnet-lab-installation-path> <graph-json-file>")
print()
print(f"Example: {sys.argv[0]} /tmp/sim_connector.sock ../meshnet-lab ./graph.json")
exit(1)
meshnetlab_path = sys.argv[1]
socket_path = sys.argv[2]
json_path = sys.argv[3]
if os.path.exists(socket_path):
os.remove(socket_path)
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind(socket_path)
os.chmod(socket_path, 0o777)
print(f"listen on {socket_path}")
def split(s):
return [x for x in s.split(",") if x]
def get_link_id(link):
s = link["source"]
t = link["target"]
if s > t:
return f"{t}-{s}"
else:
return f"{s}-{t}"
def get_link_map(graph):
links = {}
for link in graph["links"]:
links[get_link_id(link)] = link
return links
def get_node_map(graph):
nodes = {}
for node in graph["nodes"]:
nodes[node["id"]] = node
return nodes
def get_graph():
with open(json_path, "r") as file:
return json.load(file)
def print_and_send(conn, message):
print(message)
conn.send((message + "\n").encode())
def update_graph(conn, graph_new):
graph_old = get_graph()
node_ids_old_set = set(get_node_map(graph_old).keys())
node_ids_new_set = set(get_node_map(graph_new).keys())
node_ids_remove = node_ids_old_set.difference(node_ids_new_set)
node_ids_create = node_ids_new_set.difference(node_ids_old_set)
with open(json_path, "w") as file:
json.dump(graph_new, file, indent=' ', sort_keys=True)
# remove nodes => stop batman
if len(node_ids_remove) > 0:
subprocess.run([f"{meshnetlab_path}/software.py", "stop", "batman-adv"] + list(node_ids_remove))
subprocess.run([f"{meshnetlab_path}/network.py", "apply", json_path])
# create nodes => start batman
if len(node_ids_create) > 0:
subprocess.run([f"{meshnetlab_path}/software.py", "start", "batman-adv"] + list(node_ids_create))
print_and_send(conn, "done")
def convert_link_ids(links_ids):
# map [(source, target), ...] to list of link ids
ret = []
for i in range(0, len(links_ids), 2):
if (i+1) < len(links_ids):
source = links_ids[i]
target = links_ids[i+1]
ret.append(get_link_id({"source": source, "target": target}))
return ret
def remove(conn, remove_node_ids, _remove_link_ids):
remove_link_ids = convert_link_ids(_remove_link_ids)
graph = get_graph()
def delete_node(node):
return node["id"] in remove_node_ids
def delete_link(link):
return (link["source"] in remove_node_ids or link["target"] in remove_node_ids) or get_link_id(link) in remove_link_ids
# remove nodes
graph["nodes"] = [node for node in graph["nodes"] if not delete_node(node)]
# remove links
graph["links"] = [link for link in graph["links"] if not delete_link(link)]
update_graph(conn, graph)
def disconnect_nodes(conn, node_ids):
if len(node_ids) != 2:
print(f"Expected two links! Not {len(node_ids)}")
return
source_id = node_ids[0]
target_id = node_ids[1]
print(f"disconnect {source_id} and {target_id}")
graph = get_graph()
link = {"source": source_id, "target": target_id}
link_id = get_link_id(link)
nodes = get_node_map(graph)
links = get_link_map(graph)
if source_id not in nodes:
print(f"node does not exist {source_id}")
return
if target_id not in nodes:
print(f"node does not exist {target_id}")
return
if link_id not in links:
print("link does not exist")
return
del links[link_id]
graph["links"] = list(links.values())
update_graph(conn, graph)
def connect_nodes(conn, node_ids):
if len(node_ids) != 2:
print(f"Expected two links! Not {len(node_ids)}")
return
source_id = node_ids[0]
target_id = node_ids[1]
print(f"connect {source_id} and {target_id}")
graph = get_graph()
link = {"source": source_id, "target": target_id}
link_id = get_link_id(link)
nodes = get_node_map(graph)
links = get_link_map(graph)
if source_id not in nodes:
print(f"node does not exist {source_id}")
return
if target_id not in nodes:
print(f"node does not exist {target_id}")
return
if link_id in links:
print("link exists")
return
graph["links"].append(link)
update_graph(conn, graph)
def add_node(conn):
print(f"add node")
graph = get_graph()
nodes = get_node_map(graph)
node_id = None
for i in range(0, 1000):
node_id = f'{i:04x}'
if node_id not in nodes:
break
node = {"id": node_id}
graph["nodes"].append(node)
update_graph(conn, graph)
def get_node_info(conn, node_id):
graph = get_graph()
nodes = get_node_map(graph)
if node_id in nodes:
#print(f"get node info for {node_id}")
command = ['ip', 'netns', 'exec', f'ns-{node_id}', 'batctl', 'o']
result = subprocess.run(command, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
conn.send(result.stdout)
else:
print_and_send(conn, f"Node does not exist: {node_id}")
def change_property(conn, node_ids, _link_ids, key, value):
if key == "id":
print_and_send(conn, f"Bad idea to change id field => denied")
return
link_ids = convert_link_ids(_link_ids)
if len(node_ids) == 0 and len(link_ids) == 0:
print_and_send(conn, f"Nothing selected")
return
graph = get_graph()
nodes = get_node_map(graph)
links = get_link_map(graph)
for node_id in node_ids:
if node_id not in nodes:
print_and_send(conn, f"Node does not exist: {node_id}")
return
for link_id in link_ids:
if link_id not in links:
print_and_send(conn, f"Link does not exist: {link_id}")
return
def is_float(string):
try:
float(string)
return True
except ValueError:
return False
def get_typed_value(value):
if value is None:
return None
elif value.startswith('"') and value.endswith('"'):
return value[1:-1]
elif value == "{}":
return {}
elif value == "[]":
return []
elif value.isnumeric():
return int(value)
elif is_float(value):
return float(value)
elif value == "true":
return True
elif value == "false":
return False
else:
return value
typed_value = get_typed_value(value)
for node_id in node_ids:
if typed_value is None:
del nodes[node_id][key]
else:
nodes[node_id][key] = typed_value
for link_id in link_ids:
if typed_value is None:
print(f"remove link_id: {link_id}, key: {key}")
del links[link_id][key]
else:
links[link_id][key] = typed_value
update_graph(conn, graph)
connect_nodes_re = re.compile("connect_nodes '(.*)'")
disconnect_nodes_re = re.compile("disconnect_nodes '(.*)'")
remove_re = re.compile("remove '(.*)' '(.*)'")
add_node_re = re.compile("add_node")
get_node_info_re = re.compile("get_node_info '(.*)'")
set_property_re = re.compile("set '(.*)' '(.*)' '(.*)' '(.*)'")
unset_property_re = re.compile("unset '(.*)' '(.*)' '(.*)'")
while True:
server.listen(1)
conn, addr = server.accept()
datagram = conn.recv(1024)
if datagram:
try:
text = datagram.decode("ascii")
print(text)
m = connect_nodes_re.fullmatch(text)
if m:
node_ids = split(m.group(1))
connect_nodes(conn, node_ids)
continue
m = disconnect_nodes_re.fullmatch(text)
if m:
node_ids = split(m.group(1))
disconnect_nodes(conn, node_ids)
continue
m = remove_re.fullmatch(text)
if m:
node_ids = split(m.group(1))
link_ids = split(m.group(2))
remove(conn, node_ids, link_ids)
continue
m = add_node_re.fullmatch(text)
if m:
add_node(conn)
continue
m = get_node_info_re.fullmatch(text)
if m:
node_id = m.group(1)
get_node_info(conn, node_id)
continue
m = set_property_re.fullmatch(text)
if m:
node_ids = split(m.group(1))
link_ids = split(m.group(2))
key = m.group(3)
value = m.group(4)
change_property(conn, node_ids, link_ids, key, value)
continue
m = unset_property_re.fullmatch(text)
if m:
node_ids = split(m.group(1))
link_ids = split(m.group(2))
key = m.group(3)
change_property(conn, node_ids, link_ids, key, None)
continue
conn.send(f"Unknown command: {text}\n".encode())
print(f"unknown command: {text}")
except Exception as e:
conn.send(f"Error: {e}\n".encode())
print(f"Error: {e}")