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 43     43   257 use strict;
  43         92  
  43         1182  
10 43     43   195 use warnings;
  43         68  
  43         1682  
11             package Net::SIP::Endpoint;
12             use fields (
13 43         272 '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 43     43   190 );
  43         72  
17              
18 43     43   2597 use Net::SIP::Debug;
  43         85  
  43         373  
19 43     43   18530 use Net::SIP::Endpoint::Context;
  43         113  
  43         1221  
20 43     43   272 use Net::SIP::Util qw(invoke_callback);
  43         84  
  43         1743  
21 43     43   2017 use Scalar::Util 'weaken';
  43         1414  
  43         47631  
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 54     54 1 229 my ($class,$dispatcher) = @_;
31 54         149 my $self = fields::new($class);
32              
33 54         6013 $self->{dispatcher} = $dispatcher;
34 54         156 $self->{ctx} = {}; # \%hash with ( callid => $ctx )
35              
36             # announce myself as upper layer for incoming packets to
37             # the dispatcher
38 54         467 my $cb = [ \&receive,$self ];
39 54         969 weaken( $cb->[1] );
40 54         443 $dispatcher->set_receiver( $cb );
41              
42 54         1074 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 18     18 1 81 my Net::SIP::Endpoint $self = shift;
56 18         56 my $app = shift;
57 18         59 my $cb;
58 18 50       210 if ( my $sub = UNIVERSAL::can( $app,'receive' )) {
59 0         0 $cb = [ $sub,$app ];
60             } else {
61 18         95 $cb = $app; # already callback
62             }
63 18         154 $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 38     38 1 94 my Net::SIP::Endpoint $self = shift;
79 38         528 my ($ctx,$callback,$body,%args) = @_;
80 38         437 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 113     113 1 278 my Net::SIP::Endpoint $self = shift;
149 113         881 my ($method,$ctx,$callback,$body,%args) = @_;
150              
151 113 50       723 die "cannot redefine call-id" if delete $args{ 'call-id' };
152              
153 113 100       881 if ( ! UNIVERSAL::isa( $ctx,'Net::SIP::Endpoint::Context' )) {
154 35         1114 $ctx = Net::SIP::Endpoint::Context->new(%$ctx, method => $method);
155 35         287 $self->{ctx}{ $ctx->callid } = $ctx; # make sure we manage the context
156 35         249 DEBUG( 10,"create new request for $method within new call ".$ctx->callid );
157             } else {
158 78         562 DEBUG( 10,"create new request for $method within existing call ".$ctx->callid );
159             }
160              
161 113 100       881 $ctx->set_callback( $callback ) if $callback;
162              
163 113         644 my $request = $ctx->new_request( $method,$body,%args );
164 113         1014 DEBUG( 50,"request=".$request->as_string );
165              
166 113         517 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 113         1618 );
173              
174 113         1694 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         15 my Net::SIP::Endpoint::Context $ctx = shift;
189 6         17 my ($request,$callback) = @_;
190 6 50       37 my ($pkt) = $ctx->find_outstanding_requests(
    50          
191             $request ? ( request => $request ) : ( method => 'INVITE' )
192             ) or return;
193 6         57 $self->new_request( $pkt->create_cancel, $ctx, $callback );
194 6         18 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   102 my Net::SIP::Endpoint $self = shift;
212 45         107 my ($ctx,$error,$delivery_packet) = @_;
213              
214 45         176 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         261 $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 90     90 1 162 my Net::SIP::Endpoint $self = shift;
230 90         137 my $id = shift;
231 90 50       559 $id = $id->callid if ref($id);
232 90         394 DEBUG( 10,"close context call-id $id " );
233 90   100     915 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 48         223 $self->{dispatcher}->cancel_delivery( callid => $id );
239 48         100 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 199   50 199 1 953 my Net::SIP::Endpoint $self = shift || return;
254 199         573 my ($packet,$leg,$from) = @_;
255 199 100       747 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 252 my Net::SIP::Endpoint $self = shift;
271 139         286 my ($response,$leg,$from) = @_;
272              
273             # find context for response or drop
274 139         563 my $callid = $response->get_header( 'call-id' );
275 139   33     662 my $ctx = $self->{ctx}{$callid} || do {
276             DEBUG( 50,"cannot find context for packet with callid=$callid. DROP");
277             return;
278             };
279              
280 139         647 DEBUG( 10,"received reply for tid=".$response->tid );
281 139         486 $self->{dispatcher}->cancel_delivery( $response->tid );
282 139         1060 $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 60     60 1 180 my Net::SIP::Endpoint $self = shift;
295 60         181 my ($request,$leg,$from) = @_;
296              
297             # this might be a request for an existing context or for a new context
298 60         448 my $callid = $request->get_header( 'call-id' );
299 60         196 my $ctx = $self->{ctx}{$callid};
300              
301 60         322 my $method = $request->method;
302 60 100       334 if ( ! $ctx ) {
303 21 50 33     439 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         10 return;
317             }
318              
319             # create a new context;
320 18         108 $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 18     18   71 my ($self,$ctx,undef,undef,$request,$leg,$from) = @_;
332 18         96 invoke_callback( $self->{application}, $self,$ctx,$request,$leg,$from );
333 18         698 });
334             }
335              
336             # if I got an ACK cancel delivery of Response to INVITE
337 57 100       227 if ( $method eq 'ACK' ) {
338 18         219 $self->{dispatcher}->cancel_delivery( $request->tid );
339             }
340              
341 57         635 $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 71     71 1 255 my Net::SIP::Endpoint $self = shift;
355 71         328 my ($ctx,$response,$leg,$addr) = @_;
356              
357 71 50       516 $self->{ctx}{ $ctx->callid } = $ctx if $ctx; # keep context
358 71         440 $self->{dispatcher}->deliver( $response,
359             leg => $leg,
360             dst_addr => $addr,
361             );
362             }
363              
364              
365             1;