13 #include "../src/meshlink.h"
14 #include "netns_utils.h"
19 /// Create meshlink instances and network namespaces for a list of peers
20 static void create_peers(peer_config_t *peers, int npeers, const char *prefix) {
21 // We require root for network namespaces
26 for(int i = 0; i < npeers; i++) {
27 assert(asprintf(&peers[i].netns_name, "%s%d", prefix, i) > 0);
29 assert(asprintf(&command,
30 "/bin/ip netns delete %1$s 2>/dev/null || true;"
31 "/bin/ip netns add %1$s;"
32 "/bin/ip netns exec %1$s ip link set dev lo up;",
33 peers[i].netns_name));
35 assert(system(command) == 0);
38 char *netns_path = NULL;
39 assert(asprintf(&netns_path, "/run/netns/%s", peers[i].netns_name));
41 peers[i].netns = open(netns_path, O_RDONLY);
42 assert(peers[i].netns != -1);
45 char *conf_path = NULL;
46 assert(asprintf(&conf_path, "%s_conf.%d", prefix, i + 1) > 0);
48 assert(meshlink_destroy(conf_path));
50 meshlink_open_params_t *params = meshlink_open_params_init(conf_path, peers[i].name, prefix, peers[i].devclass);
52 assert(meshlink_open_params_set_netns(params, peers[i].netns));
54 peers[i].mesh = meshlink_open_ex(params);
55 assert(peers[i].mesh);
59 meshlink_enable_discovery(peers[i].mesh, false);
63 /// Set up a LAN topology where all peers can see each other directly
64 static void setup_lan_topology(peer_config_t *peers, int npeers) {
65 // Set up the LAN bridge
68 assert(asprintf(&command,
69 "/bin/ip netns exec %1$s /bin/ip link add eth0 type bridge;"
70 "/bin/ip netns exec %1$s /bin/ip link set eth0 up;",
71 peers[0].netns_name));
73 assert(system(command) == 0);
76 // Add an interface to each peer that is connected to the bridge
77 for(int i = 1; i < npeers; i++) {
79 assert(asprintf(&command,
80 "/bin/ip netns exec %1$s /bin/ip link add eth0 type veth peer eth%3$d netns %2$s;"
81 "/bin/ip netns exec %1$s /bin/ip link set dev eth0 up;"
82 "/bin/ip netns exec %2$s /bin/ip link set dev eth%3$d master eth0 up;",
83 peers[i].netns_name, peers[0].netns_name, i));
85 assert(system(command) == 0);
89 // Configure addresses
90 for(int i = 0; i < npeers; i++) {
91 change_peer_ip(&peers[i]);
95 /// Give a peer a unique IP address
96 void change_peer_ip(peer_config_t *peer) {
98 assert(asprintf(&command,
99 "/bin/ip netns exec %1$s ip addr flush dev eth0;"
100 "/bin/ip netns exec %1$s ip addr add 203.0.113.%2$d/24 dev eth0;",
101 peer->netns_name, ip));
104 assert(system(command) == 0);
108 /// Let the first peer in a list invite all the subsequent peers
109 static void link_peers(peer_config_t *peers, int npeers) {
110 for(int i = 1; i < npeers; i++) {
111 char *export_relay = meshlink_export(peers[0].mesh);
112 char *export_peer = meshlink_export(peers[i].mesh);
113 assert(export_relay);
115 assert(meshlink_import(peers[0].mesh, export_peer));
116 assert(meshlink_import(peers[i].mesh, export_relay));
122 /// Close meshlink instances and clean up
123 static void close_peers(peer_config_t *peers, int npeers) {
124 for(int i = 0; i < npeers; i++) {
125 meshlink_close(peers[i].mesh);
126 close(peers[i].netns);
127 free(peers[i].netns_name);
131 /// Set up relay, peer and NUT that are directly connected
132 peer_config_t *setup_relay_peer_nut(const char *prefix) {
133 static peer_config_t peers[] = {
134 {"relay", DEV_CLASS_BACKBONE},
135 {"peer", DEV_CLASS_STATIONARY},
136 {"nut", DEV_CLASS_STATIONARY},
139 create_peers(peers, 3, prefix);
140 setup_lan_topology(peers, 3);
141 link_peers(peers, 3);
146 void close_relay_peer_nut(peer_config_t *peers) {
147 close_peers(peers, 3);