summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatze Miau <miaukatzemiau@priveasy.de>2011-12-09 19:52:15 (GMT)
committerKatze Miau <miaukatzemiau@priveasy.de>2011-12-09 19:52:15 (GMT)
commitcd9f7aca64327e29cc8acb55d94f21a2da03330e (patch)
treee6aafe13e28045fad9cad192dd14c4b7a419d2f9
parentef0ab9ba3f0042f59c0a8f155fcb61187585303e (diff)
add FSM inetable
- implemented according to documentation - almost no tests so far
-rw-r--r--files/common/etc/fsm/inetable/common.sh5
-rw-r--r--files/common/etc/fsm/inetable/common_queen.sh13
-rw-r--r--files/common/etc/fsm/inetable/initial_state1
-rwxr-xr-xfiles/common/etc/fsm/inetable/trans/boot.leave11
-rwxr-xr-xfiles/common/etc/fsm/inetable/trans/ghost-queen.trans14
-rwxr-xr-xfiles/common/etc/fsm/inetable/trans/ghost.enter3
-rwxr-xr-xfiles/common/etc/fsm/inetable/trans/ghost.leave17
-rwxr-xr-xfiles/common/etc/fsm/inetable/trans/queen-ghost.trans15
-rwxr-xr-xfiles/common/etc/fsm/inetable/trans/queen.enter45
-rwxr-xr-xfiles/common/etc/fsm/inetable/trans/queen.leave16
-rwxr-xr-xfiles/common/etc/fsm/inetable/trans/robinson.enter31
-rwxr-xr-xfiles/common/etc/fsm/inetable/trans/robinson.leave12
-rwxr-xr-xfiles/common/etc/fsm/inetable/watch/default10
-rwxr-xr-xfiles/common/etc/fsm/inetable/watch/ghost31
l---------files/common/etc/fsm/inetable/watch/queen1
15 files changed, 225 insertions, 0 deletions
diff --git a/files/common/etc/fsm/inetable/common.sh b/files/common/etc/fsm/inetable/common.sh
new file mode 100644
index 0000000..7c73837
--- /dev/null
+++ b/files/common/etc/fsm/inetable/common.sh
@@ -0,0 +1,5 @@
+cloud_is_online () {
+ # look for mac addrs in batman gateway list
+ # TODO: remove fake gws with almost zero bandwith
+ batctl gwl | tail -n-1 | egrep -q '([0-9a-f]{2}:){5}[0-9a-f]{2}'
+}
diff --git a/files/common/etc/fsm/inetable/common_queen.sh b/files/common/etc/fsm/inetable/common_queen.sh
new file mode 100644
index 0000000..8107cd2
--- /dev/null
+++ b/files/common/etc/fsm/inetable/common_queen.sh
@@ -0,0 +1,13 @@
+# functions that are allowed to be called if we are in the queen or
+# ghost state
+
+gwiptbl=/tmp/p2ptbl/gwip
+DHCPLeaseTime=$((12 * 3600))
+
+NodeId="$(cat /etc/nodeid)"
+oct3=$(ifconfig br-mesh | egrep -o 'inet addr:[0-9.]*'|cut -f3 -d.)
+[ -n "$oct3" ]
+
+we_own_our_ip () {
+ [ "$(p2ptbl get $gwiptbl $oct3 | cut -sf2)" == "$NodeId" ]
+}
diff --git a/files/common/etc/fsm/inetable/initial_state b/files/common/etc/fsm/inetable/initial_state
new file mode 100644
index 0000000..18f191d
--- /dev/null
+++ b/files/common/etc/fsm/inetable/initial_state
@@ -0,0 +1 @@
+boot \ No newline at end of file
diff --git a/files/common/etc/fsm/inetable/trans/boot.leave b/files/common/etc/fsm/inetable/trans/boot.leave
new file mode 100755
index 0000000..233ef9e
--- /dev/null
+++ b/files/common/etc/fsm/inetable/trans/boot.leave
@@ -0,0 +1,11 @@
+#!/bin/sh -e
+
+# init gwid table by merging it with a hand-crafted table in which all
+# available IPs are stored as free with version zero. This guarantees
+# that any update on any node will have a higher version.
+for i in $(seq 0 254); do
+ echo -e "$i\t0\tfree"
+done > /tmp/gwip-init
+p2ptbl init /tmp/p2ptbl/gwip
+p2ptbl merge /tmp/p2ptbl/gwip /tmp/gwip-init
+rm /tmp/gwip-init
diff --git a/files/common/etc/fsm/inetable/trans/ghost-queen.trans b/files/common/etc/fsm/inetable/trans/ghost-queen.trans
new file mode 100755
index 0000000..117aab9
--- /dev/null
+++ b/files/common/etc/fsm/inetable/trans/ghost-queen.trans
@@ -0,0 +1,14 @@
+#!/bin/sh -e
+. ../common_queen.sh
+
+rm /tmp/ghost_since
+
+# announce our gw again
+batctl gw server
+
+# update status of our gateway ip
+if we_own_our_ip; then
+ p2ptbl update $gwiptbl $oct3 "queen\t$NodeId" br-mesh
+fi
+
+# TODO: remove traffic redirection \ No newline at end of file
diff --git a/files/common/etc/fsm/inetable/trans/ghost.enter b/files/common/etc/fsm/inetable/trans/ghost.enter
new file mode 100755
index 0000000..60bff11
--- /dev/null
+++ b/files/common/etc/fsm/inetable/trans/ghost.enter
@@ -0,0 +1,3 @@
+#!/bin/sh -e
+
+date +%s > /tmp/ghost_since
diff --git a/files/common/etc/fsm/inetable/trans/ghost.leave b/files/common/etc/fsm/inetable/trans/ghost.leave
new file mode 100755
index 0000000..747a05d
--- /dev/null
+++ b/files/common/etc/fsm/inetable/trans/ghost.leave
@@ -0,0 +1,17 @@
+#!/bin/sh -e
+
+rm /tmp/ghost_since
+
+# free our gw ip
+ifconfig br-mesh 0.0.0.0
+
+# update status of our gateway ip
+if we_own_our_ip; then
+ p2ptbl update $gwiptbl $oct3 free br-mesh
+fi
+
+# disable DHCP
+uci delete dhcp.mesh
+/etc/init.d/dnsmasq restart
+
+# TODO: remove internet redirection & splash iptable \ No newline at end of file
diff --git a/files/common/etc/fsm/inetable/trans/queen-ghost.trans b/files/common/etc/fsm/inetable/trans/queen-ghost.trans
new file mode 100755
index 0000000..7cd791d
--- /dev/null
+++ b/files/common/etc/fsm/inetable/trans/queen-ghost.trans
@@ -0,0 +1,15 @@
+#!/bin/sh -e
+. ../common_queen.sh
+
+# store since when we are a ghost
+date +%s >/tmp/ghost_since
+
+# do not announce gateway anymore
+batctl gw client
+
+# update status of our gateway ip
+if we_own_our_ip; then
+ p2ptbl update $gwiptbl $oct3 "ghost\t$NodeId" br-mesh
+fi
+
+# TODO: setup redirection of all traffic to another gateway
diff --git a/files/common/etc/fsm/inetable/trans/queen.enter b/files/common/etc/fsm/inetable/trans/queen.enter
new file mode 100755
index 0000000..a8a1a8e
--- /dev/null
+++ b/files/common/etc/fsm/inetable/trans/queen.enter
@@ -0,0 +1,45 @@
+#!/bin/sh -e
+
+gwiptbl=/tmp/p2ptbl/gwip
+
+# determine gw IP from p2ptable
+getoct () {
+ p2ptbl show $gwiptbl \
+ | cut -f1,2 \
+ | egrep "[0-9]*"$'\t'"$1" \
+ | $2 \
+ | head -n1
+}
+# get the lowest free addr
+oct3=$(getoct free "sort -n")
+# no free addrs? -> steal an addr from a random ghost
+if [ -z "$oct3" ]; then
+ oct3=$(getoct ghost "shuf")
+ # no ghost addrs? -> steal an addr from a random queen
+ if [ -z "$oct3" ]; then
+ oct3=$(getoct queen "shuf")
+ fi
+ # TODO: log warning about IPv4 addr space exhaustion
+fi
+[ -n "$oct3" ]
+p2ptbl update $gwiptbl $oct3 "queen\t$NodeId" br-mesh
+
+# activate gw mode
+batctl gw server
+
+# set up gw IP
+ifconfig br-mesh 10.17.$oct3.1
+
+# TODO: load splash status
+
+# setup DHCP
+echo "
+set dhcp.mesh=dhcp
+set dhcp.mesh.start=2
+set dhcp.mesh.limit=254
+set dhcp.mesh.leasetime=$DHCPLeaseTime
+set dhcp.mesh.interface=mesh
+set dhcp.mesh.start_ip=10.17.$oct3.2
+set dhcp.mesh.end_ip=10.17.$oct3.254
+" | uci batch
+/etc/init.d/dnsmasq restart \ No newline at end of file
diff --git a/files/common/etc/fsm/inetable/trans/queen.leave b/files/common/etc/fsm/inetable/trans/queen.leave
new file mode 100755
index 0000000..660f2e2
--- /dev/null
+++ b/files/common/etc/fsm/inetable/trans/queen.leave
@@ -0,0 +1,16 @@
+#!/bin/sh -e
+. ../common_queen.sh
+
+# do not announce gateway anymore
+batctl gw client
+
+# release IP in p2ptable (if we still own it)
+if we_own_our_ip; then
+ p2ptbl update $gwiptbl $oct3 free br-mesh
+fi
+
+# disable DHCP
+uci delete dhcp.mesh
+/etc/init.d/dnsmasq restart
+
+# TODO remove splash iptable
diff --git a/files/common/etc/fsm/inetable/trans/robinson.enter b/files/common/etc/fsm/inetable/trans/robinson.enter
new file mode 100755
index 0000000..7ea0001
--- /dev/null
+++ b/files/common/etc/fsm/inetable/trans/robinson.enter
@@ -0,0 +1,31 @@
+#!/bin/sh -e
+
+# setup random ip
+gen_randnet () {
+ randnet=21.$(($RANDOM % 256)).$(($RANDOM % 256))
+}
+
+gen_randnet
+while ! arping -I br-mesh -D -c 5 -q $randnet.1; do
+ gen_randnet
+done
+ifconfig br-mesh $randnet.1
+
+# setup dhcp
+echo "
+set dhcp.mesh=dhcp
+set dhcp.mesh.start=2
+set dhcp.mesh.limit=254
+set dhcp.mesh.leasetime=60
+set dhcp.mesh.interface=mesh
+set dhcp.mesh.start_ip=$randnet.2
+set dhcp.mesh.end_ip=$randnet.254
+" | uci batch
+/etc/init.d/dnsmasq restart
+
+# announce fake gateway; TODO: announce only tiny bandwith
+batctl gw server
+
+# TODO: start fake dns
+
+# TODO: redirect all inet traffic to local httpd
diff --git a/files/common/etc/fsm/inetable/trans/robinson.leave b/files/common/etc/fsm/inetable/trans/robinson.leave
new file mode 100755
index 0000000..b7f49b8
--- /dev/null
+++ b/files/common/etc/fsm/inetable/trans/robinson.leave
@@ -0,0 +1,12 @@
+#!/bin/sh -e
+
+# stop announcing fake gateway
+batctl gw client
+
+# stop DHCP server
+uci delete dhcp.mesh
+/etc/init.d/dnsmasq restart
+
+# TODO: stop fake dns
+
+# TODO: stop redirecting all inet traffic to local httpd
diff --git a/files/common/etc/fsm/inetable/watch/default b/files/common/etc/fsm/inetable/watch/default
new file mode 100755
index 0000000..959e7ac
--- /dev/null
+++ b/files/common/etc/fsm/inetable/watch/default
@@ -0,0 +1,10 @@
+#!/bin/sh -e
+. ../common.sh
+
+if test_connectivity internet; then
+ echo queen
+elif cloud_is_online; then
+ echo drone
+else
+ echo robinson
+fi
diff --git a/files/common/etc/fsm/inetable/watch/ghost b/files/common/etc/fsm/inetable/watch/ghost
new file mode 100755
index 0000000..54696f2
--- /dev/null
+++ b/files/common/etc/fsm/inetable/watch/ghost
@@ -0,0 +1,31 @@
+#!/bin/sh -e
+. ../common.sh
+. ../common_queen.sh
+
+# if our GW IP has been taken we must no use it again
+if ! we_own_our_ip; then
+ if ! cloud_is_online; then
+ echo robinson
+ else
+ echo drone
+ fi
+ exit
+fi
+
+if test_connectivity internet; then
+ echo queen
+ exit
+fi
+
+if [ "$1" == "ghost" ]; then
+ GhostTime=$(($(date +%s) - $(cat /tmp/ghost_since)))
+else
+ GhostTime=0
+fi
+if ! cloud_is_online; then
+ echo robinson
+elif [ $GhostTime -ge $DHCPLeaseTime ]; then
+ echo drone
+else
+ echo ghost
+fi
diff --git a/files/common/etc/fsm/inetable/watch/queen b/files/common/etc/fsm/inetable/watch/queen
new file mode 120000
index 0000000..cec3826
--- /dev/null
+++ b/files/common/etc/fsm/inetable/watch/queen
@@ -0,0 +1 @@
+ghost \ No newline at end of file
contact: Jan Huwald // Impressum