File: talk-to-ourself

package info (click to toggle)
libwww-perl 6.78-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,008 kB
  • sloc: perl: 4,148; makefile: 10; sh: 6
file content (50 lines) | stat: -rw-r--r-- 1,524 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!perl

use strict;
use warnings;

# This program check if we are able to talk to ourself.  Misconfigured
# systems that can't talk to their own 'hostname' was the most commonly
# reported libwww-failure.

use IO::Select ();
use IO::Socket ();

if (@ARGV >= 2 && $ARGV[0] eq "--port") {
    my $port = $ARGV[1];
    my $host = '127.0.0.1';
    if (my $socket = IO::Socket::INET->new(PeerAddr => "$host:$port", Timeout => 5)) {
        if (IO::Select->new($socket)->can_read(1)) {
            my($n, $buf);
            if ($n = sysread($socket, $buf, 512)) {
                exit if $buf eq "Hi there!\n";
                die "Seems to be talking to the wrong server at $host:$port, got \"$buf\"\n";
            }
            elsif (defined $n) {
                die "Immediate EOF from server at $host:$port\n";
            }
            else {
                die "Can't read from server at $host:$port: $!";
            }
        }
        die "No response from server at $host:$port\n";
    }
    die "Can't connect: $@\n";
}

# server code
my $socket = IO::Socket::INET->new(Listen => 1, Timeout => 5, LocalAddr => '127.0.0.1');
my $port = $socket->sockport;
open(my $CLIENT, qq("$^X" "$0" --port $port |)) || die "Can't run $^X $0: $!\n";

if (my $client = $socket->accept) {
    print $client "Hi there!\n";
    close($client) || die "Can't close socket: $!";
}
else {
    warn "Test server timeout\n";
}

exit if close($CLIENT);
die "Can't wait for client: $!" if $!;
die "The can-we-talk-to-ourself test failed.\n";