From 93a8150701fc6e19b27cc7f5cb37432b306b816c Mon Sep 17 00:00:00 2001 From: Sergey Ivanov Date: Tue, 13 Jun 2017 18:23:15 -0400 Subject: [PATCH] Fix file handle comparison and timeouts processing The module as it is return "connection successful" when connection timeouts. Also, '==' can not compare file handles. We can use fileno() function to compare numerically file numbers. --- lib/Core/TcpService.pm | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/Core/TcpService.pm b/lib/Core/TcpService.pm index 30ed1b1..7fafc78 100644 --- a/lib/Core/TcpService.pm +++ b/lib/Core/TcpService.pm @@ -99,7 +99,7 @@ sub handler { connect($h, $s); $c->add($h); my ($fd) = $c->can_write($timeout); - if ($fd == $h) { + if (defined($fd) && (fileno($fd) == fileno($h))) { my $error = unpack("s", getsockopt($h, Socket::SOL_SOCKET, Socket::SO_ERROR)); if ($error != 0) { close($h); @@ -107,13 +107,17 @@ sub handler { } print $h $config->{'prepost'}."\r\n" if ($config->{'prepost'}); ($fd) = $c->can_read($timeout); - if ($fd == $h) { + if (defined($fd) && (fileno($fd) == fileno($h))) { chomp($banner = <$h>); print $h $config->{'post'} if ($config->{'post'}); close($h); $banner =~ s/([^\s\d\w.,;\/\\])/sprintf "\\%o", $1/eg; } } + elsif (not defined($fd)) { + close($h); + return { 'status' => ['connection timeout', 's'] }; + } close($h); return {