-
Notifications
You must be signed in to change notification settings - Fork 2
/
sca_call_info.c
1942 lines (1643 loc) · 54.1 KB
/
sca_call_info.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id$
*
* Copyright (C) 2012 Andrew Mortensen
*
* This file is part of the sca module for sip-router, a free SIP server.
*
* The sca module is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* The sca module is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
*/
#include "sca_common.h"
#include <assert.h>
#include "sca.h"
#include "sca_appearance.h"
#include "sca_call_info.h"
#include "sca_dialog.h"
#include "sca_event.h"
#include "sca_notify.h"
#include "sca_subscribe.h"
#include "sca_util.h"
const str SCA_CALL_INFO_HEADER_STR = STR_STATIC_INIT( "Call-Info: " );
const str SCA_CALL_INFO_HEADER_NAME = STR_STATIC_INIT( "Call-Info" );
static int
sca_call_info_domain_from_uri( str *uri, str *domain )
{
assert( !SCA_STR_EMPTY( uri ));
assert( domain != NULL );
domain->s = memchr( uri->s, '@', uri->len );
if ( domain->s == NULL ) {
/* may be a sip:domain URI */
domain->s = memchr( uri->s, ':', uri->len );
if ( domain->s == NULL ) {
LM_ERR( "Bad URI %.*s", STR_FMT( uri ));
return( -1 );
}
}
domain->s++;
domain->len = ( uri->s + uri->len ) - domain->s;
/* XXX handle :port in URI? */
return( domain->len );
}
static int
sca_call_info_header_length( int given_length )
{
assert( given_length >= 0 );
/*
* given_length is assumed to contain length of index string,
* state string, and uri string, if any
*/
/* "<sip:DOMAIN>;appearance-index=N;appearance-state=STATE" */
given_length += strlen( "<sip:>=;=;" );
given_length += SCA_APPEARANCE_INDEX_STR.len;
given_length += SCA_APPEARANCE_STATE_STR.len;
return( given_length );
}
static int
sca_call_info_header_length_for_appearance( sca_appearance *appearance,
str *aor )
{
int len = 0;
str domain = STR_NULL;
str state_str = STR_NULL;
assert( aor != NULL );
/* get length of stringified index, since conversion's destructive */
(void)int2str( appearance->index, &len );
sca_appearance_state_to_str( appearance->state, &state_str );
len += state_str.len;
if ( !SCA_STR_EMPTY( &appearance->uri )) {
/* +1 for ';', +1 for '=' between param name and value, +2 for quotes */
len += SCA_APPEARANCE_URI_STR.len + 1 + 1 + 2;
len += appearance->uri.len;
}
if ( sca_call_info_domain_from_uri( aor, &domain ) < 0 ) {
return( -1 );
}
len += domain.len;
len += sca_call_info_header_length( domain.len );
return( len );
}
static int
sca_call_info_header_append_appearances( sca_mod *scam, sca_subscription *sub,
char *hdrbuf, int maxlen )
{
sca_appearance_list *app_list;
sca_appearance *app;
sca_hash_slot *slot;
str domain;
str state_str;
int slot_idx;
int len = -1;
int usedlen = -1;
slot_idx = sca_hash_table_index_for_key( scam->appearances,
&sub->target_aor );
slot = sca_hash_table_slot_for_index( scam->appearances, slot_idx );
sca_hash_table_lock_index( scam->appearances, slot_idx );
app_list = sca_hash_table_slot_kv_find_unsafe( slot, &sub->target_aor );
if ( app_list == NULL ) {
len = 0;
goto done;
}
usedlen = 0;
for ( app = app_list->appearances; app != NULL; app = app->next ) {
len = sca_call_info_header_length_for_appearance( app,
&sub->target_aor );
if ( len < 0 ) {
goto done;
}
if (( maxlen - len ) < 0 ) {
LM_ERR( "Call-Info header for AoR %.*s is too long",
STR_FMT( &sub->target_aor ));
len = -1;
goto done;
}
memcpy( hdrbuf, "<sip:", strlen( "<sip:" ));
len = strlen( "<sip:" );
if ( sca_call_info_domain_from_uri( &sub->target_aor, &domain ) < 0 ) {
return( -1 );
}
memcpy( hdrbuf + len, domain.s, domain.len );
len += domain.len;
sca_appearance_state_to_str( app->state, &state_str );
/* state_str.s is a nul-terminated string literal */
len += snprintf( hdrbuf + len, maxlen - len,
">;appearance-index=%d;appearance-state=%s",
app->index, state_str.s );
if ( !SCA_STR_EMPTY( &app->uri )) {
hdrbuf[ len ] = ';';
len += 1;
memcpy( hdrbuf + len, SCA_APPEARANCE_URI_STR.s,
SCA_APPEARANCE_URI_STR.len );
len += SCA_APPEARANCE_URI_STR.len;
hdrbuf[ len ] = '=';
len += 1;
hdrbuf[ len ] = '"';
len += 1;
memcpy( hdrbuf + len, app->uri.s, app->uri.len );
len += app->uri.len;
hdrbuf[ len ] = '"';
len += 1;
}
if ( app->next ) {
memcpy( hdrbuf + len, ",", 1 );
len++;
}
maxlen -= len;
hdrbuf += len;
usedlen += len;
}
done:
sca_hash_table_unlock_index( scam->appearances, slot_idx );
return( usedlen );
}
static int
sca_call_info_header_length_for_idle_appearance( sca_mod *scam )
{
int given_length = 0;
/* appearance-index=* */
given_length += strlen( "*" );
/* appearance-state=idle */
given_length += SCA_APPEARANCE_STATE_STR_IDLE.len;
given_length += scam->cfg->domain->len;
return( sca_call_info_header_length( given_length ));
}
static int
sca_call_info_build_idle_value( sca_mod *scam, char *hdrbuf, int maxlen )
{
int len;
if ( sca_call_info_header_length_for_idle_appearance( scam ) >= maxlen ) {
LM_ERR( "Failed to add idle appearances: Call-Info header too long" );
return( -1 );
}
#ifdef notdef
strcpy( hdrbuf, "<sip:" );
len = strlen( "<sip:" );
memcpy( hdrbuf + len, scam->cfg->domain->s, scam->cfg->domain->len );
len += scam->cfg->domain->len;
#endif /* notdef */
/* the SCA_APPEARANCE_ strs' s member are literal C strings */
len = snprintf( hdrbuf, maxlen,
"<sip:%s>;%s=*;%s=%s%s",
scam->cfg->domain->s,
SCA_APPEARANCE_INDEX_STR.s,
SCA_APPEARANCE_STATE_STR.s,
SCA_APPEARANCE_STATE_STR_IDLE.s, CRLF );
return( len );
}
int
sca_call_info_build_header( sca_mod *scam, sca_subscription *sub,
char *hdrbuf, int maxlen )
{
/* we send one Call-Info header, appearances separated by commas */
int len;
int usedlen = SCA_CALL_INFO_HEADER_STR.len;
/* begin with "Call-Info: " */
memcpy( hdrbuf, SCA_CALL_INFO_HEADER_STR.s, SCA_CALL_INFO_HEADER_STR.len );
len = sca_call_info_header_append_appearances( scam, sub,
hdrbuf + usedlen, maxlen - usedlen );
usedlen += len;
if ( usedlen > SCA_CALL_INFO_HEADER_STR.len ) {
/* we added an indexed appearance, append a comma */
memcpy( hdrbuf + usedlen, ",", 1 );
usedlen++;
}
/* line-seize NOTIFYs will contain only the seized appearance index */
if ( sub->event != SCA_EVENT_TYPE_LINE_SEIZE ) {
/* if not all appearances in use, add *-index idle */
len = sca_call_info_build_idle_value( scam,
hdrbuf + usedlen, maxlen - usedlen );
if ( len < 0 || len + usedlen >= maxlen ) {
LM_ERR( "Cannot build idle Call-Info value: buffer too small" );
return( -1 );
}
usedlen += len;
}
return( usedlen );
}
int
sca_call_info_append_header_for_appearance_index( sca_subscription *sub,
int appearance_index, char *hdrbuf, int maxlen )
{
str domain = STR_NULL;
char *app_index_p = NULL;
int len = 0, idx_len;
memcpy( hdrbuf, SCA_CALL_INFO_HEADER_STR.s, SCA_CALL_INFO_HEADER_STR.len );
len += SCA_CALL_INFO_HEADER_STR.len;
if ( len >= maxlen ) {
goto error;
}
memcpy( hdrbuf + len, "<sip:", strlen( "<sip:" ));
len += strlen( "<sip:" );
if ( len >= maxlen ) {
goto error;
}
sca_call_info_domain_from_uri( &sub->target_aor, &domain );
memcpy( hdrbuf + len, domain.s, domain.len );
len += domain.len;
if ( len >= maxlen ) {
goto error;
}
memcpy( hdrbuf + len, ">;appearance-index=",
strlen( ">;appearance-index=" ));
len += strlen( ">;appearance-index=" );
if ( len >= maxlen ) {
goto error;
}
app_index_p = int2str( appearance_index, &idx_len );
memcpy( hdrbuf + len, app_index_p, idx_len );
len += idx_len;
if ( len >= maxlen ) {
goto error;
}
memcpy( hdrbuf + len, CRLF, CRLF_LEN );
len += CRLF_LEN;
if ( len >= maxlen ) {
goto error;
}
return( len );
error:
LM_ERR( "Failed to append Call-Info header for %.*s appearance index %d",
STR_FMT( &sub->subscriber ), appearance_index );
return( -1 );
}
hdr_field_t *
sca_call_info_header_find( hdr_field_t *msg_hdrs )
{
hdr_field_t *hdr = NULL;
for ( hdr = msg_hdrs; hdr != NULL; hdr = hdr->next ) {
if ( hdr->type == HDR_OTHER_T &&
hdr->name.len == SCA_CALL_INFO_HEADER_NAME.len ) {
if ( strncasecmp( hdr->name.s, SCA_CALL_INFO_HEADER_NAME.s,
SCA_CALL_INFO_HEADER_NAME.len ) == 0) {
break;
}
}
}
return( hdr );
}
int
sca_call_info_body_parse( str *hdr_body, sca_call_info *call_info )
{
str s = STR_NULL;
char *p;
char *semi;
int len;
assert( call_info != NULL );
if ( SCA_STR_EMPTY( hdr_body )) {
LM_ERR( "Call-Info header body is empty" );
return( -1 );
}
call_info->sca_uri.s = NULL;
call_info->sca_uri.len = 0;
call_info->index = -1;
call_info->state = SCA_APPEARANCE_STATE_UNKNOWN;
call_info->uri.s = NULL;
call_info->uri.len = 0;
p = hdr_body->s;
if ( memcmp( p, "<sip:", strlen( "<sip:" )) != 0 ) {
LM_ERR( "Bad Call-Info header body: must begin with \"<sip:\"" );
return( -1 );
}
/* +5 == strlen( "<sip:" ) */
semi = memchr( p + 5, ';', hdr_body->len );
if ( semi == NULL ) {
LM_ERR( "Bad Call-Info header body: missing ';' between uri and "
"%.*s", STR_FMT( &SCA_APPEARANCE_INDEX_STR ));
return( -1 );
}
if ( *(semi - 1) != '>' ) {
LM_ERR( "Bad Call-Info header body: SCA URI missing '>' terminator" );
return( -1 );
}
call_info->sca_uri.s = p;
call_info->sca_uri.len = semi - p;
p = semi;
p++;
if ( memcmp( p, SCA_APPEARANCE_INDEX_STR.s,
SCA_APPEARANCE_INDEX_STR.len ) != 0 ) {
LM_ERR( "Bad Call-Info header body: does not begin with %.*s",
STR_FMT( &SCA_APPEARANCE_INDEX_STR ));
return( -1 );
}
p += SCA_APPEARANCE_INDEX_STR.len;
if ( *p != '=' ) {
LM_ERR( "Bad Call-Info header body: missing '=' after %.*s",
STR_FMT( &SCA_APPEARANCE_INDEX_STR ));
return( -1 );
}
p++;
len = ( hdr_body->s + hdr_body->len ) - p;
semi = memchr( p, ';', len );
if ( semi != NULL ) {
len = semi - p;
}
s.s = p;
s.len = len;
if ( str2int( &s, (unsigned int *)&call_info->index ) != 0 ) {
LM_ERR( "Bad Call-Info header: failed to convert %.*s %.*s to an "
"integer", STR_FMT( &SCA_APPEARANCE_INDEX_STR ), STR_FMT( &s ));
return( -1 );
}
if ( semi == NULL ) {
/* Call-Info header only contained an appearance-index */
goto done;
}
/* advance appearance-index value + semi-colon */
p += ( len + 1 );
if ( memcmp( p, SCA_APPEARANCE_STATE_STR.s,
SCA_APPEARANCE_STATE_STR.len ) != 0 ) {
LM_ERR( "Bad Call-Info header: missing %.*s",
STR_FMT( &SCA_APPEARANCE_STATE_STR ));
return( -1 );
}
p += SCA_APPEARANCE_STATE_STR.len;
if ( *p != '=' ) {
LM_ERR( "Bad Call-Info header body: missing '=' after %.*s",
STR_FMT( &SCA_APPEARANCE_STATE_STR ));
return( -1 );
}
p++;
len = ( hdr_body->s + hdr_body->len ) - p;
semi = memchr( p, ';', len );
if ( semi != NULL ) {
len = semi - p;
}
s.s = p;
s.len = len;
call_info->state = sca_appearance_state_from_str( &s );
if ( call_info->state == SCA_APPEARANCE_STATE_UNKNOWN ) {
LM_ERR( "Bad Call-Info header: unrecognized state \"%.*s\"",
STR_FMT( &s ));
return( -1 );
}
if ( semi == NULL ) {
/* Call-Info header only had appearance-index & appearance-state */
goto done;
}
/* advance length of state + semi-colon */
p += ( len + 1 );
if ( memcmp( p, SCA_APPEARANCE_URI_STR.s,
SCA_APPEARANCE_URI_STR.len ) != 0 ) {
LM_ERR( "Bad Call-Info header: missing %.*s",
STR_FMT( &SCA_APPEARANCE_URI_STR ));
return( -1 );
}
p += SCA_APPEARANCE_URI_STR.len;
if ( *p != '=' ) {
LM_ERR( "Bad Call-Info header: missing '=' after %.*s",
STR_FMT( &SCA_APPEARANCE_URI_STR ));
return( -1 );
}
p++;
call_info->uri.s = p;
call_info->uri.len = ( hdr_body->s + hdr_body->len ) - p;
if ( SCA_STR_EMPTY( &call_info->uri )) {
LM_ERR( "Bad Call-Info header: empty %.*s",
STR_FMT( &SCA_APPEARANCE_URI_STR ));
return( -1 );
}
done:
return( 0 );
}
/*
* return codes:
* -1: error, failed to remove call-info header.
* 0: no call-info headers found.
* >=1: removed 1 or more call-info headers.
*/
static int
sca_call_info_header_remove( sip_msg_t *msg )
{
hdr_field_t *hdr;
struct lump *ci_hdr_lump;
int rc = 0;
/* all headers must be parsed before using del_lump */
if ( parse_headers( msg, HDR_EOH_F, 0 ) < 0 ) {
LM_ERR( "Failed to parse_headers" );
return( -1 );
}
#ifdef notdef
for ( hdr = sca_call_info_header_find( msg->headers ); hdr != NULL;
hdr = sca_call_info_header_find( hdr->next )) {
#endif /* notdef */
for ( hdr = msg->headers; hdr; hdr = hdr->next ) {
if ( hdr->name.len != SCA_CALL_INFO_HEADER_NAME.len ) {
continue;
}
if ( memcmp( hdr->name.s, SCA_CALL_INFO_HEADER_NAME.s,
hdr->name.len ) != 0 ) {
continue;
}
/* del_lump takes packet, offset, lump length, & hdr type */
ci_hdr_lump = del_lump( msg, hdr->name.s - msg->buf,
hdr->len, HDR_OTHER_T );
if ( ci_hdr_lump == NULL ) {
LM_ERR( "Failed to del_lump Call-Info header" );
rc = -1;
break;
}
rc++;
}
return( rc );
}
//static int
int
sca_call_info_seize_held_call( sip_msg_t *msg, sca_call_info *call_info,
struct to_body *from, struct to_body *to, str *from_aor, str *to_aor,
str *contact_uri )
{
sca_appearance *app;
struct lump *anchor;
str callee_aor = STR_NULL;
str replaces_hdr = STR_NULL;
str prev_callid = STR_NULL;
str prev_totag = STR_NULL;
char tagbuf[ 1024 ];
char callee_buf[ 1024 ];
int slot_idx = -1;
int rc = -1;
slot_idx = sca_hash_table_index_for_key( sca->appearances, from_aor );
sca_hash_table_lock_index( sca->appearances, slot_idx );
app = sca_appearance_for_index_unsafe( sca, from_aor, call_info->index,
slot_idx );
if ( app == NULL ) {
LM_ERR( "sca_call_info_seize_held_call: no active appearances for "
"%.*s", STR_FMT( from_aor ));
goto done;
}
LM_DBG( "sca_call_info_seize_held_call: seizing %.*s index %d, callee %.*s",
STR_FMT( from_aor ), app->index, STR_FMT( &app->callee ));
/* rewrite the RURI to use the callee in this SCA dialog */
if ( msg->new_uri.s ) {
/*
* someone already rewrote the URI. shouldn't happen, but we have
* to watch for it. log our overwriting of it.
*/
LM_INFO( "SCA caller retrieving held call, but RURI was already "
"rewritten as %.*s. Overwriting with %.*s.",
STR_FMT( &msg->new_uri ), STR_FMT( &app->callee ));
pkg_free( msg->new_uri.s );
msg->new_uri.s = NULL;
msg->new_uri.len = 0;
}
/* msg->new_uri.s is free'd when transaction is torn down */
msg->new_uri.s = (char *)pkg_malloc( app->callee.len );
if ( msg->new_uri.s == NULL ) {
LM_ERR( "sca_call_info_seize_held_call: pkg_malloc new RURI %.*s "
"failed", STR_FMT( &app->callee ));
goto done;
}
SCA_STR_COPY( &msg->new_uri, &app->callee );
{
int idx;
for ( idx = 0; get_sip_branch( idx ) != NULL; idx++ )
;
for ( ; idx >= 0; idx-- ) {
drop_sip_branch( idx );
}
}
/* must reset to avoid using cached parsed RURI */
msg->parsed_uri_ok = 0;
ruri_mark_new();
/* store the previous dialog's tags for lookup of the callee */
prev_callid.s = tagbuf;
SCA_STR_COPY( &prev_callid, &app->dialog.call_id );
prev_totag.s = tagbuf + prev_callid.len;
SCA_STR_COPY( &prev_totag, &app->dialog.to_tag );
/* pkg_malloc's replaces_hdr.s, which is free'd if added as lump */
if ( sca_dialog_create_replaces_header( &app->dialog,
&replaces_hdr ) < 0 ) {
LM_ERR( "sca_call_info_seize_held_call: failed to create Replaces "
"header for %.*s from dialog %.*s",
STR_FMT( from_aor ), STR_FMT( &app->dialog.id ));
goto done;
}
/* store the callee's username for lookup of the callee by AoR */
callee_aor.s = callee_buf;
if ( sca_uri_build_aor( &callee_aor, sizeof( callee_buf ), &app->callee,
from_aor ) < 0 ) {
LM_ERR( "sca_call_info_seize_held_call: failed to create To AoR "
"from %.*s and %.*s", STR_FMT( &app->callee ),
STR_FMT( from_aor ));
pkg_free( replaces_hdr.s );
goto done;
}
/* all headers must be parsed before using lump functions */
if ( parse_headers( msg, HDR_EOH_F, 0 ) < 0 ) {
LM_ERR( "Failed to parse_headers" );
goto done;
}
anchor = anchor_lump( msg, msg->eoh - msg->buf, 0, HDR_OTHER_T );
if ( anchor == NULL ) {
LM_ERR( "Failed to anchor lump" );
goto done;
}
/* append the Replaces header before the sdp body */
if ( insert_new_lump_before( anchor, replaces_hdr.s,
replaces_hdr.len, HDR_OTHER_T ) == NULL ) {
LM_ERR( "Failed to add Replaces header %.*s", STR_FMT( &replaces_hdr ));
pkg_free( replaces_hdr.s );
goto done;
}
/*
* RFC 3891 (Replaces header) suggests, but does not require, that the
* UAS establish the dialog with the UAC replacing the existing dialog
* before sending the BYE to the original UAC. Polycom handsets appear
* to send the BYE to the original UAC first, so we save the pending
* owner here. if the 200 OK arrives first, we update the owner and
* dialog there. otherwise, we catch this in the 200 OK to the BYE
* sent by the line being replaced.
*
* if the reINVITE to seize the held line fails for some reason,
* we restore the original owner and dialog.
*/
if ( sca_appearance_update_owner_unsafe( app, contact_uri ) < 0 ) {
LM_ERR( "sca_call_info_seize_held_call: failed to update owner" );
pkg_free( replaces_hdr.s );
goto done;
}
if ( sca_appearance_update_dialog_unsafe( app, &msg->callid->body,
&from->tag_value, &to->tag_value ) < 0 ) {
LM_ERR( "sca_call_info_seize_held_call: failed to update dialog" );
goto done;
}
app->flags |= SCA_APPEARANCE_FLAG_OWNER_PENDING;
app->state = SCA_APPEARANCE_STATE_ACTIVE;
sca_hash_table_unlock_index( sca->appearances, slot_idx );
slot_idx = -1;
if ( !SCA_STR_EMPTY( &callee_aor )) {
if ( sca_uri_lock_if_shared_appearance( sca, &callee_aor, &slot_idx )) {
app = sca_appearance_for_tags_unsafe( sca, &callee_aor,
&prev_callid, &prev_totag, NULL, slot_idx );
if ( app == NULL ) {
LM_ERR( "sca_call_info_seize_held_call: failed to find "
"appearance of %.*s with dialog %.*s;%.*s",
STR_FMT( &callee_aor ), STR_FMT( &prev_callid ),
STR_FMT( &prev_totag ));
goto done;
}
app->flags |= SCA_APPEARANCE_FLAG_CALLEE_PENDING;
if ( sca_appearance_update_callee_unsafe( app, contact_uri ) < 0 ) {
LM_ERR( "sca_call_info_seize_held_call: failed to update callee" );
goto done;
}
if ( sca_appearance_update_dialog_unsafe( app, &msg->callid->body,
&to->tag_value, &from->tag_value ) < 0 ) {
LM_ERR( "sca_call_info_seize_held_call: failed to update dialog" );
goto done;
}
}
}
rc = 1;
done:
if ( slot_idx >= 0 ) {
sca_hash_table_unlock_index( sca->appearances, slot_idx );
}
return( rc );
}
static int
sca_call_info_uri_update( str *aor, sca_call_info *call_info,
struct to_body *from, struct to_body *to, str *contact_uri,
str *call_id )
{
sca_appearance *app;
sca_dialog dialog;
str state_str;
str *from_tag = &from->tag_value;
str *to_tag = &to->tag_value;
char dlg_buf[ 1024 ];
int slot_idx = -1;
int rc = -1;
assert( aor != NULL );
assert( call_info != NULL );
LM_DBG( "sca_call_info_uri_update for %.*s: From: <%.*s> To: <%.*s> "
"Contact: <%.*s> Call-ID: %.*s Call-Info: appearance-index=%d",
STR_FMT( aor ), STR_FMT( &from->uri ), STR_FMT( &to->uri ),
STR_FMT( contact_uri ), STR_FMT( call_id ), call_info->index );
if ( !sca_uri_is_shared_appearance( sca, aor )) {
return( 0 );
}
dialog.id.s = dlg_buf;
if ( sca_dialog_build_from_tags( &dialog, sizeof( dlg_buf ), call_id,
to_tag, from_tag ) < 0 ) {
LM_ERR( "sca_call_info_uri_update: Failed to build dialog from tags" );
return( -1 );
}
slot_idx = sca_hash_table_index_for_key( sca->appearances, aor );
sca_hash_table_lock_index( sca->appearances, slot_idx );
app = sca_appearance_for_index_unsafe( sca, aor, call_info->index,
slot_idx );
if ( app != NULL ) {
LM_DBG( "sca_call_info_uri_update: setting owner to %.*s",
STR_FMT( contact_uri ));
if ( sca_appearance_update_unsafe( app, call_info->state,
NULL, NULL, &dialog, contact_uri, NULL ) < 0 ) {
sca_appearance_state_to_str( call_info->state, &state_str );
LM_ERR( "sca_call_info_uri_update: failed to update appearance "
"%.*s appearance-index %d with dialog id %.*s to "
"state %.*s", STR_FMT( &app->owner ), app->index,
STR_FMT( &app->dialog.id ), STR_FMT( &state_str ));
goto done;
}
rc = 1;
} else {
app = sca_appearance_seize_index_unsafe( sca, aor, contact_uri,
call_info->index, slot_idx );
if ( app == NULL ) {
LM_ERR( "sca_call_info_uri_update: failed to seize index %d "
"for %.*s", call_info->index, STR_FMT( contact_uri ));
goto done;
}
LM_DBG( "sca_call_info_uri_update: seized %d for %.*s: From: <%.*s> "
"To: <%.*s> Call-ID: <%.*s> Dialog: <%.*s>" , app->index,
STR_FMT( &app->owner ), STR_FMT( &from->uri ),
STR_FMT( &to->uri ), STR_FMT( call_id ),
STR_FMT( &app->dialog.id ));
if ( sca_appearance_update_unsafe( app, SCA_APPEARANCE_STATE_ACTIVE,
&from->display, &from->uri, &dialog, contact_uri,
&from->uri ) < 0 ) {
sca_appearance_state_to_str( call_info->state, &state_str );
LM_ERR( "sca_call_info_uri_update: failed to update appearance "
"%.*s appearance-index %d with dialog id %.*s to "
"state %.*s", STR_FMT( &app->owner ), app->index,
STR_FMT( &app->dialog.id ), STR_FMT( &state_str ));
goto done;
}
rc = 1;
}
done:
if ( slot_idx >= 0 ) {
sca_hash_table_unlock_index( sca->appearances, slot_idx );
}
return( rc );
}
static int
sca_call_info_is_line_seize_reinvite( sip_msg_t *msg, sca_call_info *call_info,
struct to_body *from, struct to_body *to, str *from_aor, str *to_aor )
{
str *ruri;
str ruri_aor;
int state;
/*
* a handset in an SCA group is attempting to seize a held line if:
* the RURI, From URI and To URI are identical;
* the above are SCA AoRs;
* there is no to-tag;
* a Call-Info header is present
*/
if ( call_info == NULL ) {
return( 0 );
}
if ( !SCA_STR_EMPTY( &to->tag_value )) {
return( 0 );
}
ruri = GET_RURI( msg );
if ( sca_uri_extract_aor( ruri, &ruri_aor ) < 0 ) {
LM_ERR( "sca_call_info_is_line_seize_reinvite: failed to extract "
"AoR from RURI %.*s", STR_FMT( ruri ));
return( 0 );
}
if ( !SCA_STR_EQ( from_aor, to_aor )) {
return( 0 );
}
state = sca_appearance_state_for_index( sca, from_aor, call_info->index );
if ( state != SCA_APPEARANCE_STATE_HELD ) {
LM_DBG( "sca_call_info_is_line_seize_reinvite: new INVITE to "
"%.*s from %.*s appearance-index %d (not seizing held line)",
STR_FMT( to_aor ), STR_FMT( from_aor ), call_info->index );
return( 0 );
}
return( 1 );
}
/*
* to be invoked only by proxy-generated replies with error status codes
*/
static void
sca_call_info_local_error_reply_handler( sip_msg_t *msg, int status )
{
struct to_body *from;
struct to_body *to;
str aor = STR_NULL;
str contact_uri = STR_NULL;
if ( sca_get_msg_from_header( msg, &from ) < 0 ) {
LM_ERR( "sca_call_info_sl_reply_cb: failed to get From header from "
"request before stateless reply with %d", status );
return;
}
if ( sca_uri_extract_aor( &from->uri, &aor ) < 0 ) {
LM_ERR( "sca_call_info_sl_reply_cb: failed to extract AoR "
"from URI %.*s", STR_FMT( &from->uri ));
return;
}
if ( !sca_uri_is_shared_appearance( sca, &aor )) {
/* LM_DBG( "sca_call_info_sl_reply_cb: ignoring non-shared appearance "
"%.*s", STR_FMT( &aor )); */
return;
}
if ( sca_get_msg_contact_uri( msg, &contact_uri ) < 0 ) {
LM_ERR( "sca_call_info_sl_reply_cb: failed to get Contact from "
"request before stateless reply with %d", status );
return;
}
if ( sca_get_msg_to_header( msg, &to ) < 0 ) {
LM_ERR( "sca_call_info_sl_reply_cb: failed to get To header from "
"request before stateless reply with %d", status );
return;
}
if ( sca_subscription_terminate( sca, &aor,
SCA_EVENT_TYPE_LINE_SEIZE, &contact_uri,
SCA_SUBSCRIPTION_STATE_TERMINATED_NORESOURCE,
SCA_SUBSCRIPTION_TERMINATE_OPT_DEFAULT ) < 0 ) {
LM_ERR( "sca_call_info_sl_reply_cb: failed to terminate "
"line-seize subscription for %.*s", STR_FMT( &contact_uri ));
} else if ( sca_notify_call_info_subscribers( sca, &aor ) < 0 ) {
LM_ERR( "Failed to call-info NOTIFY %.*s subscribers",
STR_FMT( &aor ));
}
}
void
sca_call_info_response_ready_cb( struct cell *t, int type,
struct tmcb_params *params )
{
if ( !(type & TMCB_RESPONSE_READY)) {
return;
}
if ( params->code < 400 ) {
/* non-error final response: 1xx, 2xx, 3xx */
return;
}
sca_call_info_local_error_reply_handler( params->req, params->code );
}
int
sca_call_info_invite_request_handler( sip_msg_t *msg, sca_call_info *call_info,
struct to_body *from, struct to_body *to, str *from_aor, str *to_aor,
str *contact_uri )
{
sca_dialog dialog;
char dlg_buf[ 1024 ];
str state_str = STR_NULL;
int state = SCA_APPEARANCE_STATE_UNKNOWN;
int rc = -1;
/*
* if we get here, one of the legs is an SCA endpoint. we want to know
* when the e2e ACK comes in so we can notify other members of the group.
*/
if ( sca->tm_api->register_tmcb( msg, NULL, TMCB_E2EACK_IN,
sca_call_info_ack_cb, NULL, NULL ) < 0 ) {
LM_ERR( "sca_call_info_invite_request_handler: failed to register "
"callback for INVITE %.*s ACK", STR_FMT( from_aor ));
goto done;
}
if ( call_info == NULL ) {
/* caller isn't SCA, no more to do. update callee in reply handler. */
rc = 1;
goto done;
}
/*
* register callback to handle error responses sent from script using
* t_reply. TMCB_RESPONSE_READY will only be called from t_reply(),
* so relayed responses from upstream UASs will not triggers this.
*/
if ( sca->tm_api->register_tmcb( msg, NULL, TMCB_RESPONSE_READY,
sca_call_info_response_ready_cb, NULL, NULL ) < 0 ) {
LM_ERR( "sca_call_info_invite_request_handler: failed to register "
"callback for INVITE %.*s ACK", STR_FMT( from_aor ));
goto done;
}
if ( sca_call_is_held( msg )) {
state = SCA_APPEARANCE_STATE_HELD;
} else if ( !SCA_STR_EMPTY( &to->tag_value )) {
/* this is a reINVITE from an SCA line that put the call on hold */
state = SCA_APPEARANCE_STATE_ACTIVE;
} else if ( sca_call_info_is_line_seize_reinvite( msg, call_info,
from, to, from_aor, to_aor )) {
rc = sca_call_info_seize_held_call( msg, call_info, from, to,
from_aor, to_aor, contact_uri );
}
/* otherwise, this is an initial INVITE */
dialog.id.s = dlg_buf;
if ( sca_dialog_build_from_tags( &dialog, sizeof( dlg_buf ),
&msg->callid->body, &from->tag_value, &to->tag_value ) < 0 ) {
LM_ERR( "Failed to build dialog from tags" );