Home | History | Annotate | Download | only in examples
      1 #!/bin/sh
      2 
      3 IFNAME=$1
      4 CMD=$2
      5 
      6 kill_daemon() {
      7     NAME=$1
      8     PF=$2
      9 
     10     if [ ! -r $PF ]; then
     11 	return
     12     fi
     13 
     14     PID=`cat $PF`
     15     if [ $PID -gt 0 ]; then
     16 	if ps $PID | grep -q $NAME; then
     17 	    kill $PID
     18 	fi
     19     fi
     20     rm $PF
     21 }
     22 
     23 if [ "$CMD" = "P2P-GROUP-STARTED" ]; then
     24     GIFNAME=$3
     25     if [ "$4" = "GO" ]; then
     26 	kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid
     27 	rm /var/run/dhclient.leases-$GIFNAME
     28 	kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME
     29 	ifconfig $GIFNAME 192.168.42.1 up
     30 	if ! dnsmasq -x /var/run/dnsmasq.pid-$GIFNAME \
     31 	    -i $GIFNAME \
     32 	    -F192.168.42.11,192.168.42.99; then
     33 	    # another dnsmasq instance may be running and blocking us; try to
     34 	    # start with -z to avoid that
     35 	    dnsmasq -x /var/run/dnsmasq.pid-$GIFNAME \
     36 		-i $GIFNAME \
     37 		-F192.168.42.11,192.168.42.99 --listen-address 192.168.42.1 -z
     38 	fi
     39     fi
     40     if [ "$4" = "client" ]; then
     41 	kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid
     42 	rm /var/run/dhclient.leases-$GIFNAME
     43 	kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME
     44 	dhclient -pf /var/run/dhclient-$GIFNAME.pid \
     45 	    -lf /var/run/dhclient.leases-$GIFNAME \
     46 	    -nw \
     47 	    $GIFNAME
     48     fi
     49 fi
     50 
     51 if [ "$CMD" = "P2P-GROUP-REMOVED" ]; then
     52     GIFNAME=$3
     53     if [ "$4" = "GO" ]; then
     54 	kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME
     55 	ifconfig $GIFNAME 0.0.0.0
     56     fi
     57     if [ "$4" = "client" ]; then
     58 	kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid
     59 	rm /var/run/dhclient.leases-$GIFNAME
     60 	ifconfig $GIFNAME 0.0.0.0
     61     fi
     62 fi
     63 
     64 if [ "$CMD" = "P2P-CROSS-CONNECT-ENABLE" ]; then
     65     GIFNAME=$3
     66     UPLINK=$4
     67     # enable NAT/masquarade $GIFNAME -> $UPLINK
     68     iptables -P FORWARD DROP
     69     iptables -t nat -A POSTROUTING -o $UPLINK -j MASQUERADE
     70     iptables -A FORWARD -i $UPLINK -o $GIFNAME -m state --state RELATED,ESTABLISHED -j ACCEPT
     71     iptables -A FORWARD -i $GIFNAME -o $UPLINK -j ACCEPT
     72     sysctl net.ipv4.ip_forward=1
     73 fi
     74 
     75 if [ "$CMD" = "P2P-CROSS-CONNECT-DISABLE" ]; then
     76     GIFNAME=$3
     77     UPLINK=$4
     78     # disable NAT/masquarade $GIFNAME -> $UPLINK
     79     sysctl net.ipv4.ip_forward=0
     80     iptables -t nat -D POSTROUTING -o $UPLINK -j MASQUERADE
     81     iptables -D FORWARD -i $UPLINK -o $GIFNAME -m state --state RELATED,ESTABLISHED -j ACCEPT
     82     iptables -D FORWARD -i $GIFNAME -o $UPLINK -j ACCEPT
     83 fi
     84