summaryrefslogtreecommitdiff
path: root/contrib/scripts/netjail/netjail_setup_internet.sh
blob: ea820f38cd6b762f20ffe802020cdf4174ab0705 (plain)
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
#!/bin/bash

. "./netjail_core.sh"

set -eu
set -x

export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

PREFIX=$PPID
LOCAL_M=$1
GLOBAL_N=$2

shift 2

netjail_check $(($LOCAL_M * $GLOBAL_N))

# Starts optionally an amount of nodes without NAT starting with "92.68.151.1"
netjail_opt '--known' $@
KNOWN=$RESULT
netjail_opts '--known' 0 $@
KNOWN_NUM=$RESULT

# Starts optionally 'stunserver' on "92.68.150.254":
netjail_opt '--stun' $@
STUN=$RESULT

if [ $KNOWN -gt 0 ]; then
	shift 2

	KNOWN=$KNOWN_NUM
	
	netjail_check $(($LOCAL_M * $GLOBAL_N + $KNOWN))
fi

if [ $STUN -gt 0 ]; then
	netjail_check_bin stunserver
	
	shift 1
fi

netjail_check_bin $1

LOCAL_GROUP="192.168.15"
GLOBAL_GROUP="92.68.150"
KNOWN_GROUP="92.68.151"

CLEANUP=0
echo "Start [local: $LOCAL_GROUP.0/24, global: $GLOBAL_GROUP.0/16, stun: $STUN]"

netjail_bridge
NETWORK_NET=$RESULT

for X in $(seq $KNOWN); do
	netjail_node
	KNOWN_NODES[$X]=$RESULT
	netjail_node_link_bridge ${KNOWN_NODES[$X]} $NETWORK_NET "$KNOWN_GROUP.$X" 16
	KNOWN_LINKS[$X]=$RESULT
done

declare -A NODES
declare -A NODE_LINKS

for N in $(seq $GLOBAL_N); do
	netjail_node
	ROUTERS[$N]=$RESULT
	netjail_node_link_bridge ${ROUTERS[$N]} $NETWORK_NET "$GLOBAL_GROUP.$N" 16
	NETWORK_LINKS[$N]=$RESULT
	netjail_bridge
	ROUTER_NETS[$N]=$RESULT
	
	for M in $(seq $LOCAL_M); do
		netjail_node
		NODES[$N,$M]=$RESULT
		netjail_node_link_bridge ${NODES[$N,$M]} ${ROUTER_NETS[$N]} "$LOCAL_GROUP.$M" 24
		NODE_LINKS[$N,$M]=$RESULT
	done

	ROUTER_ADDR="$LOCAL_GROUP.$(($LOCAL_M+1))"
	netjail_node_link_bridge ${ROUTERS[$N]} ${ROUTER_NETS[$N]} $ROUTER_ADDR 24
	ROUTER_LINKS[$N]=$RESULT
	
	netjail_node_add_nat ${ROUTERS[$N]} $ROUTER_ADDR 24
	
	for M in $(seq $LOCAL_M); do
		netjail_node_add_default ${NODES[$N,$M]} $ROUTER_ADDR
	done
done

WAITING=""
KILLING=""

if [ $STUN -gt 0 ]; then
	netjail_node
	STUN_NODE=$RESULT
	netjail_node_link_bridge $STUN_NODE $NETWORK_NET "$GLOBAL_GROUP.254" 16
	STUN_LINK=$RESULT

	netjail_node_exec $STUN_NODE 0 1 stunserver &
	KILLING="$!"
fi

for X in $(seq $KNOWN); do
	INDEX=$(($X - 1))

	FD_X=$(($INDEX * 2 + 3 + 0))
	FD_Y=$(($INDEX * 2 + 3 + 1))

	netjail_node_exec ${KNOWN_NODES[$X]} $FD_X $FD_Y $@ &
	WAITING="$! $WAITING"
done

for N in $(seq $GLOBAL_N); do
	for M in $(seq $LOCAL_M); do
		INDEX=$(($LOCAL_M * ($N - 1) + $M - 1 + $KNOWN))
		
		FD_X=$(($INDEX * 2 + 3 + 0))
		FD_Y=$(($INDEX * 2 + 3 + 1))

		netjail_node_exec ${NODES[$N,$M]} $FD_X $FD_Y $@ &
		WAITING="$! $WAITING"
	done
done

cleanup() {
	if [ $STUN -gt 0 ]; then
		netjail_node_unlink_bridge $STUN_LINK
		netjail_node_clear $STUN_NODE
	fi

	for X in $(seq $KNOWN); do
		netjail_node_unlink_bridge ${KNOWN_LINKS[$X]}
		netjail_node_clear ${KNOWN_NODES[$X]}
	done

	for N in $(seq $GLOBAL_N); do
		for M in $(seq $LOCAL_M); do
			netjail_node_unlink_bridge ${NODE_LINKS[$N,$M]}
			netjail_node_clear ${NODES[$N,$M]}
		done

		netjail_node_unlink_bridge ${ROUTER_LINKS[$N]}
		netjail_bridge_clear ${ROUTER_NETS[$N]}
		netjail_node_unlink_bridge ${NETWORK_LINKS[$N]}
		netjail_node_clear ${ROUTERS[$N]}
	done

	netjail_bridge_clear $NETWORK_NET
}

trapped_cleanup() {
	netjail_killall $WAITING
	netjail_killall $KILLING

	cleanup
}

trap 'trapped_cleanup' ERR

netjail_waitall $WAITING
netjail_killall $KILLING
wait

cleanup

echo "Done"