Search code examples
perlssliosip

perl ssl tls connection with net::sip


i try to create tls net::sip connetion in perl

i have a good ssl connection

this is my sub

start_connection("2.50.44.55:5061", "100", "100");
sub start_connection{
    my @parms = @_;
    my $host  = $parms[0];
    my $user  = $parms[1];
    my $pass  = $parms[2]; 

     $sock = IO::Socket::SSL->new(
             PeerAddr => $host,
             SSL_fingerprint => 'sha1$CE68626830EAF46482F55CB7FBF4DA1B77889FDD',
        ) or die $SSL_ERROR;

    $sock->connect_SSL() or die $SSL_ERROR; 

    my $leg = Net::SIP::Leg->new(sock =>  $sock , proto => 'tls' , host =>  $host  )|| exit ;


    my $ua = Net::SIP::Simple->new(
        registrar => $host,
        domain => $host, 
        from => $user,
        auth => [ $user,$pass ],
         leg => $leg,
    )|| exit;
    $ua->register;
    my $err = $ua->error; 
    print $err;   
}

this is my IO::Socket::SSL debug

DEBUG: .../IO/Socket/SSL.pm:3010: new ctx 37429024
DEBUG: .../IO/Socket/SSL.pm:762: socket not yet connected
DEBUG: .../IO/Socket/SSL.pm:764: socket connected
DEBUG: .../IO/Socket/SSL.pm:787: ssl handshake not started
DEBUG: .../IO/Socket/SSL.pm:832: not using SNI because hostname is unknown
DEBUG: .../IO/Socket/SSL.pm:864: request OCSP stapling
DEBUG: .../IO/Socket/SSL.pm:894: call Net::SSLeay::connect
DEBUG: .../IO/Socket/SSL.pm:2864: ok=0 [1] /CN=Asterisk Private CA/O=asterisk/CN=Asterisk Private CA/O=asterisk
DEBUG: .../IO/Socket/SSL.pm:2864: ok=0 [1] /CN=Asterisk Private CA/O=asterisk/CN=Asterisk Private CA/O=asterisk
DEBUG: .../IO/Socket/SSL.pm:2864: ok=1 [1] /CN=Asterisk Private CA/O=asterisk/CN=Asterisk Private CA/O=asterisk
DEBUG: .../IO/Socket/SSL.pm:2864: ok=0 [0] /CN=Asterisk Private CA/O=asterisk/CN=localhost/O=asterisk
DEBUG: .../IO/Socket/SSL.pm:2864: ok=1 [0] /CN=Asterisk Private CA/O=asterisk/CN=localhost/O=asterisk
DEBUG: .../IO/Socket/SSL.pm:897: done Net::SSLeay::connect -> 1
DEBUG: .../IO/Socket/SSL.pm:952: ssl handshake done
DEBUG: .../IO/Socket/SSL.pm:3010: new ctx 45515568
DEBUG: .../IO/Socket/SSL.pm:3010: new ctx 46461040
Failed with error 113DEBUG: .../IO/Socket/SSL.pm:3059: free ctx 46461040 open=46461040 45515568 37429024
DEBUG: .../IO/Socket/SSL.pm:3063: free ctx 46461040 callback
DEBUG: .../IO/Socket/SSL.pm:3070: OK free ctx 46461040
DEBUG: .../IO/Socket/SSL.pm:3059: free ctx 45515568 open=45515568 37429024
DEBUG: .../IO/Socket/SSL.pm:3070: OK free ctx 45515568
DEBUG: .../IO/Socket/SSL.pm:3059: free ctx 37429024 open=37429024
DEBUG: .../IO/Socket/SSL.pm:3063: free ctx 37429024 callback
DEBUG: .../IO/Socket/SSL.pm:3070: OK free ctx 37429024

it is a good connnection

and this is my net::sip debug

When printing $ err; Be the output Failed with error 113

It is supposed to be at Print $ err; The result She is Failed with error 404 or Failed with error 403 But I think there is a problem with the leg connection

