summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2014-08-06 10:42:01 (EDT)
committer P. J. McDermott <pj@pehjota.net>2014-08-06 10:42:01 (EDT)
commit96a68750a1ba9791291a94db2f73d1c3b3ffdf20 (patch)
treed65925bb2b17ee2764ff2736ac2930a1c4d764a3
parent565110e81cafa811f2e354360ace081d7784c5f8 (diff)
Add networking hooks for udhcpd
-rw-r--r--src.etc/network/functions41
-rw-r--r--src.etc/network/if-down.d/dhcpd7
-rw-r--r--src.etc/network/if-up.d/dhcpd56
3 files changed, 104 insertions, 0 deletions
diff --git a/src.etc/network/functions b/src.etc/network/functions
new file mode 100644
index 0000000..b74fb91
--- /dev/null
+++ b/src.etc/network/functions
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+case "${0}" in
+ /etc/network/if-*.d/*) ;;
+ *) exit 1;;
+esac
+if [ "x${IFACE}" = 'x' ]; then
+ exit 1
+fi
+
+get_iface_opts()
+{
+ local opts_start_cb="${1}"
+ local opt_cb="${2}"
+ local opts_end_cb="${3}"
+ local script=
+ local opts=
+ local opt=
+
+ script="$(printf '
+ /^iface[ \\t][ \\t]*%s/,/^[^ \\t]/{
+ /^$/q;
+ s/^[ \\t][ \\t]*//p;
+ }
+ ' "${IFACE}")"
+
+ opts="$(sed -n "${script}" /var/run/net-ifaces)"
+ if [ "x${opts}" = 'x' ]; then
+ return 0
+ fi
+
+ ${opts_start_cb}
+ while read -r opt; do
+ ${opt_cb} ${opt}
+ done <<-EOF
+ ${opts}
+ EOF
+ ${opts_end_cb}
+
+ return 0
+}
diff --git a/src.etc/network/if-down.d/dhcpd b/src.etc/network/if-down.d/dhcpd
new file mode 100644
index 0000000..b6ca719
--- /dev/null
+++ b/src.etc/network/if-down.d/dhcpd
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+set -eu
+
+if [ -f "/var/run/udhcpd.${IFACE}" ]; then
+ kill "$(cat "/var/run/udhcpd.${IFACE}")"
+fi
diff --git a/src.etc/network/if-up.d/dhcpd b/src.etc/network/if-up.d/dhcpd
new file mode 100644
index 0000000..5fcc18e
--- /dev/null
+++ b/src.etc/network/if-up.d/dhcpd
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+. /etc/network/functions
+
+set -eu
+
+address=
+subnet=
+gateway=
+
+dhcpd_opts_start_cb()
+{
+ exec 3>"/var/run/udhcpd.${IFACE}.conf"
+ printf 'interface %s\n' "${IFACE}" >&3
+ printf 'pidfile /var/run/udhcpd.%s.pid\n' "${IFACE}" >&3
+ printf 'lease_file /var/lib/misc/udhcpd.%s.leases\n' "${IFACE}" >&3
+}
+
+dhcpd_opt_cb()
+{
+ local key="${1}"
+ shift 1
+ local val="${*}"
+
+ case "${key}" in
+ dhcpd-*)
+ key="$(printf '%s\n' "${key#dhcpd-}" | tr '-' '_')"
+ printf '%s %s\n' "${key}" "${val}" >&3
+ ;;
+ address)
+ address="${val}"
+ ;;
+ subnet)
+ subnet="${val}"
+ ;;
+ gateway)
+ gateway="${val}"
+ ;;
+ esac
+}
+
+dhcpd_opts_end_cb()
+{
+ printf 'option subnet %s\n' "${subnet}" >&3
+ if [ "x${gateway}" != 'x' ]; then
+ printf 'option router %s\n' "${gateway}" >&3
+ else
+ printf 'option router %s\n' "${address}" >&3
+ fi
+ sed 's/nameserver/option dns/' /etc/resolv.conf >&3
+ exec &3>-
+
+ udhcpd -S "/var/run/udhcpd.${IFACE}.conf"
+}
+
+get_iface_opts dhcpd_opts_start_cb dhcpd_opt_cb dhcpd_opts_end_cb