From 76442761b262deb6f60e2fd8042912000a17249e Mon Sep 17 00:00:00 2001
From: Bastian Bittorf <bittorf@bluebottle.com>
Date: Thu, 22 May 2014 11:01:46 +0200
Subject: [PATCH] [openwrt] add uci-fication and hotplug-script

[openwrt] add uci-fication and hotplug-script

Implementing a small subset for a working native-olsrd2 config-file.
(logging, telnet, interfaces, lan's and some more)

The file is created during startup into ramdisc and during hotplug-events
recreated and the daemon rereads to config via sending a signal.

The following is generated on my end from the included UCI-file:

root@openwrt-box:~ cat /var/etc/olsrd2.conf
[global]
        fork    	true
        failfast        no

[log]
        stderr  true
        syslog  true
        file    /var/log/olsrd2.log

[domain=0]
        table   234

[telnet]
        port    2007

[interface=lo]  	# loopback
[interface=eth0.2]      # wan
[interface=eth0.1]      # lan
[interface=wlan0]       # wlanadhoc
[interface=wlan1]	# wlanadhocRADIO1

[olsrv2]
	lan 10.63.21.64/26
	lan ::/0
	lan 0.0.0.0/0

Signed-off-by: Bastian Bittorf <bittorf@bluebottle.com>
---
 openwrt/olsrd2-git/Makefile             |  10 ++-
 openwrt/olsrd2-git/files/olsrd2.conf    |  13 ----
 openwrt/olsrd2-git/files/olsrd2.hotplug |  76 +++++++++++++++++++
 openwrt/olsrd2-git/files/olsrd2.init    | 130 +++++++++++++++++++++++++++++---
 openwrt/olsrd2-git/files/olsrd2.uci     |  25 ++++++
 5 files changed, 228 insertions(+), 26 deletions(-)
 delete mode 100644 openwrt/olsrd2-git/files/olsrd2.conf
 create mode 100755 openwrt/olsrd2-git/files/olsrd2.hotplug
 create mode 100644 openwrt/olsrd2-git/files/olsrd2.uci

diff --git a/openwrt/olsrd2-git/Makefile b/openwrt/olsrd2-git/Makefile
index 8be848f..05f9425 100644
--- a/openwrt/olsrd2-git/Makefile
+++ b/openwrt/olsrd2-git/Makefile
@@ -77,6 +77,7 @@ TARGET_CFLAGS += -I$(STAGING_DIR)/usr/include
 define Package/olsrd2-git/install
 	$(INSTALL_DIR) $(1)/etc
 	$(INSTALL_DIR) $(1)/etc/init.d
+	$(INSTALL_DIR) $(1)/etc/hotplug.d/iface
 	$(INSTALL_DIR) $(1)/usr
 	$(INSTALL_DIR) $(1)/usr/sbin
 	$(INSTALL_BIN) \
@@ -85,13 +86,16 @@ define Package/olsrd2-git/install
 	$(INSTALL_BIN) \
 		./files/olsrd2.init \
 		$(1)/etc/init.d/olsrd2
+	$(INSTALL_BIN) \
+		./files/olsrd2.hotplug \
+		$(1)/etc/hotplug.d/iface/50-olsrd2
 	$(INSTALL_DATA) \
-		./files/olsrd2.conf \
-		$(1)/etc
+		./files/olsrd2.uci \
+		$(1)/etc/config/olsrd2
 endef
 
 define Package/olsrd2-git/conffiles
-/etc/olsrd2.conf
+/etc/config/olsrd2
 endef
 
 #define Package/olsrd2-git-plugin-ff-ett/install
diff --git a/openwrt/olsrd2-git/files/olsrd2.conf b/openwrt/olsrd2-git/files/olsrd2.conf
deleted file mode 100644
index a855b00..0000000
--- a/openwrt/olsrd2-git/files/olsrd2.conf
+++ /dev/null
@@ -1,13 +0,0 @@
-#[global]
-#       fork            no
-#       failfast        no
-
-[log]
-#       file            /var/log/olsrd2.log
-       stderr          true
-       syslog          true
-#       info            all
-#       debug           all
-#
-
-[interface=wlan0]
diff --git a/openwrt/olsrd2-git/files/olsrd2.hotplug b/openwrt/olsrd2-git/files/olsrd2.hotplug
new file mode 100755
index 0000000..f6d1d08
--- /dev/null
+++ b/openwrt/olsrd2-git/files/olsrd2.hotplug
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+olsrd2_log()
+{
+	local prio="$1"
+	local message="$2"
+
+	logger -t olsrd2_hotplug -p daemon.$prio "$message"
+}
+
+olsrd2_list_configured_interfaces()
+{
+	local i=0
+	local interface
+
+	while interface="$( uci -q get olsrd2.@interface[$i].ifname )"; do {
+		case "$( uci -q get olsrd2.@interface[$i].ignore )" in
+			1|on|true|enabled|yes)
+				# must be ignored
+			;;
+			*)
+				echo "$interface"	# e.g. 'lan wan wifi'
+			;;
+		esac
+
+		i=$(( $i + 1 ))
+	} done
+}
+
+olsrd2_interface_already_in_config()
+{
+	if grep ^"\[interface=${DEVICE}\]" '/var/etc/olsrd2.conf'; then
+		olsrd2_log debug "[OK] already_active: '$INTERFACE' => '$DEVICE'"
+		return 0
+	else
+		olsrd2_log info "[OK] ifup/adding: '$INTERFACE' => '$DEVICE'"
+		return 1
+	fi
+}
+
+olsrd2_interface_needs_adding()
+{
+	local interface
+
+	# likely and cheap operation:
+	olsrd_interface_already_in_config && return 1
+
+	for interface in $(olsrd2_list_configured_interfaces); do {
+		[ "$interface" = "$INTERFACE" ] && {
+			olsrd2_interface_already_in_config || return 0
+		}
+	} done
+
+	olsrd2_log debug "[OK] interface '$INTERFACE' => '$DEVICE' not used for olsrd"
+	return 1
+}
+
+case "$ACTION" in
+	ifup)
+		# only work after the first normal startup
+		# also: no need to test, if enabled
+		[ -e '/var/etc/olsrd2.conf' ] && {
+			# used global vars:
+			# INTERFACE = e.g. 'wlanadhocRADIO1' or 'cfg144d8f'
+			# DEVICE    = e.g. 'wlan1-1'
+			olsrd2_interface_needs_adding && {
+				# UCI-fication
+				. /etc/init.d/olsrd2
+				output_config >'/var/etc/olsrd2.conf'
+
+				# rereads config
+				kill -SIGHUP $(pidof olsrd2)
+			}
+		}
+	;;
+esac
diff --git a/openwrt/olsrd2-git/files/olsrd2.init b/openwrt/olsrd2-git/files/olsrd2.init
index 9da236d..b8c5641 100755
--- a/openwrt/olsrd2-git/files/olsrd2.init
+++ b/openwrt/olsrd2-git/files/olsrd2.init
@@ -1,19 +1,129 @@
 #!/bin/sh /etc/rc.common
 
 START=82
