I'm trying to prove that a given UDP port is closed on the local machine (e.g. we've got an arbitrary security restriction that says "UDP port 111 (rpcbind)" shouldn't be open!). I know that I can test TCP open-/closed- ness by checking with an IO::Socket::INET, and that UDP is harder because it doesn't do a connection handshake. There's therefore effectively almost no such thing as "closed" (sort of), so after reading some advice here I'm trying to send an empty datagram, then check for an error. However, the following test passes. I'm looking for a test failure, because rpcbind is currently open on my machine:
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use Test::More tests => 1;
use IO::Socket;
my $expected_closed_port = 111;
my $dead_socket = IO::Socket::INET->new(PeerAddr => 'localhost', PeerP
+ort => $expected_closed_port, Proto => "udp");
if($dead_socket) {
# It could be a UDP connection, and there's no such concept as
+ UP or DOWN, so we need to try sending an
# empty datagram, and check for an error
$dead_socket->send("");
my $foo;
setsockopt( $dead_socket, SOL_SOCKET, SO_RCVTIMEO, pack('L!L!'
+, +10, 0) );
my $return_value = recv($dead_socket, $foo, 1, 0);
if(defined($return_value)) {
ok(0, "I shouldn't be able to connect to port $expected_cl
+osed_port on localhost");
} else {
ok(1, "I shouldn't be able to connect to port $expected_cl
+osed_port on localhost");
}
}
The ok/not ok is a little funky because of the other stuff that my "real" code is doing, but this is a working example of the unwanted passing of a test.
Can anyone see where I'm going wrong? I believe recv is supposed to return undef on failure, but it doesn't appear to.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.