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

Can anyone see anything obvious here? When I use TCP I get the desired output, when using UDP - I get a status of UP whether the port is up or down.
#!/usr/bin/perl -w use strict; use IO::Socket::INET; use Getopt::Long; my @hosts = ""; my $timeout = "2"; my $port = ""; my $opts = GetOptions("host=s" => \@hosts, "port=s" => \$port, "timeout=s" => \$timeout); @hosts = split /,/, join(',',@hosts); die "usage: sp -h <host|IP> -p <port>\n" unless $hosts[1]; for my $host(@hosts) { next if $host eq ''; my $status = ( connection($host, $port) == 0 ) ? "Up" : "Down"; printf " %-20s %-5s %-1s\n", $host, $port, $status; } ############## sub connection ############## { my $host = shift; my $tcp_port = shift; my $sock = IO::Socket::INET ->new( Timeout => $timeout, PeerAddr => $host, PeerPort => $tcp_port, Proto => 'udp' ); die "Unable to return $sock\n" unless return $sock ? 0 : 1; }
Thanks in advance?
Ted
--
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
  --Ralph Waldo Emerson

Replies are listed 'Best First'.
Re: UDP and IO::Socket::INET
by Fletch (Bishop) on Aug 08, 2006 at 19:17 UTC

    Erm, UDP is connectionless. There's no handshake or anything going on, you're just getting an endpoint which can be used to send datagrams (and if you look at the source of IO::Socket::INET it doesn't call connect unless it's a stream (i.e. TCP) socket).

      Arrrgh... I got a bit caught up in the results to actually think about the problem. Thanks for pointing out the obvious.

      Ted
      --
      "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
        --Ralph Waldo Emerson
Re: UDP and IO::Socket::INET
by ikegami (Patriarch) on Aug 08, 2006 at 19:29 UTC

    Two quickies first.

    • die "Unable to return $sock\n" will never get executed. return never returns.
    • $tcp_port should be $udp_port or just $port. (TCP and UDP ports are unrelated.)

    Now on to the meat.

    TCP requires that a connection be made before high-level communication can begin. This requires the exchange packets (handshacking), and will fail if the peer is not listening for incoming connections on the specified port.

    UDP, on the other hand, is a connection-less protocol. There is no handshacking during socket creation, so it's impossible to know if the other end is listening at that time.

    Even sending a datagram to the peer is insufficient to detect if the peer is listening, because UDP is also an unreliable protocol. An unreliable protocol is one that doesn't notify its sender whether the data was sent successfully or not.

    To ping a UDP port, it is necessary to send a datagram to the peer which provokes a response from the peer. I don't know of a generic way of doing this for UDP. As far as I know, it requires knowing how to communicate with the application using the UDP port. ( Now I do. See betterworld's reply )

    Because UDP is unreliable, you need to try a few times in case the request or the response are lost in transit.

      To ping a UDP port, it is necessary to send a datagram to the peer which provokes a response from the peer. I don't know of a generic way of doing this for UDP. As far as I know, it requires knowing how to communicate with the application using the UDP port.
      You send an empty datagram to the port. If you get an ICMP port unreachable, the port is closed. If you don't, it's open. (In either case, it might as well have been because of the firewall.) (See man nmap.)
      I ended up parsing /cat/proc/ip_conntrack - which, isn't perfect - but good enough for what Im doing.

      Thanks

      Ted
      --
      "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
        --Ralph Waldo Emerson