conn

A script repository to manage connections in Linux.
git clone git://r-36.net/conn
Log | Files | Refs | LICENSE

commit e28b32c29674e44dd34bc01486b781c8037a650a
Author: Christoph Lohmann <20h@r-36.net>
Date:   Mon, 21 Feb 2011 23:12:07 +0100

Initial commit of conn.

Diffstat:
LICENSE | 21+++++++++++++++++++++
Makefile | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
README.md | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TODO.md | 14++++++++++++++
bin/conn | 5+++++
config.mk | 9+++++++++
etc/conn/common.sh | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
etc/conn/eth/README.md | 6++++++
etc/conn/eth/run.sh | 38++++++++++++++++++++++++++++++++++++++
etc/conn/run.sh | 31+++++++++++++++++++++++++++++++
etc/conn/wifi/README.md | 9+++++++++
etc/conn/wifi/networks.tbl | 2++
etc/conn/wifi/networks/skkmswp.sh | 19+++++++++++++++++++
etc/conn/wifi/run.sh | 40++++++++++++++++++++++++++++++++++++++++
etc/conn/wifi/wlan0-action.sh | 41+++++++++++++++++++++++++++++++++++++++++
etc/conn/wwan/README.md | 11+++++++++++
etc/conn/wwan/f3507g-x201-init | 47+++++++++++++++++++++++++++++++++++++++++++++++
etc/conn/wwan/f3507g-x201-usbnet | 26++++++++++++++++++++++++++
etc/conn/wwan/run.sh | 20++++++++++++++++++++
etc/conn/wwan/wwan0-run.sh | 34++++++++++++++++++++++++++++++++++
20 files changed, 604 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,21 @@ +MIT/X Consortium License + +© 2011 Christoph Lohmann <20h@r-36.net> + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile @@ -0,0 +1,60 @@ +# conn - connection manager +# See LICENSE file for copyright and license details. + +include config.mk + +SRC = dwm.c +OBJ = ${SRC:.c=.o} + +all: options dwm + +options: + @echo dwm build options: + @echo "CFLAGS = ${CFLAGS}" + @echo "LDFLAGS = ${LDFLAGS}" + @echo "CC = ${CC}" + +.c.o: + @echo CC $< + @${CC} -c ${CFLAGS} $< + +${OBJ}: config.h config.mk + +config.h: + @echo creating $@ from config.def.h + @cp config.def.h $@ + +dwm: ${OBJ} + @echo CC -o $@ + @${CC} -o $@ ${OBJ} ${LDFLAGS} + +clean: + @echo cleaning + @rm -f dwm ${OBJ} dwm-${VERSION}.tar.gz + +dist: clean + @echo creating dist tarball + @mkdir -p dwm-${VERSION} + @cp -R LICENSE Makefile README config.def.h config.mk \ + dwm.1 ${SRC} dwm-${VERSION} + @tar -cf dwm-${VERSION}.tar dwm-${VERSION} + @gzip dwm-${VERSION}.tar + @rm -rf dwm-${VERSION} + +install: all + @echo installing executable file to ${DESTDIR}${PREFIX}/bin + @mkdir -p ${DESTDIR}${PREFIX}/bin + @cp -f dwm ${DESTDIR}${PREFIX}/bin + @chmod 755 ${DESTDIR}${PREFIX}/bin/dwm + @echo installing manual page to ${DESTDIR}${MANPREFIX}/man1 + @mkdir -p ${DESTDIR}${MANPREFIX}/man1 + @sed "s/VERSION/${VERSION}/g" < dwm.1 > ${DESTDIR}${MANPREFIX}/man1/dwm.1 + @chmod 644 ${DESTDIR}${MANPREFIX}/man1/dwm.1 + +uninstall: + @echo removing executable file from ${DESTDIR}${PREFIX}/bin + @rm -f ${DESTDIR}${PREFIX}/bin/dwm + @echo removing manual page from ${DESTDIR}${MANPREFIX}/man1 + @rm -f ${DESTDIR}${MANPREFIX}/man1/dwm.1 + +.PHONY: all options clean dist install uninstall diff --git a/README.md b/README.md @@ -0,0 +1,56 @@ +# Conn - connection scripts + +This collection of scripts is intended to standardize the way to handle +many different connections in a Unix like environment. + +## Dependencies + +* bash (Could be easily rewritten.) +* dhcpcd +* wpa-supplicant +* ping +* awk +* sed +* various utilities for specific connection types + +## Architecture + +/usr/bin/conn The main script, which accesses all functionality. + +/etc/conn/run.sh The script which conn calls and does the redirection + to the connections. + +/etc/conn The base directory for all logic behind connd. + +/etc/conn/common.sh Common functions all scripts can use. + +/etc/conn/$conn These are the connection types which are available. + +/etc/conn/$conn/run.sh This script is run in every connection, when the + connection is invoked by the run.sh in the main + etc directory. + +/etc/conn/$conn/$files These files depend solely on the connection, which + is served. Look at them for details. + +### Calltree +* conn + * conn/run.sh + * conn/$conn/run.sh + * conn/$conn/$files + +### Examples + + % conn -l + Available network types: + eth + wifi + wwan + % conn -s wifi + % conn -u wifi + % conn -k wifi + % conn -s eth + % conn -k eth + % conn -s wwan + + diff --git a/TODO.md b/TODO.md @@ -0,0 +1,14 @@ +## TODO + +* add state + * based on the state connections can be automatically started + or killed + * let's see, if a running daemon can be avoided + +* add support for other connection types + * bluetooth + * tunnels on top of given connections + * VPN + * DNStunnel + * ICMPtunnel + diff --git a/bin/conn b/bin/conn @@ -0,0 +1,5 @@ +#!/bin/sh + +cd /etc/conn +./run.sh $* + diff --git a/config.mk b/config.mk @@ -0,0 +1,9 @@ +# conn version +VERSION = 0.2 + +# Customize below to fit your system + +# paths +PREFIX = /usr +MANPREFIX = ${PREFIX}/share/man + diff --git a/etc/conn/common.sh b/etc/conn/common.sh @@ -0,0 +1,115 @@ +#!/bin/sh + +LOGGING=1 + +## Common dirs +RUNDIR="/var/run/conn" +ETCDIR="/etc/conn" +WIFIDIR="${ETCDIR}/wifi" +WWANDIR="${ETCDIR}/wwan" +ETHDIR="${ETCDIR}/eth" + +mkpaths() { + for i in $RUNDIR $ETCDIR $WIFIDIR $WWANDIR \ + $ETHDIR; + do + [ ! -e $i ] && mkdir -p $i + done +} +mkpaths + +## WPA handling +WPAPID="${RUNDIR}/wpa_supplicant.pid" +WPACONF="/etc/wpa_supplicant.conf" +WPACMD="/usr/sbin/wpa_supplicant -B -D wext -c ${WPACONF}" +WPACLIPID="${RUNDIR}/wpa_cli.pid" +WPACLICMD="/usr/sbin/wpa_cli" +WPACLIREQ="/usr/sbin/wpa_cli" + +startwpa() { + $WPACMD -P $WPAPID.$1 -i $1 + $WPACLICMD -P $WPACLIPID.$1 -i $1 -a \ + $WIFIDIR/$1-action.sh 2>&1 >/dev/null & +} + +hupwpa() { + [ -e $WPAPID.$1 ] && /bin/kill -HUP `cat WPAPID.$1` +} + +stopwpa() { + [ -e $WPAPID.$1 ] && /bin/kill -KILL `cat $WPAPID.$1` + [ -e $WPACLIPID.$1 ] && /bin/kill -KILL `cat $WPACLIPID.$1` +} + +# $WPA_ID must be set from within the calling action script. +getssid() { + $WPACLIREQ -i $1 list_networks \ + | grep "^$WPA_ID" \ + | awk -F'\t' '{print $2}' +} + +## DHCP handling +DHCPCMD="/sbin/dhcpcd -L" +DHCPRELEASECMD="/sbin/dhcpcd -k" +DHCPKILLCMD="/sbin/dhcpcd -x" + +startdhcp() { + $DHCPRELEASECMD $1 2>&1 >/dev/null + $DHCPCMD $1 2>&1 >/dev/null +} + +stopdhcp() { + $DHCPKILLCMD $1 2>&1 >/dev/null +} + +## Connection handling +runconnection() { + SRVDIR="${ETCDIR}/$1" + SRVRUN="${ETCDIR}/$1/run.sh" + shift 1 + if [ -e $SRVRUN ]; + then + cd $SRVDIR + sh $SRVRUN $* + fi +} + +## Interface handling +PINGPID="${RUNDIR}/pingd.pid" +# Should be available everywhere. +PINGHOST="8.8.8.8" +PINGCMD="/bin/ping -q" +startpingd() { + $PINGCMD $PINGHOST 2>&1 >/dev/null & + + echo $! > $PINGPID.$1 +} + +stoppingd() { + /bin/kill -KILL `cat $PINGPID.$1` + rm $PINGPID.$1 +} + +getinterface() { + basename $1 | awk -F'-' '{print $1}' +} + +## Link handling +setlinkup() { + ip link set $1 up +} + +setlinkdown() { + ip link set $1 down +} + +islinkup() { + state=`ip link show $1 2>/dev/null | grep ',UP'` + if [ "$state" == "" ]; + then + return 1 + else + return 0 + fi +} + diff --git a/etc/conn/eth/README.md b/etc/conn/eth/README.md @@ -0,0 +1,6 @@ +## Ethernet - stable and gross + +Much could be done here, but the most obvious setup is to run dhcp on it. + +A combination with ifplugd could be done. + diff --git a/etc/conn/eth/run.sh b/etc/conn/eth/run.sh @@ -0,0 +1,38 @@ +#!/bin/sh + +. ../common.sh + +if [ "$2" == "" ]; +then + interface="eth0" +else + interface=$2 +fi + +case "$1" in + -s) + if ! islinkup $interface; + then + setlinkup $interface + fi + startdhcp $interface + exit $? + ;; + -k) + stopdhcp $interface + islinkup $interface && setlinkdown $interface + exit $? + ;; + -u) + exit $? + ;; + -r) + $0 -k $interface; + $0 -s $interface; + ;; + *) + echo "usage: $0 [-s|-k|-u|-r] interface" + exit 1 + ;; +esac + diff --git a/etc/conn/run.sh b/etc/conn/run.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +. ./common.sh + +connection=$2 + +case "$1" in + -s|-k|-u) + arg=$1 + shift 2 + runconnection $connection $arg $* + exit $? + ;; + -l) + echo "Available network types:" + for i in `ls -d */ | sed 's~/~~g'`; + do + echo $i + done + ;; + -r) + shift 1 + $0 -k $* + $0 -s $* + ;; + *) + echo "usage: $0 [-s|-k|-u|-r|-l] connection [args ...]" + exit 1 + ;; +esac + diff --git a/etc/conn/wifi/README.md b/etc/conn/wifi/README.md @@ -0,0 +1,9 @@ +# Wifi resolving + +1. run.sh starts wpa-supplicant and wpa-cli. +2. wpa-cli runs $interface-action.sh on an event. +3. $interface-action.sh greps in networks.tbl for the ssid of the + relevant network and calls the given script. +4. The script networks/$script is run and gets the interface and + the changed state as argument. + diff --git a/etc/conn/wifi/networks.tbl b/etc/conn/wifi/networks.tbl @@ -0,0 +1,2 @@ +skkmswp.sh SKKMSWP + diff --git a/etc/conn/wifi/networks/skkmswp.sh b/etc/conn/wifi/networks/skkmswp.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +interface="$1" + +case "$2" in + CONNECTED) + ip addr add 192.168.1.6/24 dev $interface \ + brd 192.168.1.255 + ip route add default via 192.168.1.2 + echo "nameserver 8.8.8.8" > /etc/resolv.conf + ;; + DISCONNECTED) + ip addr del 192.168.1.6/24 dev $interface + ;; + *) + exit 1; + ;; +esac + diff --git a/etc/conn/wifi/run.sh b/etc/conn/wifi/run.sh @@ -0,0 +1,40 @@ +#!/bin/sh + +. ../common.sh + +if [ "$2" == "" ]; +then + interface="wlan0" +else + interface=$2 +fi + +case "$1" in + -s) + if ! islinkup $interface; + then + setlinkup $interface + fi + startwpa $interface + exit $? + ;; + -k) + stopwpa $interface + + islinkup $interface && setlinkdown $interface + exit $? + ;; + -u) + hupwpa $interface + exit $? + ;; + -r) + $0 -k $interface; + $0 -s $interface; + ;; + *) + echo "usage: $0 [-s|-k|-u|-r] interface" + exit 1 + ;; +esac + diff --git a/etc/conn/wifi/wlan0-action.sh b/etc/conn/wifi/wlan0-action.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +. ../common.sh + +interface="$1" +action="$2" +ssid=`getssid $interface` + +if [ "$LOGGING" == "1" ]; +then + logger -t "$interface-action" "Got request for $interface $ssid $action." +fi + +getscript() { + cat networks.tbl \ + | grep "$ssid\$" \ + | awk -F'\t' '{print $1}' +} + +script=`getscript $ssid` +if [ "$script" != "" ]; +then + cd networks + ./$script $interface $action + exit $? +fi + +case "$action" in + CONNECTED) + startdhcp $interface + ;; + DISCONNECTED) + stopdhcp $interface + ;; + *) + exit 1; + ;; +esac + +exit 0 + diff --git a/etc/conn/wwan/README.md b/etc/conn/wwan/README.md @@ -0,0 +1,11 @@ +# WWAN - the never ending story + +Wwan on Linux depends on the hardware you are using and how to initialize +it. On my Laptop (X200), this is an Ericsson F3507g modem, which is setup +via AT commands and exports an usb ethernet device for communication. + +Since it's my only device, the script for init and the usbnet are run +in serial, after which dhcp is run on it. Besides this a ping is started, +which increases the stability of a wwan connection; at least here in Ger- +many. + diff --git a/etc/conn/wwan/f3507g-x201-init b/etc/conn/wwan/f3507g-x201-init @@ -0,0 +1,47 @@ +#!/bin/bash + +CONTROL_DEVICE="/dev/ttyACM1" +PIN="XXXXX" +APN="surfo2" + +case "$1" in + -s) + modprobe zaurus + + echo -n "Powering up F3507g card.." + echo enable > /proc/acpi/ibm/wan + rfkill unblock wwan + while [ ! -c $CONTROL_DEVICE ]; do sleep 0.5; echo -n "."; done + echo "done" + echo -n "Turning on F3507g card..." + sleep 5 + if [ -n "$PIN" ]; then + echo -n "PIN..." + /usr/sbin/chat -v "" "AT+CPIN?" "SIM PIN" "AT" "OK" "AT+CPIN=\"$PIN\"" "OK" > $CONTROL_DEVICE < $CONTROL_DEVICE + fi + echo -n "CFUN..." + /usr/sbin/chat -v "" "AT+CPIN?" "OK" "AT+CFUN=1" "+PACSP0" "AT" "OK" > $CONTROL_DEVICE < $CONTROL_DEVICE + echo "done" + ;; + -k) + echo -n "Turning off F3507g card..." + /usr/sbin/chat -v "" "AT+CFUN=4" "OK" > $CONTROL_DEVICE < $CONTROL_DEVICE + echo "done" + echo -n "Powering down F3507g card.." + rfkill block wwan + echo disable > /proc/acpi/ibm/wan + while [ -c $CONTROL_DEVICE ]; do sleep 0.5; echo -n "."; done + echo "done" + + rmmod zaurus + rmmod cdc_ether + ;; + -r) + $0 -k + $0 -s + ;; + *) + echo "usage: $0 [-s|-k|-r]" +esac +exit 0 + diff --git a/etc/conn/wwan/f3507g-x201-usbnet b/etc/conn/wwan/f3507g-x201-usbnet @@ -0,0 +1,26 @@ +#!/bin/bash + +CONTROL_DEVICE="/dev/ttyACM1" +APN="surfo2" + +case "$1" in + -s) + echo -n "Starting usbnet connection..." + /usr/sbin/chat -v "" "AT+CGDCONT=1,\"IP\",\"$APN\"" "OK" "AT*ENAP=1,1" "OK" > $CONTROL_DEVICE < $CONTROL_DEVICE + sleep 1 + echo "done" + ;; + -k) + echo -n "Stopping usbnet connection..." + /usr/sbin/chat -v "" "AT*ENAP=0" "OK" > $CONTROL_DEVICE < $CONTROL_DEVICE + echo "done" + ;; + -r) + $0 -k + $0 -s + ;; + *) + echo "usage: $0 [-s|-k|-r]" +esac +exit 0 + diff --git a/etc/conn/wwan/run.sh b/etc/conn/wwan/run.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +. ../common.sh + +if [ "$2" == "" ]; +then + interface="wwan0" +else + interface="$2" +fi + +iffile="${WWANDIR}/${interface}-run.sh" + +if [ ! -e $iffile ]; +then + exit 1 +fi + +$iffile $1 + diff --git a/etc/conn/wwan/wwan0-run.sh b/etc/conn/wwan/wwan0-run.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +. ../common.sh + +interface=`getinterface $0` + +case "$1" in + -s) + ${WWANDIR}/f3507g-x201-init -s + ${WWANDIR}/f3507g-x201-usbnet -s + startdhcp $interface + startpingd $interface + exit $? + ;; + -k) + stoppingd $interface + stopdhcp $interface + ${WWANDIR}/f3507g-x201-usbnet -k + ${WWANDIR}/f3507g-x201-init -k + exit $? + ;; + -u) + exit 0 + ;; + -r) + $0 -k $interface; + $0 -s $interface; + ;; + *) + echo "usage: $0 [-s|-k|-u|-r] interface" + exit 1 + ;; +esac +