| 1 |
# Copyright 2004-2007 Gentoo Foundation
|
| 2 |
# Distributed under the terms of the GNU General Public License v2
|
| 3 |
|
| 4 |
arping_depend() {
|
| 5 |
program /sbin/arping /usr/sbin/arping2
|
| 6 |
before interface
|
| 7 |
}
|
| 8 |
|
| 9 |
arping_address() {
|
| 10 |
local ip=${1%%/*} mac="$2" spoof="$3" foundmac= i= w= opts=
|
| 11 |
|
| 12 |
# We only handle IPv4 addresses
|
| 13 |
case "${ip}" in
|
| 14 |
0.0.0.0|0) return 1 ;;
|
| 15 |
*.*.*.*) ;;
|
| 16 |
*) return 1 ;;
|
| 17 |
esac
|
| 18 |
|
| 19 |
# We need to bring the interface up to test
|
| 20 |
_exists "${iface}" || return 1
|
| 21 |
_up "${iface}"
|
| 22 |
|
| 23 |
eval w=\$arping_wait_${IFVAR}
|
| 24 |
[ -z "${w}" ] && w=${arping_wait:-5}
|
| 25 |
|
| 26 |
if type arping2 >/dev/null 2>&1; then
|
| 27 |
[ -z "$(_get_inet_address)" ] && opts="${opts} -0"
|
| 28 |
[ -n "${spoof}" ] && opts="${opts} -S ${spoof}"
|
| 29 |
while [ ${w} -gt 0 -a -z "${foundmac}" ]; do
|
| 30 |
foundmac="$(arping2 ${opts} -r -c 1 -0 -i "${IFACE}" "${ip}" 2>/dev/null | \
|
| 31 |
sed -e 'y/abcdef/ABCDEF/')"
|
| 32 |
w=$((${w} - 1))
|
| 33 |
done
|
| 34 |
else
|
| 35 |
[ -z "$(_get_inet_address)" ] && opts="${opts} -D"
|
| 36 |
|
| 37 |
foundmac="$(arping -w "${w}" ${opts} -f -I "${IFACE}" "${ip}" 2>/dev/null | \
|
| 38 |
sed -n -e 'y/abcdef/ABCDEF/' -e 's/.*\[\([^]]*\)\].*/\1/p')"
|
| 39 |
fi
|
| 40 |
[ -z "${foundmac}" ] && return 1
|
| 41 |
|
| 42 |
if [ -n "${mac}" ] ; then
|
| 43 |
if [ "${mac}" != "${foundmac}" ] ; then
|
| 44 |
vewarn "Found ${ip} but MAC ${foundmac} does not match"
|
| 45 |
return 1
|
| 46 |
fi
|
| 47 |
fi
|
| 48 |
|
| 49 |
return 0
|
| 50 |
}
|
| 51 |
|
| 52 |
_arping_in_config() {
|
| 53 |
_get_array "config_${IFVAR}" | while read i; do
|
| 54 |
[ "${i}" = "arping" ] && return 1
|
| 55 |
done
|
| 56 |
return 1
|
| 57 |
}
|
| 58 |
|
| 59 |
arping_start() {
|
| 60 |
local gateways= x= conf= i=
|
| 61 |
einfo "Pinging gateways on ${IFACE} for configuration"
|
| 62 |
|
| 63 |
eval gateways=\$gateways_${IFVAR}
|
| 64 |
if [ -z "${gateways}" ] ; then
|
| 65 |
eerror "No gateways have been defined (gateways_${IFVAR}=\"...\")"
|
| 66 |
return 1
|
| 67 |
fi
|
| 68 |
|
| 69 |
eindent
|
| 70 |
|
| 71 |
for x in ${gateways}; do
|
| 72 |
local IFS=,
|
| 73 |
set -- ${x}
|
| 74 |
local ip=$1 mac=$2 spoof=$3 extra=
|
| 75 |
unset IFS
|
| 76 |
|
| 77 |
if [ -n "${mac}" ] ; then
|
| 78 |
mac="$(echo "${mac}" | tr '[:lower:]' '[:upper:]')"
|
| 79 |
extra="(MAC ${mac})"
|
| 80 |
fi
|
| 81 |
|
| 82 |
vebegin "${ip} ${extra}"
|
| 83 |
if arping_address "${ip}" "${mac}" "${spoof}" ; then
|
| 84 |
local IFS=.
|
| 85 |
for i in ${ip} ; do
|
| 86 |
if [ "${#i}" = "2" ] ; then
|
| 87 |
conf="${conf}0${i}"
|
| 88 |
elif [ "${#i}" = "1" ] ; then
|
| 89 |
conf="${conf}00${i}"
|
| 90 |
else
|
| 91 |
conf="${conf}${i}"
|
| 92 |
fi
|
| 93 |
done
|
| 94 |
unset IFS
|
| 95 |
[ -n "${mac}" ] && conf="${conf}_$(echo "${mac}" | sed -e 's/://g')"
|
| 96 |
|
| 97 |
eend 0
|
| 98 |
eoutdent
|
| 99 |
veinfo "Configuring ${IFACE} for ${ip} ${extra}"
|
| 100 |
_configure_variables ${conf}
|
| 101 |
|
| 102 |
# Call the system module as we've aleady passed it by ....
|
| 103 |
# And it *has* to be pre_start for other things to work correctly
|
| 104 |
system_pre_start
|
| 105 |
|
| 106 |
# Ensure that we have a valid config - ie arping is no longer there
|
| 107 |
local IFS="$__IFS"
|
| 108 |
for i in $(_get_array "config_${IFVAR}"); do
|
| 109 |
if [ "${i}" = "arping" ]; then
|
| 110 |
eend 1 "No config found for ${ip} (config_${conf}=\"...\")"
|
| 111 |
continue 2
|
| 112 |
fi
|
| 113 |
done
|
| 114 |
unset IFS
|
| 115 |
|
| 116 |
_load_config
|
| 117 |
return 0
|
| 118 |
fi
|
| 119 |
veend 1
|
| 120 |
done
|
| 121 |
|
| 122 |
eoutdent
|
| 123 |
return 1
|
| 124 |
}
|
| 125 |
|
| 126 |
# vim: set ts=4 :
|