summaryrefslogtreecommitdiff
path: root/contrib/scripts/netjail/netjail_setup_internet.sh
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/scripts/netjail/netjail_setup_internet.sh')
-rwxr-xr-xcontrib/scripts/netjail/netjail_setup_internet.sh92
1 files changed, 38 insertions, 54 deletions
diff --git a/contrib/scripts/netjail/netjail_setup_internet.sh b/contrib/scripts/netjail/netjail_setup_internet.sh
index 6ae047274..c9a6fd6d8 100755
--- a/contrib/scripts/netjail/netjail_setup_internet.sh
+++ b/contrib/scripts/netjail/netjail_setup_internet.sh
@@ -1,4 +1,10 @@
#!/bin/sh
+
+NAMESPACE_FD=$(mktemp)
+INTERFACE_FD=$(mktemp)
+
+trap "rm -f $NAMESPACE_FD $INTERFACE_FD; exit" ERR EXIT
+
. "./netjail_core.sh"
set -eu
@@ -6,6 +12,7 @@ set -x
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
+PREFIX=$PPID
LOCAL_M=$1
GLOBAL_N=$2
@@ -32,8 +39,6 @@ if [ $STUN -gt 0 ]; then
netjail_check_bin stunserver
shift 1
-
- STUN_NODE=$(netjail_print_name "S" 254)
fi
netjail_check_bin $1
@@ -45,43 +50,33 @@ KNOWN_GROUP="92.68.151"
CLEANUP=0
echo "Start [local: $LOCAL_GROUP.0/24, global: $GLOBAL_GROUP.0/16, stun: $STUN]"
-NETWORK_NET=$(netjail_print_name "n" $GLOBAL_N $LOCAL_M)
-
-netjail_bridge $NETWORK_NET
+NETWORK_NET=$(netjail_bridge)
for X in $(seq $KNOWN); do
- KNOWN_NODE=$(netjail_print_name "K" $X)
-
- netjail_node $KNOWN_NODE
- netjail_node_link_bridge $KNOWN_NODE $NETWORK_NET "$KNOWN_GROUP.$X" 16
+ KNOWN_NODES[$X]=$(netjail_node)
+ KNOWN_LINKS[$X]=$(netjail_node_link_bridge ${KNOWN_NODES[$X]} $NETWORK_NET "$KNOWN_GROUP.$X" 16)
done
-for N in $(seq $GLOBAL_N); do
- ROUTER=$(netjail_print_name "R" $N)
-
- netjail_node $ROUTER
- netjail_node_link_bridge $ROUTER $NETWORK_NET "$GLOBAL_GROUP.$N" 16
-
- ROUTER_NET=$(netjail_print_name "r" $N)
+declare -A NODES
+declare -A NODE_LINKS
- netjail_bridge $ROUTER_NET
+for N in $(seq $GLOBAL_N); do
+ ROUTERS[$N]=$(netjail_node)
+ NETWORK_LINKS[$N]=$(netjail_node_link_bridge ${ROUTERS[$N]} $NETWORK_NET "$GLOBAL_GROUP.$N" 16)
+ ROUTER_NETS[$N]=$(netjail_bridge)
for M in $(seq $LOCAL_M); do
- NODE=$(netjail_print_name "N" $N $M)
-
- netjail_node $NODE
- netjail_node_link_bridge $NODE $ROUTER_NET "$LOCAL_GROUP.$M" 24
+ NODES[$N,$M]=$(netjail_node)
+ NODE_LINKS[$N,$M]=$(netjail_node_link_bridge ${NODES[$N,$M]} ${ROUTER_NETS[$N]} "$LOCAL_GROUP.$M" 24)
done
ROUTER_ADDR="$LOCAL_GROUP.$(($LOCAL_M+1))"
-
- netjail_node_link_bridge $ROUTER $ROUTER_NET $ROUTER_ADDR 24
- netjail_node_add_nat $ROUTER $ROUTER_ADDR 24
+ ROUTER_LINKS[$N]=$(netjail_node_link_bridge ${ROUTERS[$N]} ${ROUTER_NETS[$N]} $ROUTER_ADDR 24)
+
+ netjail_node_add_nat ${ROUTERS[$N]} $ROUTER_ADDR 24
for M in $(seq $LOCAL_M); do
- NODE=$(netjail_print_name "N" $N $M)
-
- netjail_node_add_default $NODE $ROUTER_ADDR
+ netjail_node_add_default ${NODES[$N,$M]} $ROUTER_ADDR
done
done
@@ -89,67 +84,56 @@ WAITING=""
KILLING=""
if [ $STUN -gt 0 ]; then
- netjail_node $STUN_NODE
- netjail_node_link_bridge $STUN_NODE $NETWORK_NET "$GLOBAL_GROUP.254" 16
+ STUN_NODE=$(netjail_node)
+ STUN_LINK=$(netjail_node_link_bridge $STUN_NODE $NETWORK_NET "$GLOBAL_GROUP.254" 16)
netjail_node_exec $STUN_NODE 0 1 stunserver &
KILLING="$!"
fi
for X in $(seq $KNOWN); do
- KNOWN_NODE=$(netjail_print_name "K" $X)
INDEX=$(($X - 1))
-
+
FD_X=$(($INDEX * 2 + 3 + 0))
FD_Y=$(($INDEX * 2 + 3 + 1))
- netjail_node_exec $KNOWN_NODE $FD_X $FD_Y $@ &
+ 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
- NODE=$(netjail_print_name "N" $N $M)
INDEX=$(($LOCAL_M * ($N - 1) + $M - 1 + $KNOWN))
-
+
FD_X=$(($INDEX * 2 + 3 + 0))
FD_Y=$(($INDEX * 2 + 3 + 1))
- netjail_node_exec $NODE $FD_X $FD_Y $@ &
+ netjail_node_exec ${NODES[$N,$M]} $FD_X $FD_Y $@ &
WAITING="$! $WAITING"
done
done
cleanup() {
if [ $STUN -gt 0 ]; then
- STUN_NODE=$(netjail_print_name "S" 254)
-
- netjail_node_unlink_bridge $STUN_NODE $NETWORK_NET
+ netjail_node_unlink_bridge $STUN_LINK
netjail_node_clear $STUN_NODE
fi
for X in $(seq $KNOWN); do
- KNOWN_NODE=$(netjail_print_name "K" $X)
-
- netjail_node_unlink_bridge $KNOWN_NODE $NETWORK_NET
- netjail_node_clear $KNOWN_NODE
+ netjail_node_unlink_bridge ${KNOWN_LINKS[$X]}
+ netjail_node_clear ${KNOWN_NODES[$X]}
done
for N in $(seq $GLOBAL_N); do
- ROUTER_NET=$(netjail_print_name "r" $N)
-
for M in $(seq $LOCAL_M); do
- NODE=$(netjail_print_name "N" $N $M)
-
- netjail_node_unlink_bridge $NODE $ROUTER_NET
- netjail_node_clear $NODE
+ netjail_node_unlink_bridge ${NODE_LINKS[$N,$M]}
+ netjail_node_clear ${NODES[$N,$M]}
done
- ROUTER=$(netjail_print_name "R" $N)
-
- netjail_bridge_clear $ROUTER_NET
- netjail_node_unlink_bridge $ROUTER $NETWORK_NET
- netjail_node_clear $ROUTER
+ 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
@@ -162,7 +146,7 @@ trapped_cleanup() {
cleanup
}
-trap 'trapped_cleanup' 2
+trap 'trapped_cleanup' ERR
netjail_waitall $WAITING
netjail_killall $KILLING