-APP=/usr/sbin/olsrd2
-ARGS="--load /etc/olsrd2.conf --set global.fork=true --set global.failfast=true"
 
-start() {
-	service_start $APP $ARGS
+output_config()
+{
+	local keyword value section ifname list_ifname i lan list_lan
+	local bool_yes='1|on|true|enabled|yes'
+	local bool_no='0|off|false|disabled|no'
+	local tab='	'
+
+	output_bool()
+	{
+		local value="$( uci -q get olsrd2.@${section}[0].$keyword )"
+
+		case "$value" in
+			'')
+				# ignore empty
+			;;
+			1|on|true|enabled|yes)
+				echo "${tab}${keyword}${tab}true"
+			;;
+			0|off|false|disabled|no)
+				echo "${tab}${keyword}${tab}no"
+			;;
+			*)
+				echo "${tab}${keyword}${tab}${value}"
+			;;
+		esac
+	}
+
+	output_string()
+	{
+		local value="$( uci -q get olsrd2.@${section}[0].$keyword )"
+		[ -n "$value" ] && echo "${tab}${keyword}${tab}${value}"
+	}
+
+	section='global'
+	echo "[$section]"
+
+	for keyword in fork failfast; do {
+		output_bool "$keyword"
+	} done
+
+
+	section='log'
+	echo
+	echo "[$section]"
+
+	for keyword in stderr syslog info debug; do {
+		output_bool "$keyword"
+	} done
+
+	for keyword in file; do {
+		output_string "$keyword"
+	} done
+
+	section='global'
+	echo
+	echo "[domain=0]"
+
+	for keyword in table; do {
+		output_string "$keyword"
+	} done
+
+	i=0; while uci -q get olsrd2.@plugin[$i] >/dev/null; do {
+		for value in $( uci show olsrd2.@plugin[$i] | cut -d'.' -f3 ); do {
+			case "$value" in
+				'name='*)
+					echo
+					echo "[${value#*=}]"
+				;;
+				*)
+					echo "${tab}${value%=*}${tab}${value#*=}"
+				;;
+			esac
+		} done
+
+		i=$(( $i + 1 ))
+	} done
+
+	i=0; while list_ifname="$( uci -q get olsrd2.@interface[$i].ifname )"; do {
+		lan=
+		for ifname in $list_ifname; do {
+			case "$( uci -q get olsrd2.@interface[$i].ignore )" in
+				1|on|true|enabled|yes)
+					# ignore/disabled interfaces, also affects LAN (local attached network)
+				;;
+				*)
+					devname="$( ifstatus "$ifname" | fgrep '"device": "' | cut -d'"' -f4 )"
+					[ -n "$devname" ] && {
+						echo
+						echo "[interface=$devname]${tab}# $ifname"
+
+						# only add once per section and only when at least 1 device per section is up
+						[ -z "$lan" ] && {
+							for lan in $( uci -q get olsrd2.@interface[$i].lan ); do {
+								lan_list="$lan_list $lan"
+							} done
+						}
+					}
+				;;
+			esac
+		} done
+
+		i=$(( $i + 1 ))
+	} done
+
+	[ -n "$lan_list" ] && {
+		echo
+		echo "[olsrv2]"
+
+		for lan in $lan_list; do {
+			echo "${tab}lan $lan"
+		} done
+	}
 }
 
