#!/usr/bin/perl -w 
use IO::Socket;

$host="1.2.3.4";
$port=4201;

$SIG{PIPE} = \&pipeHandler;

foreach $argnum (0 .. $#ARGV)
{
   if ($ARGV[$argnum] eq "-d")
   {
   	  $DEBUG=1;
   }
   if ($ARGV[$argnum] eq "-l")
   {
   	  $LOG=1;
   }

}

sub pipeHandler
{
  $sig = shift @_;
  msg("Caught SIGPIPE: $sig $1");
  return;
}

sub sendData($)
{
  my $data = shift;
  my @byte = split(/ /, $data);
  foreach (@byte)
  {
    print $socket chr int hex $_;
  }
}

sub msg($)
{
  my $msg = shift;
  if ($DEBUG)
  {
    if ($LOG)
    {
      ($sec,$min,$hour,$mday,$mon,$year)=localtime();
      $year+=1900;
      $mon+=1;
      $mon=substr("00".$mon, -2);
      $mday=substr("00".$mday, -2);
      $hour=substr("00".$hour, -2);
      $min=substr("00".$min, -2);
      $sec=substr("00".$sec, -2);
      open(LOGFILE, ">>/var/log/bmctl.log") || die "Logfile /var/log/bmctl.log could not be opened for writing!\n"; 
      print LOGFILE $mday.".".$mon.".".$year." ".$hour.":".$min.":".$sec." ".$msg."\n";    	
	  close(LOGFILE);
    }
    else
    {
      print STDERR $msg."\n";
    }
  } 
}

while (1)
{
  msg(""); 
  msg("============================================================");
  msg("");

  msg("Connecting to $host port $port");

  # create a tcp connection to the specified host and port
  $socket = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $host, PeerPort => $port) or die "can't connect to port $port on $host: $!";

  # using autoflush(0) to keep data get  there right away
  $socket->autoflush(0);

  # send the compression off packet
  msg("Sending 'compression off' packet (257 byte)");

  sendData("62 6d 69 63 02 01 01 01 80 00 00 24 42 36 37 33");
  sendData("32 33 35 31 2d 32 30 37 45 2d 34 31 33 41 2d 42");
  sendData("45 35 38 2d 35 31 34 30 37 45 44 30 32 44 42 41");
  sendData("10 00 00 05 32 2e 31 2e 36 10 01 00 01 31 80 04");
  sendData("00 04 31 30 32 34 80 05 00 03 37 36 38 80 01 00");
  sendData("0a 57 69 6e 64 6f 77 73 20 58 50 80 06 00 02 49");
  sendData("45 80 07 00 0f 4f 75 74 6c 6f 6f 6b 20 45 78 70");
  sendData("72 65 73 73 50 03 00 04 20 00 00 00 50 01 00 04");
  sendData("20 02 00 00 50 02 00 04 20 02 00 00 50 04 00 04");
  sendData("20 02 00 00 50 00 00 08 20 03 00 00 20 01 00 00");
  sendData("90 01 00 05 21 00 00 00 00 90 00 00 09 21 00 00");
  sendData("00 00 00 00 00 00 30 0c 00 01 30 30 0b 00 01 30");
  sendData("30 0a 00 01 30 30 09 00 01 30 30 08 00 01 30 30");
  sendData("07 00 01 30 30 06 00 01 30 30 03 00 01 30 30 02");
  sendData("00 03 31 30 30 30 01 00 03 31 30 30 40 00 00 11");
  sendData("01 c8 30 4f cc 41 55 8f 82 96 10 d2 88 32 4a 8a");
  sendData("90");

  $socket->flush();

  # read answer from socket
  $x=sysread ($socket, $answer, 65536);
  msg("Received ".length($answer)." bytes");

  while ($x>0)
  {
    # sleep for 30 minutes
    msg("Sleeping for 30 minutes");
    sleep(1800);

    # sending the keep_alive data
    msg("Sending 'keep-alive' packet (8 byte)");
    sendData("62 6d 69 63 02 03 00 08");
    $socket->flush();

    # read answer from socket
    $x=sysread ($socket, $answer, 65536);
    msg("Received ".$x." bytes");
  
  }

  msg("No answer received, terminating the connection!");
  close($socket);

  msg("Sleeping for 1 minute to prevent to fast respawning");
  sleep(60);  

}

