line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
############################################################################ |
3
|
|
|
|
|
|
|
# Net::SIP::Endpoint::Context |
4
|
|
|
|
|
|
|
# the calling context for a call managed by the endpoint |
5
|
|
|
|
|
|
|
############################################################################ |
6
|
|
|
|
|
|
|
|
7
|
43
|
|
|
43
|
|
255
|
use strict; |
|
43
|
|
|
|
|
88
|
|
|
43
|
|
|
|
|
1113
|
|
8
|
43
|
|
|
43
|
|
214
|
use warnings; |
|
43
|
|
|
|
|
80
|
|
|
43
|
|
|
|
|
2383
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
package Net::SIP::Endpoint::Context; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use fields ( |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# ===== can be set with new() |
15
|
43
|
|
|
|
|
296
|
'method', # initiated by which method |
16
|
|
|
|
|
|
|
'from', # from where |
17
|
|
|
|
|
|
|
'to', # to where |
18
|
|
|
|
|
|
|
'auth', # [ user,pass ] or { realm1 => [ user1,pass1 ], realm2 => [ user2,pass2 ],... } |
19
|
|
|
|
|
|
|
# or callback(realm,user)->pass |
20
|
|
|
|
|
|
|
# if given, handle_response might automatically try to authorize requests |
21
|
|
|
|
|
|
|
'contact', # optional local contact |
22
|
|
|
|
|
|
|
'remote_contact', # remote contact from response |
23
|
|
|
|
|
|
|
'callid', # call-id value |
24
|
|
|
|
|
|
|
'cseq', # number in cseq header |
25
|
|
|
|
|
|
|
'route', # for 'route' header, comes usually from 'record-route' info in response |
26
|
|
|
|
|
|
|
'via', # for 'via' header in created responses, comes from incoming request |
27
|
|
|
|
|
|
|
'incoming', # flag if call is incoming, e.g. 'to' is myself |
28
|
|
|
|
|
|
|
'local_tag', # local tag which gets assigned to either from or to depending on incoming |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# ===== Internals |
31
|
|
|
|
|
|
|
# \@array of hashrefs for infos about pending transactions |
32
|
|
|
|
|
|
|
'_transactions', |
33
|
|
|
|
|
|
|
# arrayref specifying a user defined callback for request success or failure |
34
|
|
|
|
|
|
|
'_callback', |
35
|
|
|
|
|
|
|
# cseq counter for incoming requests |
36
|
|
|
|
|
|
|
'_cseq_incoming', |
37
|
|
|
|
|
|
|
# last request in current incoming transaction |
38
|
|
|
|
|
|
|
'_last_transreq', |
39
|
|
|
|
|
|
|
|
40
|
43
|
|
|
43
|
|
267
|
); |
|
43
|
|
|
|
|
91
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
43
|
|
|
43
|
|
5761
|
use Digest::MD5 'md5_hex'; |
|
43
|
|
|
|
|
81
|
|
|
43
|
|
|
|
|
1829
|
|
44
|
43
|
|
|
43
|
|
253
|
use Net::SIP::Request; |
|
43
|
|
|
|
|
133
|
|
|
43
|
|
|
|
|
1028
|
|
45
|
43
|
|
|
43
|
|
208
|
use Net::SIP::Response; |
|
43
|
|
|
|
|
71
|
|
|
43
|
|
|
|
|
1246
|
|
46
|
43
|
|
|
43
|
|
209
|
use Net::SIP::Debug; |
|
43
|
|
|
|
|
91
|
|
|
43
|
|
|
|
|
310
|
|
47
|
43
|
|
|
43
|
|
297
|
use Errno qw( EINVAL EPERM EFAULT ); |
|
43
|
|
|
|
|
81
|
|
|
43
|
|
|
|
|
2044
|
|
48
|
43
|
|
|
43
|
|
220
|
use Hash::Util 'lock_keys'; |
|
43
|
|
|
|
|
68
|
|
|
43
|
|
|
|
|
287
|
|
49
|
43
|
|
|
43
|
|
2096
|
use List::Util 'first'; |
|
43
|
|
|
|
|
76
|
|
|
43
|
|
|
|
|
2123
|
|
50
|
43
|
|
|
43
|
|
245
|
use Net::SIP::Util ':all'; |
|
43
|
|
|
|
|
72
|
|
|
43
|
|
|
|
|
103053
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
############################################################################ |
53
|
|
|
|
|
|
|
# Creates new context |
54
|
|
|
|
|
|
|
# Args: ($class,@args) |
55
|
|
|
|
|
|
|
# @args: either single \%args (hash-ref) or %args (hash) with at least |
56
|
|
|
|
|
|
|
# values for from and to |
57
|
|
|
|
|
|
|
# callid,cseq will be generated if not given |
58
|
|
|
|
|
|
|
# routes will default to undef and usually set from record-route header |
59
|
|
|
|
|
|
|
# in response packets |
60
|
|
|
|
|
|
|
# Returns: $self |
61
|
|
|
|
|
|
|
############################################################################ |
62
|
|
|
|
|
|
|
sub new { |
63
|
53
|
|
|
53
|
1
|
183
|
my $class = shift; |
64
|
53
|
50
|
|
|
|
696
|
my %args = @_ == 1 ? %{ shift(@_) } : @_; |
|
0
|
|
|
|
|
0
|
|
65
|
53
|
|
|
|
|
246
|
my $self = fields::new( $class ); |
66
|
53
|
|
|
|
|
7770
|
%$self = %args; |
67
|
53
|
|
66
|
|
|
964
|
$self->{callid} ||= md5_hex( time(), rand(2**32) ); |
68
|
53
|
|
50
|
|
|
409
|
$self->{cseq} ||= 0; |
69
|
53
|
|
|
|
|
115
|
$self->{_transactions} = []; |
70
|
53
|
|
|
|
|
120
|
$self->{_cseq_incoming} = undef; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# create tag on my side (to|from) |
73
|
53
|
100
|
|
|
|
365
|
my $side = $self->{incoming} ? 'to':'from'; |
74
|
53
|
|
|
|
|
294
|
my ($data,$param) = sip_hdrval2parts( $side => $self->{$side} ); |
75
|
53
|
50
|
|
|
|
194
|
if ( my $tag = $param->{tag} ) { |
76
|
|
|
|
|
|
|
# FIXME: what to do if local_tag was already set to different value? |
77
|
0
|
|
|
|
|
0
|
$self->{local_tag} = $tag; |
78
|
|
|
|
|
|
|
} else { |
79
|
|
|
|
|
|
|
$self->{$side}.=";tag=".( |
80
|
53
|
|
|
|
|
1278
|
$self->{local_tag} = md5_hex( time(), rand(2**32), $self->{$side} ) |
81
|
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
53
|
|
|
|
|
511
|
DEBUG( 100,"CREATE context $self callid=$self->{callid}" ); |
85
|
53
|
|
|
|
|
280
|
return $self |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# destroying of fields in perl5.8 cleanup can cause strange errors, where |
89
|
|
|
|
|
|
|
# it complains, that it cannot coerce array into hash. So use this function |
90
|
|
|
|
|
|
|
# on your own risks and rename it to DETSTROY if you want to have debugging |
91
|
|
|
|
|
|
|
# info |
92
|
|
|
|
|
|
|
sub _DESTROY { |
93
|
0
|
|
|
0
|
|
0
|
DEBUG( 100,"DESTROY context $_[0] callid=$_[0]->{callid}" ); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
############################################################################ |
97
|
|
|
|
|
|
|
# returns callid for context |
98
|
|
|
|
|
|
|
# Args: $self |
99
|
|
|
|
|
|
|
# Returns: $id |
100
|
|
|
|
|
|
|
############################################################################ |
101
|
|
|
|
|
|
|
sub callid { |
102
|
308
|
|
|
308
|
1
|
601
|
my Net::SIP::Endpoint::Context $self = shift; |
103
|
308
|
|
|
|
|
1228
|
return $self->{callid}; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
############################################################################ |
107
|
|
|
|
|
|
|
# get peer |
108
|
|
|
|
|
|
|
# Args: $self |
109
|
|
|
|
|
|
|
# Returns: $peer |
110
|
|
|
|
|
|
|
# $peer: for incoming calls this is 'from', for outgoing 'to' |
111
|
|
|
|
|
|
|
############################################################################ |
112
|
|
|
|
|
|
|
sub peer { |
113
|
0
|
|
|
0
|
1
|
0
|
my Net::SIP::Endpoint::Context $self = shift; |
114
|
0
|
0
|
|
|
|
0
|
my $peer = $self->{incoming} ? $self->{from} : $self->{to}; |
115
|
0
|
|
|
|
|
0
|
my ($data) = sip_hdrval2parts( from => $peer ); # strip parameters like tag etc |
116
|
0
|
|
|
|
|
0
|
return $data; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
############################################################################ |
120
|
|
|
|
|
|
|
# return list of outstanding requests matching filter, if no filter is given |
121
|
|
|
|
|
|
|
# returns all requests |
122
|
|
|
|
|
|
|
# Args: ($self,%filter) |
123
|
|
|
|
|
|
|
# %filter |
124
|
|
|
|
|
|
|
# method => name: filter for requests with given method |
125
|
|
|
|
|
|
|
# request => packet: filter for packet, e.g. finds if packet is outstanding |
126
|
|
|
|
|
|
|
# Returns: @requests |
127
|
|
|
|
|
|
|
# returns all matching requests (Net::SIP::Request objects), newest |
128
|
|
|
|
|
|
|
# requests first |
129
|
|
|
|
|
|
|
############################################################################ |
130
|
|
|
|
|
|
|
sub find_outstanding_requests { |
131
|
6
|
|
|
6
|
1
|
14
|
my Net::SIP::Endpoint::Context $self = shift; |
132
|
6
|
|
|
|
|
61
|
my %filter = @_; |
133
|
6
|
50
|
|
|
|
18
|
my @trans = @{$self->{_transactions}} or return; |
|
6
|
|
|
|
|
36
|
|
134
|
6
|
50
|
|
|
|
27
|
if ( my $pkt = $filter{request} ) { |
135
|
0
|
0
|
|
|
|
0
|
@trans = grep { $pkt == $_->{request} } @trans or return; |
|
0
|
|
|
|
|
0
|
|
136
|
|
|
|
|
|
|
} |
137
|
6
|
50
|
|
|
|
21
|
if ( my $method = $filter{method} ) { |
138
|
6
|
50
|
|
|
|
13
|
@trans = grep { $method eq $_->{request}->method } @trans or return; |
|
6
|
|
|
|
|
42
|
|
139
|
|
|
|
|
|
|
} |
140
|
6
|
|
|
|
|
16
|
return map { $_->{request} } @trans; |
|
6
|
|
|
|
|
30
|
|
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
############################################################################ |
144
|
|
|
|
|
|
|
# creates a new SIP request packet within this context |
145
|
|
|
|
|
|
|
# Args: ($self,$method;$body,%args) |
146
|
|
|
|
|
|
|
# $method: method for request, eg 'INVITE','BYE'... |
147
|
|
|
|
|
|
|
# or already a Net::SIP::Request object |
148
|
|
|
|
|
|
|
# $body: (optional) body for SIP packet |
149
|
|
|
|
|
|
|
# %args: (optional) additional args given to Net::SIP::Request->new |
150
|
|
|
|
|
|
|
# Returns: $request |
151
|
|
|
|
|
|
|
# $request: Net::SIP::Request object |
152
|
|
|
|
|
|
|
############################################################################ |
153
|
|
|
|
|
|
|
sub new_request { |
154
|
113
|
|
|
113
|
1
|
225
|
my Net::SIP::Endpoint::Context $self = shift; |
155
|
113
|
|
|
|
|
358
|
my ($method,$body,%args) = @_; |
156
|
|
|
|
|
|
|
|
157
|
113
|
|
|
|
|
245
|
my $rsp40x = delete $args{resp40x}; |
158
|
|
|
|
|
|
|
|
159
|
113
|
|
|
|
|
381
|
my $request; |
160
|
113
|
100
|
|
|
|
343
|
if ( ref($method)) { |
161
|
|
|
|
|
|
|
# already a request object |
162
|
48
|
|
|
|
|
95
|
$request = $method; |
163
|
48
|
|
|
|
|
217
|
$method = $request->method; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
} else { |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# increase cseq unless its explicitly specified |
168
|
|
|
|
|
|
|
# the latter case is useful for ACK and CANCEL |
169
|
|
|
|
|
|
|
# which need the same sequence number as the INVITE |
170
|
|
|
|
|
|
|
# they belong to |
171
|
65
|
|
33
|
|
|
584
|
my $cseq = delete $args{cseq} || ++$self->{cseq}; |
172
|
|
|
|
|
|
|
|
173
|
65
|
|
|
|
|
231
|
$method = uc($method); |
174
|
65
|
|
|
|
|
137
|
my $uri = delete $args{uri}; |
175
|
|
|
|
|
|
|
my ($to,$from) = $self->{incoming} ? ($self->{from},$self->{to}) |
176
|
65
|
100
|
|
|
|
404
|
: ($self->{to},$self->{from}); |
177
|
65
|
50
|
|
|
|
253
|
if ( !$uri ) { |
178
|
|
|
|
|
|
|
$uri = $self->{remote_contact} |
179
|
65
|
|
66
|
|
|
978
|
|| (sip_hdrval2parts(to => $to))[0]; |
180
|
|
|
|
|
|
|
# XXX handle quotes right, e.g "" |
181
|
65
|
100
|
|
|
|
476
|
$uri = $1 if $uri =~m{<(\S+)>$}; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# contact is mandatory for INVITE |
185
|
|
|
|
|
|
|
# will be added within Leg |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
$request = Net::SIP::Request->new( |
188
|
|
|
|
|
|
|
$method, # Method |
189
|
|
|
|
|
|
|
$uri, # URI |
190
|
|
|
|
|
|
|
{ |
191
|
|
|
|
|
|
|
from => $from, |
192
|
|
|
|
|
|
|
to => $to, |
193
|
|
|
|
|
|
|
$self->{contact} ? ( contact => $self->{contact} ):(), |
194
|
|
|
|
|
|
|
cseq => "$cseq $method", |
195
|
|
|
|
|
|
|
'call-id' => $self->{callid}, |
196
|
65
|
50
|
|
|
|
2088
|
'max-forwards' => 70, |
197
|
|
|
|
|
|
|
%args, |
198
|
|
|
|
|
|
|
}, |
199
|
|
|
|
|
|
|
$body |
200
|
|
|
|
|
|
|
); |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
# overwrite any route header in request if we already learned a route |
204
|
113
|
50
|
|
|
|
453
|
$request->set_header( route => $self->{route} ) if $self->{route}; |
205
|
|
|
|
|
|
|
|
206
|
113
|
0
|
33
|
|
|
296
|
if ( $rsp40x and $self->{auth} and $request->authorize( $rsp40x, $self->{auth} )) { |
|
|
|
0
|
|
|
|
|
207
|
|
|
|
|
|
|
# update local cseq |
208
|
0
|
|
|
|
|
0
|
($self->{cseq}) = $request->cseq =~m{(\d+)}; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
# create new transaction |
212
|
|
|
|
|
|
|
my %trans = ( |
213
|
|
|
|
|
|
|
tid => $request->tid, |
214
|
|
|
|
|
|
|
request => $request, |
215
|
|
|
|
|
|
|
callback => $self->{_callback}, |
216
|
113
|
|
|
|
|
551
|
); |
217
|
113
|
|
|
|
|
515
|
lock_keys(%trans); |
218
|
113
|
|
|
|
|
1159
|
unshift @{ $self->{_transactions} }, \%trans; # put as first |
|
113
|
|
|
|
|
429
|
|
219
|
|
|
|
|
|
|
|
220
|
113
|
|
|
|
|
650
|
return $request; |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
############################################################################ |
224
|
|
|
|
|
|
|
# set callback for context |
225
|
|
|
|
|
|
|
# Args: ($self,$cb) |
226
|
|
|
|
|
|
|
# $cb: [ \&sub,@arg ] |
227
|
|
|
|
|
|
|
# Returns: NONE |
228
|
|
|
|
|
|
|
############################################################################ |
229
|
|
|
|
|
|
|
sub set_callback { |
230
|
107
|
|
|
107
|
1
|
291
|
my Net::SIP::Endpoint::Context $self = shift; |
231
|
107
|
|
|
|
|
332
|
$self->{_callback} = shift; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
############################################################################ |
235
|
|
|
|
|
|
|
# notify context that current delivery is permanently done (e.g successful |
236
|
|
|
|
|
|
|
# or failed). On failure call current callback to notify upper layer about |
237
|
|
|
|
|
|
|
# permanent failure of request |
238
|
|
|
|
|
|
|
# This is used for errors from the transport layer, errors from the SIP |
239
|
|
|
|
|
|
|
# layer (e.g response with 400 Bad request) are handled by handle_response() |
240
|
|
|
|
|
|
|
# Args: ($self,$tid;$error) |
241
|
|
|
|
|
|
|
# $tid: Transaction ID |
242
|
|
|
|
|
|
|
# $error: errno if error occurred |
243
|
|
|
|
|
|
|
# Returns: NONE |
244
|
|
|
|
|
|
|
############################################################################ |
245
|
|
|
|
|
|
|
sub request_delivery_done { |
246
|
45
|
|
|
45
|
1
|
106
|
my Net::SIP::Endpoint::Context $self = shift; |
247
|
45
|
|
|
|
|
128
|
my ($endpoint,$tid,$error) = @_; |
248
|
45
|
50
|
|
|
|
291
|
return if ! $error; # notify of success once I get response |
249
|
|
|
|
|
|
|
|
250
|
0
|
|
|
|
|
0
|
my $trans = $self->{_transactions}; |
251
|
0
|
|
|
|
|
0
|
my @ntrans; |
252
|
0
|
|
|
|
|
0
|
foreach my $tr (@$trans) { |
253
|
0
|
0
|
|
|
|
0
|
if ( $tr->{tid} eq $tid ) { |
254
|
0
|
|
|
|
|
0
|
$self->{_transactions} = \@ntrans; |
255
|
0
|
0
|
|
|
|
0
|
if ( my $cb = $tr->{callback} ) { |
256
|
|
|
|
|
|
|
# permanently failed |
257
|
0
|
|
|
|
|
0
|
invoke_callback( $cb,$endpoint,$self,$error ); |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
} else { |
260
|
0
|
|
|
|
|
0
|
push @ntrans,$tr |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
############################################################################ |
266
|
|
|
|
|
|
|
# handle response packet for this context |
267
|
|
|
|
|
|
|
# cseq of response must match the cseq of the current delivery! |
268
|
|
|
|
|
|
|
# if there is no current delivery or the cseq does not match the response |
269
|
|
|
|
|
|
|
# gets dropped |
270
|
|
|
|
|
|
|
# Args: ($self,$response,$leg,$from,$endpoint) |
271
|
|
|
|
|
|
|
# $response: incoming Net::SIP::Response packet |
272
|
|
|
|
|
|
|
# $leg: Net::SIP::Leg through which the response came in |
273
|
|
|
|
|
|
|
# $from: hash with information where response came in |
274
|
|
|
|
|
|
|
# $endpoint: endpoint responsable for this context, used for redeliveries... |
275
|
|
|
|
|
|
|
# Returns: NONE |
276
|
|
|
|
|
|
|
############################################################################ |
277
|
|
|
|
|
|
|
sub handle_response { |
278
|
138
|
|
|
138
|
1
|
297
|
my Net::SIP::Endpoint::Context $self = shift; |
279
|
138
|
|
|
|
|
366
|
my ($response,$leg,$from,$endpoint) = @_; |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
# find and remove transaction because I got response for it |
282
|
|
|
|
|
|
|
# if response does not terminates transaction one need to add |
283
|
|
|
|
|
|
|
# it again |
284
|
138
|
|
|
|
|
363
|
my $tid = $response->tid; |
285
|
138
|
|
|
|
|
580
|
my $method = $response->method; |
286
|
138
|
|
|
|
|
371
|
my $trans = $self->{_transactions}; |
287
|
138
|
|
|
|
|
274
|
my (@ntrans,$tr); |
288
|
138
|
|
|
|
|
625
|
foreach my $t (@$trans) { |
289
|
198
|
100
|
100
|
|
|
1743
|
if ( !$tr and $t->{tid} eq $tid and $method eq $t->{request}->method) { |
|
|
|
100
|
|
|
|
|
290
|
136
|
|
|
|
|
335
|
$tr = $t; |
291
|
|
|
|
|
|
|
} else { |
292
|
62
|
|
|
|
|
137
|
push @ntrans,$t |
293
|
|
|
|
|
|
|
} |
294
|
|
|
|
|
|
|
} |
295
|
138
|
100
|
|
|
|
409
|
$tr || do { |
296
|
|
|
|
|
|
|
# no delivery pending |
297
|
2
|
|
|
|
|
7
|
DEBUG( 10,"got response for unkown transaction. DROP" ); |
298
|
2
|
|
|
|
|
7
|
return; |
299
|
|
|
|
|
|
|
}; |
300
|
136
|
|
|
|
|
326
|
$self->{_transactions} = \@ntrans; |
301
|
|
|
|
|
|
|
|
302
|
136
|
|
|
|
|
793
|
DEBUG( 10,"got response for transaction ".$tr->{request}->dump ); |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
# match response to client transaction, RFC3261 17.1.3 |
305
|
|
|
|
|
|
|
# check if the response came in through the same leg, where the |
306
|
|
|
|
|
|
|
# request was send, e.g that the branch tag is the same |
307
|
136
|
50
|
|
|
|
610
|
$leg->check_via( $response ) || do { |
308
|
0
|
|
|
|
|
0
|
DEBUG( 10,"response came in through the wrong leg" ); |
309
|
0
|
|
|
|
|
0
|
return; |
310
|
|
|
|
|
|
|
}; |
311
|
|
|
|
|
|
|
|
312
|
136
|
|
|
|
|
333
|
my $cb = $tr->{callback}; |
313
|
136
|
|
|
|
|
417
|
my @arg = ($endpoint,$self); |
314
|
136
|
|
|
|
|
816
|
my $code = $response->code; |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
# for 300-699 an ACK must be created (RFC3261, 17.1.1.2) |
317
|
|
|
|
|
|
|
# notification of upper layer will be done down in the method |
318
|
|
|
|
|
|
|
# XXXXXXXXXXXXXX do we need to wait that the ACK was accepted |
319
|
|
|
|
|
|
|
# XXXXXXXXXXXXXX before sending new request?? |
320
|
|
|
|
|
|
|
# XXXXXXXXXXXXXX (e.g for 401,407,302..) |
321
|
136
|
100
|
100
|
|
|
1027
|
if ( $method eq 'INVITE' && $code>=300 ) { |
322
|
|
|
|
|
|
|
# must create ACK |
323
|
9
|
|
|
|
|
47
|
DEBUG( 50,"code=$code, must generate ACK" ); |
324
|
9
|
|
|
|
|
52
|
my $ack = $tr->{request}->create_ack( $response ); |
325
|
9
|
|
|
|
|
56
|
$endpoint->new_request( $ack,$self,undef,undef,leg => $leg); |
326
|
|
|
|
|
|
|
} |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
# transaction is not done |
329
|
136
|
100
|
|
|
|
864
|
if ( $code =~m{^1\d\d} ) { |
330
|
63
|
|
|
|
|
188
|
push @ntrans,$tr; |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
# forward preliminary responses to INVITE to app |
333
|
|
|
|
|
|
|
# ignore all other preliminary responses |
334
|
63
|
50
|
|
|
|
198
|
if ( $method eq 'INVITE' ) { |
335
|
63
|
|
|
|
|
300
|
invoke_callback($cb,@arg,0,$code,$response,$leg,$from); |
336
|
|
|
|
|
|
|
} |
337
|
63
|
|
|
|
|
292
|
return; |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
# Authorization required |
341
|
73
|
100
|
66
|
|
|
864
|
if ( $code == 401 || $code == 407 ) { |
342
|
3
|
|
|
|
|
7
|
my $r = $tr->{request}; |
343
|
3
|
|
|
|
|
6
|
my $auth = $self->{auth}; |
344
|
3
|
50
|
33
|
|
|
34
|
if ( $auth && $r->authorize( $response, $auth )) { |
345
|
3
|
|
|
|
|
20
|
DEBUG(10,"retrying with authorization"); |
346
|
|
|
|
|
|
|
# found something to authorize |
347
|
|
|
|
|
|
|
# redo request |
348
|
|
|
|
|
|
|
# update local cseq from cseq in request |
349
|
3
|
|
|
|
|
9
|
($self->{cseq}) = $r->cseq =~m{(\d+)}; |
350
|
3
|
|
|
|
|
21
|
$endpoint->new_request( $r,$self ); |
351
|
|
|
|
|
|
|
} else { |
352
|
|
|
|
|
|
|
# need user feedback |
353
|
0
|
|
|
|
|
0
|
DEBUG(10,"no (usable) authorization data available"); |
354
|
0
|
|
|
|
|
0
|
invoke_callback($cb,@arg,EPERM,$code,$response,$leg,$from); |
355
|
|
|
|
|
|
|
} |
356
|
3
|
|
|
|
|
25
|
return; |
357
|
|
|
|
|
|
|
} |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
# Don't care about the response for a CANCEL or a BYE |
360
|
|
|
|
|
|
|
# because this connection close is issued by this side |
361
|
|
|
|
|
|
|
# and no matter what the peer wants the call be will closed |
362
|
|
|
|
|
|
|
# But invoke callback to notify upper layer |
363
|
70
|
100
|
100
|
|
|
518
|
if ( $method eq 'CANCEL' or $method eq 'BYE' ) { |
364
|
33
|
|
|
|
|
174
|
invoke_callback($cb,@arg,0,$code,$response,$leg,$from); |
365
|
|
|
|
|
|
|
# close context only for BYE, |
366
|
|
|
|
|
|
|
# for CANCEL we will close the context on receiving the |
367
|
|
|
|
|
|
|
# response and sending the ACK |
368
|
33
|
100
|
|
|
|
145
|
$endpoint->close_context( $self ) if $method eq 'BYE'; |
369
|
33
|
|
|
|
|
1528
|
return; |
370
|
|
|
|
|
|
|
} |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
# final response in non-dialog (only INVITE can create dialog) |
373
|
37
|
0
|
0
|
|
|
177
|
if ( $self->{method} ne 'INVITE' and |
|
|
|
33
|
|
|
|
|
374
|
|
|
|
|
|
|
($code>=200 and $code<300 or $code>=400)) { |
375
|
0
|
|
|
|
|
0
|
$endpoint->close_context($self); |
376
|
|
|
|
|
|
|
} |
377
|
|
|
|
|
|
|
|
378
|
37
|
100
|
33
|
|
|
462
|
if ( $code =~m{^2\d\d} ) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
379
|
|
|
|
|
|
|
# 2xx OK |
380
|
|
|
|
|
|
|
|
381
|
30
|
50
|
|
|
|
118
|
if ( $method eq 'INVITE' ) { |
382
|
|
|
|
|
|
|
# is response to INVITE, create ACK |
383
|
|
|
|
|
|
|
# and propagate to upper layer |
384
|
30
|
|
|
|
|
111
|
my $req = $tr->{request}; |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
# extract route information on INVITE, but not on re-INVITE |
387
|
|
|
|
|
|
|
# we assume, that it is a re-INVITE, if we have a remote_contact |
388
|
|
|
|
|
|
|
# already |
389
|
30
|
50
|
66
|
|
|
379
|
if ( ! $self->{remote_contact} |
390
|
|
|
|
|
|
|
and my @route = $response->get_header( 'record-route' )) { |
391
|
0
|
|
|
|
|
0
|
$self->{route} = [ reverse @route ]; |
392
|
|
|
|
|
|
|
} |
393
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
# 12.1.2 - set URI for dialog to contact given in response which |
395
|
|
|
|
|
|
|
# establishes the dialog |
396
|
30
|
50
|
|
|
|
121
|
if ( my $contact = $response->get_header( 'contact' )) { |
397
|
30
|
50
|
|
|
|
152
|
$contact = $1 if $contact =~m{<(\w+:[^>\s]+)>}; |
398
|
30
|
|
|
|
|
84
|
$self->{remote_contact} = $contact; |
399
|
30
|
|
|
|
|
151
|
$req->set_uri( $contact ); |
400
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
} |
402
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
# use to-tag from this request to update 'to' |
404
|
|
|
|
|
|
|
# FIXME: this should probably be better done by the upper layer |
405
|
|
|
|
|
|
|
# which decides, which call to accept (in case of call-forking with |
406
|
|
|
|
|
|
|
# multiple 2xx responses) |
407
|
30
|
100
|
|
|
|
208
|
$self->{to} = $response->get_header( 'to' ) if ! $self->{incoming}; |
408
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
# create ACK |
410
|
|
|
|
|
|
|
# if 2xx response changed contact use it as the new URI |
411
|
30
|
|
|
|
|
212
|
my $ack = $req->create_ack( $response ); |
412
|
30
|
|
|
|
|
194
|
invoke_callback($cb,@arg,0,$code,$response,$leg,$from,$ack); |
413
|
30
|
|
|
|
|
222
|
$endpoint->new_request( $ack,$self,undef,undef,leg => $leg); |
414
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
} else { |
417
|
|
|
|
|
|
|
# response to ACK, REGISTER... |
418
|
|
|
|
|
|
|
# simply propagate to upper layer, only INVITE needs |
419
|
|
|
|
|
|
|
# special handling |
420
|
0
|
|
|
|
|
0
|
invoke_callback($cb,@arg,0,$code,$response,$leg,$from); |
421
|
|
|
|
|
|
|
} |
422
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
} elsif ( $code == 300 || $code == 301 ) { |
424
|
|
|
|
|
|
|
# need user feedback in these cases |
425
|
|
|
|
|
|
|
# 21.3.1 300 multiple choices |
426
|
|
|
|
|
|
|
# 21.3.2 301 moved permanently |
427
|
0
|
|
|
|
|
0
|
invoke_callback($cb,@arg,EFAULT,$code,$response,$leg,$from); |
428
|
|
|
|
|
|
|
|
429
|
|
|
|
|
|
|
} elsif ( $code == 302 ) { |
430
|
|
|
|
|
|
|
# 21.3.3 302 moved temporarily |
431
|
|
|
|
|
|
|
# redo request and insert request again |
432
|
0
|
|
|
|
|
0
|
my $contact = $self->{to} = $response->get_header( 'contact' ); |
433
|
0
|
0
|
|
|
|
0
|
$contact = $1 if $contact =~m{<(\w+:[^>\s]+)>}; |
434
|
0
|
|
|
|
|
0
|
$self->{remote_contact} = $contact; |
435
|
0
|
|
|
|
|
0
|
( my $r = $tr->{request} )->set_uri( $contact ); |
436
|
0
|
|
|
|
|
0
|
$r->set_cseq( ++$self->{cseq} ); |
437
|
0
|
|
|
|
|
0
|
$endpoint->new_request( $r,$self ); |
438
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
} elsif ( $code == 305 ) { |
440
|
|
|
|
|
|
|
# 21.3.4 305 use proxy |
441
|
|
|
|
|
|
|
# set proxy as the first route and insert request again |
442
|
0
|
|
0
|
|
|
0
|
my $route = $self->{route} ||= []; |
443
|
0
|
|
|
|
|
0
|
unshift @$route,$response->get_header( 'contact' ); |
444
|
0
|
|
|
|
|
0
|
( my $r = $tr->{request} )->set_header( route => $route ); |
445
|
0
|
|
|
|
|
0
|
$r->set_cseq( ++$self->{cseq} ); |
446
|
0
|
|
|
|
|
0
|
$endpoint->new_request( $r,$self ); |
447
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
} else { |
449
|
|
|
|
|
|
|
# some kind of unrecoverable error |
450
|
7
|
|
|
|
|
70
|
invoke_callback($cb,@arg,EINVAL,$code,$response,$leg,$from); |
451
|
|
|
|
|
|
|
} |
452
|
|
|
|
|
|
|
} |
453
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
############################################################################ |
455
|
|
|
|
|
|
|
# handle incoming request |
456
|
|
|
|
|
|
|
# Args: ($self,$request,$leg,$endpoint) |
457
|
|
|
|
|
|
|
# $request: incoming Net::SIP::Request packet |
458
|
|
|
|
|
|
|
# $leg: Net::SIP::Leg through which the request came in |
459
|
|
|
|
|
|
|
# $from: ip:port where request came in |
460
|
|
|
|
|
|
|
# $endpoint: endpoint responsable for this context, used for responses... |
461
|
|
|
|
|
|
|
# Returns: NONE |
462
|
|
|
|
|
|
|
# Comment: only new requests will be delivered to this method, because the dispatcher |
463
|
|
|
|
|
|
|
# cares about retransmits, eg requests for which I issued already a response |
464
|
|
|
|
|
|
|
# within the last 64*T1 |
465
|
|
|
|
|
|
|
############################################################################ |
466
|
|
|
|
|
|
|
sub handle_request { |
467
|
57
|
|
|
57
|
1
|
165
|
my Net::SIP::Endpoint::Context $self = shift; |
468
|
57
|
|
|
|
|
209
|
my ($request,$leg,$from,$endpoint) = @_; |
469
|
|
|
|
|
|
|
|
470
|
57
|
|
|
|
|
445
|
my $cseq = $request->cseq; |
471
|
57
|
|
|
|
|
529
|
my ($cseq_num) = $cseq=~m{^(\d+)}; |
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
DEBUG( 100,"method=%s cseq=%s/%s inc=%s", $request->method, $cseq_num,$cseq, |
474
|
57
|
100
|
|
|
|
296
|
defined($self->{_cseq_incoming}) ? $self->{_cseq_incoming} : '' ); |
475
|
57
|
50
|
66
|
|
|
624
|
if ( defined $self->{_cseq_incoming} |
476
|
|
|
|
|
|
|
and $cseq_num < $self->{_cseq_incoming} ) { |
477
|
|
|
|
|
|
|
# must be an retransmit of an really old request, drop |
478
|
0
|
|
|
|
|
0
|
DEBUG( 10,"retransmit of really old request? Dropping" ); |
479
|
0
|
|
|
|
|
0
|
return; |
480
|
|
|
|
|
|
|
} |
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
# check with last request in transaction |
483
|
57
|
|
|
|
|
149
|
my $ctx_is_new; |
484
|
57
|
100
|
|
|
|
222
|
if ( my $trans = $self->{_last_transreq} ) { |
485
|
37
|
|
|
|
|
154
|
my $last_cseq = $trans->cseq; |
486
|
37
|
50
|
|
|
|
164
|
if ( $last_cseq eq $cseq ) { |
487
|
0
|
|
|
|
|
0
|
DEBUG( 10,"retransmit of last request. DROP" ); |
488
|
0
|
|
|
|
|
0
|
return; |
489
|
|
|
|
|
|
|
} |
490
|
|
|
|
|
|
|
} else { |
491
|
20
|
|
|
|
|
69
|
$ctx_is_new = 1; |
492
|
|
|
|
|
|
|
} |
493
|
57
|
|
|
|
|
175
|
$self->{_last_transreq} = $request; |
494
|
|
|
|
|
|
|
|
495
|
57
|
|
|
|
|
204
|
my $method = $request->method; |
496
|
|
|
|
|
|
|
|
497
|
57
|
100
|
100
|
|
|
488
|
if ( $method eq 'ACK' || $method eq 'CANCEL' ) { |
498
|
|
|
|
|
|
|
# must be have same cseq_num as last request, otherwise drop |
499
|
21
|
50
|
33
|
|
|
394
|
if ( defined $self->{_cseq_incoming} |
500
|
|
|
|
|
|
|
and $cseq_num != $self->{_cseq_incoming} ) { |
501
|
0
|
|
|
|
|
0
|
DEBUG( 10,"received $method for unreceived INVITE: $cseq_num|$self->{_cseq_incoming}" ); |
502
|
0
|
|
|
|
|
0
|
return; |
503
|
|
|
|
|
|
|
} |
504
|
|
|
|
|
|
|
} else { |
505
|
|
|
|
|
|
|
# cannot have the same cseq_num as last request |
506
|
36
|
50
|
66
|
|
|
262
|
if ( defined $self->{_cseq_incoming} |
507
|
|
|
|
|
|
|
and $cseq_num == $self->{_cseq_incoming} ) { |
508
|
0
|
|
|
|
|
0
|
DEBUG( 10,"reused cseq for $method. DROP" ); |
509
|
0
|
|
|
|
|
0
|
return; |
510
|
|
|
|
|
|
|
} |
511
|
|
|
|
|
|
|
} |
512
|
57
|
|
|
|
|
188
|
$self->{_cseq_incoming} = $cseq_num; |
513
|
|
|
|
|
|
|
|
514
|
57
|
|
33
|
|
|
241
|
my $cb = $self->{_callback} || do { |
515
|
|
|
|
|
|
|
DEBUG( 50,"no callback at context!" ); |
516
|
|
|
|
|
|
|
return; |
517
|
|
|
|
|
|
|
}; |
518
|
57
|
|
|
|
|
182
|
my @arg = ($endpoint,$self); |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
# extract route information for future requests to the UAC (re-invites) |
521
|
|
|
|
|
|
|
# only for INVITE (rfc3261,12.1.1) |
522
|
57
|
50
|
66
|
|
|
557
|
if ( $ctx_is_new and $method eq 'INVITE' and |
|
|
|
66
|
|
|
|
|
523
|
|
|
|
|
|
|
my @route = $request->get_header( 'record-route' )) { |
524
|
0
|
|
|
|
|
0
|
$self->{route} = \@route; |
525
|
|
|
|
|
|
|
} |
526
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
{ |
528
|
|
|
|
|
|
|
# check if to has already a (my) tag, if not add it to request, |
529
|
|
|
|
|
|
|
# so that it gets added to responses |
530
|
57
|
|
|
|
|
136
|
my $to = $request->get_header( 'to' ); |
|
57
|
|
|
|
|
201
|
|
531
|
57
|
|
|
|
|
221
|
my ($data,$param) = sip_hdrval2parts( to => $to ); |
532
|
57
|
100
|
|
|
|
304
|
if ( ! $param->{tag} ) { |
533
|
21
|
|
|
|
|
99
|
DEBUG( 50,"added my tag to to header in request" ); |
534
|
21
|
|
|
|
|
160
|
$param->{tag} = $self->{local_tag}; |
535
|
21
|
|
|
|
|
217
|
$to = sip_parts2hdrval( 'to',$data,$param ); |
536
|
21
|
|
|
|
|
197
|
$request->set_header( to => $to ); |
537
|
|
|
|
|
|
|
} |
538
|
|
|
|
|
|
|
} |
539
|
|
|
|
|
|
|
|
540
|
57
|
100
|
100
|
|
|
516
|
if ( $method eq 'BYE' || $method eq 'CANCEL' ) { |
541
|
|
|
|
|
|
|
# if the peer wants to hangup we must confirm |
542
|
18
|
|
|
|
|
137
|
my $response = $request->create_response( '200','Closing' ); |
543
|
18
|
|
|
|
|
153
|
$endpoint->new_response( $self,$response,$leg,$from ); |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
# invoke callback before closing context, so that we have more |
546
|
|
|
|
|
|
|
# information about the current call |
547
|
18
|
|
|
|
|
131
|
invoke_callback($cb,@arg,0,0,$request,$leg,$from); |
548
|
|
|
|
|
|
|
|
549
|
18
|
100
|
|
|
|
1486
|
if ( $method eq 'CANCEL' ) { |
550
|
|
|
|
|
|
|
# must create 487 Request canceled |
551
|
3
|
|
|
|
|
26
|
my $response = $request->create_response( '487','Request canceled' ); |
552
|
3
|
|
33
|
|
|
16
|
$response->set_header( |
553
|
|
|
|
|
|
|
cseq => $response->cseq =~m{(\d+)} && "$1 INVITE" ); |
554
|
3
|
|
|
|
|
48
|
DEBUG(10,"send response: ".$response->dump(1)); |
555
|
3
|
|
|
|
|
19
|
$endpoint->new_response($self,$response,$leg,$from); |
556
|
|
|
|
|
|
|
} |
557
|
|
|
|
|
|
|
|
558
|
18
|
|
|
|
|
113
|
$endpoint->close_context($self); |
559
|
18
|
|
|
|
|
592
|
return; |
560
|
|
|
|
|
|
|
} |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
# If new INVITE, send 100 Trying |
563
|
39
|
100
|
|
|
|
168
|
if ( $method eq 'INVITE' ) { |
564
|
21
|
|
|
|
|
316
|
my $response = $request->create_response( '100','Trying' ); |
565
|
21
|
|
|
|
|
226
|
$endpoint->new_response( $self,$response,$leg,$from ); |
566
|
|
|
|
|
|
|
} |
567
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
# propagate to upper layer, which needs |
570
|
|
|
|
|
|
|
# - for INVITE send 180 Ringing periodically and after some time a final response |
571
|
|
|
|
|
|
|
# - for ACK to establish the call |
572
|
|
|
|
|
|
|
# - BYE|CANCEL is already handled above |
573
|
|
|
|
|
|
|
# - for everything else to handle the Option fully, eg issue final response.. |
574
|
|
|
|
|
|
|
|
575
|
39
|
|
|
|
|
632
|
invoke_callback($cb,@arg,0,0,$request,$leg,$from); |
576
|
|
|
|
|
|
|
} |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
1; |