summaryrefslogtreecommitdiff
blob: 3c646fe935f7c5a1c23c516154160f50a2bdda07 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright (c) 2004-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# Contributed by Roy Marples (uberlord@gentoo.org)

# Fix any potential localisation problems
# Note that LC_ALL trumps LC_anything_else according to locale(7)
udhcpc() {
	LC_ALL=C /sbin/udhcpc "$@"
}

# void udhcpc_depend(void)
#
# Sets up the dependancies for the module
udhcpc_depend() {
	after interface
	provide dhcp
	functions interface_exists interface_get_address
}

# void udhcpc_expose(void)
#
# Expose variables that can be configured
udhcpc_expose() {
	variables udhcpc dhcp
}

# bool udhcpc_check_installed(void)
#
# Returns 1 if udhcpc is installed, otherwise 0
udhcpc_check_installed() {
	[[ -x /sbin/udhcpc ]] && return 0
	${1:-false} && eerror "For DHCP (udhcpc) support, emerge net-misc/udhcp"
	return 1
}

# bool udhcpc_stop(char *iface)
#
# Stops udhcpc running on an interface
# Return 1 if we fail to stop udhcpc (if it's running) otherwise 0
udhcpc_stop() {
	local iface="$1" pidfile="/var/run/udhcpc-$1.pid" d

	[[ ! -f ${pidfile} ]] && return 0

	ebegin "Stopping udhcpc on ${iface}"
	local pid="$(<"${pidfile}")" e=true

	local ifvar="$(bash_variable "${iface}")"
	d="dhcp_${ifvar}"
	[[ -z ${!d} ]] && d="dhcp" 

	if [[ " ${!d} " == *" release "* ]]; then
		kill -s USR2 "${pid}" &>/dev/null
		[[ -f "/var/cache/udhcpc-${iface}.lease" ]] \
			&& rm "/var/cache/udhcpc-${iface}.lease"
	fi

	start-stop-daemon --stop --exec /sbin/udhcpc --pidfile "${pidfile}"
	eend $? || return 1

	[[ -e /var/run/udhcpc-"${iface}".conf ]] \
		&& rm -f /var/run/udhcpc-"${iface}".conf
	return 0
}

# bool udhcpc_start(char *iface)
#
# Start DHCP on an interface by calling udhcpc $iface $options
#
# Returns 0 (true) when a DHCP address is obtained, otherwise 1
udhcpc_start() {
	local iface="$1" opts pidfile="/var/run/udhcpc-$1.pid"
	local cachefile="/var/cache/udhcpc-$1.lease" d

	interface_exists "${iface}" true || return 1

	local ifvar="$(bash_variable "${iface}" )" opts 
	opts="udhcpc_${ifvar}"
	opts="${!opts}"

	d="dhcp_${ifvar}"
	[[ -z ${!d} ]] && d="dhcp"

	if [[ " ${!d} " != *" nosendhost "* ]]; then
		if [[ ! " ${opts}" =~ " -([hH] |-hostname=)" ]]; then
			local hname="$(hostname)"
			[[ -n ${hname} && ${hname} != "(none)" && ${hname} != "localhost" ]] \
				&& opts="${opts} --hostname=${hname}"
		fi
	fi

	# Setup options for the udhcpc script
	# We pass these via a seperate config file so we're nice and fast
	# maybe oneday, udhcp will accept custom env vars.
	local conf="/var/run/udhcpc-${iface}.conf"
	echo "# udhcpc runtime configuration for interface ${iface}" > "${conf}"
	if [[ " ${!d} " == *" nodns "* ]] ; then
		#opts="${opts} --env PEER_DNS=no"
		echo "PEER_DNS=no" >> "${conf}"
	else
		#opts="${opts} --env PEER_DNS=yes"
		echo "PEER_DNS=yes" >> "${conf}"
	fi
	if [[ " ${!d} " == *" nontp "* ]] ; then
		#opts="${opts} --env PEER_NTP=no"
		echo "PEER_NTP=no" >> "${conf}"
	else
		#opts="${opts} --env PEER_NTP=yes"
		echo "PEER_NTP=yes" >> "${conf}"
	fi
	local metric="metric_${ifvar}"
	if [[ -n ${!metric} ]] ; then
		#opts="${opts} --env IF_METRIC=${!metric}"
		echo "IF_METRIC=${!metric}" >> "${conf}"
	fi

	# Bring up DHCP for this interface (or alias)
	ebegin "Running udhcpc"

	# Try and load the cache if it exists
	if [[ -f ${cachefile} ]]; then
		if [[ " ${opts}" != *" --request="* && " ${opts} " != *" -r "* ]]; then
			local x="$(<"${cachefile}")"
			# Check for a valid ip
			[[ ${x} == *.*.*.* ]] && opts="${opts} --request=${x}"
		fi
	fi

	eval start-stop-daemon --start --exec /sbin/udhcpc \
		--pidfile "${pidfile}" \
		-- "${opts}" --interface="${iface}" --now --quiet \
		--pidfile="${pidfile}"
	eend $? || return 1

	# DHCP succeeded, show address retrieved
	local addr="$(interface_get_address "${iface}")"
	einfo "${iface} received address ${addr}"

	return 0
}

# vim: set ts=4 :