From 7427c871abee6768cdd1c12d0ddf23f13cb37197 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Mon, 15 Feb 2010 23:47:16 +0100 Subject: Various stylistic improvements suggested by Max (such as using the // op). --- emergencyc.pl | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) (limited to 'emergencyc.pl') diff --git a/emergencyc.pl b/emergencyc.pl index a7d55d2..b8bebb6 100755 --- a/emergencyc.pl +++ b/emergencyc.pl @@ -46,18 +46,8 @@ if ( defined($opts{K}) ) { } die "No key specified (use -K or -k option)" unless defined($key); -my $host; -if ( defined($opts{h}) ) { - $host = $opts{h}; -} else { - $host = "localhost"; -} -my $port; -if ( defined($opts{p}) ) { - $port = $opts{p}; -} else { - $port = DEFAULT_PORT; -} +my $host = $opts{h} // "localhost"; +my $port = $opts{p} // DEFAULT_PORT; my @res = getaddrinfo($host, $port, AF_UNSPEC, SOCK_DGRAM) or die "Cannot resolve host $host port $port"; @@ -72,10 +62,8 @@ sub curtime { return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",$year+1900,$mon+1,$mday,$hour,$min,$sec); } -my $command = $ARGV[0]; -$command = "PING" unless defined($command); -my $timestamp = $opts{t}; -$timestamp = curtime unless defined($timestamp); +my $command = $ARGV[0] // "PING"; +my $timestamp = $opts{t} // curtime; my $validate = "$command|$timestamp"; my $maccheck = hmac_sha256_hex($validate, $key); send $socket, "$command|$timestamp|$maccheck", 0, $haddr; -- cgit v1.2.3 From c93beedc46c0495a45c449fc66a7eb04630567f4 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Tue, 16 Feb 2010 17:28:50 +0100 Subject: Work against the dreadful habit of concatenatingallmywords in identifiers. --- emergencyc.pl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'emergencyc.pl') diff --git a/emergencyc.pl b/emergencyc.pl index b8bebb6..296297f 100755 --- a/emergencyc.pl +++ b/emergencyc.pl @@ -10,8 +10,8 @@ # Options recognized: # -# -K specifies the key to use; or -k specifies a -# keyfile (the client will use the first line as key). +# -K specifies the key to use; or -k specifies a key +# file (the client will use the first line as key). # # -h and -p specifies the host and port to connect # to. @@ -39,10 +39,10 @@ my $key; if ( defined($opts{K}) ) { $key = $opts{K}; } elsif ( defined($opts{k}) ) { - open my $keyfile, "<", $opts{k} or die "Cannot open key file $opts{k}: $!"; - $key = <$keyfile>; + open my $key_file, "<", $opts{k} or die "Cannot open key file $opts{k}: $!"; + $key = <$key_file>; chomp $key; - close $keyfile; + close $key_file; } die "No key specified (use -K or -k option)" unless defined($key); @@ -65,8 +65,8 @@ sub curtime { my $command = $ARGV[0] // "PING"; my $timestamp = $opts{t} // curtime; my $validate = "$command|$timestamp"; -my $maccheck = hmac_sha256_hex($validate, $key); -send $socket, "$command|$timestamp|$maccheck", 0, $haddr; +my $mac_check = hmac_sha256_hex($validate, $key); +send $socket, "$command|$timestamp|$mac_check", 0, $haddr; my $buf; my $sender; -- cgit v1.2.3 From 426c4f4bcad9dd890ad8dbbc28be5b3690fd6664 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Tue, 16 Feb 2010 17:32:23 +0100 Subject: Rewrap long lines. --- emergencyc.pl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'emergencyc.pl') diff --git a/emergencyc.pl b/emergencyc.pl index 296297f..86c9da7 100755 --- a/emergencyc.pl +++ b/emergencyc.pl @@ -39,7 +39,8 @@ my $key; if ( defined($opts{K}) ) { $key = $opts{K}; } elsif ( defined($opts{k}) ) { - open my $key_file, "<", $opts{k} or die "Cannot open key file $opts{k}: $!"; + open my $key_file, "<", $opts{k} + or die "Cannot open key file $opts{k}: $!"; $key = <$key_file>; chomp $key; close $key_file; @@ -59,7 +60,8 @@ bind $socket, sockaddr_in6(0, in6addr_any); sub curtime { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time); - return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",$year+1900,$mon+1,$mday,$hour,$min,$sec); + return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", + $year+1900,$mon+1,$mday,$hour,$min,$sec); } my $command = $ARGV[0] // "PING"; -- cgit v1.2.3 From 8eeeb14f4284e2ee1fa040ae0209a6c02432005d Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Tue, 16 Feb 2010 17:46:19 +0100 Subject: Check recv() return value. --- emergencyc.pl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'emergencyc.pl') diff --git a/emergencyc.pl b/emergencyc.pl index 86c9da7..5d59be1 100755 --- a/emergencyc.pl +++ b/emergencyc.pl @@ -77,10 +77,13 @@ eval { alarm 5; do { $sender = recv($socket, $buf, 16384, 0); - } while ( $sender ne $haddr ); + } while ( defined($sender) && $sender ne $haddr ); }; if ( $@ ) { - printf "timeout\n"; + print "timeout\n"; + exit 1; +} elsif ( !defined($sender) ) { + die "Failed to receive packet: $!"; } else { printf "%s", $buf; } -- cgit v1.2.3