1590637618.9245 DEBUG:<99> Net::SIP::Dispatcher::Eventloop::addFD[61]: Net::SIP::Dispatcher::Eventloop=HASH(0x18f29c0) added fn=3 rw(0) sock=103.145.12.104:60188
1590637618.9249 DEBUG:<100> Net::SIP::Dispatcher::Eventloop::addFD[64]: maxfd=4
1590637618.9257 DEBUG:<100> Net::SIP::Endpoint::Context::new[84]: CREATE context Net::SIP::Endpoint::Context=HASH(0x18f67f8) callid=16c05c388bcb952fd738fd5aeb9dbcd2
1590637618.9260 DEBUG:<10> Net::SIP::Endpoint::new_request[156]: create new request for REGISTER within new call 16c05c388bcb952fd738fd5aeb9dbcd2
1590637618.9270 DEBUG:<50> Net::SIP::Endpoint::new_request[164]: request=REGISTER sip:2.50.44.55:5061 SIP/2.0
1590637618.9270 DEBUG:<50>      Call-id: 16c05c388bcb952fd738fd5aeb9dbcd2
1590637618.9270 DEBUG:<50>      Contact: 100 <sips:100@[2.50.44.55:5061]:60188>
1590637618.9270 DEBUG:<50>      Cseq: 1 REGISTER
1590637618.9270 DEBUG:<50>      Expires: 900
1590637618.9270 DEBUG:<50>      From: 100 <sips:[email protected]:5061>;tag=8a2c944e529b5155822a25c60e8b8865
1590637618.9270 DEBUG:<50>      Max-forwards: 70
1590637618.9270 DEBUG:<50>      To: 100 <sips:[email protected]:5061>
1590637618.9270 DEBUG:<50>      Content-length: 0
1590637618.9270 DEBUG:<50>
1590637618.9270 DEBUG:<50>
1590637618.9295 DEBUG:<100> Net::SIP::Dispatcher::deliver[392]: deliver Net::SIP::Request=HASH(0x18fbb80)
1590637618.9300 DEBUG:<100> Net::SIP::Dispatcher::Packet::prepare_retransmits[1318]: retransmits 1590637618 + 0.5 1.5 3.5 7.5 11.5 15.5 19.5 23.5 27.5 31.5
1590637618.9303 DEBUG:<100> Net::SIP::Dispatcher::__deliver[607]: no dst_addr or leg yet, uri='sip:2.50.44.55:5061'
1590637618.9307 DEBUG:<100> Net::SIP::Dispatcher::resolve_uri[736]: domain=55.44.50.2.in-addr.arpa
1590637618.9309 DEBUG:<50> Net::SIP::Dispatcher::resolve_uri[766]: setting dst_addr from URI because IP address given
1590637618.9317 DEBUG:<100> Net::SIP::Dispatcher::__resolve_uri_final[837]: $VAR1 = [
1590637618.9317 DEBUG:<100>               {
1590637618.9317 DEBUG:<100>                 'proto' => 'udp',
1590637618.9317 DEBUG:<100>                 'prio' => 65536,
1590637618.9317 DEBUG:<100>                 'addr' => '2.50.44.55',
1590637618.9317 DEBUG:<100>                 'port' => '5061',
1590637618.9317 DEBUG:<100>                 'host' => '2.50.44.55',
1590637618.9317 DEBUG:<100>                 'family' => 2
1590637618.9317 DEBUG:<100>               },
1590637618.9317 DEBUG:<100>               {
1590637618.9317 DEBUG:<100>                 'proto' => 'tcp',
1590637618.9317 DEBUG:<100>                 'prio' => 65536,
1590637618.9317 DEBUG:<100>                 'addr' => '2.50.44.55',
1590637618.9317 DEBUG:<100>                 'port' => '5061',
1590637618.9317 DEBUG:<100>                 'host' => '2.50.44.55',
1590637618.9317 DEBUG:<100>                 'family' => 2
1590637618.9317 DEBUG:<100>               }
1590637618.9317 DEBUG:<100>             ];
1590637618.9317 DEBUG:<100>
1590637618.9357 DEBUG:<50> Net::SIP::Dispatcher::__resolve_uri_final[864]: no leg with udp to 2.50.44.55:5061
1590637618.9360 DEBUG:<50> Net::SIP::Dispatcher::__resolve_uri_final[864]: no leg with tcp to 2.50.44.55:5061
1590637618.9679 DEBUG:<100> Net::SIP::Simple::error[244]: 1590637618.9364 DEBUG: Net::SIP::Debug::stacktrace[194]: set error to Failed with error 113 at /root/perl5/lib/perl5/Net/SIP/Simple.pm line 244.
1590637618.9679 DEBUG:<100>             Net::SIP::Simple::error('Net::SIP::Simple=HASH(0x18f2948)', 'Failed with error 113') called at /root/perl5/lib/perl5/Net/SIP/Simple.pm line 377
1590637618.9679 DEBUG:<100>             Net::SIP::Simple::__ANON__('Net::SIP::Simple=HASH(0x18f2948)', 'SCALAR(0x15dc8b8)', 'SCALAR(0x15dc9d8)', 'Net::SIP::Endpoint=HASH(0x18f6078)', 'Net::SIP::Endpoint::Context=HASH(0x18f67f8)', 113) called at /root/perl5/lib/perl5/Net/SIP/Util.pm line 474
1590637618.9679 DEBUG:<100>             Net::SIP::Util::invoke_callback('ARRAY(0x18f6570)', 'Net::SIP::Endpoint=HASH(0x18f6078)', 'Net::SIP::Endpoint::Context=HASH(0x18f67f8)', 113) called at /root/perl5/lib/perl5/Net/SIP/Endpoint/Context.pm line 258
1590637618.9679 DEBUG:<100>             Net::SIP::Endpoint::Context::request_delivery_done('Net::SIP::Endpoint::Context=HASH(0x18f67f8)', 'Net::SIP::Endpoint=HASH(0x18f6078)', '16c05c388bcb952fd738fd5aeb9dbcd2 1', 113) called at /root/perl5/lib/perl5/Net/SIP/Endpoint.pm line 218
1590637618.9679 DEBUG:<100>             Net::SIP::Endpoint::_request_delivery_callback('Net::SIP::Endpoint=HASH(0x18f6078)', 'Net::SIP::Endpoint::Context=HASH(0x18f67f8)', 113, 'Net::SIP::Dispatcher::Packet=HASH(0x18fbd48)') called at /root/perl5/lib/perl5/Net/SIP/Util.pm line 474
1590637618.9679 DEBUG:<100>             Net::SIP::Util::invoke_callback('ARRAY(0x18f6708)', 113, 'Net::SIP::Dispatcher::Packet=HASH(0x18fbd48)') called at /root/perl5/lib/perl5/Net/SIP/Dispatcher.pm line 1351
1590637618.9679 DEBUG:<100>             Net::SIP::Dispatcher::Packet::trigger_callback('Net::SIP::Dispatcher::Packet=HASH(0x18fbd48)', 113) called at /root/perl5/lib/perl5/Net/SIP/Dispatcher.pm line 612
1590637618.9679 DEBUG:<100>             Net::SIP::Dispatcher::__ANON__('Net::SIP::Dispatcher=HASH(0x18f2c30)', 'Net::SIP::Dispatcher::Packet=HASH(0x18fbd48)', 113) called at /root/perl5/lib/perl5/Net/SIP/Util.pm line 474
1590637618.9679 DEBUG:<100>             Net::SIP::Util::invoke_callback('ARRAY(0x18fc7b0)', 113) called at /root/perl5/lib/perl5/Net/SIP/Dispatcher.pm line 868
1590637618.9679 DEBUG:<100>             Net::SIP::Dispatcher::__resolve_uri_final('ARRAY(0x18fc330)', 'ARRAY(0x18fc168)', 'ARRAY(0x18fc7e0)', 5061, 'ARRAY(0x18fc7b0)', 'ARRAY(0x1406e38)') called at /root/perl5/lib/perl5/Net/SIP/Dispatcher.pm line 820
1590637618.9679 DEBUG:<100>             Net::SIP::Dispatcher::resolve_uri('Net::SIP::Dispatcher=HASH(0x18f2c30)', 'sip:2.50.44.55:5061', 'ARRAY(0x18fc330)', 'ARRAY(0x18fc168)', 'ARRAY(0x18fc7b0)', undef) called at /root/perl5/lib/perl5/Net/SIP/Dispatcher.pm line 618
1590637618.9679 DEBUG:<100>             Net::SIP::Dispatcher::__deliver('Net::SIP::Dispatcher=HASH(0x18f2c30)', 'Net::SIP::Dispatcher::Packet=HASH(0x18fbd48)') called at /root/perl5/lib/perl5/Net/SIP/Dispatcher.pm line 416
1590637618.9679 DEBUG:<100>             Net::SIP::Dispatcher::deliver('Net::SIP::Dispatcher=HASH(0x18f2c30)', 'Net::SIP::Request=HASH(0x18fbb80)', 'id', '16c05c388bcb952fd738fd5aeb9dbcd2 1', 'callback', 'ARRAY(0x18f6708)', 'leg', undef, 'dst_addr', ...) called at /root/perl5/lib/perl5/Net/SIP/Endpoint.pm line 167
1590637618.9679 DEBUG:<100>             Net::SIP::Endpoint::new_request('Net::SIP::Endpoint=HASH(0x18f6078)', 'REGISTER', 'HASH(0x144a3d0)', 'ARRAY(0x18f6570)', undef, 'uri', 'sip:2.50.44.55:5061', 'expires', 900, ...) called at /root/perl5/lib/perl5/Net/SIP/Endpoint.pm line 114
1590637618.9679 DEBUG:<100>             Net::SIP::Endpoint::register('Net::SIP::Endpoint=HASH(0x18f6078)', 'auth', 'ARRAY(0x18f2930)', 'registrar', 'sip:2.50.44.55:5061', 'from', '100 <sips:[email protected]:5061>', 'contact', '100 <sips:100@[2.50.44.55:5061]:60188>', ...) called at /root/perl5/lib/perl5/Net/SIP/Simple.pm line 386
1590637618.9679 DEBUG:<100>             Net::SIP::Simple::register('Net::SIP::Simple=HASH(0x18f2948)') called at dd.pl line 37
1590637618.9679 DEBUG:<100>             main::start_connection('2.50.44.55:5061', 100, 100) called at dd.pl line 13
1590637618.9679 DEBUG:<100>
1590637618.9731 DEBUG:<100> Net::SIP::Dispatcher::cancel_delivery[449]: cancel packet id: 16c05c388bcb952fd738fd5aeb9dbcd2 1
1590637618.9734 DEBUG:<100> Net::SIP::Dispatcher::Eventloop::loop[156]: timeout = <undef>
1590637618.9737 DEBUG:<100> Net::SIP::Dispatcher::Eventloop::loop[188]: timeout = 0.950617074966431
1590637618.9739 DEBUG:<50> Net::SIP::Dispatcher::Eventloop::loop[191]: stopvar triggered

