#!/usr/bin/perl
### This file is covered by the GNU General Public License version 2 or later
### or licenses same of etcnet or vzctl packages
###
### Copyright (C) 2013  ALT Linux Team
### Author: Sergey Y. Afonin <asy@altlinux.org>

use Sys::Syslog;
use constant ScriptName => "vzarpset";
use constant SysLogFacility => "local4";
use constant SysLogPriority => "info";

@arplist=`arp -vn|egrep "\\\*[[:space:]]*\\\*[[:space:]]*MP"|awk '{print(\$1);}'`;

@veiplist=`cat /proc/vz/veip|grep -v "Version"|sed "s/[[:space:]]*\\([0-9.]*\\)[[:space:]]*.*/\\1/"`;

my %arphash;
my %veiphash;
my %arpadd;
my %arprm;

foreach $ip (@arplist ) {
	$arphash{$ip} = 1
}

foreach $ip (@veiplist) {
	$veiphash{$ip} = 1;
	if (! $arphash{$ip}) {$arpadd{$ip} = 1}
}

foreach $ip (@arplist) {
	if (! $veiphash{$ip}) {$arprm{$ip} = 1}
}

@arpadd = keys %arpadd;
@arprm = keys %arprm;

foreach $ip (@arpadd) {
	chomp($ip);
	openlog(ScriptName . "[$$]", "ndelay", SysLogFacility);
	syslog(SysLogPriority, "arp -s " . $ip . " 0:0:0:0:0:0 -i ether0 pub");
	closelog;
	system "arp -s " . $ip . " 0:0:0:0:0:0 -i ether0 pub";
}

foreach $ip (@arprm) {
	chomp($ip);
	openlog(ScriptName . "[$$]", "ndelay", SysLogFacility);
	syslog(SysLogPriority, "arp -d " . $ip . " -i ether0");
	closelog;
	system "arp -d " . $ip . " -i ether0";
}
