davis has asked for the wisdom of the Perl Monks concerning the following question:
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:
davis
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.#!/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"); } }
davis
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Proving a UDP port is closed
by marto (Cardinal) on Feb 01, 2011 at 15:27 UTC | |
Re: Proving a UDP port is closed
by Limbic~Region (Chancellor) on Feb 01, 2011 at 14:31 UTC | |
by gman (Friar) on Feb 01, 2011 at 14:59 UTC | |
by davis (Vicar) on Feb 01, 2011 at 15:17 UTC | |
by davis (Vicar) on Feb 01, 2011 at 14:54 UTC | |
by Limbic~Region (Chancellor) on Feb 01, 2011 at 15:00 UTC | |
by davis (Vicar) on Feb 01, 2011 at 15:11 UTC | |
by Anonymous Monk on Feb 01, 2011 at 15:29 UTC | |
|
Back to
Seekers of Perl Wisdom