Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tunneldigger.init: add support for multiple tunneldigger sections #198

Closed
wants to merge 2 commits into from
Closed

tunneldigger.init: add support for multiple tunneldigger sections #198

wants to merge 2 commits into from

Conversation

pmelange
Copy link

@pmelange pmelange commented Oct 9, 2018

The init script was not successfully starting tunneldigger when there
were multiple tunneldigger sections specified in the config file. The
config_cb has been substitued with a handle_td function which is called
with a config_foreach, thereby simplifying the script.

Signed-off-by: pmelange [email protected]

The init script was not successfully starting tunneldigger when there
were multiple tunneldigger sections specified in the config file.  The
config_cb has been substitued with a handle_td function which is called
with a config_foreach, thereby simplifying the script.

Signed-off-by: pmelange <[email protected]>
@pmelange
Copy link
Author

Is there any change that this PR can be merged? We, the freifunk-berlin community, would like to use this. Currently we are patching the file in our build, but we would prefer not to have to patch

Thanks.

An issue arrises when a router is not connected to a wan and only meshes.
The wan is not connected to anything, yet tunneldigger tries to make a
connection anyhow.  To aviod this, the init script has been modified
to _not_ make tunnels when they are configured with the bind_interface
option.  Additionally, a hotplug script has been created to create
and destroy tunnels on demand as the interfaces go up and down.  The
hoplug script only works with tunnels configured with the bind_interface
option.

More info at freifunk-berlin/firmware#606

Signed-off-by: pmelange <[email protected]>
@pmelange
Copy link
Author

Another issue with the init script has surfaced. Support for tunneldigger interface which are configured with the bind_interface option are now handled with a hotplug script.

see freifunk-berlin/firmware#606

@mweinelt
Copy link
Contributor

Can we get a review from someone familiar with the tunneldigger package? @lcb01a maybe?

@rotanid
Copy link
Member

rotanid commented Feb 15, 2019

@pmelange maybe you can find someone else doing a review of this yourself?
the active developers of Gluon don't use tunneldigger and we don't want to merge something like this without a review...

@pmelange
Copy link
Author

I had only wanted to offer my changes, but if they are not important to gluon, then I can just simply withdraw the PR.

@pmelange pmelange closed this Feb 21, 2019
@pmelange pmelange deleted the tunneldigger_init branch February 21, 2019 17:53
@mweinelt
Copy link
Contributor

Most of us honestly lack the setup to test tunneldigger and people who have submitted the tunneldigger integration have since went silent. Sorry this isn't happening.

@pmelange
Copy link
Author

@mweinelt, It's really no problem. I'm developing it all further for freifunk-berlin.

@mweinelt
Copy link
Contributor

For posterity

From 4d8e5f2534f3c59781008927128c917e6426048d Mon Sep 17 00:00:00 2001
From: pmelange <[email protected]>
Date: Tue, 9 Oct 2018 11:42:18 +0200
Subject: [PATCH 1/2] tunneldigger.init: add support for multiple tunneldigger
 sections

The init script was not successfully starting tunneldigger when there
were multiple tunneldigger sections specified in the config file.  The
config_cb has been substitued with a handle_td function which is called
with a config_foreach, thereby simplifying the script.

Signed-off-by: pmelange <[email protected]>
---
 net/tunneldigger/files/tunneldigger.init | 126 ++++++++++++-----------
 1 file changed, 67 insertions(+), 59 deletions(-)

diff --git a/net/tunneldigger/files/tunneldigger.init b/net/tunneldigger/files/tunneldigger.init
index 09d08cf3..94f63a87 100755
--- a/net/tunneldigger/files/tunneldigger.init
+++ b/net/tunneldigger/files/tunneldigger.init
@@ -8,72 +8,80 @@ PIDPATH=/var/run
 tunnel_id=1
 
 missing() {
-	echo "Not starting tunneldigger - missing $1" >&2
+	echo "Not starting tunneldigger \"$1\" - missing $2" >&2
 }
 
