View | Details | Raw Unified | Return to bug 15006
Collapse All | Expand All

(-)a/C4/SIP/SIPServer.pm (-3 / +6 lines)
Lines 15-20 use C4::SIP::Sip::Constants qw(:all); Link Here
15
use C4::SIP::Sip::Configuration;
15
use C4::SIP::Sip::Configuration;
16
use C4::SIP::Sip::Checksum qw(checksum verify_cksum);
16
use C4::SIP::Sip::Checksum qw(checksum verify_cksum);
17
use C4::SIP::Sip::MsgType qw( handle login_core );
17
use C4::SIP::Sip::MsgType qw( handle login_core );
18
use C4::SIP::Sip qw( get_timeout );
18
19
19
use base qw(Net::Server::PreFork);
20
use base qw(Net::Server::PreFork);
20
21
Lines 128-134 sub raw_transport { Link Here
128
    my $strikes = 3;
129
    my $strikes = 3;
129
130
130
    local $SIG{ALRM} = sub { die "raw_transport Timed Out!\n"; };
131
    local $SIG{ALRM} = sub { die "raw_transport Timed Out!\n"; };
131
    my $timeout = $service->{timeout} || 30;
132
    my $timeout = get_timeout( $self, { transport => 1 } );
132
    syslog("LOG_DEBUG", "raw_transport: timeout is %d", $timeout);
133
    syslog("LOG_DEBUG", "raw_transport: timeout is %d", $timeout);
133
134
134
    while( $strikes && ( !$self->{account} || !$self->{account}->{id} )) {
135
    while( $strikes && ( !$self->{account} || !$self->{account}->{id} )) {
Lines 190-196 sub telnet_transport { Link Here
190
    my $account = undef;
191
    my $account = undef;
191
    my $input;
192
    my $input;
192
    my $config  = $self->{config};
193
    my $config  = $self->{config};
193
    my $timeout = $self->{service}->{timeout} || $config->{timeout} || 30;
194
    my $timeout = get_timeout( $self, { transport => 1 } );
194
    syslog("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
195
    syslog("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
195
196
196
    eval {
197
    eval {
Lines 253-259 sub sip_protocol_loop { Link Here
253
    my $self = shift;
254
    my $self = shift;
254
    my $service = $self->{service};
255
    my $service = $self->{service};
255
    my $config  = $self->{config};
256
    my $config  = $self->{config};
256
    my $timeout = $self->{service}->{client_timeout} || $config->{client_timeout};
257
    my $timeout = get_timeout( $self, { client => 1 } );
257
258
258
    # The spec says the first message will be:
259
    # The spec says the first message will be:
259
    #     SIP v1: SC_STATUS
260
    #     SIP v1: SC_STATUS
Lines 341-345 sub read_request { Link Here
341
      syslog( 'LOG_INFO', "INPUT MSG: '$buffer'" );
342
      syslog( 'LOG_INFO', "INPUT MSG: '$buffer'" );
342
      return $buffer;
343
      return $buffer;
343
}
344
}
345
344
1;
346
1;
347
345
__END__
348
__END__
(-)a/C4/SIP/Sip.pm (-2 / +45 lines)
Lines 22-35 BEGIN { Link Here
22
	@ISA = qw(Exporter);
22
	@ISA = qw(Exporter);
23
23
24
	@EXPORT_OK = qw(y_or_n timestamp add_field maybe_add add_count
24
	@EXPORT_OK = qw(y_or_n timestamp add_field maybe_add add_count
25
            denied sipbool boolspace write_msg
25
            denied sipbool boolspace write_msg get_timeout
26
		    $error_detection $protocol_version $field_delimiter
26
		    $error_detection $protocol_version $field_delimiter
27
		    $last_response);
27
		    $last_response);
28
28
29
	%EXPORT_TAGS = (
29
	%EXPORT_TAGS = (
30
		    all => [qw(y_or_n timestamp add_field maybe_add
30
		    all => [qw(y_or_n timestamp add_field maybe_add
31
			       add_count denied sipbool boolspace write_msg
31
			       add_count denied sipbool boolspace write_msg
32
			       $error_detection $protocol_version
32
                   get_timeout
33
                   $error_detection $protocol_version
33
			       $field_delimiter $last_response)]);
34
			       $field_delimiter $last_response)]);
34
}
35
}
35
36
Lines 197-200 sub write_msg { Link Here
197
    $last_response = $msg;
198
    $last_response = $msg;
198
}
199
}
199
200
201
# get_timeout( $server, { $type => 1, fallback => $fallback } );
202
#     where $type is transport | client | policy
203
#
204
# Centralizes all timeout logic.
205
# Transport refers to login process, client to active connections.
206
# Policy timeout is transaction timeout (used in ACS status message).
207
#
208
# Fallback is optional. If you do not pass transport, client or policy,
209
# you will get fallback or hardcoded default.
210
211
sub get_timeout {
212
    my ( $server, $params ) = @_;
213
    my $fallback = $params->{fallback} || 30;
214
    my $service = $server->{service} // {};
215
    my $config = $server->{config} // {};
216
217
    if( $params->{transport} ||
218
        ( $params->{client} && !exists $service->{client_timeout} )) {
219
        # We do not allow zero values here.
220
        # Note: config/timeout seems to be deprecated.
221
        return $service->{timeout} || $config->{timeout} || $fallback;
222
223
    } elsif( $params->{client} ) {
224
        # We know that client_timeout exists now.
225
        # We do allow zero values here to indicate no timeout.
226
        return 0 if $service->{client_timeout} =~ /^0+$|\D/;
227
        return $service->{client_timeout};
228
229
    } elsif( $params->{policy} ) {
230
        my $policy = $server->{policy} // {};
231
        my $rv = sprintf( "%03d", $policy->{timeout} // 0 );
232
        if( length($rv) != 3 ) {
233
            syslog( "LOG_ERR", "Policy timeout has wrong size: '%s'", $rv );
234
            return '000';
235
        }
236
        return $rv;
237
238
    } else {
239
        return $fallback;
240
    }
241
}
242
200
1;
243
1;
(-)a/C4/SIP/Sip/MsgType.pm (-6 / +1 lines)
Lines 1505-1518 sub send_acs_status { Link Here
1505
    $ACS_renewal_policy = sipbool( $policy->{renewal} );
1505
    $ACS_renewal_policy = sipbool( $policy->{renewal} );
1506
    $status_update_ok   = sipbool( $ils->status_update_ok );
1506
    $status_update_ok   = sipbool( $ils->status_update_ok );
1507
    $offline_ok         = sipbool( $ils->offline_ok );
1507
    $offline_ok         = sipbool( $ils->offline_ok );
1508
    $timeout            = sprintf( "%03d", $policy->{timeout} );
1508
    $timeout            = get_timeout( $server, { policy => 1 } );
1509
    $retries            = sprintf( "%03d", $policy->{retries} );
1509
    $retries            = sprintf( "%03d", $policy->{retries} );
1510
1510
1511
    if ( length($timeout) != 3 ) {
1512
        syslog( "LOG_ERR", "handle_acs_status: timeout field wrong size: '%s'", $timeout );
1513
        $timeout = '000';
1514
    }
1515
1516
    if ( length($retries) != 3 ) {
1511
    if ( length($retries) != 3 ) {
1517
        syslog( "LOG_ERR", "handle_acs_status: retries field wrong size: '%s'", $retries );
1512
        syslog( "LOG_ERR", "handle_acs_status: retries field wrong size: '%s'", $retries );
1518
        $retries = '000';
1513
        $retries = '000';
(-)a/etc/SIPconfig.xml (-2 / +2 lines)
Lines 28-36 Link Here
28
      protocol="SIP/2.00"
28
      protocol="SIP/2.00"
29
      timeout="60" />
29
      timeout="60" />
30
<!-- client_timeout times out active connections which have not received
30
<!-- client_timeout times out active connections which have not received
31
     input from the client. Many persistemt connections will send a status request
31
     input from the client. Many persistent connections will send a status request
32
     every 5-7 mins so setting this to less than that will add instability to the connection
32
     every 5-7 mins so setting this to less than that will add instability to the connection
33
     if not set then no timeout is applied to the connection
33
     if explicitly set to zero, no timeout is applied to the connection
34
     NB the parameter timeout applies to the login process only and should be set to a lower value
34
     NB the parameter timeout applies to the login process only and should be set to a lower value
35
     to time out failed connections
35
     to time out failed connections
36
-->
36
-->
(-)a/t/SIP_Sip.t (-25 / +66 lines)
Lines 17-58 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 9;
20
use Test::More tests => 3;
21
use Test::Warn;
21
use Test::Warn;
22
22
23
BEGIN {
23
BEGIN {
24
        use_ok('C4::SIP::Sip');
24
    use_ok('C4::SIP::Sip', qw|get_timeout| );
25
}
25
}
26
26
27
my $date_time = C4::SIP::Sip::timestamp();
27
subtest 'Timestamp' => sub {
28
like( $date_time, qr/^\d{8}    \d{6}$/, 'Timestamp format no param');
28
    plan tests => 8;
29
    test_timestamp();
30
};
31
subtest 'Get_timeout' => sub {
32
    plan tests => 11;
33
    test_get_timeout();
34
};
29
35
30
my $t = time();
36
sub test_timestamp {
37
    my $date_time = C4::SIP::Sip::timestamp();
38
    like( $date_time, qr/^\d{8}    \d{6}$/, 'Timestamp format no param');
31
39
32
$date_time = C4::SIP::Sip::timestamp($t);
40
    my $t = time();
33
like( $date_time, qr/^\d{8}    \d{6}$/, 'Timestamp format secs');
34
41
35
$date_time = C4::SIP::Sip::timestamp('2011-01-12');
42
    $date_time = C4::SIP::Sip::timestamp($t);
36
ok( $date_time eq '20110112    235900', 'Timestamp iso date string');
43
    like( $date_time, qr/^\d{8}    \d{6}$/, 'Timestamp format secs');
37
44
38
my $myChecksum = C4::SIP::Sip::Checksum::checksum("12345");
45
    $date_time = C4::SIP::Sip::timestamp('2011-01-12');
39
my $checker = 65281;
46
    ok( $date_time eq '20110112    235900', 'Timestamp iso date string');
40
my $stringChecksum = C4::SIP::Sip::Checksum::checksum("teststring");
41
my $stringChecker = 64425;
42
47
43
is( $myChecksum, $checker, "Checksum: $myChecksum matches expected output");
48
    my $myChecksum = C4::SIP::Sip::Checksum::checksum("12345");
44
is( $stringChecksum, $stringChecker, "Checksum: $stringChecksum matches expected output");
49
    my $checker = 65281;
50
    my $stringChecksum = C4::SIP::Sip::Checksum::checksum("teststring");
51
    my $stringChecker = 64425;
45
52
46
my $testdata = "abcdAZ";
53
    is( $myChecksum, $checker, "Checksum: $myChecksum matches expected output");
47
my $something = C4::SIP::Sip::Checksum::checksum($testdata);
54
    is( $stringChecksum, $stringChecker, "Checksum: $stringChecksum matches expected output");
48
55
49
$something =  sprintf("%4X", $something);
56
    my $testdata = "abcdAZ";
50
ok( C4::SIP::Sip::Checksum::verify_cksum($testdata.$something), "Checksum: $something is valid.");
57
    my $something = C4::SIP::Sip::Checksum::checksum($testdata);
51
58
52
my $invalidTest;
59
    $something =  sprintf("%4X", $something);
53
warning_is { $invalidTest = C4::SIP::Sip::Checksum::verify_cksum("1234567") }
60
    ok( C4::SIP::Sip::Checksum::verify_cksum($testdata.$something), "Checksum: $something is valid.");
54
            'verify_cksum: no sum detected',
61
55
            'verify_cksum prints the expected warning for an invalid checksum';
62
    my $invalidTest;
56
is($invalidTest, 0, "Checksum: 1234567 is invalid as expected");
63
    warning_is { $invalidTest = C4::SIP::Sip::Checksum::verify_cksum("1234567") }
64
                'verify_cksum: no sum detected',
65
                'verify_cksum prints the expected warning for an invalid checksum';
66
    is($invalidTest, 0, "Checksum: 1234567 is invalid as expected");
67
}
68
69
sub test_get_timeout {
70
    my $server = { policy => { timeout => 1 },
71
                   config => { timeout => 2 },
72
                   service => {
73
                       timeout => 3,
74
                       client_timeout => 4,
75
                   },
76
    };
77
78
    is( get_timeout(), 30, "Default fallback" );
79
    is( get_timeout( undef, { fallback => 25 } ), 25, "Fallback parameter" );
80
    is( get_timeout( $server, { transport => 1 } ), 3, "Transport value" );
81
    is( get_timeout( $server, { client => 1 } ), 4, "Client value" );
82
    is( get_timeout( $server, { policy => 1 } ), '001', "Policy value" );
83
84
    delete $server->{policy}->{timeout};
85
    is( get_timeout( $server, { policy => 1 } ), '000', "No policy" );
86
87
    $server->{service}->{client_timeout} = '0';
88
    is( get_timeout( $server, { client => 1 } ), 0, "Client zero" );
89
    $server->{service}->{client_timeout} = 'no';
90
    is( get_timeout( $server, { client => 1 } ), 0, "Client no" );
91
    delete $server->{service}->{client_timeout};
92
    is( get_timeout( $server, { client => 1 } ), 3, "Fallback to service" );
93
94
    delete $server->{service}->{timeout};
95
    is( get_timeout( $server, { transport => 1 } ), 2, "Back to old config" );
96
    delete $server->{config}->{timeout};
97
    is( get_timeout( $server, { transport => 1 } ), 30, "Fallback again" );
98
}
57
99
58
1;
100
1;
59
- 

Return to bug 15006