- # case, the LOGIN message has already been processed (above).
- #
- # In short, we'll take any valid message here.
- #my $expect = SC_STATUS;
- my $expect = '';
- my $strikes = 3;
- while ($input = Sip::read_SIP_packet(*STDIN)) {
- # begin input hacks ... a cheap stand in for better Telnet layer
- $input =~ s/^[^A-z0-9]+//s; # Kill leading bad characters... like Telnet handshakers
- $input =~ s/[^A-z0-9]+$//s; # Same on the end, should get DOSsy ^M line-endings too.
- while (chomp($input)) {warn "Extra line ending on input";}
- unless ($input) {
- if ($strikes--) {
- syslog("LOG_ERR", "sip_protocol_loop: empty input skipped");
- next;
- } else {
- syslog("LOG_ERR", "sip_protocol_loop: quitting after too many errors");
- die "sip_protocol_loop: quitting after too many errors";
- }
- }
- # end cheap input hacks
- my $status = Sip::MsgType::handle($input, $self, $expect);
- if (!$status) {
- syslog("LOG_ERR", "sip_protocol_loop: failed to handle %s",substr($input,0,2));
- die "sip_protocol_loop: failed Sip::MsgType::handle('$input', $self, '$expect')";
- }
- next if $status eq REQUEST_ACS_RESEND;
- if ($expect && ($status ne $expect)) {
- # We received a non-"RESEND" that wasn't what we were expecting.
- syslog("LOG_ERR", "sip_protocol_loop: expected %s, received %s, exiting", $expect, $input);
- die "sip_protocol_loop: exiting: expected '$expect', received '$status'";
- }
- # We successfully received and processed what we were expecting
- $expect = '';
- }
+ # case, the LOGIN message has already been processed (above).
+
+ # In short, we'll take any valid message here.
+ eval {
+ local $SIG{ALRM} = sub {
+ syslog( 'LOG_DEBUG', 'Inactive: timed out' );
+ die "Timed Out!\n";
+ };
+ my $previous_alarm = alarm($timeout);
+
+ while ( my $inputbuf = read_request() ) {
+ if ( !defined $inputbuf ) {
+ return; #EOF
+ }
+ alarm($timeout);
+
+ unless ($inputbuf) {
+ syslog( "LOG_ERR", "sip_protocol_loop: empty input skipped" );
+ print("96$CR");
+ next;
+ }
+
+ my $status = C4::SIP::Sip::MsgType::handle( $inputbuf, $self, q{} );
+ if ( !$status ) {
+ syslog(
+ "LOG_ERR",
+ "sip_protocol_loop: failed to handle %s",
+ substr( $inputbuf, 0, 2 )
+ );
+ }
+ next if $status eq REQUEST_ACS_RESEND;
+ }
+ alarm($previous_alarm);
+ return;
+ };
+ if ( $@ =~ m/timed out/i ) {
+ return;
+ }
+ return;
+}
+
+sub read_request {
+ my $raw_length;
+ local $/ = "\015";
+
+ # proper SPEC: (octal) \015 = (hex) x0D = (dec) 13 = (ascii) carriage return
+ my $buffer = <STDIN>;
+ if ( defined $buffer ) {
+ STDIN->flush(); # clear an extra linefeed
+ chomp $buffer;
+ $raw_length = length $buffer;
+ $buffer =~ s/^\s*[^A-z0-9]+//s;
+# Every line must start with a "real" character. Not whitespace, control chars, etc.
+ $buffer =~ s/[^A-z0-9]+$//s;
+
+# Same for the end. Note this catches the problem some clients have sending empty fields at the end, like |||
+ $buffer =~ s/\015?\012//g; # Extra line breaks must die
+ $buffer =~ s/\015?\012//s; # Extra line breaks must die
+ $buffer =~ s/\015*\012*$//s;
+
+ # treat as one line to include the extra linebreaks we are trying to remove!
+ }
+ else {
+ syslog( 'LOG_DEBUG', 'EOF returned on read' );
+ return;
+ }
+ my $len = length $buffer;
+ if ( $len != $raw_length ) {
+ my $trim = $raw_length - $len;
+ syslog( 'LOG_DEBUG', "read_request trimmed $trim character(s) " );
+ }
+
+ syslog( 'LOG_INFO', "INPUT MSG: '$buffer'" );
+ return $buffer;
+}
+
+# $server->get_timeout({ $type => 1, fallback => $fallback });
+# where $type is transport | client | policy
+#
+# Centralizes all timeout logic.
+# Transport refers to login process, client to active connections.
+# Policy timeout is transaction timeout (used in ACS status message).
+#
+# Fallback is optional. If you do not pass transport, client or policy,
+# you will get fallback or hardcoded default.
+
+sub get_timeout {
+ my ( $server, $params ) = @_;
+ my $fallback = $params->{fallback} || 30;
+ my $service = $server->{service} // {};
+ my $config = $server->{config} // {};
+
+ if( $params->{transport} ||
+ ( $params->{client} && !exists $service->{client_timeout} )) {
+ # We do not allow zero values here.
+ # Note: config/timeout seems to be deprecated.
+ return $service->{timeout} || $config->{timeout} || $fallback;
+
+ } elsif( $params->{client} ) {
+ # We know that client_timeout exists now.
+ # We do allow zero values here to indicate no timeout.
+ return 0 if $service->{client_timeout} =~ /^0+$|\D/;
+ return $service->{client_timeout};
+
+ } elsif( $params->{policy} ) {
+ my $policy = $server->{policy} // {};
+ my $rv = sprintf( "%03d", $policy->{timeout} // 0 );
+ if( length($rv) != 3 ) {
+ syslog( "LOG_ERR", "Policy timeout has wrong size: '%s'", $rv );
+ return '000';
+ }
+ return $rv;
+
+ } else {
+ return $fallback;
+ }