-config_cb() {
-	local cfg="$CONFIG_SECTION"
-	config_get configname "$cfg" TYPE
-	case "$configname" in
-		broker)
-			config_get_bool enabled "$cfg" enabled 1
-			config_get addresses "$cfg" address
-			config_get uuid "$cfg" uuid
-			config_get interface "$cfg" interface
-			config_get group "$cfg" group
-			config_get limit_bw_down "$cfg" limit_bw_down
-			config_get hook_script "$cfg" hook_script
-			config_get bind_interface "$cfg" bind_interface
-			config_get broker_selection "$cfg" broker_selection
+handle_td() {
+	local cfg=$1
+	local enabled
+	local addresses
+	local uuid
+	local interface
+	local group
+	local limit_bw_down
+	local hook_script
+	local bind_interface
+	local broker_selection
+
+
+	config_get_bool enabled "$cfg" enabled 1
+	config_get addresses "$cfg" address
+	config_get uuid "$cfg" uuid
+	config_get interface "$cfg" interface
+	config_get group "$cfg" group
+	config_get limit_bw_down "$cfg" limit_bw_down
+	config_get hook_script "$cfg" hook_script
+	config_get bind_interface "$cfg" bind_interface
+	config_get broker_selection "$cfg" broker_selection
 			
-			[ $enabled -eq 0 ] && return
-
-			local broker_opts=""
-			for address in $addresses; do
-			  append broker_opts "-b ${address}"
-			done
-
-			[ ! -z "${limit_bw_down}" ] && append broker_opts "-L ${limit_bw_down}"
-			[ ! -z "${hook_script}" ] && append broker_opts "-s ${hook_script}"
-			[ ! -z "${bind_interface}" ] && {
-				# Resolve logical interface name.
-				unset _bind_interface
-				network_get_device _bind_interface "${bind_interface}" || _bind_interface="${bind_interface}"
-				append broker_opts "-I ${_bind_interface}"
-			}
-			[ ! -z "${broker_selection}" ] && {
-				# Set broker selection.
-				case "${broker_selection}" in
-					usage)
-						append broker_opts "-a"
-					;;
-					first)
-						append broker_opts "-g"
-					;;
-					random)
-						append broker_opts "-r"
-					;;
-				esac
-			}
-
-			if [ -z "$uuid" ]; then
-				missing uuid
-				return
-			elif [ -z "$interface" ]; then
-				missing interface
-				return
-			fi
-
-			echo "Starting tunneldigger on ${interface}"
-			/sbin/start-stop-daemon -S -q -b -m -c root:${group} -p ${PIDPATH}/tunneldigger.${interface}.pid -x /usr/bin/tunneldigger -- -u ${uuid} -i ${interface} -t ${tunnel_id} ${broker_opts}
-
-			let tunnel_id++
-		;;
-	esac
+	[ $enabled -eq 0 ] && return
+
+	local broker_opts=""
+	local address
+	for address in $addresses; do
+		append broker_opts "-b ${address}"
+	done
+
+	[ ! -z "${limit_bw_down}" ] && append broker_opts "-L ${limit_bw_down}"
+	[ ! -z "${hook_script}" ] && append broker_opts "-s ${hook_script}"
+	[ ! -z "${bind_interface}" ] && {
+		# Resolve logical interface name.
+		unset _bind_interface
+		network_get_device _bind_interface "${bind_interface}" || _bind_interface="${bind_interface}"
+		append broker_opts "-I ${_bind_interface}"
+	}
+	[ ! -z "${broker_selection}" ] && {
+		# Set broker selection.
+		case "${broker_selection}" in
+			usage)
+				append broker_opts "-a"
+			;;
+			first)
+				append broker_opts "-g"
+			;;
+			random)
+				append broker_opts "-r"
+			;;
+		esac
+	}
+
+	if [ -z "$uuid" ]; then
+		missing $cfg uuid
+		return
+	elif [ -z "$interface" ]; then
+		missing $cfg interface
+		return
+	fi
+
+	echo "Starting tunneldigger \"$cfg\" on ${interface}"
+	/sbin/start-stop-daemon -S -q -b -m -c root:${group} -p ${PIDPATH}/tunneldigger.${interface}.pid -x /usr/bin/tunneldigger -- -u ${uuid} -i ${interface} -t ${tunnel_id} ${broker_opts}
+
+	let tunnel_id++
 }
 
 start() {
 	config_load tunneldigger
+	config_foreach handle_td broker
 }
 
 stop() {

From 680f968bca44cc0d3f283c44c248b5de32048488 Mon Sep 17 00:00:00 2001
From: pmelange <[email protected]>
Date: Wed, 24 Oct 2018 11:33:30 +0200
Subject: [PATCH 2/2] update tunneldigger files to add hotplug functionality

An issue arrises when a router is not connected to a wan and only meshes.
The wan is not connected to anything, yet tunneldigger tries to make a
connection anyhow.  To aviod this, the init script has been modified
to _not_ make tunnels when they are configured with the bind_interface
option.  Additionally, a hotplug script has been created to create
and destroy tunnels on demand as the interfaces go up and down.  The
hoplug script only works with tunnels configured with the bind_interface
option.

More info at https://github.com/freifunk-berlin/firmware/issues/606

Signed-off-by: pmelange <[email protected]>
---
 net/tunneldigger/Makefile                   |   2 +
 net/tunneldigger/files/tunneldigger.hotplug | 114 ++++++++++++++++++++
 net/tunneldigger/files/tunneldigger.init    |  17 ++-
 3 files changed, 122 insertions(+), 11 deletions(-)
 create mode 100644 net/tunneldigger/files/tunneldigger.hotplug

diff --git a/net/tunneldigger/Makefile b/net/tunneldigger/Makefile
index f46bc1d4..38c67a05 100644
--- a/net/tunneldigger/Makefile
+++ b/net/tunneldigger/Makefile
@@ -33,6 +33,8 @@ define Package/tunneldigger/install
 	$(INSTALL_BIN) $(PKG_BUILD_DIR)/tunneldigger $(1)/usr/bin/tunneldigger
 	$(INSTALL_DIR) $(1)/etc/init.d
 	$(INSTALL_BIN) ./files/tunneldigger.init $(1)/etc/init.d/tunneldigger
+	$(INSTALL_DIR) $(1)/etc/hotplug.d/iface
+	$(INSTALL_BIN) ./files/tunneldigger.hotplug $(1)/etc/hotplug.d/iface/60-tunneldigger
 	$(INSTALL_DIR) $(1)/etc/config
 	$(INSTALL_DATA) ./files/config.default $(1)/etc/config/tunneldigger
 endef
diff --git a/net/tunneldigger/files/tunneldigger.hotplug b/net/tunneldigger/files/tunneldigger.hotplug
new file mode 100644
index 00000000..68709b2a
--- /dev/null
+++ b/net/tunneldigger/files/tunneldigger.hotplug
@@ -0,0 +1,114 @@
+#!/bin/sh
+
+. /lib/functions.sh
+. /lib/functions/network.sh
+
+PIDPATH=/var/run
+tunnel_id=0
+
+missing() {
+	logger -t td-client "Not starting tunneldigger \"$1\" - missing $2" >&2
+}
+
+handle_td_ifup() {
+	local cfg=$1
+	local enabled
+	local addresses
+	local uuid
+	local interface
+	local group
+	local limit_bw_down
+	local hook_script
+	local bind_interface
+	local broker_selection
+
+
+	config_get_bool enabled "$cfg" enabled 1
+	config_get addresses "$cfg" address
+	config_get uuid "$cfg" uuid
+	config_get interface "$cfg" interface
+	config_get group "$cfg" group
+	config_get limit_bw_down "$cfg" limit_bw_down
+	config_get hook_script "$cfg" hook_script
+	config_get bind_interface "$cfg" bind_interface
+	config_get broker_selection "$cfg" broker_selection
+
+	let tunnel_id++
+
+	[ $enabled -eq 0 ] && return
+
+	[ "$INTERFACE" != "${bind_interface}" ] && return
+
+	local broker_opts=""
+	local address
+	for address in $addresses; do
+		append broker_opts "-b ${address}"
+	done
+
+	[ ! -z "${limit_bw_down}" ] && append broker_opts "-L ${limit_bw_down}"
+	[ ! -z "${hook_script}" ] && append broker_opts "-s ${hook_script}"
+	[ ! -z "${bind_interface}" ] && {
+		# Resolve logical interface name.
+		unset _bind_interface
+		network_get_device _bind_interface "${bind_interface}" || _bind_interface="${bind_interface}"
+		append broker_opts "-I ${_bind_interface}"
+	}
+	[ ! -z "${broker_selection}" ] && {
+		# Set broker selection.
+		case "${broker_selection}" in
+			usage)
+				append broker_opts "-a"
+			;;
+			first)
+				append broker_opts "-g"
+			;;
+			random)
+				append broker_opts "-r"
+			;;
+		esac
+	}
+
+	if [ -z "$uuid" ]; then
+		missing $cfg uuid
+		return
+	elif [ -z "$interface" ]; then
+		missing $cfg interface
+		return
+	fi
+
+	logger -t td-client "Starting tunneldigger \"$cfg\" on ${interface}"
+	/sbin/start-stop-daemon -S -q -b -m -c root:${group} -p ${PIDPATH}/tunneldigger.${interface}.pid -x /usr/bin/tunneldigger -- -u ${uuid} -i ${interface} -t ${tunnel_id} ${broker_opts}
+}
+
+handle_td_ifdown() {
+	local cfg=$1
+	local enabled
+	local interface
+	local bind_interface
+
+	config_get_bool enabled "$cfg" enabled 1
+	config_get interface "$cfg" interface
+	config_get bind_interface "$cfg" bind_interface
+
+	if [ "$INTERFACE" = "${bind_interface}" ]; then
+		local PIDFILE=${PIDPATH}/tunneldigger.${interface}.pid
+		local PID="$(cat ${PIDFILE})"
+		logger -t td-client "Stopping tunneldigger \"$cfg\" on ${interface} PIDFILE=${PIDFILE}"
+		/sbin/start-stop-daemon -K -q -p $PIDFILE
+		while test -d "/proc/${PID}"; do
+			logger -t td-client "  waiting for tunneldigger to stop"
+			sleep 1
+		done
+		logger -t td-client "  tunneldigger stopped"
+	fi
+}
+
+config_load tunneldigger
+if [ "$ACTION" = ifup ]; then
+	config_foreach handle_td_ifup broker
+fi
+
+if [ "$ACTION" = ifdown ]; then
+	config_foreach handle_td_ifdown broker
+fi
+
diff --git a/net/tunneldigger/files/tunneldigger.init b/net/tunneldigger/files/tunneldigger.init
index 94f63a87..1b2a1c7c 100755
--- a/net/tunneldigger/files/tunneldigger.init
+++ b/net/tunneldigger/files/tunneldigger.init
@@ -5,7 +5,7 @@
 START=90
 
 PIDPATH=/var/run
