From 5d1eaf3c9e9aa4507e1e47d47a0d3e3baf06ef94 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Thu, 12 Sep 2013 16:15:54 -0400 Subject: [PATCH] SIP Multiplex repairs * mux_input params / string handling - no reading from $fh * until proven otherwise, capture mux $fh for later writing -- may be unnecessary. * minor thinkos Signed-off-by: Bill Erickson --- SIPServer.pm | 30 +++++++++++++++++++++--------- Sip.pm | 4 +++- Sip/MsgType.pm | 1 + 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/SIPServer.pm b/SIPServer.pm index 0332665..e60532e 100644 --- a/SIPServer.pm +++ b/SIPServer.pm @@ -29,9 +29,11 @@ use Net::Server::Multiplex; use Net::Server::PreFork; use Net::Server::Proto; use IO::Socket::INET; +use IO::String; use Socket qw(:crlf); use Data::Dumper; # For debugging require UNIVERSAL::require; +use POSIX qw/:sys_wait_h :errno_h/; #use Sip qw(readline); use Sip::Constants qw(:all); @@ -165,23 +167,30 @@ my $kid_count; sub REAPER { for (keys(%kid_hash)) { - if ( my $reaped = waitpid($_, &WNOHANG) > 0 ) { + if ( my $reaped = waitpid($_, WNOHANG) > 0 ) { # Mourning... done. $kid_count--; delete $kid_hash{$_}; } } - $SIG{CHLD} = &REAPER(); + $SIG{CHLD} = sub { REAPER() }; } my %active_connections; sub mux_input { my $mself = shift; my $mux = shift; - my $fh = shift; + my $mux_fh = shift; + my $str_ref = shift; + + # clone the mux string into a file handle + my $str_fh = IO::String->new(''.$$str_ref); + + # clear read data from the mux string ref + $$str_ref = ''; my $self; - my $conn_id = fileno($fh); + my $conn_id = fileno($mux_fh); # check for kids that went away REAPER(); @@ -215,7 +224,7 @@ sub mux_input { return; } - &$transport($self, $fh); + &$transport($self, $str_fh); $active_connections{$conn_id} = { id => $conn_id, @@ -234,6 +243,7 @@ sub mux_input { } $self = $active_connections{$conn_id}->{net_server}; + $self->{output_fh} = $mux_fh; my $pid = fork(); die "Cannot fork: $!" unless (defined($pid) && $pid > -1); @@ -243,7 +253,8 @@ sub mux_input { # build the connection we deleted after logging in $self->{ils} = $active_connections{$conn_id}->{ils}->new($self->{institution}, $self->{account}); - my $input = Sip::read_SIP_packet($fh); + # build the connection we deleted after logging in + my $input = Sip::read_SIP_packet($str_fh); $input =~ s/[\r\n]+$//sm; # Strip off any trailing line ends my $status = Sip::MsgType::handle($input, $self, ''); @@ -268,7 +279,8 @@ sub mux_input { sub raw_transport { my $self = shift; - my $fh ||= *STDIN; + my $fh = shift || *STDIN; + my ($uid, $pwd); my $input; my $service = $self->{service}; @@ -309,12 +321,12 @@ sub raw_transport { syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'", $self->{account}->{id}, $self->{account}->{institution}); - } sub telnet_transport { my $self = shift; - my $fh ||= *STDIN; + my $fh = shift || *STDIN; + my ($uid, $pwd); my $strikes = 3; my $account = undef; diff --git a/Sip.pm b/Sip.pm index 9dea1c1..07a4cd7 100644 --- a/Sip.pm +++ b/Sip.pm @@ -226,6 +226,8 @@ sub read_SIP_packet { sub write_msg { my ($self, $msg, $file) = @_; + $file ||= $self->{output_fh}; + if ($error_detection) { if (defined($self->{seqno})) { $msg .= 'AY' . $self->{seqno}; @@ -239,9 +241,9 @@ sub write_msg { print $file "$msg\r"; } else { print "$msg\r"; - syslog("LOG_INFO", "OUTPUT MSG: '$msg'"); } + syslog("LOG_INFO", "OUTPUT MSG: '$msg'"); $last_response = $msg; } diff --git a/Sip/MsgType.pm b/Sip/MsgType.pm index 6fb4793..4648ef4 100644 --- a/Sip/MsgType.pm +++ b/Sip/MsgType.pm @@ -423,6 +423,7 @@ sub handle { $req && (substr($msg, 0, 2) ne $req)) { return substr($msg, 0, 2); } + $self->{output_fh} = $server->{output_fh}; return($self->{handler}->($self, $server)); } -- 2.11.0