#!/usr/bin/perl
#
# A CGI script to scan ports (in a specified range) for GoServers
# and print HTML tables containing the results

use IO::Socket;

# Set the port-scanning range here:
$portstart = 30000;
$portend   = 30004;

# ---------------------------------------------------------------

$servername = $ENV{'SERVER_NAME'};

print "Content-type: text/html", "\n\n";

print "<title>BKGo Web System</title>";
print "<center><h1>BKGo Servers </h1></center>";

print "<h2>Select a GoServer:</h2>";

print "Here is a list of servers currently "; 
print "running on <b>".$servername."</b>:<p>";
print "<i>(You may need to hit your browser's 'reload' button)</i><p>";

# Start building a table

print <<end_intro;

<table border=1>
<tr><td><b>Port</b></td><td><b>Status</b></td><td><b>Options</b></td></tr>

end_intro

# Now, for each port in our range:

    for ($port = $portstart; $port <= $portend; $port++) {

	print "<tr><td>$port</td><td>";

	if ($socket = IO::Socket::INET->new(PeerAddr => $servername,
					PeerPort => $port,
					Proto    => "tcp",
					Type     => SOCK_STREAM))
	{
	    $serverver = <$socket>;
	    print $socket "Perl scanner client\n";
	    $ok = <$socket>;
	    print $socket "status\n";
	    while (<$socket>) {
		print $_."<br>";
	    }
	    close($socket);
	    print "</td><td>";
	    print "<a href=\"start_applet.pl?port=".$port."\">Connect</a> ";
	    print "to this server<br>\n";
	    print "<a href=\"verify_shutdown.pl?port=".$port."\">Shut down</a>";
	    print " this server</td></tr>\n";
	}
	else 
	{
	    print "No server.</td>";
	    print "<td><a href=\"start_server.pl?port=".$port."\">Create</a> ";
	    print "a server here<br>\n";
	    print "</td></tr>\n";
	}
    }


print "</table>\n";
