Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Proving a UDP port is closed by davis

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (8)
    As of 2018-05-23 17:37 GMT
    Find Nodes?
      Voting Booth?