use File::Path;
use Socket;
use Config;
+use IO::Socket;
if( $ARGV[0] eq "--debug" ) { $debug = 1; shift; }
if( $ARGV[0] eq "--restart" ) { $restart = 1; shift; }
# just continues to listen for update messages.
sub listen_for_updates {
- # Set up the socket to listen to
+
$SIG{'PIPE'} = 'IGNORE';
$SIG{'QUIT'} = sub {&debug('spong updates caught QUIT signal, exiting');
close SERVER; exit;};
- my $proto = getprotobyname( 'tcp' );
- my $p = pack( "l", 1 );
-
- socket( SERVER, PF_INET, SOCK_STREAM, $proto ) || die "socket: $!";
- setsockopt( SERVER, SOL_SOCKET, SO_REUSEADDR, $p ) || die "sockopt: $!";
- bind( SERVER, sockaddr_in( $SPONG_UPDATE_PORT, INADDR_ANY ) ) ||
- die "bind: $!";
- listen( SERVER, SOMAXCONN ) || die "listen: $!";
+ # Set up the socket to listen to
+ my( $sock, $client );
+ $sock = IO::Socket::INET->new( Listen => SOMAXCONN,
+ LocalPort => $main::SPONG_UPDATE_PORT,
+ Proto => 'tcp',
+ Timeout => 30,
+ Reuse => 1,
+ );
+ if (! defined $sock ) { die "socket: $!"; }
&debug( "update server socket setup, listening for connections" );
while( 1 ) {
- next unless ( $paddr = accept( CLIENT, SERVER ) );
+ next unless ( $client = $sock->accept() );
+ $paddr = $client->peerhost();
# &validate_connection( $paddr ); - need to do something here...
local $SIG{'ALRM'} = sub { die; };
alarm($SPONG_SERVER_ALARM) if $SPONG_SERVER_ALARM;
- $header = <CLIENT>; chomp $header;
- while( defined( $line = <CLIENT> ) ) {
+ $header = <$client>; chomp $header;
+ while( defined( $line = <$client> ) ) {
last if ($cnt += length($line)) > 100000;
$message .= $line;
}
$ok = 1;
};
alarm(0);
- close( CLIENT );
+ $client->close();
if ( ! $ok ) {
- &error( "ss_update: Connection from $paddr timed out" );
+ &error( "listen_for_update: Connection from $paddr timed out" );
next;
}