#!/bin/bash
#  Copyright (c) 2015-2017, Parallels International GmbH
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#  Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200
#  Schaffhausen, Switzerland.

VEROOT=${1}

if [[ -z ${VEROOT} ]]; then
	exit 1
fi

if [ ! -d ${VEROOT} ]; then
	echo "${VEROOT} is not a directory"
	exit 1
fi

if [ ! -d "${VEROOT}/sys/class/net" ]; then
	echo "${VEROOT}/sys/class/net is not a directory"
	exit 1
fi

ARPING_CMD=/sbin/arping
ARPING_ARGS="-f -c 1 -w 1"
IP_CMD=/sbin/ip
NDSEND_CMD=/usr/sbin/ndsend

for BINARY in ${ARPING_CMD} ${IP_CMD} ${NDSEND_CMD}; do
	if [ ! -x ${BINARY} ]; then
		echo "There is no ${BINARY}!"
		exit 1
	fi
done

for DIR in ${VEROOT}/sys/class/net/*; do
	NETDEV=$(basename ${DIR})
	[ -d ${DIR}/bridge ] && continue
	[ ${NETDEV} = "lo" ] && continue
	FLAGS=$(cat ${DIR}/flags 2>/dev/null) || continue
	((${FLAGS} & 0x10 )) && continue # ignore point-to-point devices

	IPS=$(${IP_CMD} -o a l ${NETDEV} 2>/dev/null | sed -rn 's/.*inet ([^\/]*).*/\1/p')
	if [ ${PIPESTATUS[0]} -eq 0 ]; then
		for IP in ${IPS}; do
			${ARPING_CMD} ${ARPING_ARGS} -U -I ${NETDEV} ${IP} > /dev/null 2>&1
		done
	fi

	IPS6=$(${IP_CMD} -o a l ${NETDEV} 2>/dev/null | sed -rn 's/.*inet6 ([^\/]*).*/\1/p')
	if [ ${PIPESTATUS[0]} -eq 0 ]; then
		for IP6 in ${IPS6}; do
			${NDSEND_CMD} ${NETDEV} ${IP6} > /dev/null 2>&1
		done
	fi
done

