aboutsummaryrefslogtreecommitdiff
path: root/net.Linux/arping.sh
blob: 977b19111e0450a8332586bd4eff98dfcf8c14ba (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
143
144
145
146
147
148
149
150
151
# Copyright 2004-2007 Gentoo Foundation
# Copyright 2007 Roy Marples
# All rights reserved

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

arping_depend() {
	program /sbin/arping /usr/sbin/arping2
	before interface
}

arping_address() {
	local ip=${1%%/*} mac="$2" spoof="$3" foundmac= i= w= opts=

	# We only handle IPv4 addresses
	case "${ip}" in
		0.0.0.0|0) return 1 ;;
		*.*.*.*) ;;
		*) return 1 ;;
	esac

	# We need to bring the interface up to test
	_exists "${iface}" || return 1 
	_up "${iface}"

	eval w=\$arping_wait_${IFVAR}
	[ -z "${w}" ] && w=${arping_wait:-5}

	if type arping2 >/dev/null 2>&1; then
		if [ -n "${spoof}" ]; then
			opts="${opts} -S ${spoof}"
		else
			[ -z "$(_get_inet_address)" ] && opts="${opts} -0"
		fi
		while [ ${w} -gt 0 -a -z "${foundmac}" ]; do
			foundmac="$(arping2 ${opts} -r -c 1 -i "${IFACE}" "${ip}" 2>/dev/null | \
			sed -e 'y/abcdef/ABCDEF/')"
			w=$((${w} - 1))
		done
	else
		[ -z "$(_get_inet_address)" ] && opts="${opts} -D"

		foundmac="$(arping -w "${w}" ${opts} -f -I "${IFACE}" "${ip}" 2>/dev/null | \
		sed -n -e 'y/abcdef/ABCDEF/' -e 's/.*\[\([^]]*\)\].*/\1/p')"
	fi
	[ -z "${foundmac}" ] && return 1
	
	if [ -n "${mac}" ] ; then
		if [ "${mac}" != "${foundmac}" ] ; then
			vewarn "Found ${ip} but MAC ${foundmac} does not match"
			return 1
		fi
	fi

	return 0
}

_arping_in_config() {
	_get_array "config_${IFVAR}" | while read i; do
		[ "${i}" = "arping" ] && return 1
	done
	return 1
}

arping_start() {
	local gateways= x= conf= i=
	einfo "Pinging gateways on ${IFACE} for configuration"

	eval gateways=\$gateways_${IFVAR}
	if [ -z "${gateways}" ] ; then
		eerror "No gateways have been defined (gateways_${IFVAR}=\"...\")"
		return 1
	fi

	eindent
	
	for x in ${gateways}; do
		local IFS=,
		set -- ${x}
		local ip=$1 mac=$2 spoof=$3 extra=
		unset IFS

		if [ -n "${mac}" ] ; then
			mac="$(echo "${mac}" | tr '[:lower:]' '[:upper:]')"
			extra="(MAC ${mac})"
		fi

		vebegin "${ip} ${extra}"
		if arping_address "${ip}" "${mac}" "${spoof}" ; then
			local IFS=.
			for i in ${ip} ; do
				if [ "${#i}" = "2" ] ; then
					conf="${conf}0${i}"
				elif [ "${#i}" = "1" ] ; then
					conf="${conf}00${i}"
				else
					conf="${conf}${i}"
				fi
			done
			unset IFS
			[ -n "${mac}" ] && conf="${conf}_$(echo "${mac}" | sed -e 's/://g')"

			eend 0
			eoutdent
			veinfo "Configuring ${IFACE} for ${ip} ${extra}"
			_configure_variables ${conf}

			# Call the system module as we've aleady passed it by ....
			# And it *has* to be pre_start for other things to work correctly
			system_pre_start

			# Ensure that we have a valid config - ie arping is no longer there
			local IFS="$__IFS"
			for i in $(_get_array "config_${IFVAR}"); do
				if [ "${i}" = "arping" ]; then
					eend 1 "No config found for ${ip} (config_${conf}=\"...\")"
					continue 2
				fi
			done
			unset IFS
			
			_load_config
			return 0
		fi
		veend 1
	done

	eoutdent
	return 1
}

# vim: set ts=4 :