#!/usr/bin/perl -w
#
# Simple radius client for testing
#
# 

use IO::Socket;
use Net::hostent;

use RADIUS::Dictionary;
use RADIUS::Packet;
use Fcntl;


# sendPacket(socket, host, port, packet)
sub sendPacket {
  my ($host,$port,$buffer) = @_;

  my $sock = IO::Socket::INET->new(PeerAddr => $host,
				   PeerPort => $port,
				   Proto    => 'udp');

  if (!defined $sock) {
    die "ERROR:  Unable to create socket! ($!)\n";
  }
  print "Sending <" . length($buffer) . ":$buffer> to $host:$port\n";
  $sock->send($buffer) or die "send: $!\n";

  return $sock;
} # sendPacket


# This is a VERY simple RADIUS client.
my $secret = "fred";  # Shared secret on the term server

# Parse the RADIUS dictionary file (must have dictionary in current dir)
my $dict = new RADIUS::Dictionary "/home/chaos/work/dictionary"
  or die "Couldn't read dictionary: $!";

my $authenticator = '1234567890123456';
my $identifier = 1;

# build a packet, and send it
my $packet = new RADIUS::Packet $dict;

# Initialize the Request Packet
$packet->set_code('Accounting-Request');

$packet->set_identifier($identifier);
$packet->set_authenticator($authenticator);

$packet->set_attr('NAS-IP-Address', 0x01020304);
$packet->set_attr('NAS-Port-Type', 'ISDN');
$packet->set_attr('Acct-Status-Type', 'Stop');
$packet->set_attr('Acct-Delay-Time', 0);

my $sock = &sendPacket("geodude",1646,$packet->pack);


# Now, receive the response.
print "Waiting for a response . . . \n";
$sock->recv($message,4096,0) ||
  die "Unable to receive message $!";
print "Got a response!\n";

$sock->shutdown();;


# Now, play with our packet
#
#&Daves::Utils::hexdump("Packet received",$message);
# Unpack it
my $rp = new RADIUS::Packet $dict, $message;

##&Daves::Utils::hexdump("Comparing Authenticator",$rp->authenticator());
#&Daves::Utils::hexdump("With orig: ",$authenticator);

$rp->dump();



