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