| 1 |
# Copyright (c) 2004-2006 Gentoo Foundation |
| 2 |
# Distributed under the terms of the GNU General Public License v2 |
| 3 |
# Contributed by Roy Marples (uberlord@gentoo.org) |
| 4 |
|
| 5 |
# Fix any potential localisation problems |
| 6 |
# Note that LC_ALL trumps LC_anything_else according to locale(7) |
| 7 |
dhclient() { |
| 8 |
LC_ALL=C /sbin/dhclient "$@" |
| 9 |
} |
| 10 |
|
| 11 |
# void dhclient_depend(void) |
| 12 |
# |
| 13 |
# Sets up the dependancies for the module |
| 14 |
dhclient_depend() { |
| 15 |
after interface |
| 16 |
provide dhcp |
| 17 |
functions interface_exists interface_get_address |
| 18 |
} |
| 19 |
|
| 20 |
# void dhclient_expose(void) |
| 21 |
# |
| 22 |
# Expose variables that can be configured |
| 23 |
dhclient_expose() { |
| 24 |
variables dhclient dhcp |
| 25 |
} |
| 26 |
|
| 27 |
# bool dhclient_check_installed(void) |
| 28 |
# |
| 29 |
# Returns 1 if dhclient is installed, otherwise 0 |
| 30 |
dhclient_check_installed() { |
| 31 |
[[ -x /sbin/dhclient ]] && return 0 |
| 32 |
${1:-false} && eerror "For DHCP (dhclient) support, emerge net-misc/dhcp" |
| 33 |
return 1 |
| 34 |
} |
| 35 |
|
| 36 |
# bool dhclient_stop(char *iface) |
| 37 |
# |
| 38 |
# Stop dhclient on an interface |
| 39 |
# Always returns 0 |
| 40 |
dhclient_stop() { |
| 41 |
local iface="$1" pidfile="/var/run/dhclient-$1.pid" |
| 42 |
|
| 43 |
[[ ! -f ${pidfile} ]] && return 0 |
| 44 |
|
| 45 |
ebegin "Stopping dhclient on ${iface}" |
| 46 |
local ifvar="$(bash_variable "${iface}")" |
| 47 |
local d="dhcp_${ifvar}" |
| 48 |
[[ -z ${!d} ]] && d="dhcp" |
| 49 |
if [[ " ${!d} " == *" release "* ]] ; then |
| 50 |
dhclient -q -r -pf "${pidfile}" "${iface}" |
| 51 |
else |
| 52 |
start-stop-daemon --stop --exec /sbin/dhclient --pidfile "${pidfile}" |
| 53 |
fi |
| 54 |
eend $? |
| 55 |
} |
| 56 |
|
| 57 |
# bool dhclient_start(char *iface) |
| 58 |
# |
| 59 |
# Start DHCP on an interface by calling dhclient $iface $options |
| 60 |
# |
| 61 |
# Returns 0 (true) when a DHCP address is obtained, otherwise 1 |
| 62 |
dhclient_start() { |
| 63 |
local iface="$1" ifvar="$(bash_variable "$1")" dhconf= |
| 64 |
local pidfile="/var/run/dhclient-${iface}.pid" |
| 65 |
|
| 66 |
interface_exists "${iface}" true || return 1 |
| 67 |
|
| 68 |
# Load our default options |
| 69 |
opts="dhclient_${ifvar}" |
| 70 |
opts="${!opts}" |
| 71 |
|
| 72 |
local d="dhcp_${ifvar}" |
| 73 |
[[ -z ${!d} ]] && d="dhcp" |
| 74 |
|
| 75 |
# Add our peer and metric options |
| 76 |
if [[ " ${!d} " == *" nogateway "* ]] ; then |
| 77 |
opts="${opts} -e PEER_ROUTERS=no" |
| 78 |
elif [[ " ${opts} " != *" -e PEER_ROUTERS="* ]] ; then |
| 79 |
opts="${opts} -e PEER_ROUTERS=yes" |
| 80 |
fi |
| 81 |
if [[ " ${!d} " == *" nodns "* ]] ; then |
| 82 |
opts="${opts} -e PEER_DNS=no" |
| 83 |
elif [[ " ${opts} " != *" -e PEER_DNS="* ]] ; then |
| 84 |
opts="${opts} -e PEER_DNS=yes" |
| 85 |
fi |
| 86 |
if [[ " ${!d} " == *" nontp "* ]] ; then |
| 87 |
opts="${opts} -e PEER_NTP=no" |
| 88 |
elif [[ " ${opts} " != *" -e PEER_NTP="* ]] ; then |
| 89 |
opts="${opts} -e PEER_NTP=yes" |
| 90 |
fi |
| 91 |
local metric="metric_${ifvar}" |
| 92 |
if [[ -n ${!metric} && ${!metric} != "0" ]] ; then |
| 93 |
opts="${opts} -e IF_METRIC=${!metric}" |
| 94 |
fi |
| 95 |
|
| 96 |
# Send our hostname by editing cffile |
| 97 |
if [[ " ${!d} " != *" nosendhost "* ]] ; then |
| 98 |
local hname="$(hostname)" |
| 99 |
if [[ ${hname} != "(none)" && ${hname} != "localhost" ]]; then |
| 100 |
dhconf="${dhconf} interface \"${iface}\" {\n" |
| 101 |
dhconf="${dhconf} send host-name \"${hname}\"\n;" |
| 102 |
dhconf="${dhconf}}" |
| 103 |
fi |
| 104 |
fi |
| 105 |
|
| 106 |
# Bring up DHCP for this interface (or alias) |
| 107 |
ebegin "Running dhclient" |
| 108 |
echo -e "${dhconf}" | start-stop-daemon --start --exec /sbin/dhclient \ |
| 109 |
--pidfile "${pidfile}" -- ${opts} -q -1 -pf "${pidfile}" |
| 110 |
eend $? || return 1 |
| 111 |
|
| 112 |
# DHCP succeeded, show address retrieved |
| 113 |
local addr="$(interface_get_address "${iface}")" |
| 114 |
einfo "${iface} received address ${addr}" |
| 115 |
|
| 116 |
return 0 |
| 117 |
} |
| 118 |
|
| 119 |
# vim: set ts=4 : |