File Coverage

blib/lib/Net/SIP/Endpoint.pm
Criterion Covered Total %
statement 99 109 90.8
branch 19 28 67.8
condition 6 11 54.5
subroutine 19 20 95.0
pod 11 11 100.0
total 154 179 86.0


line stmt bran cond sub pod time code
1              
2             ############################################################################
3             # package Net::SIP::Endpoint
4             # implements the behavior of an endpoint (SIP phone).
5             # packet managment (lower layer) is done by Net::SIP::Dispatcher while
6             # call managment is done with Net::SIP::Endpoint::Context
7             ############################################################################
8              
9 41     41   295 use strict;
  41         83  
  41         1376  
10 41     41   219 use warnings;
  41         92  
  41         1952  
11             package Net::SIP::Endpoint;
12             use fields (
13 41         293 'dispatcher', # lower layer, delivers and receives packets through the legs
14             'application', # upper layer, e.g user interface..
15             'ctx' # hash of ( callid => Net::SIP::Endpoint::Context )
16 41     41   259 );
  41         120  
17              
18 41     41   3176 use Net::SIP::Debug;
  41         107  
  41         290  
19 41     41   20153 use Net::SIP::Endpoint::Context;
  41         132  
  41         1374  
20 41     41   326 use Net::SIP::Util qw(invoke_callback);
  41         91  
  41         3653  
21 41     41   1935 use Scalar::Util 'weaken';
  41         166  
  41         56124  
22              
23             ############################################################################
24             # create a new endpoint
25             # Args: ($class,$dispatcher)
26             # $dispatcher: lower layer which handles the delivery and receiving of packets
27             # Returns: $self
28             ############################################################################
29             sub new {
30 52     52 1 462 my ($class,$dispatcher) = @_;
31 52         202 my $self = fields::new($class);
32              
33 52         4917 $self->{dispatcher} = $dispatcher;
34 52         1265 $self->{ctx} = {}; # \%hash with ( callid => $ctx )
35              
36             # announce myself as upper layer for incoming packets to
37             # the dispatcher
38 52         566 my $cb = [ \&receive,$self ];
39 52         296 weaken( $cb->[1] );
40 52         360 $dispatcher->set_receiver( $cb );
41              
42 52         169 return $self;
43             }
44              
45             ############################################################################
46             # set upper layer (application)
47             # Args: ($self,$app)
48             # $app: upper layer which needs to have method receive( $request )
49             # to handle new request, which this layer cannot handle alone
50             # (e.g INVITE to a new dialog)
51             # or this can be \&sub, [ \&sub,@arg ]...
52             # Returns: NONE
53             ############################################################################
54             sub set_application {
55 16     16 1 155 my Net::SIP::Endpoint $self = shift;
56 16         129 my $app = shift;
57 16         112 my $cb;
58 16 50       266 if ( my $sub = UNIVERSAL::can( $app,'receive' )) {
59 0         0 $cb = [ $sub,$app ];
60             } else {
61 16         113 $cb = $app; # already callback
62             }
63 16         126 $self->{application} = $cb;
64             }
65              
66             ############################################################################
67             # create a new call or re-invite on a existing call
68             # wrapper around new_request()
69             # Args: ($self,$ctx;$callback,$body,%args)
70             # $ctx: Context|\%args, see new_request()
71             # $callback: optional Callback, see new_request()
72             # $body: optional Body
73             # %args: additional args for Net::SIP::Request::new
74             # Returns: $ctx
75             # $ctx: see new_request()
76             ############################################################################
77             sub invite {
78 36     36 1 114 my Net::SIP::Endpoint $self = shift;
79 36         138 my ($ctx,$callback,$body,%args) = @_;
80 36         328 return $self->new_request( 'INVITE',$ctx,$callback,$body,%args );
81             }
82              
83             ############################################################################
84             # registers UAC
85             # Args: ($self,%args)
86             # %args: at minimum there must be
87             # from: the sip-address to register
88             # contact: to which local address should it registered
89             # registrar: SIP address of registrar
90             # there can be:
91             # expires: Expires header, defaults to 900 if not given
92             # callback: callback which will be called on response
93             # callid: callid used for calling context
94             # all other args will be used in creation of request
95             # Returns: NONE
96             ############################################################################
97             sub register {
98 0     0 1 0 my Net::SIP::Endpoint $self = shift;
99 0         0 my %args = @_;
100              
101             my ($me,$registrar,$contact) =
102 0         0 delete @args{qw( from registrar contact )};
103              
104 0         0 my $expires = delete $args{expires};
105 0 0       0 $expires = 900 if !defined($expires);
106              
107             my %ctx = (
108             to => $me,
109             from => $me,
110             contact => $contact,
111             auth => delete $args{auth},
112             callid => delete $args{callid},
113 0         0 );
114             return $self->new_request(
115             'REGISTER',
116             \%ctx,
117 0         0 delete($args{callback}),
118             undef,
119             uri => $registrar,
120             expires => $expires,
121             %args,
122             );
123             }
124              
125              
126             ############################################################################
127             # starts new request, e.g creates request packet and delivers it
128             # Args: ($self,$method,$ctx;$callback,$body,%args)
129             # $method: method name, e.g. 'INVITE','REGISTER',..
130             # can also be a full Net::SIP::Request already (used for retries after
131             # 302,305 responses)
132             # $ctx: already established context (Net::SIP::Endpoint::Context)
133             # or \%hash to create a new one (see Net::SIP::Endpoint::Context->new)
134             # $callback: [ \&sub,@arg ] which will be called if the layer receives
135             # responses important to the upper layer (e.g 180 Ringing, 200 Ok,
136             # 401/407 Authorization required...)
137             # if callback is omitted the callback from the context is used,
138             # if callback is set it will be the new callback for the context
139             # $body: optional Body, either scalar or smth with method as_string
140             # (like Net::SIP::SDP)
141             # %args: additional args for Net::SIP::Endpoint::Context::new_request
142             # Returns: $ctx
143             # $ctx: context, eg the original one or newly created
144             # Comment: if it cannot create a new context (because of missing args)
145             # or something else fatal happens it will die()
146             ############################################################################
147             sub new_request {
148 109     109 1 270 my Net::SIP::Endpoint $self = shift;
149 109         880 my ($method,$ctx,$callback,$body,%args) = @_;
150              
151 109 50       540 die "cannot redefine call-id" if delete $args{ 'call-id' };
152              
153 109 100       650 if ( ! UNIVERSAL::isa( $ctx,'Net::SIP::Endpoint::Context' )) {
154 35         1353 $ctx = Net::SIP::Endpoint::Context->new(%$ctx, method => $method);
155 35         211 $self->{ctx}{ $ctx->callid } = $ctx; # make sure we manage the context
156 35         241 DEBUG( 10,"create new request for $method within new call ".$ctx->callid );
157             } else {
158 74         726 DEBUG( 10,"create new request for $method within existing call ".$ctx->callid );
159             }
160              
161 109 100       1039 $ctx->set_callback( $callback ) if $callback;
162              
163 109         813 my $request = $ctx->new_request( $method,$body,%args );
164 109         1094 DEBUG( 50,"request=".$request->as_string );
165              
166 109         509 my $tid = $request->tid;
167             $self->{dispatcher}->deliver( $request,
168             id => $tid,
169             callback => [ \&_request_delivery_callback, $self,$ctx ],
170             leg => $args{leg},
171             dst_addr => $args{dst_addr},
172 109         1461 );
173              
174 109         1987 return $ctx;
175             }
176              
177             ############################################################################
178             # Cancel last pending INVITE request
179             # Args: ($self,$ctx,$request,$cb)
180             # $ctx: context for call
181             # $request: request to cancel, will only cancel it, if request is
182             # outstanding in context, will cancel latest INVITE if not given
183             # $cb: callback for generated CANCEL request
184             # Returns: number of requests canceled (e.g 0 if no outstanding INVITE)
185             ############################################################################
186             sub cancel_invite {
187 6     6 1 16 my Net::SIP::Endpoint $self = shift;
188 6         12 my Net::SIP::Endpoint::Context $ctx = shift;
189 6         17 my ($request,$callback) = @_;
190 6 50       51 my ($pkt) = $ctx->find_outstanding_requests(
    50          
191             $request ? ( request => $request ) : ( method => 'INVITE' )
192             ) or return;
193 6         50 $self->new_request( $pkt->create_cancel, $ctx, $callback );
194 6         83 return 1;
195             }
196              
197             ############################################################################
198             # internal callback used for delivery
199             # will be called from dispatcher if the request was definitely successfully
200             # delivered (tcp only) or an error occurred
201             # Args: ($self,$ctx,$error,$delivery_packet)
202             # $ctx: Net::SIP::Endpoint::Context
203             # $error: errno if error occurred
204             # $delivery_packet: Net::SIP::Dispatcher::Packet which encapsulates
205             # the original request and information about leg, dst_addr...
206             # and has method use_next_dstaddr to try the next dstaddr if for the
207             # current no (more) retries are possible
208             # Returns: NONE
209             ############################################################################
210             sub _request_delivery_callback {
211 45     45   104 my Net::SIP::Endpoint $self = shift;
212 45         114 my ($ctx,$error,$delivery_packet) = @_;
213              
214 45         188 my $tid = $delivery_packet->tid;
215              
216             # either successfully send over reliable transport
217             # or permanently failed, e.g no (more) retries possible
218 45         292 $ctx->request_delivery_done( $self,$tid,$error )
219             }
220              
221             ############################################################################
222             # remove context from Endpoint and cancel all outstanding deliveries
223             # Args: ($self,$id)
224             # $id: either id for ctx or context object or SIP packet
225             # Returns: $ctx
226             # $ctx: removed context object
227             ############################################################################
228             sub close_context {
229 86     86 1 183 my Net::SIP::Endpoint $self = shift;
230 86         152 my $id = shift;
231 86 50       443 $id = $id->callid if ref($id);
232 86         454 DEBUG( 10,"close context call-id $id " );
233 86   100     769 my $ctx = delete $self->{ctx}{$id} || do {
234             DEBUG( 50,"no context for call-id $id found" );
235             return;
236             };
237             # cancel all outstanding deliveries
238 46         286 $self->{dispatcher}->cancel_delivery( callid => $id );
239 46         117 return $ctx;
240             }
241              
242              
243             ############################################################################
244             # receive packet from dispatcher and forwards it to receive_response
245             # or receive_request depending on type of packet
246             # Args: ($self,$packet,$leg,$from)
247             # $packet: Net::SIP::Packet
248             # $leg: Net::SIP::Leg through which the packets was received
249             # $from: hash with information where it got packet from
250             # Returns: NONE
251             ############################################################################
252             sub receive {
253 193   50 193 1 683 my Net::SIP::Endpoint $self = shift || return;
254 193         865 my ($packet,$leg,$from) = @_;
255 193 100       702 return $packet->is_response
256             ? $self->receive_response( $packet,$leg,$from )
257             : $self->receive_request( $packet,$leg,$from )
258             ;
259             }
260              
261             ############################################################################
262             # Handle incoming response packet
263             # Args: ($self,$response,$leg,$from)
264             # $response: incoming Net::SIP::Response packet
265             # $leg: where response came in
266             # $from: hash with information where it got response from
267             # Returns: NONE
268             ############################################################################
269             sub receive_response {
270 139     139 1 256 my Net::SIP::Endpoint $self = shift;
271 139         276 my ($response,$leg,$from) = @_;
272              
273             # find context for response or drop
274 139         577 my $callid = $response->get_header( 'call-id' );
275 139   33     617 my $ctx = $self->{ctx}{$callid} || do {
276             DEBUG( 50,"cannot find context for packet with callid=$callid. DROP");
277             return;
278             };
279              
280 139         537 DEBUG( 10,"received reply for tid=".$response->tid );
281 139         537 $self->{dispatcher}->cancel_delivery( $response->tid );
282 139         1055 $ctx->handle_response( $response,$leg,$from,$self );
283             }
284              
285             ############################################################################
286             # Handle incoming request packet
287             # Args: ($self,$request,$leg,$from)
288             # $request: incoming Net::SIP::Request packet
289             # $leg: where response came in
290             # $from: hash with information where it got response from
291             # Returns: NONE
292             ############################################################################
293             sub receive_request {
294 54     54 1 188 my Net::SIP::Endpoint $self = shift;
295 54         178 my ($request,$leg,$from) = @_;
296              
297             # this might be a request for an existing context or for a new context
298 54         292 my $callid = $request->get_header( 'call-id' );
299 54         228 my $ctx = $self->{ctx}{$callid};
300              
301 54         280 my $method = $request->method;
302 54 100       243 if ( ! $ctx ) {
303 19 50 33     341 if ( $method eq 'BYE' || $method eq 'CANCEL' ) {
    100          
304             # no context for this call, reply with 481 call does not exist
305             # (RFC3261 15.1.2)
306 0         0 $self->new_response(
307             undef,
308             $request->create_response( 481,'call does not exist' ),
309             $leg, # send back thru same leg
310             $from, # and back to the sender
311             );
312 0         0 return;
313             } elsif ( $method eq 'ACK' ) {
314             # call not exists (maybe closed because of CANCEL)
315 3         18 DEBUG(99,'ignoring ACK for non-existing call');
316 3         9 return;
317             }
318              
319             # create a new context;
320 16         126 $ctx = Net::SIP::Endpoint::Context->new(
321             incoming => 1,
322             method => $method,
323             from => scalar( $request->get_header( 'from' )),
324             to => scalar( $request->get_header( 'to' )),
325             remote_contact => scalar( $request->get_header( 'contact' )),
326             callid => scalar( $request->get_header( 'call-id' )),
327             via => [ $request->get_header( 'via' ) ],
328             );
329              
330             $ctx->set_callback( sub {
331 16     16   57 my ($self,$ctx,undef,undef,$request,$leg,$from) = @_;
332 16         123 invoke_callback( $self->{application}, $self,$ctx,$request,$leg,$from );
333 16         561 });
334             }
335              
336             # if I got an ACK cancel delivery of Response to INVITE
337 51 100       256 if ( $method eq 'ACK' ) {
338 16         195 $self->{dispatcher}->cancel_delivery( $request->tid );
339             }
340              
341 51         527 $ctx->handle_request( $request,$leg,$from,$self );
342             }
343              
344             ############################################################################
345             # deliver a response packet
346             # Args: ($self,$ctx,$response,$leg,$addr)
347             # $ctx : Net::SIP::Endpoint::Context which generated response
348             # $response: Net::SIP::Response packet
349             # $leg : leg to send out response, eg where the request came in
350             # $addr : where to send respone (ip:port), eg where the request came from
351             # Returns: NONE
352             ############################################################################
353             sub new_response {
354 65     65 1 239 my Net::SIP::Endpoint $self = shift;
355 65         229 my ($ctx,$response,$leg,$addr) = @_;
356              
357 65 50       460 $self->{ctx}{ $ctx->callid } = $ctx if $ctx; # keep context
358 65         455 $self->{dispatcher}->deliver( $response,
359             leg => $leg,
360             dst_addr => $addr,
361             );
362             }
363              
364              
365             1;