From: Stephen L Johnson Date: Fri, 18 Feb 2000 22:28:13 +0000 (+0000) Subject: changed listen_for_updates() to use IO:Socket::INET for sockets X-Git-Tag: spong-2_6e^0 X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a6c4642ca72ba72a0e8eee06aa70a1db3145f90c;p=spong.git changed listen_for_updates() to use IO:Socket::INET for sockets --- diff --git a/src/spong-server.pl b/src/spong-server.pl index 153e017..61026a4 100755 --- a/src/spong-server.pl +++ b/src/spong-server.pl @@ -28,6 +28,7 @@ use Sys::Hostname; 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; } @@ -98,7 +99,7 @@ die "Error: Exiting spong-server!\n"; # 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'); @@ -107,19 +108,21 @@ sub listen_for_updates { 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... @@ -134,8 +137,8 @@ sub listen_for_updates { local $SIG{'ALRM'} = sub { die; }; alarm($SPONG_SERVER_ALARM) if $SPONG_SERVER_ALARM; - $header = ; chomp $header; - while( defined( $line = ) ) { + $header = <$client>; chomp $header; + while( defined( $line = <$client> ) ) { last if ($cnt += length($line)) > 100000; $message .= $line; } @@ -143,10 +146,10 @@ sub listen_for_updates { $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; }