File Coverage

blib/lib/Net/SIP/Endpoint.pm
Criterion Covered Total %
statement 100 110 90.9
branch 19 28 67.8
condition 6 11 54.5
subroutine 19 20 95.0
pod 11 11 100.0
total 155 180 86.1


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   267 use strict;
  43         78  
  43         1255  
10 43     43   187 use warnings;
  43         76  
  43         1527  
11             package Net::SIP::Endpoint;
12             use fields (
13 43         236 '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   217 );
  43         84  
17              
18 43     43   2709 use Net::SIP::Debug;
  43         99  
  43         223  
19 43     43   18341 use Net::SIP::Endpoint::Context;
  43         129  
  43         1402  
20 43     43   292 use Net::SIP::Util qw(invoke_callback);
  43         78  
  43         1868  
21 43     43   1776 use Scalar::Util 'weaken';
  43         1356  
  43         49921  
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 215 my ($class,$dispatcher) = @_;
31 54         167 my $self = fields::new($class);
32              
33 54         4420 $self->{dispatcher} = $dispatcher;
34 54         169 $self->{ctx} = {}; # \%hash with ( callid => $ctx )
35              
36             # announce myself as upper layer for incoming packets to
37             # the dispatcher
38 54         509 my $cb = [ \&receive,$self ];
39 54         951 weaken( $cb->[1] );
40 54         298 $dispatcher->set_receiver( $cb );
41              
42 54         187 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 93 my Net::SIP::Endpoint $self = shift;
56 18         85 my $app = shift;
57 18         92 my $cb;
58 18 50       285 if ( my $sub = UNIVERSAL::can( $app,'receive' )) {
59 0         0 $cb = [ $sub,$app ];
60             } else {
61 18         57 $cb = $app; # already callback
62             }
63 18         111 $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 98 my Net::SIP::Endpoint $self = shift;
79 38         115 my ($ctx,$callback,$body,%args) = @_;
80 38         292 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 429 my Net::SIP::Endpoint $self = shift;
149 113         730 my ($method,$ctx,$callback,$body,%args) = @_;
150              
151 113 50       450 die "cannot redefine call-id" if delete $args{ 'call-id' };
152 113         444 my ($leg,$dst_addr) = delete @args{qw(leg dst_addr)};
153              
154 113 100       562 if ( ! UNIVERSAL::isa( $ctx,'Net::SIP::Endpoint::Context' )) {
155 35         1018 $ctx = Net::SIP::Endpoint::Context->new(%$ctx, method => $method);
156 35         292 $self->{ctx}{ $ctx->callid } = $ctx; # make sure we manage the context
157 35         162 DEBUG( 10,"create new request for $method within new call ".$ctx->callid );
158             } else {
159 78         570 DEBUG( 10,"create new request for $method within existing call ".$ctx->callid );
160             }
161              
162 113 100       724 $ctx->set_callback( $callback ) if $callback;
163              
164 113         505 my $request = $ctx->new_request( $method,$body,%args );
165 113         912 DEBUG( 50,"request=".$request->as_string );
166              
167 113         437 my $tid = $request->tid;
168 113         1050 $self->{dispatcher}->deliver( $request,
169             id => $tid,
170             callback => [ \&_request_delivery_callback, $self,$ctx ],
171             leg => $leg,
172             dst_addr => $dst_addr,
173             );
174              
175 113         1569 return $ctx;
176             }
177              
178             ############################################################################
179             # Cancel last pending INVITE request
180             # Args: ($self,$ctx,$request,$cb)
181             # $ctx: context for call
182             # $request: request to cancel, will only cancel it, if request is
183             # outstanding in context, will cancel latest INVITE if not given
184             # $cb: callback for generated CANCEL request
185             # Returns: number of requests canceled (e.g 0 if no outstanding INVITE)
186             ############################################################################
187             sub cancel_invite {
188 6     6 1 15 my Net::SIP::Endpoint $self = shift;
189 6         12 my Net::SIP::Endpoint::Context $ctx = shift;
190 6         15 my ($request,$callback) = @_;
191 6 50       45 my ($pkt) = $ctx->find_outstanding_requests(
    50          
192             $request ? ( request => $request ) : ( method => 'INVITE' )
193             ) or return;
194 6         54 $self->new_request( $pkt->create_cancel, $ctx, $callback );
195 6         79 return 1;
196             }
197              
198             ############################################################################
199             # internal callback used for delivery
200             # will be called from dispatcher if the request was definitely successfully
201             # delivered (tcp only) or an error occurred
202             # Args: ($self,$ctx,$error,$delivery_packet)
203             # $ctx: Net::SIP::Endpoint::Context
204             # $error: errno if error occurred
205             # $delivery_packet: Net::SIP::Dispatcher::Packet which encapsulates
206             # the original request and information about leg, dst_addr...
207             # and has method use_next_dstaddr to try the next dstaddr if for the
208             # current no (more) retries are possible
209             # Returns: NONE
210             ############################################################################
211             sub _request_delivery_callback {
212 45     45   109 my Net::SIP::Endpoint $self = shift;
213 45         119 my ($ctx,$error,$delivery_packet) = @_;
214              
215 45         203 my $tid = $delivery_packet->tid;
216              
217             # either successfully send over reliable transport
218             # or permanently failed, e.g no (more) retries possible
219 45         293 $ctx->request_delivery_done( $self,$tid,$error )
220             }
221              
222             ############################################################################
223             # remove context from Endpoint and cancel all outstanding deliveries
224             # Args: ($self,$id)
225             # $id: either id for ctx or context object or SIP packet
226             # Returns: $ctx
227             # $ctx: removed context object
228             ############################################################################
229             sub close_context {
230 90     90 1 188 my Net::SIP::Endpoint $self = shift;
231 90         142 my $id = shift;
232 90 50       412 $id = $id->callid if ref($id);
233 90         421 DEBUG( 10,"close context call-id $id " );
234 90   100     675 my $ctx = delete $self->{ctx}{$id} || do {
235             DEBUG( 50,"no context for call-id $id found" );
236             return;
237             };
238             # cancel all outstanding deliveries
239 48         242 $self->{dispatcher}->cancel_delivery( callid => $id );
240 48         110 return $ctx;
241             }
242              
243              
244             ############################################################################
245             # receive packet from dispatcher and forwards it to receive_response
246             # or receive_request depending on type of packet
247             # Args: ($self,$packet,$leg,$from)
248             # $packet: Net::SIP::Packet
249             # $leg: Net::SIP::Leg through which the packets was received
250             # $from: hash with information where it got packet from
251             # Returns: NONE
252             ############################################################################
253             sub receive {
254 198   50 198 1 602 my Net::SIP::Endpoint $self = shift || return;
255 198         717 my ($packet,$leg,$from) = @_;
256 198 100       706 return $packet->is_response
257             ? $self->receive_response( $packet,$leg,$from )
258             : $self->receive_request( $packet,$leg,$from )
259             ;
260             }
261              
262             ############################################################################
263             # Handle incoming response packet
264             # Args: ($self,$response,$leg,$from)
265             # $response: incoming Net::SIP::Response packet
266             # $leg: where response came in
267             # $from: hash with information where it got response from
268             # Returns: NONE
269             ############################################################################
270             sub receive_response {
271 138     138 1 247 my Net::SIP::Endpoint $self = shift;
272 138         303 my ($response,$leg,$from) = @_;
273              
274             # find context for response or drop
275 138         1041 my $callid = $response->get_header( 'call-id' );
276 138   33     614 my $ctx = $self->{ctx}{$callid} || do {
277             DEBUG( 50,"cannot find context for packet with callid=$callid. DROP");
278             return;
279             };
280              
281 138         568 DEBUG( 10,"received reply for tid=".$response->tid );
282 138         427 $self->{dispatcher}->cancel_delivery( $response->tid );
283 138         938 $ctx->handle_response( $response,$leg,$from,$self );
284             }
285              
286             ############################################################################
287             # Handle incoming request packet
288             # Args: ($self,$request,$leg,$from)
289             # $request: incoming Net::SIP::Request packet
290             # $leg: where response came in
291             # $from: hash with information where it got response from
292             # Returns: NONE
293             ############################################################################
294             sub receive_request {
295 60     60 1 175 my Net::SIP::Endpoint $self = shift;
296 60         174 my ($request,$leg,$from) = @_;
297              
298             # this might be a request for an existing context or for a new context
299 60         348 my $callid = $request->get_header( 'call-id' );
300 60         239 my $ctx = $self->{ctx}{$callid};
301              
302 60         242 my $method = $request->method;
303 60 100       247 if ( ! $ctx ) {
304 21 50 33     429 if ( $method eq 'BYE' || $method eq 'CANCEL' ) {
    100          
305             # no context for this call, reply with 481 call does not exist
306             # (RFC3261 15.1.2)
307 0         0 $self->new_response(
308             undef,
309             $request->create_response( 481,'call does not exist' ),
310             $leg, # send back thru same leg
311             $from, # and back to the sender
312             );
313 0         0 return;
314             } elsif ( $method eq 'ACK' ) {
315             # call not exists (maybe closed because of CANCEL)
316 3         23 DEBUG(99,'ignoring ACK for non-existing call');
317 3         11 return;
318             }
319              
320             # create a new context;
321 18         108 $ctx = Net::SIP::Endpoint::Context->new(
322             incoming => 1,
323             method => $method,
324             from => scalar( $request->get_header( 'from' )),
325             to => scalar( $request->get_header( 'to' )),
326             remote_contact => scalar( $request->get_header( 'contact' )),
327             callid => scalar( $request->get_header( 'call-id' )),
328             via => [ $request->get_header( 'via' ) ],
329             );
330              
331             $ctx->set_callback( sub {
332 18     18   60 my ($self,$ctx,undef,undef,$request,$leg,$from) = @_;
333 18         161 invoke_callback( $self->{application}, $self,$ctx,$request,$leg,$from );
334 18         1042 });
335             }
336              
337             # if I got an ACK cancel delivery of Response to INVITE
338 57 100       221 if ( $method eq 'ACK' ) {
339 18         176 $self->{dispatcher}->cancel_delivery( $request->tid );
340             }
341              
342 57         509 $ctx->handle_request( $request,$leg,$from,$self );
343             }
344              
345             ############################################################################
346             # deliver a response packet
347             # Args: ($self,$ctx,$response,$leg,$addr)
348             # $ctx : Net::SIP::Endpoint::Context which generated response
349             # $response: Net::SIP::Response packet
350             # $leg : leg to send out response, eg where the request came in
351             # $addr : where to send respone (ip:port), eg where the request came from
352             # Returns: NONE
353             ############################################################################
354             sub new_response {
355 70     70 1 244 my Net::SIP::Endpoint $self = shift;
356 70         238 my ($ctx,$response,$leg,$addr) = @_;
357              
358 70 50       502 $self->{ctx}{ $ctx->callid } = $ctx if $ctx; # keep context
359 70         533 $self->{dispatcher}->deliver( $response,
360             leg => $leg,
361             dst_addr => $addr,
362             );
363             }
364              
365              
366             1;