-tunnel_id=1
+tunnel_id=0
 
 missing() {
 	echo "Not starting tunneldigger \"$1\" - missing $2" >&2
@@ -23,7 +23,6 @@ handle_td() {
 	local bind_interface
 	local broker_selection
 
-
 	config_get_bool enabled "$cfg" enabled 1
 	config_get addresses "$cfg" address
 	config_get uuid "$cfg" uuid
@@ -33,8 +32,12 @@ handle_td() {
 	config_get hook_script "$cfg" hook_script
 	config_get bind_interface "$cfg" bind_interface
 	config_get broker_selection "$cfg" broker_selection
-			
+
+	let tunnel_id++
+
 	[ $enabled -eq 0 ] && return
+	# The hotplug script will take care of tunnels bound to an interface
+	[ ! -z "${bind_interface}" ] && return
 
 	local broker_opts=""
 	local address
@@ -44,12 +47,6 @@ handle_td() {
 
 	[ ! -z "${limit_bw_down}" ] && append broker_opts "-L ${limit_bw_down}"
 	[ ! -z "${hook_script}" ] && append broker_opts "-s ${hook_script}"
-	[ ! -z "${bind_interface}" ] && {
-		# Resolve logical interface name.
-		unset _bind_interface
-		network_get_device _bind_interface "${bind_interface}" || _bind_interface="${bind_interface}"
-		append broker_opts "-I ${_bind_interface}"
-	}
 	[ ! -z "${broker_selection}" ] && {
 		# Set broker selection.
 		case "${broker_selection}" in
@@ -75,8 +72,6 @@ handle_td() {
 
 	echo "Starting tunneldigger \"$cfg\" on ${interface}"
 	/sbin/start-stop-daemon -S -q -b -m -c root:${group} -p ${PIDPATH}/tunneldigger.${interface}.pid -x /usr/bin/tunneldigger -- -u ${uuid} -i ${interface} -t ${tunnel_id} ${broker_opts}
-
-	let tunnel_id++
 }
 
 start() {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants