From Pickwiki
Jump to navigationJump to search


A perl script that listens for remote connections, and feeds them thru a named pipe to CGI.MASTER

sunset /root/perl> cat    
use IO::Socket;

# Delimiter to seperate rows is @AM
# Delimiter to seperate fields is @VM

# Start listening on socket 1999
$server = IO::Socket::INET->new([[LocalPort]] => 1999,
                                 Type     => [[SOCK_STREAM]],
                                 Reuse    => 1,
                                 Listen   => 10 )
or die "Couldn't be a server on 1999: $!\n";

open (LOG, ">cgi.log") or die "Cannot open cgi.log: $!\n";

# Turn off buffering on the log filehandle
my $oldfh = select LOG; $| = 1; select $oldfh;

log_msg("Listening on port 1999");

# Keep accepting connections, forever
while ( $client = $server->accept() ) {
   if ($acc ne "TRINITY" && $acc ne "SFSI") {
      print $client "<H1>Internal cgi error - must specify an account!</H1>\n";
      log_msg("Internal cgi error - must specify an account!");
   # Use process id for temp file
   $rr = $$;
   # This is the named pipe that the basic program is monitoring
   $infile = "/samba_share/web/in/$acc/in_from_perl";
   open(INFO, ">$infile") or die "Cannot open $infile: $!\n";

   # Make the named pipe to read from, later
   $outfile = "/samba_share/web/out/$acc/$rr";
   if (system('mknod', $outfile, 'p')) {
      die "mknod $outfile failed: $!\n";
   chmod 0777, $outfile;

   # First, tell CGI.MASTER the place to write its output - the named pipe
   # we just created.  as soon as we send everything, we'll wait for some
   # output from the named pipe
   print INFO $outfile,$rec_delim;

   while(<$client>) {
      if (/^EOF$/) {
      # Send the data thru the named pipe to CGI.MASTER
      print INFO $_,$rec_delim;
   close(INFO) or die "Cannot close $infile: $!\n";

   # This next line will block until something is sent to the named pipe
   open(OUTF, "<$outfile") or die "Cannot open $outfile: $!\n";

   # Ok, now we've got some data, send it back thru the pipe, to the web server
   # will pass on to the browser... oh, the tangled webs we weave!
   while (<OUTF>) {
      # We undo the messing around with field delims on the way back
      # Send the html back thru the net connection
      print $client $_;
   # Finally, get rid of the temporary named pipe
   unlink($outfile) or die "Cannot unlink $outfile:$!\n";

   # Shutdown this network connection

sub log_msg {
   my ($msg) = @_;
   print LOG scalar localtime, " $msg\n";