#!/usr/bin/perl -w
#
# $Id$
# Copyright B. Cornec 2005-2014
# Provided under the GPL v2
#
# Get the complete network configuration of the system
#
use strict;
use IO::Interface::Simple;
use Net::IPv4Addr qw( :all );;
use ProjectBuilder::Base;

=pod

=head1 NAME

mr-net-get-config keeps track of the network configuration of the system studied

=head1 DESCRIPTION

mr-net-get-config keeps track of the network configuration of the system studied wrt a network share passed as first parameter and store the result in the file given as second parameter (typically /tmp/mondorestore.cfg)

=head1 SYNOPSIS

mr-net-get-config server-ip:/export /path/to/file

=head1 ARGUMENTS

=over 4

=item B<server-ip:/export> 

This is the IP address of the network server, followed by a ':' and the exported directory

=item B</path/to/file> 

This is the path of the configuration file into which we want to store the network configuration

=back

=head1 WEB SITES

The main Web site of the project is available at L<http://www.mondorescue.org>. Bug reports should be filled using the trac instance of the project at L<http://trac.mondorescue.org/>.

=head1 USER MAILING LIST

For community exchanges around MondoRescue please use the list L<http://sourceforge.net/mailarchive/forum.php?forum_name=mondo-devel>

=head1 AUTHORS

The MondoRescue team lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.

=head1 COPYRIGHT

MondoRescue is distributed under the GPL v2.0 license or later,
described in the file C<COPYING> included with the distribution.

=cut

my $verbose = undef;
if ((defined $ARGV[0]) && ($ARGV[0] eq "-v")) {
	$verbose = 1;
	shift @ARGV;
}

if ((not defined $ARGV[0]) || (not defined $ARGV[1])) {
	die "Syntax: mr-net-get-config [proto:]server-ip:/export /path/to/file";
}

open(CFG, ">> $ARGV[1]") || die "Unable to append into $ARGV[1]: $!";

my ($netfs_proto,$netfs_server_ip,$netfs_server_mount);
# Protocol for sharing is NFS by default
$netfs_proto = "nfs";

my ($scheme, $account, $host, $port, $path) = pb_get_uri($ARGV[0]);

# We need to loop on all if to see which one is on the same LAN as the server
for my $if (IO::Interface::Simple->interfaces) {
	# Skip fake interfaces
	next if (not defined $if->address);
	my $netfs_ipaddr = $if->address;
	my $netfs_netmask = $if->netmask;
	my $netfs_broadcast = $if->broadcast;
	my $netfs_hwaddr = $if->hwaddr;
	if (defined $verbose) {
		print "interface = $if\n";
		print "--\n";
		print "addr =      ",$netfs_ipaddr,"\n",
			"broadcast = ",$netfs_broadcast,"\n",
			"netmask =   ",$netfs_netmask,"\n",
			"dstaddr =   ",$if->dstaddr,"\n",
			"hwaddr =    ",$netfs_hwaddr,"\n",
			"mtu =       ",$if->mtu,"\n",
			"metric =    ",$if->metric,"\n",
			"index =     ",$if->index,"\n";
		print "is running\n"     if $if->is_running;
		print "is broadcast\n"   if $if->is_broadcast;
		print "is p-to-p\n"      if $if->is_pt2pt;
		print "is loopback\n"    if $if->is_loopback;
		print "is promiscuous\n" if $if->is_promiscuous;
		print "is multicast\n"   if $if->is_multicast;
		print "is notrailers\n"  if $if->is_notrailers;
		print "is noarp\n"       if $if->is_noarp;
		print "--\n";
	}
	if (ipv4_in_network($if->address, $if->netmask, $host)) {
		print "Netfs server $host is in network $netfs_ipaddr/$netfs_netmask\n";
		# So generate conf file
		print CFG "netfs-dev $if\n";
		print CFG "netfs-proto $scheme\n";
		print CFG "netfs-client-ipaddr $netfs_ipaddr\n";
		print CFG "netfs-client-netmask $netfs_netmask\n";
		print CFG "netfs-client-broadcast $netfs_broadcast\n";
		print CFG "netfs-client-hwaddr $netfs_hwaddr\n";
		print CFG "netfs-server-mount $ARGV[0]\n";
		print CFG "netfs-server-user $account\n";
		print CFG "netfs-server-path $path\n";
		print CFG "netfs-server-ipaddr $host\n";
		open(ROUTE, "/proc/net/route") || die "Unable to read /proc/net/route: $!";
		while (<ROUTE>) {
			my ($iface,$dest,$gw,$foo) = split(/\s+/,$_);
			# Keep only default route
			next if ($dest !~ /^00000000$/);
			$gw =~ s/([A-z0-9]{2})([A-z0-9]{2})([A-z0-9]{2})([A-z0-9]{2})/hex($4).".".hex($3).".".hex($2).".".hex($1)/eg;
			print CFG "netfs-client-defgw $gw\n";
		}
		close(ROUTE);
		last if (not defined $verbose);
	}
}
close(CFG);
