#!/usr/bin/perl -w

my $key="RUTYOURSECRETHMLANSTICKERKEYHERE";
my $hmlan="192.168.1.2";

BEGIN {
	use File::Basename;
	push @INC, dirname($0);
}

use HMLAN_CRYPT;
use IO::Socket::INET;
use IO::Select;

#This should be random...
my $my_iv = '00112233445566778899AABBCCDDEEFF';
my $hmlan_iv = '';

my $sock = IO::Socket::INET->new(
	PeerAddr => $hmlan,
	PeerPort => 1000,
	Proto => 'tcp',
	Blocking => 0);

my $read_set = new IO::Select();
$read_set->add($sock);

$read_set->can_read();

chomp(my $iv = <$sock>);
if ($iv =~ /^V(................................)\r$/) {
	$hmlan_iv = $1;
} else {
	print STDERR "Unknown IV received: ${iv}\n";
	exit(1);
}

print $sock "V${my_iv}\r\n";

my $cipher_hmlan=HMLAN_CRYPT->new(pack("H*", $key), pack("H*", $my_iv));
my $cipher_pc=HMLAN_CRYPT->new(pack("H*", $key), pack("H*", $hmlan_iv));

$read_set->add(\*STDIN);

while(1) {
	my @ready = $read_set->can_read();
	foreach my $r (@ready) {
		if ($r == $sock) {
			while(my $buf=<$r>) {
				print $cipher_hmlan->decrypt($buf);
			}
		} else {
			my $buf = <$r>;
			exit(1) if (!$buf);

			print $sock $cipher_pc->encrypt($buf);
		}
	}
}
