#!/usr/local/bin/perl
#
# update a master ARP table
#
# $Id$
package

sub file_read {
    my $self = shift;
    my $file = shift;

    my ($mac, $ip, $t, $first);

    $self->{ERROR} = "";

    if (!open (I, $file)) {
    	$self->{ERROR} = "could not open file";
	return undef;
    }

    while (<I>) {
    	next if (/^\s*#/ || /^\s*$/);
	s/\s*$//;

	#
	# IP address
	#
	if (/^(\d+\.\d+\.\d+\.\d+)/) {
	    $ip = $1;
	    $first = 1;
	    next;

	} elsif (/^\s+(([0-9a-f]{2}:){5}[0-9a-f]{2})\s(\d+)/) {
	    ($mac, $t) = ($1, $2);

	    if (!defined $ip) {
		$self->{ERROR} = "no IP address as of line $."
		close (I);
		return undef;
	    }

	    if ($first) {
	    	$self->{ARP}->{$ip}->{CURRENTMAC} = $mac;
		$self->{ARP}->{$ip}->{CURRENTTIME} = $t;
		$first = 0;

	    } else {
		push @{$self->{ARP}->{$ip}->{HISTORY}}, [$mac, $t];
		next;
	    }

	#
	# mac address
	#
	} else {
	    print STDERR "malformed line at $., skipping\n";
	    next;
	}
	($ip, $mac) = split;

    }
}

sub file_write {
}
