#!/usr/bin/perl use strict; use warnings; use IO::Socket; use IO::Select; use Time::Local; use File::Basename; ######################################################################################## #xml Variables my ($xinput_path, $xmlout, $debug, $port, $logfile, $new_file, $rules); my ($hb, $hb_prev, $now, $hb_email, $hb_duration, $buffer, $max_clients, $slm); my ($checkdir_thresh, $checkdir_duration, $checkdir_prev, $manager); #Socket Variables my ($remote, $sock, @client_list, $sel, $new_remote); #Misc Variables my ($VERSION); ######################################################################################## my $rc = 0; $VERSION = "1.0.0"; my $timeout = 5; my $save_dir = './files' ; unless (&Create_Socket()) { print "Problem with Creating a Socket: $!\n"; exit(99); } my ($filesave, $filesize, $filename, $data_content); while(1) { my @client_list = $sel->can_read(1); foreach $remote(@client_list) { if ($remote == $sock) { $new_remote = $sock->accept; $sel->add($new_remote) if ($new_remote); } else { my $in; if (sysread($remote, $in, 1024)) { if (defined $remote) { print "Got: $in\n"; if ($in =~ /#:#$/) { $filename = $in; $filename =~ s/#:#$// ; print "Filename = $filename\n"; } elsif ($in =~ /_$/) { $filesize = $in; $filesize =~ s/_$// ; print "Filename = $filename\n"; print "Filesize = $filesize\n"; } elsif ($in =~ /__END$/) { close (FILENEW) ; print "...\n\n" ; $sel->remove($remote); close($remote); ($filesave, $filesize, $filename, $data_content) = 0; } else { print "About to save file\n"; unless ($filesave) { $filesave = "$save_dir/$filename" ; print "FileSave - $filesave\n"; if (-e $filesave) { unlink ($filesave) ; } print "Saving: $filesave ($filesize bytes)\n" ; } open (FILENEW,">>$filesave") ; binmode(FILENEW) ; print FILENEW $in ; } } else { print "DEBUG - \$remote Client NOT defined\n"; } } else { print "WARNING - X-InputServer Could not Read data from a connected socket: @_\n"; $sel->remove($remote); close($remote); } } } } exit($rc); ######################################################################################## ## ## SUB ROUTINES ## ######################################################################################## #Create a Socket to listen on sub Create_Socket { $SIG{CHLD} = 'IGNORE'; $sock = IO::Socket::INET->new( LocalPort => 6123, Proto => 'tcp', Reuse => 1, Listen => 100 ) or return(0); $sel = IO::Select->new($sock); print "INFORMATION - Socket Created ... Successful\n"; print "INFORMATION - Awaiting messages on: 6123\n"; return(1); }