Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello
I want to send one UDP packet from a Windows to a port on a Windows and se if the port is open.

I want to print the answer if the port is open or closed.
I can send the packet but how can i read the answer "ICMP udp port portnumber unreachable" if the port is closed?.

use strict;

use IO::Socket::INET;

my $MySocket=new IO::Socket::INET->new(PeerPort=>888,
Proto=>'udp',
PeerAddr=>'192.168.1.10');

my $msg = "x";
$MySocket->send($msg);


// Anders Andersson

Replies are listed 'Best First'.
Re: How read ICMP UDP port unreachable?
by kvale (Monsignor) on Mar 27, 2004 at 00:42 UTC
    If you want ot try an ICMP udp ping, it might be easier to use the Net::Ping module:
    use Net::Ping; $p = Net::Ping->new("udp"); print "$host is alive.\n" if $p->ping($host); $p->close();

    -Mark

Re: How read ICMP UDP port unreachable?
by castaway (Parson) on Mar 28, 2004 at 09:51 UTC
    I've played around some with NetPacket::ICMP and such. What I think you'd need to do, to test that particular UDP port, would be to listen locally on the ICMP port (which is a protocol like UDP and TCP), and read any incoming packets that occur when you connect via UDP.

    Anyway, this is as far as I got, it doesn't seem to get a ICMP packet type that makes any sense, though, anyone have an idea why?

    #!/usr/bin/perl -w use Data::Dumper; use NetPacket::ICMP; use IO::Socket; my $icmps = IO::Socket::INET->new("LocalHost" => "192.168.1.2", "Proto" => "icmp") or die "Cant icmp listen ($!)"; print "boo\n"; my $udptest = IO::Socket::INET->new("PeerAddr" => "192.168.1.20", "PeerPort" => "2222", "Proto" => "udp", "Timeout" => 20); $udptest->print("test"); my $y = $icmps->recv($buffer, 1024, 0); if($y) { ($xport, $iaddr) = unpack_sockaddr_in($y); $remotehost = inet_ntoa($iaddr); print("peerhost $remotehost:$xport\n"); } my $dec = NetPacket::ICMP->decode($buffer); print Dumper($dec);
    Which outputs:
    boo peerhost 192.168.1.20:0 $VAR1 = bless( { '_parent' => undef, 'data' => 'c @À¨À¨E Àl@@öùÀ¨À¨® qtest', 'cksum' => 60, 'type' => 69, '_frame' => 'EÀ<c @À¨À¨E Àl@@öùÀ¨À¨® qtest', 'code' => 192 }, 'NetPacket::ICMP' );
    Where 69 is not any of the possible packet types listed in NetPacket::ICMP.. So, no clue whats going haywire..

    C.

Re: How read ICMP UDP port unreachable?
by neniro (Priest) on Mar 27, 2004 at 10:33 UTC
    Because UDP is stateless (as mentioned above), none reaction is a good reaction, but firewalls will drop your packets silently so it looks like a connection but it isn't. If there are no firewalls you won't get in trouble with this awkward behavior.
Re: How read ICMP UDP port unreachable?
by flyingmoose (Priest) on Mar 27, 2004 at 02:40 UTC
    UDP is by definition unreliable, unordered, and sessionless. If you need to know if a port is open, you probably want to initiate a TCP/IP connection to that particular port. if you want to be creative, you could perhaps use nmap for this scanning, as that is well suited for checking a large number of ports over a network very efficiently -- just be careful to ensure you are scanning only computers you own, else you could easily get into trouble with your ISP, or worse...
      Note, UDP and TCP ports are completely separate things, thus checking using TCP/IP if a particular port is open will not tell you anything about whether the UDP port with the same number is open, at all.

      C.