-
Notifications
You must be signed in to change notification settings - Fork 564
/
dlg.c
2965 lines (2657 loc) · 70.7 KB
/
dlg.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 entities modules
*
* 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)
* 2011-06-27 added authentication support (Ovidiu Sas)
*/
#include <stdio.h>
#include <stdlib.h>
#include "../../data_lump_rpl.h"
#include "../../parser/parse_rr.h"
#include "../../parser/contact/parse_contact.h"
#include "../../parser/parse_from.h"
#include "../../parser/parse_methods.h"
#include "../../parser/parse_content.h"
#include "../../parser/parse_authenticate.h"
#include "../../parser/sdp/sdp.h"
#include "../../locking.h"
#include "../../script_cb.h"
#include "../uac_auth/uac_auth.h"
#include "../presence/hash.h"
#include "../../action.h"
#include "../../trim.h"
#include "dlg.h"
#include "b2b_entities.h"
#include "b2be_db.h"
#define BUF_LEN 1024
str ack = str_init(ACK);
str bye = str_init(BYE);
/* used to make WRITE_THROUGH db mode more efficient */
b2b_dlg_t* current_dlg= NULL;
#define UPDATE_DBFLAG(dlg) do{ \
if(b2be_db_mode == WRITE_BACK && dlg->db_flag==NO_UPDATEDB_FLAG) \
dlg->db_flag = UPDATEDB_FLAG; \
} while(0)
void print_b2b_dlg(b2b_dlg_t *dlg)
{
dlg_leg_t *leg = dlg->legs;
LM_DBG("dlg[%p][%p][%p]: [%.*s] id=[%d] param=[%.*s] state=[%d] db_flag=[%d]\n",
dlg, dlg->prev, dlg->next, dlg->ruri.len, dlg->ruri.s,
dlg->id, dlg->param.len, dlg->param.s, dlg->state, dlg->db_flag);
LM_DBG(" from=[%.*s] [%.*s]\n",
dlg->from_dname.len, dlg->from_dname.s, dlg->from_uri.len, dlg->from_uri.s);
LM_DBG(" to=[%.*s] [%.*s]\n",
dlg->to_dname.len, dlg->to_dname.s, dlg->to_uri.len, dlg->to_uri.s);
LM_DBG("callid=[%.*s] tag=[%.*s][%.*s]\n",
dlg->callid.len, dlg->callid.s,
dlg->tag[0].len, dlg->tag[0].s, dlg->tag[1].len, dlg->tag[1].s);
while(leg)
{
LM_DBG("leg[%p][%p] id=[%d] tag=[%.*s] cseq=[%d]\n",
leg, leg->next, leg->id, leg->tag.len, leg->tag.s, leg->cseq);
leg = leg->next;
}
return;
}
b2b_dlg_t* b2b_search_htable_next_dlg(b2b_dlg_t* start_dlg, b2b_table table, unsigned int hash_index,
unsigned int local_index, str* to_tag, str* from_tag, str* callid)
{
b2b_dlg_t* dlg;
str dlg_from_tag={NULL, 0};
dlg_leg_t* leg;
LM_DBG("entering with start=%p, table=%p, hash=%d, label=%d \n",
start_dlg,table,hash_index,local_index);
if(callid)
LM_DBG("searching callid %d[%.*s]\n", callid->len,callid->len, callid->s);
if(to_tag)
LM_DBG("searching totag %d[%.*s]\n", to_tag->len,to_tag->len, to_tag->s);
if(from_tag)
LM_DBG("searching fromtag %d[%.*s]\n", from_tag->len,from_tag->len, from_tag->s);
dlg= start_dlg?start_dlg->next:table[hash_index].first;
while(dlg)
{
if(dlg->id != local_index)
{
dlg = dlg->next;
continue;
}
/*check if the dialog information correspond */
if(table == server_htable)
{
if(!from_tag || !callid)
return NULL;
dlg_from_tag= dlg->tag[CALLER_LEG];
/* check from tag and callid */
if(dlg_from_tag.len==from_tag->len &&
strncmp(dlg_from_tag.s, from_tag->s, dlg_from_tag.len)==0
&& dlg->callid.len==callid->len && strncmp(dlg->callid.s, callid->s, callid->len)==0)
{
LM_DBG("Match for server dlg [%p] dlg->uas_tran=[%p]\n",
dlg, dlg->uas_tran);
return dlg;
}
}
else
{
/*
LM_DBG("dialog totag [%.*s] with state %d\n",
dlg->tag[CALLER_LEG].len, dlg->tag[CALLER_LEG].s, dlg->state);
*/
/* it is an UAC dialog (callid is the key)*/
if(to_tag && dlg->tag[CALLER_LEG].len == to_tag->len &&
strncmp(dlg->tag[CALLER_LEG].s, to_tag->s, to_tag->len)== 0)
{
leg = dlg->legs;
if(dlg->state < B2B_CONFIRMED || dlg->state>=B2B_DESTROYED)
{
if(from_tag == NULL || from_tag->len==0 || leg==NULL)
{
LM_DBG("Match for client dlg [%p] last_method=%d"
" dlg->uac_tran=[%p]\n",
dlg, dlg->last_method, dlg->uac_tran);
return dlg;
}
}
if(from_tag == NULL || from_tag->s==NULL)
{
dlg = dlg->next;
continue;
}
/* if it is an already confirmed dialog match the to_tag also*/
while(leg)
{
if(leg->tag.len == from_tag->len &&
strncmp(leg->tag.s, from_tag->s, from_tag->len)== 0)
return dlg;
leg = leg->next;
}
if(dlg->state < B2B_CONFIRMED || dlg->state>=B2B_DESTROYED) /* state not confirmed yet and a new leg */
return dlg;
}
}
dlg = dlg->next;
}
return NULL;
}
b2b_dlg_t* b2b_search_htable_dlg(b2b_table table, unsigned int hash_index,
unsigned int local_index, str* to_tag, str* from_tag, str* callid)
{
return b2b_search_htable_next_dlg(NULL, table, hash_index, local_index,
to_tag, from_tag, callid);
}
b2b_dlg_t* b2b_search_htable_next(b2b_dlg_t* start_dlg, b2b_table table, unsigned int hash_index,
unsigned int local_index)
{
b2b_dlg_t* dlg;
dlg= start_dlg?start_dlg->next:table[hash_index].first;
while(dlg && dlg->id != local_index)
dlg = dlg->next;
if(dlg == NULL || dlg->id!=local_index)
{
LM_DBG("No dialog with hash_index=[%d] and local_index=[%d] found\n",
hash_index, local_index);
return NULL;
}
return dlg;
}
b2b_dlg_t* b2b_search_htable(b2b_table table, unsigned int hash_index,
unsigned int local_index)
{
return b2b_search_htable_next(NULL, table, hash_index, local_index);
}
/* this is only called by server new */
str* b2b_htable_insert(b2b_table table, b2b_dlg_t* dlg, int hash_index, int src, int reload)
{
b2b_dlg_t * it, *prev_it= NULL;
str* b2b_key;
if(!reload)
lock_get(&table[hash_index].lock);
dlg->prev = dlg->next = NULL;
it = table[hash_index].first;
if(it == NULL)
{
table[hash_index].first = dlg;
}
else
{
while(it)
{
prev_it = it;
it = it->next;
}
prev_it->next = dlg;
dlg->prev = prev_it;
}
/* if an insert in server_htable -> copy the b2b_key in the to_tag */
b2b_key = b2b_generate_key(hash_index, dlg->id);
if(b2b_key == NULL)
{
if(!reload)
lock_release(&table[hash_index].lock);
LM_ERR("Failed to generate b2b key\n");
return NULL;
}
if(src == B2B_SERVER)
{
dlg->tag[CALLEE_LEG].s = (char*)shm_malloc(b2b_key->len);
if(dlg->tag[CALLEE_LEG].s == NULL)
{
LM_ERR("No more shared memory\n");
if(!reload)
lock_release(&table[hash_index].lock);
return 0;
}
memcpy(dlg->tag[CALLEE_LEG].s, b2b_key->s, b2b_key->len);
dlg->tag[CALLEE_LEG].len = b2b_key->len;
if(!reload && b2be_db_mode == WRITE_THROUGH)
b2be_db_insert(dlg, src);
}
if(!reload)
lock_release(&table[hash_index].lock);
return b2b_key;
}
/* key format : B2B.hash_index.local_index.timestamp *
*/
int b2b_parse_key(str* key, unsigned int* hash_index, unsigned int* local_index)
{
char* p;
str s;
if(!key || !key->s)
return -1;
if(strncmp(key->s, b2b_key_prefix.s, b2b_key_prefix.len) != 0 ||
key->len<(b2b_key_prefix.len +4) || key->s[b2b_key_prefix.len]!='.')
{
LM_DBG("Does not have b2b_entities prefix\n");
return -1;
}
s.s = key->s + b2b_key_prefix.len+1;
p= strchr(s.s, '.');
if(p == NULL || ((p-s.s) > key->len) )
{
LM_DBG("Wrong format for b2b key\n");
return -1;
}
s.len = p - s.s;
if(str2int(&s, hash_index) < 0)
{
LM_DBG("Could not extract hash_index [%.*s]\n", key->len, key->s);
return -1;
}
p++;
s.s = p;
p= strchr(s.s, '.');
if(p == NULL || ((p-s.s) > key->len) )
{
LM_DBG("Wrong format for b2b key\n");
return -1;
}
s.len = p - s.s;
if(str2int(&s, local_index)< 0)
{
LM_DBG("Wrong format for b2b key\n");
return -1;
}
/* we do not really care about the third part of the key */
LM_DBG("hash_index = [%d] - local_index= [%d]\n", *hash_index, *local_index);
return 0;
}
str* b2b_generate_key(unsigned int hash_index, unsigned int local_index)
{
char buf[B2B_MAX_KEY_SIZE];
str* b2b_key;
int len;
len = sprintf(buf, "%s.%d.%d.%ld",
b2b_key_prefix.s, hash_index, local_index, startup_time+get_ticks());
b2b_key = (str*)pkg_malloc(sizeof(str)+ len);
if(b2b_key== NULL)
{
LM_ERR("no more private memory\n");
return NULL;
}
b2b_key->s = (char*)b2b_key + sizeof(str);
memcpy(b2b_key->s, buf, len);
b2b_key->len = len;
return b2b_key;
}
str* b2b_key_copy_shm(str* b2b_key)
{
str* b2b_key_shm = NULL;
b2b_key_shm = (str*)shm_malloc(sizeof(str)+ b2b_key->len);
if(b2b_key_shm== NULL)
{
LM_ERR("no more shared memory\n");
return 0;
}
b2b_key_shm->s = (char*)b2b_key_shm + sizeof(str);
memcpy(b2b_key_shm->s, b2b_key->s, b2b_key->len);
b2b_key_shm->len = b2b_key->len;
return b2b_key_shm;
}
b2b_dlg_t* b2b_dlg_copy(b2b_dlg_t* dlg)
{
b2b_dlg_t* new_dlg;
int size;
size = sizeof(b2b_dlg_t) + dlg->callid.len+ dlg->from_uri.len+ dlg->to_uri.len+
dlg->tag[0].len + dlg->tag[1].len+ dlg->route_set[0].len+ dlg->route_set[1].len+
dlg->contact[0].len+ dlg->contact[1].len+ dlg->ruri.len+ B2BL_MAX_KEY_LEN+
dlg->from_dname.len + dlg->to_dname.len;
new_dlg = (b2b_dlg_t*)shm_malloc(size);
if(new_dlg == 0)
{
LM_ERR("No more shared memory\n");
return 0;
}
memset(new_dlg, 0, size);
size = sizeof(b2b_dlg_t);
if(dlg->ruri.s)
CONT_COPY(new_dlg, new_dlg->ruri, dlg->ruri);
CONT_COPY(new_dlg, new_dlg->callid, dlg->callid);
CONT_COPY(new_dlg, new_dlg->from_uri, dlg->from_uri);
CONT_COPY(new_dlg, new_dlg->to_uri, dlg->to_uri);
if(dlg->tag[0].len && dlg->tag[0].s)
CONT_COPY(new_dlg, new_dlg->tag[0], dlg->tag[0]);
if(dlg->tag[1].len && dlg->tag[1].s)
CONT_COPY(new_dlg, new_dlg->tag[1], dlg->tag[1]);
if(dlg->route_set[0].len && dlg->route_set[0].s)
CONT_COPY(new_dlg, new_dlg->route_set[0], dlg->route_set[0]);
if(dlg->route_set[1].len && dlg->route_set[1].s)
CONT_COPY(new_dlg, new_dlg->route_set[1], dlg->route_set[1]);
if(dlg->contact[0].len && dlg->contact[0].s)
CONT_COPY(new_dlg, new_dlg->contact[0], dlg->contact[0]);
if(dlg->contact[1].len && dlg->contact[1].s)
CONT_COPY(new_dlg, new_dlg->contact[1], dlg->contact[1]);
if(dlg->param.s)
{
new_dlg->param.s= (char*)new_dlg+ size;
memcpy(new_dlg->param.s, dlg->param.s, dlg->param.len);
new_dlg->param.len= dlg->param.len;
size+= B2BL_MAX_KEY_LEN;
}
if(dlg->from_dname.s)
CONT_COPY(new_dlg, new_dlg->from_dname, dlg->from_dname);
if(dlg->to_dname.s)
CONT_COPY(new_dlg, new_dlg->to_dname, dlg->to_dname);
new_dlg->cseq[0] = dlg->cseq[0];
new_dlg->cseq[1] = dlg->cseq[1];
new_dlg->id = dlg->id;
new_dlg->state = dlg->state;
new_dlg->b2b_cback = dlg->b2b_cback;
new_dlg->add_dlginfo = dlg->add_dlginfo;
new_dlg->last_invite_cseq = dlg->last_invite_cseq;
new_dlg->db_flag = dlg->db_flag;
new_dlg->send_sock = dlg->send_sock;
return new_dlg;
}
void b2b_delete_legs(dlg_leg_t** legs)
{
dlg_leg_t* leg, *aux_leg;
leg = *legs;
while(leg)
{
aux_leg = leg->next;
shm_free(leg);
leg = aux_leg;
}
*legs = NULL;
}
char* DLG_FLAGS_STR(int type)
{
switch(type){
case DLGCB_EARLY: return "DLGCB_EARLY";
case DLGCB_REQ_WITHIN: return "DLGCB_EARLY";
case DLGCB_RESPONSE_WITHIN: return "DLGCB_RESPONSE_WITHIN";
}
return "Flag not known";
}
void set_dlg_state(b2b_dlg_t* dlg, int meth)
{
switch(meth)
{
case METHOD_INVITE:
if (dlg->state != B2B_NEW_AUTH)
dlg->state= B2B_MODIFIED;
break;
case METHOD_CANCEL:
case METHOD_BYE:
dlg->state= B2B_TERMINATED;
break;
case METHOD_ACK:
dlg->state= B2B_ESTABLISHED;
break;
default:
break;
}
}
b2b_dlg_t* b2bl_search_iteratively(str* callid, str* from_tag, str* ruri,
unsigned int hash_index)
{
b2b_dlg_t* dlg= NULL;
LM_DBG("Search for record with callid= %.*s, tag= %.*s\n",
callid->len, callid->s, from_tag->len, from_tag->s);
/* must search iteratively */
lock_get(&server_htable[hash_index].lock);
dlg = server_htable[hash_index].first;
while(dlg)
{
LM_DBG("Found callid= %.*s, tag= %.*s\n", dlg->callid.len, dlg->callid.s,
dlg->tag[CALLER_LEG].len, dlg->tag[CALLER_LEG].s);
if(dlg->callid.len == callid->len && strncmp(dlg->callid.s, callid->s, callid->len)== 0 &&
dlg->tag[CALLER_LEG].len == from_tag->len &&
strncmp(dlg->tag[CALLER_LEG].s, from_tag->s, from_tag->len)== 0)
{
if(!ruri)
break;
if(ruri->len == dlg->ruri.len && strncmp(ruri->s, dlg->ruri.s, ruri->len)== 0)
break;
}
dlg = dlg->next;
}
return dlg;
}
int b2b_prescript_f(struct sip_msg *msg, void *uparam)
{
str b2b_key;
b2b_dlg_t* dlg = 0, *aux_dlg;
unsigned int hash_index, local_index;
b2b_notify_t b2b_cback;
str param= {NULL,0};
b2b_table table = NULL;
int method_value;
str from_tag;
str to_tag;
str callid;
struct cell* tm_tran;
int ret;
str host;
int port;
int etype= B2B_NONE;
int dlg_state = 0;
struct sip_uri puri;
struct hdr_field *route_hdr;
rr_t *rt;
/* check if a b2b request */
if (parse_headers(msg, HDR_EOH_F, 0) < 0)
{
LM_ERR("failed to parse message\n");
return SCB_RUN_ALL;
}
LM_DBG("start - method = %.*s\n", msg->first_line.u.request.method.len,
msg->first_line.u.request.method.s);
if(msg->route)
{
LM_DBG("Found route headers\n");
route_hdr = msg->route;
/* we accept Route hdrs only if preloaded route with out IPs */
if (parse_rr(route_hdr) < 0) {
LM_ERR("failed to parse Route HF\n");
return SCB_RUN_ALL;
}
rt = (rr_t*)route_hdr->parsed;
/* check if first route is local*/
if ( parse_uri(rt->nameaddr.uri.s,rt->nameaddr.uri.len,&puri)!=0 ) {
LM_ERR("Route uri is not valid <%.*s>\n",
rt->nameaddr.uri.len,rt->nameaddr.uri.s);
return SCB_RUN_ALL;
}
if (check_self( &puri.host, puri.port_no?puri.port_no:SIP_PORT,
puri.proto?puri.proto:PROTO_UDP)!= 1 ) {
LM_DBG("First Route uri is not mine\n");
return SCB_RUN_ALL; /* not for b2b */
}
/* check if second route is local*/
rt = rt->next;
if (rt==NULL) {
if (msg->route->sibling) {
route_hdr = msg->route->sibling;
if (parse_rr(route_hdr) < 0) {
LM_ERR("failed to parse second Route HF\n");
return SCB_RUN_ALL;
}
rt = (rr_t*)route_hdr->parsed;
}
}
if (rt) {
if ( parse_uri(rt->nameaddr.uri.s,rt->nameaddr.uri.len,&puri)!=0 ) {
LM_ERR("Second route uri is not valid <%.*s>\n",
rt->nameaddr.uri.len,rt->nameaddr.uri.s);
return SCB_RUN_ALL;
}
if (check_self( &puri.host, puri.port_no?puri.port_no:SIP_PORT,
puri.proto?puri.proto:PROTO_UDP)!= 1 ) {
LM_DBG("Second Route uri is not mine\n");
return SCB_RUN_ALL; /* not for b2b */
}
/* check the presence of the third route */
if (rt->next || route_hdr->sibling) {
LM_DBG("More than 2 route hdr -> not for me\n");
return SCB_RUN_ALL; /* not for b2b */
}
}
/* "route" hdr checking is ok, continue */
}
method_value = msg->first_line.u.request.method_value;
if(method_value == METHOD_ACK)
{
goto search_dialog;
}
if(parse_sip_msg_uri(msg)< 0)
{
LM_ERR("Failed to parse uri\n");
return SCB_RUN_ALL;
}
host = msg->parsed_uri.host;
port = msg->parsed_uri.port_no;
/* check if RURI points to me */
if(method_value!= METHOD_CANCEL)
{
LM_DBG("<uri> host:port [%.*s][%d]\n", host.len, host.s, port);
if (!check_self( &host, port ? port : SIP_PORT, msg->rcv.proto))
{
LM_DBG("RURI does not point to me\n");
return SCB_RUN_ALL;
}
}
if(method_value == METHOD_PRACK)
{
LM_DBG("Received a PRACK - send 200 reply\n");
str reason={"OK", 2};
/* send 200 OK and exit */
tmb.t_reply(msg, 200, &reason);
tm_tran = tmb.t_gett();
if(tm_tran && tm_tran!=T_UNDEFINED)
tmb.unref_cell(tm_tran);
/* No need to apply lumps */
if(req_routeid > 0)
run_top_route(rlist[req_routeid].a, msg);
goto done;
}
search_dialog:
if( msg->callid==NULL || msg->callid->body.s==NULL)
{
LM_ERR("no callid header found\n");
return SCB_RUN_ALL;
}
/* examine the from header */
if (!msg->from || !msg->from->body.s)
{
LM_ERR("cannot find 'from' header!\n");
return SCB_RUN_ALL;
}
if (msg->from->parsed == NULL)
{
if ( parse_from_header( msg )<0 )
{
LM_ERR("cannot parse From header\n");
return SCB_RUN_ALL;
}
}
callid = msg->callid->body;
from_tag = ((struct to_body*)msg->from->parsed)->tag_value;
if (from_tag.len==0 || from_tag.s==NULL) {
LM_ERR("From header has no TAG parameter\n");
return SCB_RUN_ALL;
}
/* if a CANCEL request - search iteratively in the server_htable*/
if(method_value == METHOD_CANCEL)
{
/*str ruri= msg->first_line.u.request.uri;*/
str reply_text={"canceling", 9};
/* This makes no sense - why not accepting a CANCEL that was
generated by other b2b instance ? or ourselves ? - bogdan
if(b2b_parse_key(&callid, &hash_index, &local_index) >= 0)
{
LM_DBG("received a CANCEL message that I sent\n");
return SCB_RUN_ALL;
}
*/
hash_index = core_hash(&callid, &from_tag, server_hsize);
/* As per RFC3261, the RURI must be used when matching the CANCEL
against the INVITE, but we should not do it here as B2B learns
a RURI that may have been changed in script (before invoking the
B2B module), while the CANCEL has the original RURI (as received)
*/
dlg = b2bl_search_iteratively(&callid, &from_tag, NULL/*&ruri*/, hash_index);
if(dlg == NULL)
{
lock_release(&server_htable[hash_index].lock);
LM_DBG("No dialog found for cancel\n");
return SCB_RUN_ALL;
}
table = server_htable;
/* send 200 canceling */
ret = tmb.t_newtran(msg);
if(ret < 1)
{
if(ret== 0)
{
LM_DBG("It is a retransmission, drop\n");
}
else
LM_DBG("Error when creating tm transaction\n");
lock_release(&server_htable[hash_index].lock);
return SCB_DROP_MSG;
}
if(tmb.t_reply(msg, 200, &reply_text) < 0)
{
LM_ERR("failed to send reply for CANCEL\n");
lock_release(&server_htable[hash_index].lock);
return SCB_RUN_ALL;
}
tmb.unref_cell(tmb.t_gett());
b2b_key = dlg->tag[CALLEE_LEG];
goto logic_notify;
}
/* we are interested only in request inside dialog */
/* examine the to header */
if (msg->to==NULL || msg->to->parsed == NULL ||
((struct to_body *)msg->to->parsed)->error != PARSE_OK )
{
LM_DBG("'To' header COULD NOT parsed\n");
return SCB_DROP_MSG;
}
to_tag = get_to(msg)->tag_value;
if(to_tag.s == NULL || to_tag.len == 0)
{
LM_DBG("Not an inside dialog request- not interested.\n");
return SCB_RUN_ALL;
}
b2b_key = to_tag;
/* check if the to tag has the b2b key format -> meaning that it is a server request */
if(b2b_key.s && b2b_parse_key(&b2b_key, &hash_index, &local_index) >= 0)
{
LM_DBG("Received a b2b server request [%.*s]\n",
msg->first_line.u.request.method.len,
msg->first_line.u.request.method.s);
table = server_htable;
}
else
{
/* check if the callid is in b2b format -> meaning that this is a client request */
b2b_key = msg->callid->body;
if(b2b_parse_key(&b2b_key, &hash_index, &local_index) >= 0)
{
LM_DBG("received a b2b client request [%.*s]\n",
msg->first_line.u.request.method.len,
msg->first_line.u.request.method.s);
table = client_htable;
}
else /* if also not a client request - not for us */
{
if(method_value != METHOD_UPDATE)
{
LM_DBG("Not a b2b request\n");
return SCB_RUN_ALL;
}
else
{
/* for server UPDATE sent before dialog confirmed */
table = server_htable;
hash_index = core_hash(&callid, &from_tag, server_hsize);
dlg = b2bl_search_iteratively(&callid, &from_tag,NULL, hash_index);
if(dlg == NULL)
{
lock_release(&server_htable[hash_index].lock);
LM_DBG("No dialog found for cancel\n");
return SCB_RUN_ALL;
}
}
}
}
if(!dlg)
{
lock_get(&table[hash_index].lock);
dlg = b2b_search_htable_dlg(table, hash_index, local_index,
&to_tag, &from_tag, &callid);
if(dlg== NULL)
{
LM_DBG("No dialog found\n");
if(method_value != METHOD_ACK)
{
str ok = str_init("OK");
if(method_value == METHOD_BYE)
tmb.t_reply(msg, 200, &ok);
else
LM_ERR("No dialog found, callid= [%.*s], method=%.*s\n",
callid.len, callid.s,msg->first_line.u.request.method.len,
msg->first_line.u.request.method.s);
}
lock_release(&table[hash_index].lock);
return SCB_RUN_ALL;
}
}
if(dlg->state < B2B_CONFIRMED)
{
if(method_value != METHOD_UPDATE)
{
LM_DBG("I can not accept requests if the state is not confimed\n");
lock_release(&table[hash_index].lock);
return SCB_DROP_MSG;
}
}
LM_DBG("Received request method[%.*s] for dialog[%p]\n",
msg->first_line.u.request.method.len,
msg->first_line.u.request.method.s, dlg);
if(method_value == METHOD_ACK && dlg->state == B2B_ESTABLISHED)
{
LM_DBG("It is a ACK retransmission, drop\n");
lock_release(&table[hash_index].lock);
return SCB_DROP_MSG;
}
logic_notify:
etype = (table==server_htable?B2B_SERVER:B2B_CLIENT);
if(req_routeid > 0)
{
lock_release(&table[hash_index].lock);
run_top_route(rlist[req_routeid].a, msg);
if (b2b_apply_lumps(msg))
{
if (parse_from_header(msg) < 0)
{
LM_ERR("cannot parse From header\n");
return SCB_DROP_MSG;
}
callid = msg->callid->body;
from_tag = ((struct to_body*)msg->from->parsed)->tag_value;
to_tag = get_to(msg)->tag_value;
if (table == server_htable)
{
if (method_value != METHOD_CANCEL)
b2b_key = to_tag;
}
else
{
b2b_key = msg->callid->body;
}
}
lock_get(&table[hash_index].lock);
}
if(method_value != METHOD_CANCEL)
{
ret = tmb.t_newtran(msg);
if(ret < 1 && method_value != METHOD_ACK)
{
if(ret== 0)
{
LM_DBG("It is a retransmission, drop\n");
}
else
LM_DBG("Error when creating tm transaction\n");
lock_release(&table[hash_index].lock);
return SCB_DROP_MSG;
}
tm_tran = tmb.t_gett();
if(method_value != METHOD_ACK)
{
if(method_value == METHOD_UPDATE)
{
dlg->update_tran = tm_tran;
}
else
{
if(method_value == METHOD_INVITE || method_value == METHOD_BYE)
{
tmb.t_setkr(REQ_FWDED);
}
if(dlg->uas_tran && dlg->uas_tran!=T_UNDEFINED)
{
if(dlg->uas_tran->uas.request)
/* there is another transaction for which no reply
* was sent out */
{
/* send reply */
LM_DBG("Received another request when the previous "
"one was in process\n");
str text = str_init("Request Pending");
if(tmb.t_reply_with_body( tm_tran, 491,
&text, 0, 0, &to_tag) < 0)
{
LM_ERR("failed to send reply with tm\n");
}
LM_DBG("Sent reply [491] and unreffed the cell %p\n",
tm_tran);
}
tmb.unref_cell(tm_tran); /* for t_newtran() */
lock_release(&table[hash_index].lock);
return SCB_DROP_MSG;
}
dlg->uas_tran = tm_tran;
LM_DBG("Saved uas_tran=[%p] for dlg[%p]\n", tm_tran, dlg);
}
}
else
{
if(!tm_tran || tm_tran==T_UNDEFINED)
tm_tran = tmb.t_get_e2eackt();
if(tm_tran && tm_tran!=T_UNDEFINED)
tmb.unref_cell(tm_tran);
}
}
b2b_cback = dlg->b2b_cback;
if(dlg->param.s)
{
param.s = (char*)pkg_malloc(dlg->param.len);
if(param.s == NULL)
{
LM_ERR("No more private memory\n");
lock_release(&table[hash_index].lock);
return SCB_RUN_ALL;
}
memcpy(param.s, dlg->param.s, dlg->param.len);
param.len = dlg->param.len;
}
set_dlg_state( dlg, method_value);
UPDATE_DBFLAG(dlg);
/*
if(b2be_db_mode == WRITE_THROUGH && dlg->state>B2B_CONFIRMED)
{
if(b2be_db_update(dlg, etype) < 0)
LM_ERR("Failed to update in database\n");
}
*/
current_dlg = dlg;
dlg_state = dlg->state;
lock_release(&table[hash_index].lock);
b2b_cback(msg, &b2b_key, B2B_REQUEST, param.s?¶m:0);
if(param.s)
pkg_free(param.s);
done:
current_dlg = 0;
if(b2be_db_mode == WRITE_THROUGH && etype!=B2B_NONE && dlg_state>B2B_CONFIRMED)
{
/* search the dialog */
lock_get(&table[hash_index].lock);
for(aux_dlg = table[hash_index].first; aux_dlg; aux_dlg = aux_dlg->next)
{
if(aux_dlg == dlg)
break;
}
if(!aux_dlg)
{
LM_DBG("Record not found anymore\n");
lock_release(&table[hash_index].lock);
return SCB_DROP_MSG;
}
if(b2be_db_update(dlg, etype) < 0)
LM_ERR("Failed to update in database\n");
lock_release(&table[hash_index].lock);
}
return SCB_DROP_MSG;
}
int init_b2b_htables(void)
{
int i;
server_htable = (b2b_table)shm_malloc(server_hsize* sizeof(b2b_entry_t));
client_htable = (b2b_table)shm_malloc(client_hsize* sizeof(b2b_entry_t));
if(!server_htable || !client_htable)
ERR_MEM(SHARE_MEM);
memset(server_htable, 0, server_hsize* sizeof(b2b_entry_t));
memset(client_htable, 0, client_hsize* sizeof(b2b_entry_t));
for(i= 0; i< server_hsize; i++)
{
lock_init(&server_htable[i].lock);
}
for(i= 0; i< client_hsize; i++)
{
lock_init(&client_htable[i].lock);
}
return 0;
error:
return -1;
}
void destroy_b2b_htables(void)
{
int i;
b2b_dlg_t* dlg, *aux;
if(server_htable)
{
for(i= 0; i< server_hsize; i++)
{
lock_destroy(&server_htable[i].lock);
dlg = server_htable[i].first;
while(dlg)
{
aux = dlg->next;
if(dlg->tag[CALLEE_LEG].s)
shm_free(dlg->tag[CALLEE_LEG].s);
if(dlg->ack_sdp.s)
shm_free(dlg->ack_sdp.s);
shm_free(dlg);