summaryrefslogtreecommitdiffstats
path: root/src.etc/network/if-up.d/dhcpd
blob: 1f86512553ca11c9a5823fd1e8cca3ed040d37bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh

. /etc/network/functions

set -eu

dhcpd_enabled=false
address=
subnet=
gateway=

dhcpd_start_config()
{
	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-*)
			if ! ${dhcpd_enabled}; then
				dhcpd_start_config
				dhcpd_enabled=true
			fi
			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_end_config()
{
	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_opt_cb
if ${dhcpd_enabled}; then
	dhcpd_end_config
fi