Solution

  • In SIP the underlying SIP session is independent of the actual TCP/UDP connections. This means that on the client side one should not use a connected socket when creating a leg. Instead one should provide only the necessary information to create a socket or provide an unconnected socket. The latter makes on the client side only sense for UDP where sending from an unconnected socket is possible. With TCP an unconnected socket will not work since it needs to be connected for sending but cannot be connected multiple times in case the connection breaks.

    Based on this the code should look like this:

    use strict;
    use warnings;
    use Net::SIP;
    
    start_connection("2.50.44.55","5061","100", "100");
    
    sub start_connection{
        my ($host,$port,$user,$pass) = @_;
    
        my $ua = Net::SIP::Simple->new(
            registrar => "sips:$host:$port",
            domain => $host,
            from => $user,
            auth => [ $user,$pass ],
            tls => {
                SSL_fingerprint => 'sha1$CE68626830EAF46482F55CB7FBF4DA1B77889FDD',
            },
        ) or die "failed to create UA";
        $ua->register or die $ua->error;
        print "ok\n";
    }
    

    Here only the necessary information are given to create the socket but the connected socket itself will be created internally. Make sure that the URL of the registrar has an explicit sips:... in front to specify how the registrar should be contacted.

    Note that as it is the code seems to hang. Looking deeper (using perl -MNet::SIP::Debug=50 ...) one can see that the authentication with the given credentials fails and that the server requests authentication again and again, with same realm but different opaque parameter. Net::SIP was using a combination of both parameters to check if this is the same authentication target and thus it retried the authentication with the new opaque instead of failing. This is fixed in 908f5c8a0 so please check out the latest version when trying.