-stop() {
-	service_stop $APP $ARGS
+start()
+{
+	output_config >'/var/etc/olsrd2.conf'
+	echo "$(cat /proc/uptime) firstwrite" >>/tmp/HPO
+	service_start /usr/sbin/olsrd2 --load /var/etc/olsrd2.conf
 }
 
-restart() {
-	stop
-	sleep 1
-	start
+stop()
+{
+	service_stop /usr/sbin/olsrd2
 }
diff --git a/openwrt/olsrd2-git/files/olsrd2.uci b/openwrt/olsrd2-git/files/olsrd2.uci
new file mode 100644
index 0000000..0dd0a12
--- /dev/null
+++ b/openwrt/olsrd2-git/files/olsrd2.uci
@@ -0,0 +1,25 @@
+config global
+	option 'fork' 		'yes'
+	option 'failfast'	'no'
+	option 'table'		'234'
+
+config log
+	option 'file'		'/var/log/olsrd2.log'
+	option 'stderr'		'true'
+	option 'syslog'		'true'
+#	option 'info'		'all'
+#	option 'debug'		'all'
+
+config plugin
+	option 'name' 'telnet'
+	option 'port' '2007'
+
+config interface
+	option 'ifname' 'loopback'
+#	option 'ignore' '1'
+#	option 'lan'	'10.63.21.64/26'
+
+config interface
+	list 'ifname' 'wan lan wlanadhoc wlanadhocRADIO1'
+#	list 'lan'	'::/0'
+#	list 'lan'	'0.0.0.0/0'
-- 
1.8.1.2

