#!/usr/bin/perl

use strict;

use gateway_lib;

use Getopt::Long;

my $tapno;
my $addr;
my $dest;
my $iface;
my $fedkit_dir= "/usr/local/federation";
my $perl = "/usr/bin/perl";
my $peer;
my $use_file;
my %opts = (
    'tapno=s' => \$tapno,
    'addr=s' => \$addr,
    'dest=s' => \$dest,
    'peer=s' => \$peer,
    'use_file', \$use_file,
);

exit(20) unless GetOptions(%opts);
gateway_lib::read_config(gateway_lib::emulab_config_filename(), \%opts)
    if $use_file;

die "Only one of dest and addr is allowed\n" if $addr && $dest;

$iface = $dest ? 
    gateway_lib::dest_to_iface($dest) : gateway_lib::addr_to_iface($addr) 
	if !$iface;

print "iface is $iface\n";

gateway_lib::bind_tap_to_iface($tapno, $iface);
gateway_lib::ping_peer($peer)
    if $peer;

exit(0);

=pod

=head1 NAME

B<setup_bridge.pl> - Connect a tap and local interface.  Called both remotely
and locally to establish the two sides of the bridge.

=head1 OPTIONS

=over 8

=item B<addr=>I<address>

Address of the local interface to connect to the given tap interface on the
given bridge interface.

=item B<dest=>I<destination>

Attach the interface on which I<destination> can be reached to the given
tap/bridge combo.

=item B<tapno=>I<integer>

The tap interafce to connect. This will be the integer that follows tap and
bridge in the interface names.

=item B<use_file>

If given read additional parameters from the file in
/proj/I<project>/exp/I<experiment>/tmp/I<hostname>.gw/conf where those are the
current testbed project and experiment and the hostname is before the first
dot.  The file is option: value.


=back

=head1 SYNOPSIS

B<setup_bridge.pl> Connects the established ssh tunnel (a tap interface) to 
a local interface through a bridge interface.  Traffic on the local interface
will cross the network over the ssh tunnel and be released onto the net via a
symmetrical setup on the peer.

The interface to attach can be given either by its interface or an address/DNS
name to reach.  In either case, all IP addresses are removed from the
bound interface.

=head1 AUTHORS

Ted Faber <faber@isi.edu>

=cut
