#!/bin/sh setglobal IFNAME = $1 setglobal CMD = $2 proc kill_daemon { setglobal NAME = $1 setglobal PF = $2 if test ! -r $PF { return } setglobal PID = $[cat $PF] if test $PID -gt 0 { if ps $PID | grep -q $NAME { kill $PID } } rm $PF } if test $CMD = "P2P-GROUP-STARTED" { setglobal GIFNAME = $3 if test $4 = "GO" { kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid rm /var/run/dhclient.leases-$GIFNAME kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME ifconfig $GIFNAME 192.168.42.1 up if ! dnsmasq -x /var/run/dnsmasq.pid-$GIFNAME \ -i $GIFNAME \ -F192.168.42.11,192.168.42.99 { # another dnsmasq instance may be running and blocking us; try to # start with -z to avoid that dnsmasq -x /var/run/dnsmasq.pid-$GIFNAME \ -i $GIFNAME \ -F192.168.42.11,192.168.42.99 --listen-address 192.168.42.1 -z -p 0 } } if test $4 = "client" { kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid rm /var/run/dhclient.leases-$GIFNAME kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME setglobal ipaddr = $[echo "$ifsjoin(ARGV)" | sed 's/.* ip_addr=\([^ ]*\).*/\1/] setglobal ipmask = $[echo "$ifsjoin(ARGV)" | sed 's/.* ip_mask=\([^ ]*\).*/\1/] setglobal goipaddr = $[echo "$ifsjoin(ARGV)" | sed 's/.* go_ip_addr=\([^ ]*\).*/\1/] if echo "$ipaddr$ipmask$goipaddr" | grep -q ' ' { setglobal ipaddr = ''"" setglobal ipmask = ''"" setglobal goipaddr = ''"" } if test -n $ipaddr { sudo ifconfig $GIFNAME $ipaddr netmask $ipmask sudo ip ro re default via $goipaddr exit 0 } dhclient -pf /var/run/dhclient-$GIFNAME.pid \ -lf /var/run/dhclient.leases-$GIFNAME \ -nw \ $GIFNAME } } if test $CMD = "P2P-GROUP-REMOVED" { setglobal GIFNAME = $3 if test $4 = "GO" { kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME ifconfig $GIFNAME 0.0.0.0 } if test $4 = "client" { kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid rm /var/run/dhclient.leases-$GIFNAME ifconfig $GIFNAME 0.0.0.0 } } if test $CMD = "P2P-CROSS-CONNECT-ENABLE" { setglobal GIFNAME = $3 setglobal UPLINK = $4 # enable NAT/masquarade $GIFNAME -> $UPLINK iptables -P FORWARD DROP iptables -t nat -A POSTROUTING -o $UPLINK -j MASQUERADE iptables -A FORWARD -i $UPLINK -o $GIFNAME -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A FORWARD -i $GIFNAME -o $UPLINK -j ACCEPT sysctl net.ipv4.ip_forward=1 } if test $CMD = "P2P-CROSS-CONNECT-DISABLE" { setglobal GIFNAME = $3 setglobal UPLINK = $4 # disable NAT/masquarade $GIFNAME -> $UPLINK sysctl net.ipv4.ip_forward=0 iptables -t nat -D POSTROUTING -o $UPLINK -j MASQUERADE iptables -D FORWARD -i $UPLINK -o $GIFNAME -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -D FORWARD -i $GIFNAME -o $UPLINK -j ACCEPT }