-
Notifications
You must be signed in to change notification settings - Fork 590
/
Copy pathlogic.c
3866 lines (3389 loc) · 98.6 KB
/
logic.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
/*
* back-to-back logic module
*
* Copyright (C) 2009 Free Software Fundation
*
* This file is part of opensips, a free SIP server.
*
* opensips 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
*
* opensips 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* --------
* 2009-08-03 initial version (Anca Vamanu)
*/
#include <stdio.h>
#include <stdlib.h>
#include "../../dprint.h"
#include "../../dset.h"
#include "../../error.h"
#include "../../parser/parse_from.h"
#include "../../parser/parse_to.h"
#include "../../parser/parse_content.h"
#include "../../parser/parse_methods.h"
#include "../../parser/parse_hname2.h"
#include "../../parser/parse_refer_to.h"
#include "../../parser/parse_replaces.h"
#include "../../parser/parse_uri.h"
#include "../../strcommon.h"
#include "../../ut.h"
#include "../../pt.h"
#include "../../trim.h"
#include "../../mem/shm_mem.h"
#include "../../mem/mem.h"
#include "../../msg_translator.h"
#include "../b2b_entities/b2be_load.h"
#include "../presence/hash.h"
#include "../presence/utils_func.h"
#include "../../lib/csv.h"
#include "records.h"
#include "b2b_logic.h"
#include "b2bl_db.h"
#include "entity_storage.h"
#include "bridging.h"
static str cancel_reason_hdr=
{"Reason: SIP;cause=200;text=\"Call completed elsewhere\"\r\n", 55};
extern int b2bl_key_avp_name;
extern unsigned short b2bl_key_avp_type;
extern b2bl_tuple_t *local_ctx_tuple;
extern struct b2b_ctx_val *local_ctx_vals;
extern int req_routeid;
extern int reply_routeid;
struct b2bl_route_ctx cur_route_ctx;
struct to_body* get_b2bl_from(struct sip_msg* msg);
int post_cb_sanity_check(b2bl_tuple_t **tuple, unsigned int hash_index, unsigned int local_index,
b2bl_entity_id_t **entity, int etype, str *ekey);
int udh_to_uri(str user, str host, str port, str* uri);
int insert_entity_term_tl(b2bl_entity_id_t *entity);
static str method_invite= {INVITE, INVITE_LEN};
static str method_bye = {BYE, BYE_LEN};
static str method_cancel= {CANCEL, CANCEL_LEN};
static str ok = str_init("OK");
static str notAcceptable = str_init("Not Acceptable");
str requestTerminated = str_init("Request Terminated");
int get_new_entities(struct b2bl_new_entity **entity1,
struct b2bl_new_entity **entity2)
{
if (!current_processing_ctx) {
LM_ERR("no current processing ctx!\n");
*entity1 = NULL;
*entity2 = NULL;
return -1;
}
*entity1 = context_get_ptr(CONTEXT_GLOBAL, current_processing_ctx,
new_ent_1_ctx_idx);
*entity2 = context_get_ptr(CONTEXT_GLOBAL, current_processing_ctx,
new_ent_2_ctx_idx);
return 0;
}
void new_ent_ctx_destroy(void *e)
{
pkg_free(e);
}
int entity_add_dlginfo(b2bl_entity_id_t* entity, b2b_dlginfo_t* dlginfo)
{
b2b_dlginfo_t* new_dlginfo= NULL;
int size;
size = sizeof(b2b_dlginfo_t)+ dlginfo->callid.len;
if( dlginfo->totag.s)
size += dlginfo->totag.len;
if(dlginfo->fromtag.s)
size+= dlginfo->fromtag.len;
new_dlginfo = (b2b_dlginfo_t*)shm_malloc(size);
if(new_dlginfo == NULL)
{
LM_ERR("No more shared memory\n");
return -1;
}
memset(new_dlginfo, 0, size);
size = sizeof(b2b_dlginfo_t);
if( dlginfo->totag.s)
CONT_COPY(new_dlginfo, new_dlginfo->totag, dlginfo->totag);
if(dlginfo->fromtag.s)
CONT_COPY(new_dlginfo, new_dlginfo->fromtag, dlginfo->fromtag);
CONT_COPY(new_dlginfo, new_dlginfo->callid, dlginfo->callid);
entity->dlginfo = new_dlginfo;
return 0;
}
int b2b_add_dlginfo(str* key, str* entity_key, int src, b2b_dlginfo_t* dlginfo, void *param)
{
b2bl_tuple_t* tuple;
b2bl_entity_id_t* entity = NULL;
b2bl_entity_id_t** ent_head = NULL;
unsigned int hash_index, local_index;
if(b2bl_parse_key(key, &hash_index, &local_index) < 0)
{
LM_ERR("Failed to parse key\n");
return -1;
}
B2BL_LOCK_GET(hash_index);
tuple = b2bl_search_tuple_safe(hash_index, local_index);
if(tuple == NULL)
{
LM_ERR("No entity found\n");
B2BL_LOCK_RELEASE(hash_index);
return -1;
}
/* a connected call */
if(max_duration)
tuple->lifetime = get_ticks() + max_duration;
else
tuple->lifetime = 0;
entity = b2bl_search_entity(tuple, entity_key, src, &ent_head);
if(entity == NULL)
{
LM_ERR("No b2b_key match found\n");
B2BL_LOCK_RELEASE(hash_index);
return -1;
}
if(entity->dlginfo)
{
shm_free(entity->dlginfo);
entity->dlginfo = NULL;
}
if(entity_add_dlginfo(entity, dlginfo) < 0)
{
LM_ERR("Failed to add dialoginfo\n");
B2BL_LOCK_RELEASE(hash_index);
return -1;
}
/* log the dialog pair */
if(entity->peer && entity->peer->dlginfo)
{
LM_INFO("Dialog pair: [%.*s] - [%.*s]\n",
entity->peer->dlginfo->callid.len, entity->peer->dlginfo->callid.s,
dlginfo->callid.len, dlginfo->callid.s);
}
B2BL_LOCK_RELEASE(hash_index);
return 0;
}
int msg_add_dlginfo(b2bl_entity_id_t* entity, struct sip_msg* msg, str* totag)
{
b2b_dlginfo_t *dlginfo = b2b_fill_dlginfo(msg, totag);
if (!dlginfo)
{
LM_ERR("cannot fill dlginfo!\n");
return -1;
}
if(entity_add_dlginfo(entity, dlginfo) < 0)
{
LM_ERR("Failed to add dialoginfo\n");
return -1;
}
return 0;
}
int b2b_msg_get_to(struct sip_msg* msg, str* to_uri, int flags)
{
struct to_body *pto;
if (flags & B2BL_FLAG_TRANSPARENT_TO)
{
if (!msg || !msg->to || !msg->to->body.s)
{
LM_ERR("cannot find 'to' header!\n");
return -1;
}
if (msg->to->parsed == NULL)
{
if (parse_to_uri( msg ) == NULL)
{
LM_ERR("cannot parse To header\n");
return -1;
}
}
pto = (struct to_body*)msg->to->parsed;
pkg_str_dup(to_uri, &pto->uri);
} else {
if(!msg || parse_sip_msg_uri(msg)< 0)
{
LM_ERR("failed to parse R-URI\n");
return -1;
}
if(udh_to_uri(msg->parsed_uri.user, msg->parsed_uri.host,
msg->parsed_uri.port, to_uri)< 0)
{
LM_ERR("failed to construct uri from user and domain\n");
return -1;
}
}
return 0;
}
int b2b_msg_get_from(struct sip_msg* msg, str* from_uri, str* from_dname)
{
struct to_body *pfrom;
pfrom = get_b2bl_from(msg);
if (pfrom)
{
*from_uri = pfrom->uri;
*from_dname = pfrom->display;
return 0;
}
/* examine the from header */
if (!msg || !msg->from || !msg->from->body.s)
{
LM_ERR("cannot find 'from' header!\n");
return -1;
}
if (msg->from->parsed == NULL)
{
if ( parse_from_header( msg )<0 )
{
LM_ERR("cannot parse From header\n");
return -1;
}
}
pfrom = (struct to_body*)msg->from->parsed;
*from_uri = pfrom->uri;
*from_dname = pfrom->display;
return 0;
}
int b2b_msg_get_maxfwd(struct sip_msg *msg)
{
str vals;
unsigned int valn;
if (!msg->maxforwards) {
if (parse_headers(msg, HDR_MAXFORWARDS_F, 0) == -1) {
LM_ERR("parsing MAX_FORWARD header failed!\n");
return -1;
}
if (!msg->maxforwards) {
LM_DBG("max_forwards header not found!\n");
return -1;
}
}
trim_len(vals.len, vals.s, msg->maxforwards->body);
if (str2int(&vals, &valn) < 0) {
LM_ERR("Failed to parse Max-Forwards value\n");
return -1;
}
return valn;
}
b2bl_entity_id_t* b2bl_create_new_entity(enum b2b_entity_type type, str* entity_id,
str* to_uri, str *proxy, str* from_uri,str*from_dname, str* ssid, str* hdrs,
str *adv_ct, struct sip_msg* msg)
{
unsigned int size;
b2bl_entity_id_t* entity;
size = sizeof(b2bl_entity_id_t) + ((ssid!=NULL)?ssid->len:0) +
((entity_id!=NULL)?entity_id->len:0)+ ((to_uri !=NULL)?to_uri->len:0)
+ ((from_uri!=NULL)?from_uri->len:0)+ ((from_dname!=NULL)?from_dname->len:0)
+ ((proxy!=NULL)?proxy->len:0)+ ((hdrs!=NULL)?hdrs->len:0)
+ ((adv_ct!=NULL)?adv_ct->len:0);
entity = (b2bl_entity_id_t*)shm_malloc(size);
if(entity == NULL)
{
LM_ERR("No more shared memory\n");
return NULL;
}
memset(entity, 0, size);
size = sizeof(b2bl_entity_id_t);
if(entity_id)
{
entity->key.s= (char*)entity+ size;
memcpy(entity->key.s, entity_id->s, entity_id->len);
entity->key.len= entity_id->len;
size+= entity_id->len;
}
//CONT_COPY_P(entity, entity->key, entity_id);
if(ssid)
{
entity->scenario_id.s= (char*)entity+ size;
memcpy(entity->scenario_id.s, ssid->s, ssid->len);
entity->scenario_id.len= ssid->len;
size+= ssid->len;
}
//CONT_COPY_P(entity, entity->scenario_id, ssid);
if(to_uri)
{
entity->to_uri.s= (char*)entity+ size;
memcpy(entity->to_uri.s, to_uri->s, to_uri->len);
entity->to_uri.len= to_uri->len;
size+= to_uri->len;
}
if(proxy)
{
entity->proxy.s= (char*)entity+ size;
memcpy(entity->proxy.s, proxy->s, proxy->len);
entity->proxy.len= proxy->len;
size+= proxy->len;
}
//CONT_COPY_P(entity, entity->to_uri, to_uri);
if(from_uri)
{
entity->from_uri.s= (char*)entity+ size;
memcpy(entity->from_uri.s, from_uri->s, from_uri->len);
entity->from_uri.len= from_uri->len;
size+= from_uri->len;
}
// CONT_COPY_P(entity, entity->from_uri, from_uri);
if(from_dname)
{
entity->from_dname.s= (char*)entity+ size;
memcpy(entity->from_dname.s, from_dname->s, from_dname->len);
entity->from_dname.len= from_dname->len;
size+= from_dname->len;
}
if(hdrs)
{
entity->hdrs.s= (char*)entity+ size;
memcpy(entity->hdrs.s, hdrs->s, hdrs->len);
entity->hdrs.len= hdrs->len;
size+= hdrs->len;
}
if(adv_ct)
{
entity->adv_contact.s= (char*)entity+ size;
memcpy(entity->adv_contact.s, adv_ct->s, adv_ct->len);
entity->adv_contact.len= adv_ct->len;
size+= adv_ct->len;
}
entity->type = type;
if(type == B2B_SERVER && msg)
{
if( msg_add_dlginfo(entity, msg, entity_id)< 0 )
{
LM_ERR("Failed to add dialog information to b2b_logic entity\n");
shm_free(entity);
return NULL;
}
}
entity->stats.start_time = get_ticks();
entity->stats.call_time = 0;
LM_DBG("new entity type [%d] [%p]->[%.*s]\n",
entity->type, entity, entity->key.len, entity->key.s);
return entity;
}
void b2b_end_dialog(b2bl_entity_id_t* bentity, b2bl_tuple_t* tuple,
unsigned int hash_index)
{
str *method;
b2b_req_data_t req_data;
if(!bentity)
return;
if (bentity->next || bentity->prev)
{
LM_ERR("Inconsistent state for entity [%p]\n", bentity);
b2bl_print_tuple(tuple, L_ERR);
return;
}
if(bentity->key.s)
{
if(!bentity->disconnected && !bentity->rejected)
{
if(bentity->state == B2BL_ENT_CONFIRMED)
{
method = &method_bye;
}
else
{
method = &method_cancel;
}
memset(&req_data, 0, sizeof(b2b_req_data_t));
PREP_REQ_DATA(bentity);
req_data.method =method;
b2b_api.send_request(&req_data);
bentity->disconnected = 1;
}
}
else
{
LM_DBG("It is not connected yet - delete\n");
b2bl_delete_entity(bentity, tuple, hash_index, 1);
}
}
void b2b_mark_todel( b2bl_tuple_t* tuple)
{
tuple->to_del = 1;
tuple->lifetime = 30 + get_ticks();
tuple->state = B2B_CANCEL_STATE;
LM_DBG("%p\n", tuple);
}
int b2b_get_local_contact(struct sip_msg *msg, str *from_uri, str *local_contact)
{
struct sip_uri ct_uri,server_uri;
const struct socket_info *send_sock = msg ?
(msg->force_send_socket?msg->force_send_socket:msg->rcv.bind_address):NULL;
if (server_address.len) {
if (pv_printf_s(msg, server_address_pve, local_contact) != 0) {
LM_WARN("Failed to print format string from 'server_address'\n");
goto msg_contact;
} else {
/* validate what we have built */
if (parse_uri(local_contact->s, local_contact->len, &server_uri) < 0) {
LM_ERR("Not a valid server sip uri [%.*s]\n", local_contact->len, local_contact->s);
goto msg_contact;
}
/* we have expanded the server address, need to add username, if needed */
if (contact_user) {
send_sock = grep_sock_info( &server_uri.host, server_uri.port_no, server_uri.proto);
if (send_sock == NULL) {
LM_ERR("Failed to find send socket for server address [%.*s]\n", local_contact->len, local_contact->s);
goto msg_contact;
}
memset(&ct_uri, 0, sizeof(struct sip_uri));
if (parse_uri(from_uri->s, from_uri->len, &ct_uri) < 0) {
LM_ERR("Not a valid FROM sip uri [%.*s]\n", from_uri->len, from_uri->s);
goto done;
}
get_local_contact(send_sock, &ct_uri.user, local_contact);
goto done;
} else
/* no contact username needed, we have our valid Contact header based on the server_address */
goto done;
}
} else {
msg_contact:
if (msg) {
memset(&ct_uri, 0, sizeof(struct sip_uri));
if (contact_user && parse_uri(from_uri->s, from_uri->len, &ct_uri) < 0) {
LM_ERR("Not a valid sip uri [%.*s]\n", from_uri->len, from_uri->s);
return -1;
}
get_local_contact(send_sock, &ct_uri.user, local_contact);
} else {
LM_ERR("'server_address' not defined and no current SIP message\n");
return -1;
}
}
done:
return 0;
}
b2bl_entity_id_t *b2bl_new_client(client_info_t *ci, b2bl_tuple_t *tuple,
str *ssid, str *adv_ct, struct sip_msg *msg)
{
str* client_id;
b2bl_entity_id_t* entity;
ci->method = method_invite;
ci->send_sock = msg ? msg->force_send_socket : NULL;
ci->pref_sock = msg ? msg->rcv.bind_address : NULL;
if (adv_ct) {
ci->local_contact = *adv_ct;
} else if (b2b_get_local_contact(msg, &ci->from_uri, &ci->local_contact) < 0) {
LM_ERR("Failed to build Contact\n");
return NULL;
}
if(msg)
{
if (str2int( &(get_cseq(msg)->number), &ci->cseq)!=0 )
{
LM_ERR("cannot parse cseq number\n");
return NULL;
}
}
client_id = b2b_api.client_new(ci, b2b_client_notify, b2b_add_dlginfo,
&b2bl_mod_name, tuple->key, get_tracer(tuple), NULL, NULL);
if(client_id == NULL)
{
LM_ERR("Failed to create client id\n");
return NULL;
}
/* save the client_id in the structure */
entity = b2bl_create_new_entity(B2B_CLIENT, client_id, &ci->to_uri, 0,
&ci->from_uri, 0, ssid, ci->client_headers, adv_ct, 0);
if(entity == NULL)
{
LM_ERR("failed to create new client entity\n");
pkg_free(client_id);
return NULL;
}
pkg_free(client_id);
return entity;
}
int post_cb_sanity_check(b2bl_tuple_t **tuple, unsigned int hash_index, unsigned int local_index,
b2bl_entity_id_t **entity, int etype, str *ekey)
{
int index;
int not_found = 1;
b2bl_entity_id_t *e;
*tuple = b2bl_search_tuple_safe(hash_index, local_index);
if(*tuple == NULL)
{
LM_DBG("B2B logic record doesn't exist after B2B_BYE_CB\n");
return -1;
}
if(ekey == NULL)
{
LM_DBG("entity key does not exist!\n");
return -1;
}
if(etype == B2B_SERVER)
{
for (index = 0; index < MAX_B2BL_ENT && not_found; index++)
{
e = (*tuple)->servers[index];
while (e)
{
if(e == *entity && e->key.len == ekey->len &&
strncmp(e->key.s, ekey->s, ekey->len)==0)
{
not_found = 0;
break;
}
e = e->next;
}
}
if(not_found)
{
LM_DBG("Server Entity does not exist anymore\n");
return -2;
}
else
{
return 0;
}
}
else
if(etype == B2B_CLIENT)
{
for (index = 0; index < MAX_B2BL_ENT && not_found; index++)
{
e = (*tuple)->clients[index];
while (e)
{
LM_DBG("[%p] vs [%p]\n", e, *entity);
LM_DBG("[%.*s] vs [%.*s]\n", e->key.len, e->key.s, ekey->len, ekey->s);
if(e == *entity && e->key.len == ekey->len &&
strncmp(e->key.s, ekey->s, ekey->len)==0)
{
not_found = 0;
break;
}
e = e->next;
}
}
if(not_found)
{
LM_DBG("Client Entity does not exist anymore\n");
return -3;
}
else
{
return 0;
}
}
else
{
LM_ERR("Unexpected entity type [%d]\n", etype);
return -4;
}
return -5;
}
int run_init_negreply_cb(struct sip_msg *msg, b2bl_tuple_t *tuple,
b2bl_entity_id_t *entity)
{
b2bl_cback_f cbf = NULL;
str ekey= {NULL, 0};
b2bl_cb_params_t cb_params;
b2bl_dlg_stat_t stats;
int ret;
int entity_no;
int etype;
/* call the callback for brigding failure */
cbf = tuple->cb.f;
if(cbf && (tuple->cb.mask&B2B_REJECT_CB))
{
etype = entity->type;
entity_no = bridge_get_entityno(tuple, entity);
memset(&cb_params, 0, sizeof(b2bl_cb_params_t));
cb_params.param = tuple->cb.param;
stats.start_time = entity->stats.start_time;
stats.setup_time = get_ticks() - entity->stats.start_time;
stats.key.s = NULL; stats.key.len = 0;
cb_params.stat = &stats;
ekey.s = (char*)pkg_malloc(entity->key.len);
if(ekey.s == NULL)
{
LM_ERR("No more memory\n");
return -1;
}
memcpy(ekey.s, entity->key.s, entity->key.len);
ekey.len = entity->key.len;
cb_params.msg = msg;
cb_params.entity = entity_no;
cb_params.key = tuple->key;
B2BL_LOCK_RELEASE(cur_route_ctx.hash_index);
ret = cbf(&cb_params, B2B_REJECT_CB);
LM_DBG("ret = %d\n", ret);
B2BL_LOCK_GET(cur_route_ctx.hash_index);
/* must search the tuple again
* you can't know what might have happened with it */
if (0!=post_cb_sanity_check(&tuple, cur_route_ctx.hash_index,
cur_route_ctx.local_index, &entity, etype, &ekey))
{
pkg_free(ekey.s);
return 1;
}
pkg_free(ekey.s);
if(ret == B2B_DROP_MSG_CB_RET)
{
/* drop the negative reply */
if(entity_no == 1)
b2bl_delete_entity(entity, tuple, cur_route_ctx.hash_index, 1);
return 1;
}
}
return 0;
}
str *b2b_scenario_hdrs(struct b2bl_new_entity *entity);
int retry_init_bridge(struct sip_msg *msg, b2bl_tuple_t* tuple,
b2bl_entity_id_t *entity, struct b2bl_new_entity *new_entity)
{
str *client_id= NULL;
str method = {INVITE, INVITE_LEN};
b2bl_entity_id_t* client_entity = NULL;
client_info_t ci;
str *hdrs;
struct sip_uri ct_uri;
b2bl_delete_entity(entity, tuple, tuple->hash_index, 1);
hdrs = b2b_scenario_hdrs(new_entity);
memset(&ci, 0, sizeof(client_info_t));
ci.method = method;
ci.req_uri = new_entity->dest_uri;
ci.to_uri = tuple->bridge_entities[0]->to_uri;
ci.dst_uri = new_entity->proxy;
ci.from_uri = tuple->bridge_entities[0]->from_uri;
ci.from_dname = tuple->bridge_entities[0]->from_dname;
ci.extra_headers = tuple->extra_headers;
ci.client_headers= hdrs;
ci.body = &tuple->bridge_entities[0]->in_sdp;
ci.send_sock = msg ? msg->force_send_socket : NULL;
ci.pref_sock = msg ? msg->rcv.bind_address : NULL;
ci.maxfwd = tuple->bridge_entities[0]->init_maxfwd;
if (new_entity->adv_contact.s) {
ci.local_contact = new_entity->adv_contact;
} else {
if (ci.send_sock) {
memset(&ct_uri, 0, sizeof(struct sip_uri));
if (contact_user && parse_uri(ci.from_uri.s, ci.from_uri.len, &ct_uri) < 0)
{
LM_ERR("Not a valid sip uri [%.*s]\n", ci.from_uri.len, ci.from_uri.s);
goto error;
}
get_local_contact(ci.send_sock, &ct_uri.user, &ci.local_contact);
} else {
ci.local_contact = tuple->local_contact;
}
}
client_id = b2b_api.client_new(&ci, b2b_client_notify, b2b_add_dlginfo,
&b2bl_mod_name, tuple->key, get_tracer(tuple), NULL, NULL);
if(client_id == NULL)
{
LM_ERR("failed to create new b2b client instance\n");
goto error;
}
client_entity = b2bl_create_new_entity(B2B_CLIENT, client_id,
&new_entity->dest_uri, 0, &tuple->bridge_entities[0]->from_uri, 0,
new_entity->id.s ? &new_entity->id : NULL, hdrs,
new_entity->adv_contact.s ? &new_entity->adv_contact : NULL, 0);
if(client_entity == NULL)
{
LM_ERR("failed to create new client entity\n");
pkg_free(client_id);
goto error;
}
pkg_free(client_id);
if (0 != b2bl_add_client(tuple, client_entity))
goto error;
client_entity->no = 1;
tuple->bridge_entities[1] = tuple->clients[0];
if (shm_str_dup(&client_entity->out_sdp,
&tuple->bridge_entities[0]->in_sdp) < 0) {
LM_ERR("Failed to save SDP\n");
goto error;
}
tuple->bridge_entities[0]->peer = tuple->bridge_entities[1];
tuple->bridge_entities[1]->peer = tuple->bridge_entities[0];
return 0;
error:
return -1;
}
#define SEND_REPLY_TO_PEER_OR_GOTO_DONE \
do{ \
memset(&rpl_data, 0, sizeof(b2b_rpl_data_t)); \
rpl_data.et =peer->type; \
rpl_data.b2b_key =&peer->key; \
rpl_data.method =method_value; \
rpl_data.code =statuscode; \
rpl_data.text =&msg->first_line.u.reply.reason; \
rpl_data.body = cur_route_ctx.body->s?cur_route_ctx.body:NULL; \
rpl_data.extra_headers = \
cur_route_ctx.extra_headers->s?cur_route_ctx.extra_headers:NULL;\
rpl_data.dlginfo =peer->dlginfo; \
if(b2b_api.send_reply(&rpl_data) < 0) \
{ \
LM_ERR("Sending reply failed - %d, [%.*s]\n", \
statuscode, peer->key.len, peer->key.s);\
goto done; \
} \
}while(0)
static int ack_and_term_entity(b2bl_tuple_t *tuple, b2bl_entity_id_t *entity,
unsigned int statuscode)
{
b2b_req_data_t req_data;
if (statuscode >= 200 && statuscode < 300) {
memset(&req_data, 0, sizeof(b2b_req_data_t));
PREP_REQ_DATA(entity);
req_data.method = &str_init("ACK");
b2b_api.send_request(&req_data);
}
memset(&req_data, 0, sizeof(b2b_req_data_t));
PREP_REQ_DATA(entity);
req_data.method = &str_init("BYE");
b2b_api.send_request(&req_data);
entity->disconnected = 1;
return 0;
}
int _b2b_handle_reply(struct sip_msg *msg, b2bl_tuple_t *tuple,
b2bl_entity_id_t *entity, b2bl_entity_id_t **entity_head)
{
str method;
b2bl_entity_id_t *peer, *e, *ent;
int statuscode;
int ret;
unsigned int method_value;
b2bl_cback_f cbf = NULL;
str ekey= {NULL, 0};
b2bl_cb_params_t cb_params;
b2b_rpl_data_t rpl_data;
b2b_req_data_t req_data;
b2b_dlginfo_t dlginfo;
int do_unlock = 0;
static str method_ack = {ACK, ACK_LEN};
if (!tuple) {
B2BL_LOCK_GET(cur_route_ctx.hash_index);
do_unlock = 1;
tuple = b2bl_search_tuple_safe(cur_route_ctx.hash_index,
cur_route_ctx.local_index);
if(tuple == NULL)
{
LM_ERR("B2B logic record not found\n");
goto error;
}
entity = b2bl_search_entity(tuple, &cur_route_ctx.entity_key,
cur_route_ctx.entity_type, &entity_head);
if(entity == NULL)
{
LM_ERR("No b2b_key match found [%.*s], src=%d\n",
cur_route_ctx.entity_key.len, cur_route_ctx.entity_key.s,
cur_route_ctx.entity_type);
goto error;
}
if (entity->no > 1)
{
LM_ERR("unexpected entity->no [%d] for tuple [%p]\n", entity->no, tuple);
goto error;
}
LM_DBG("b2b_entity key = %.*s\n",
cur_route_ctx.entity_key.len, cur_route_ctx.entity_key.s);
}
method = get_cseq(msg)->method;
if(parse_method(method.s, method.s+method.len, &method_value) == NULL)
{
LM_ERR("Failed to parse method\n");
goto error;
}
statuscode = msg->first_line.u.reply.statuscode;
peer = entity->peer;
if (IS_BRIDGING_STATE(tuple->state)) {
LM_DBG("Received a reply [%d] while in BRIDGING scenario\n",
statuscode);
/* if the scenario state is B2B_BRIDGING_STATE -> we should have a reply for INVITE */
if(method_value != METHOD_INVITE)
{
LM_ERR("Wrong scenario state [B2B_BRIDGING_STATE] for this"
" reply(for method %d)\n", method_value);
goto error;
}
/* Reply from new bridge entity */
if(statuscode >= 200 &&
entity == (tuple->bridge_entities[2]?tuple->bridge_entities[2]:tuple->bridge_entities[1]) &&
tuple->bridge_flags & B2BL_BR_FLAG_NOTIFY && tuple->bridge_initiator != 0)
{
send_bridge_notify(tuple->bridge_initiator, cur_route_ctx.hash_index, msg);
if(statuscode == 200 || !(tuple->bridge_flags & B2BL_BR_FLAG_RETURN_AFTER_FAILURE))
{
if (!(tuple->bridge_flags & B2BL_BR_FLAG_DONT_DELETE_BRIDGE_INITIATOR)) {
b2b_end_dialog(tuple->bridge_initiator, tuple, tuple->hash_index);
tuple->bridge_initiator = 0;
}
}
}
/* if a negative reply */
if(statuscode >= 300)
{
if ((tuple->bridge_flags & B2BL_BR_FLAG_RENEW_SDP) && statuscode == 491) {
/* it is very likely that the new entity is trying to send itself a re-INVITE
* to lock down the codecs, therefore we no longer need this step - thus, for now,
* we simply ACK the ongoing bridging entity, and arm a re-negociation attempt
*/
memset(&req_data, 0, sizeof(b2b_req_data_t));
req_data.et = tuple->bridge_entities[0]->type;
req_data.b2b_key = &tuple->bridge_entities[0]->key;
req_data.method = &method_ack;
req_data.body = &tuple->bridge_entities[1]->in_sdp;
req_data.dlginfo = tuple->bridge_entities[0]->dlginfo;
b2b_api.send_request(&req_data);
if (b2bl_push_bridge_retry(tuple) == 0) {
tuple->bridge_flags |= B2BL_BR_FLAG_PENDING_SDP;
tuple->state = B2B_BRIDGED_STATE;
goto done;
}
/* else, fallback to rejecting the call */
}
entity->rejected = 1;
ret = process_bridge_negreply(tuple, tuple->hash_index, entity, msg);
if(ret < 0)
{
LM_ERR("Failed to process negative reply while in bridging state\n");
goto error;
}
else
if(ret == 1)
goto done1;
if(!entity->peer || !entity->peer->key.s)
{
LM_DBG("Delete this b2bl record\n");
b2bl_delete(tuple, tuple->hash_index, 1, 1);
tuple = 0;
}
goto done;
}
else
if(statuscode < 200)
{
goto done;
}
if(process_bridge_200OK(msg, tuple->extra_headers,
(cur_route_ctx.body->s?cur_route_ctx.body:0), tuple,
tuple->hash_index, entity)< 0)
{
LM_ERR("Failed to process bridging 200OK for Invite\n");
goto error;
}
goto done;
}