-
Notifications
You must be signed in to change notification settings - Fork 909
/
dlg_handlers.c
1736 lines (1491 loc) · 44.6 KB
/
dlg_handlers.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
/*
* Copyright (C) 2006 Voice System SRL
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio 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
*
* Kamailio 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
*
*/
/*!
* \file
* \brief Functions related to dialog handling
* \ingroup dialog
* Module: \ref dialog
*/
#include <string.h>
#include <time.h>
#include "../../trim.h"
#include "../../pvar.h"
#include "../../timer.h"
#include "../../lib/kcore/statistics.h"
#include "../../action.h"
#include "../../script_cb.h"
#include "../../pt.h"
#include "../../fmsg.h"
#include "../../parser/parse_from.h"
#include "../../parser/parse_cseq.h"
#include "../../parser/contact/parse_contact.h"
#include "../../parser/parse_from.h"
#include "../../parser/parse_rr.h"
#include "../../modules/tm/tm_load.h"
#include "../rr/api.h"
#include "dlg_hash.h"
#include "dlg_timer.h"
#include "dlg_cb.h"
#include "dlg_cseq.h"
#include "dlg_handlers.h"
#include "dlg_req_within.h"
#include "dlg_db_handler.h"
#include "dlg_profile.h"
#include "dlg_var.h"
#include "dlg_dmq.h"
static str rr_param; /*!< record-route parameter for matching */
static int dlg_flag_mask=0; /*!< flag for dialog tracking */
static pv_spec_t *timeout_avp; /*!< AVP for timeout setting */
static int default_timeout; /*!< default dialog timeout */
static int seq_match_mode; /*!< dlg_match mode */
static int shutdown_done = 0; /*!< 1 when destroy_dlg_handlers was called */
extern int detect_spirals;
extern int dlg_timeout_noreset;
extern int initial_cbs_inscript;
extern int dlg_send_bye;
extern int dlg_event_rt[DLG_EVENTRT_MAX];
extern int dlg_wait_ack;
extern int dlg_enable_dmq;
int spiral_detected = -1;
extern struct rr_binds d_rrb; /*!< binding to record-routing module */
extern pv_elem_t *ruri_param_model; /*!< pv-string to get r-uri */
static unsigned int CURR_DLG_LIFETIME = 0; /*!< current dialog lifetime */
static unsigned int CURR_DLG_STATUS = 0; /*!< current dialog state */
static unsigned int CURR_DLG_ID = 0xffffffff; /*!< current dialog id */
/*! size of the dialog record-route parameter */
#define RR_DLG_PARAM_SIZE (2*2*sizeof(int)+3+MAX_DLG_RR_PARAM_NAME)
/*! separator inside the record-route paramter */
#define DLG_SEPARATOR '.'
int dlg_set_tm_callbacks(tm_cell_t *t, sip_msg_t *req, dlg_cell_t *dlg,
int mode);
int dlg_set_tm_waitack(tm_cell_t *t, dlg_cell_t *dlg);
/*!
* \brief Initialize the dialog handlers
* \param rr_param_p added record-route parameter
* \param dlg_flag_p dialog flag
* \param timeout_avp_p AVP for timeout setting
* \param default_timeout_p default timeout
* \param seq_match_mode_p matching mode
*/
void init_dlg_handlers(char *rr_param_p, int dlg_flag_p,
pv_spec_t *timeout_avp_p ,int default_timeout_p,
int seq_match_mode_p)
{
rr_param.s = rr_param_p;
rr_param.len = strlen(rr_param.s);
if(dlg_flag_p>=0) dlg_flag_mask = 1<<dlg_flag_p;
timeout_avp = timeout_avp_p;
default_timeout = default_timeout_p;
seq_match_mode = seq_match_mode_p;
}
/*!
* \brief Shutdown operation of the module
*/
void destroy_dlg_handlers(void)
{
shutdown_done = 1;
}
/*!
* \brief Add record-route parameter for dialog tracking
* \param req SIP request
* \param entry dialog hash entry
* \param id dialog hash id
* \return 0 on success, -1 on failure
*/
static inline int add_dlg_rr_param(struct sip_msg *req, unsigned int entry,
unsigned int id)
{
static char buf[RR_DLG_PARAM_SIZE];
str s;
int n;
char *p;
s.s = p = buf;
*(p++) = ';';
memcpy(p, rr_param.s, rr_param.len);
p += rr_param.len;
*(p++) = '=';
n = RR_DLG_PARAM_SIZE - (p-buf);
if (int2reverse_hex( &p, &n, entry)==-1)
return -1;
*(p++) = DLG_SEPARATOR;
n = RR_DLG_PARAM_SIZE - (p-buf);
if (int2reverse_hex( &p, &n, id)==-1)
return -1;
s.len = p-buf;
if (d_rrb.add_rr_param( req, &s)<0) {
LM_ERR("failed to add rr param\n");
return -1;
}
return 0;
}
/*!
* \brief Parse SIP message and populate leg informations
*
* Parse SIP message and populate leg informations.
* \param dlg the dialog to add cseq, contact & record_route
* \param msg sip message
* \param t transaction
* \param leg type of the call leg
* \param tag SIP To tag
* \return 0 on success, -1 on failure
* \note for a request: get record route in normal order, for a reply get
* in reverse order, skipping the ones from the request and the proxies' own
*/
int populate_leg_info( struct dlg_cell *dlg, struct sip_msg *msg,
struct cell* t, unsigned int leg, str *tag)
{
unsigned int skip_recs;
str cseq;
str contact;
str rr_set;
dlg->bind_addr[leg] = msg->rcv.bind_address;
/* extract the cseq number as string */
if (leg==DLG_CALLER_LEG) {
if((!msg->cseq && (parse_headers(msg,HDR_CSEQ_F,0)<0 || !msg->cseq))
|| !msg->cseq->parsed){
LM_ERR("bad sip message or missing CSeq hdr :-/\n");
goto error0;
}
cseq = (get_cseq(msg))->number;
} else {
/* use the same as in request */
cseq = dlg->cseq[DLG_CALLEE_LEG];
}
/* extract the contact address */
if (!msg->contact&&(parse_headers(msg,HDR_CONTACT_F,0)<0||!msg->contact)){
LM_ERR("bad sip message or missing Contact hdr\n");
goto error0;
}
if ( parse_contact(msg->contact)<0 ||
((contact_body_t *)msg->contact->parsed)->contacts==NULL ||
((contact_body_t *)msg->contact->parsed)->contacts->next!=NULL ) {
LM_ERR("bad Contact HDR\n");
goto error0;
}
contact = ((contact_body_t *)msg->contact->parsed)->contacts->uri;
/* extract the RR parts */
if(!msg->record_route && (parse_headers(msg,HDR_EOH_F,0)<0) ){
LM_ERR("failed to parse record route header\n");
goto error0;
}
if (leg==DLG_CALLER_LEG) {
skip_recs = 0;
} else {
/* was the 200 OK received or local generated */
skip_recs = dlg->from_rr_nb +
((t->relayed_reply_branch>=0)?
((t->uac[t->relayed_reply_branch].flags&TM_UAC_FLAG_R2)?2:
((t->uac[t->relayed_reply_branch].flags&TM_UAC_FLAG_RR)?1:0))
:0);
}
if(msg->record_route){
if( print_rr_body(msg->record_route, &rr_set, leg,
&skip_recs) != 0 ){
LM_ERR("failed to print route records \n");
goto error0;
}
} else {
rr_set.s = 0;
rr_set.len = 0;
}
if(leg==DLG_CALLER_LEG)
dlg->from_rr_nb = skip_recs;
LM_DBG("route_set %.*s, contact %.*s, cseq %.*s and bind_addr %.*s\n",
rr_set.len, rr_set.s, contact.len, contact.s,
cseq.len, cseq.s,
msg->rcv.bind_address->sock_str.len,
msg->rcv.bind_address->sock_str.s);
if (dlg_set_leg_info( dlg, tag, &rr_set, &contact, &cseq, leg)!=0) {
LM_ERR("dlg_set_leg_info failed\n");
if (rr_set.s) pkg_free(rr_set.s);
goto error0;
}
if (rr_set.s) pkg_free(rr_set.s);
return 0;
error0:
return -1;
}
/*!
* \brief Clone dialog internal unique id to shared memory
*/
dlg_iuid_t *dlg_get_iuid_shm_clone(dlg_cell_t *dlg)
{
dlg_iuid_t *iuid = NULL;
if(dlg==NULL)
return NULL;
iuid = (dlg_iuid_t*)shm_malloc(sizeof(dlg_iuid_t));
if(iuid==NULL)
{
LM_ERR("failed to clone dialog iuid\n");
return NULL;
}
memset(iuid, 0, sizeof(dlg_iuid_t));
iuid->h_entry = dlg->h_entry;
iuid->h_id = dlg->h_id;
return iuid;
}
/*!
* \brief Free dialog internal unique id stored in shared memory
*/
void dlg_iuid_sfree(void *iuid)
{
if(iuid) {
LM_DBG("freeing dlg iuid [%u:%u] (%p)\n",
((dlg_iuid_t*)iuid)->h_entry,
((dlg_iuid_t*)iuid)->h_id, iuid);
shm_free(iuid);
}
}
/*!
* \brief Function that executes BYE reply callbacks
* \param t transaction, unused
* \param type type of the callback, should be TMCB_RESPONSE_FWDED
* \param params saved dialog structure inside the callback
*/
static void dlg_terminated_confirmed(tm_cell_t *t, int type,
struct tmcb_params* params)
{
dlg_cell_t *dlg = NULL;
dlg_iuid_t *iuid = NULL;
if(!params || !params->req || !params->param)
{
LM_ERR("invalid parameters!\n");
return;
}
iuid = (dlg_iuid_t*)*params->param;
if(iuid==NULL)
return;
dlg = dlg_get_by_iuid(iuid);
if(dlg==NULL)
{
LM_ERR("failed to get dialog from params!\n");
return;
}
/* dialog termination confirmed (BYE reply) */
run_dlg_callbacks(DLGCB_TERMINATED_CONFIRMED,
dlg,
params->req,
params->rpl,
DLG_DIR_UPSTREAM,
0);
dlg_release(dlg);
}
/*!
* \brief Execute callback for the BYE request and register callback for the BYE reply
* \param req request message
* \param dlg corresponding dialog
* \param dir message direction
*/
static void dlg_terminated(sip_msg_t *req, dlg_cell_t *dlg, unsigned int dir)
{
dlg_iuid_t *iuid = NULL;
if(!req) {
LM_ERR("request is empty!");
return;
}
if(!dlg) {
LM_ERR("dialog is empty!");
return;
}
/* dialog terminated (BYE) */
run_dlg_callbacks(DLGCB_TERMINATED, dlg, req, NULL, dir, 0);
iuid = dlg_get_iuid_shm_clone(dlg);
if(iuid==NULL)
return;
/* register callback for the coresponding reply */
if (d_tmb.register_tmcb(req,
0,
TMCB_RESPONSE_OUT,
dlg_terminated_confirmed,
(void*)iuid,
dlg_iuid_sfree) <= 0 ) {
LM_ERR("cannot register response callback for BYE request\n");
return;
}
}
/*!
* \brief Function that is registered as TM callback and called on T destroy
*
* - happens when wait_ack==1
*
*/
static void dlg_ontdestroy(struct cell* t, int type, struct tmcb_params *param)
{
dlg_cell_t *dlg = NULL;
dlg_iuid_t *iuid = NULL;
iuid = (dlg_iuid_t*)(*param->param);
dlg = dlg_get_by_iuid(iuid);
if(dlg==0)
return;
/* 1 for callback and 1 for dlg lookup */
dlg_unref(dlg, 2);
}
/*!
* \brief Function that is registered as TM callback and called on replies
*
* Function that is registered as TM callback and called on replies. It
* parses the reply and set the appropriate event. This is then used to
* update the dialog state, run eventual dialog callbacks and save or
* update the necessary informations about the dialog.
* \see next_state_dlg
* \param t transaction, unused
* \param type type of the entered callback
* \param param saved dialog structure in the callback
*/
static void dlg_onreply(struct cell* t, int type, struct tmcb_params *param)
{
dlg_cell_t *dlg = NULL;
dlg_iuid_t *iuid = NULL;
int new_state, old_state, unref, event;
str tag;
sip_msg_t *req = param->req;
sip_msg_t *rpl = param->rpl;
if (shutdown_done)
return;
iuid = (dlg_iuid_t*)(*param->param);
dlg = dlg_get_by_iuid(iuid);
if(dlg==0)
return;
unref = 0;
if (type & (TMCB_RESPONSE_IN|TMCB_ON_FAILURE)) {
/* Set the dialog context so it is available in onreply_route and failure_route*/
set_current_dialog(req, dlg);
dlg_set_ctx_iuid(dlg);
goto done_early;
}
if (type==TMCB_RESPONSE_FWDED) {
/* The state does not change, but the msg is mutable in this callback*/
run_dlg_callbacks(DLGCB_RESPONSE_FWDED, dlg, req, rpl, DLG_DIR_UPSTREAM, 0);
goto done_early;
}
if (type==TMCB_DESTROY)
event = DLG_EVENT_TDEL;
else if (param->code<200)
event = DLG_EVENT_RPL1xx;
else if (param->code<300)
event = DLG_EVENT_RPL2xx;
else
event = DLG_EVENT_RPL3xx;
next_state_dlg( dlg, event, &old_state, &new_state, &unref);
dlg_run_event_route(dlg, (rpl==FAKED_REPLY)?NULL:rpl, old_state, new_state);
if (new_state==DLG_STATE_EARLY) {
run_dlg_callbacks(DLGCB_EARLY, dlg, req, rpl, DLG_DIR_UPSTREAM, 0);
if (old_state!=DLG_STATE_EARLY)
if_update_stat(dlg_enable_stats, early_dlgs, 1);
goto done;
}
if (new_state==DLG_STATE_CONFIRMED_NA &&
old_state!=DLG_STATE_CONFIRMED_NA && old_state!=DLG_STATE_CONFIRMED ) {
LM_DBG("dialog %p confirmed (ACK pending)\n",dlg);
if (rpl != FAKED_REPLY) {
/* get to tag*/
if ( !rpl->to && ((parse_headers(rpl, HDR_TO_F,0)<0)
|| !rpl->to) ) {
LM_ERR("bad reply or missing TO hdr :-/\n");
tag.s = 0;
tag.len = 0;
} else {
tag = get_to(rpl)->tag_value;
if (tag.s==0 || tag.len==0) {
LM_ERR("missing TAG param in TO hdr :-/\n");
tag.s = 0;
tag.len = 0;
}
}
/* save callee's tag, cseq, contact and record route*/
if (populate_leg_info( dlg, rpl, t, DLG_CALLEE_LEG, &tag) !=0) {
LM_ERR("could not add further info to the dialog\n");
}
} else {
LM_ERR("Faked reply!\n");
}
/* set start time */
dlg->start_ts = (unsigned int)(time(0));
/* save the settings to the database,
* if realtime saving mode configured- save dialog now
* else: the next time the timer will fire the update*/
dlg->dflags |= DLG_FLAG_NEW;
if ( dlg_db_mode==DB_MODE_REALTIME )
update_dialog_dbinfo(dlg);
if (0 != insert_dlg_timer( &dlg->tl, dlg->lifetime )) {
LM_CRIT("Unable to insert dlg %p [%u:%u] on event %d [%d->%d] "
"with clid '%.*s' and tags '%.*s' '%.*s'\n",
dlg, dlg->h_entry, dlg->h_id, event, old_state, new_state,
dlg->callid.len, dlg->callid.s,
dlg->tag[DLG_CALLER_LEG].len, dlg->tag[DLG_CALLER_LEG].s,
dlg->tag[DLG_CALLEE_LEG].len, dlg->tag[DLG_CALLEE_LEG].s);
} else {
/* dialog pointer inserted in timer list */
dlg_ref(dlg, 1);
}
/* dialog confirmed (ACK pending) */
run_dlg_callbacks( DLGCB_CONFIRMED_NA, dlg, req, rpl, DLG_DIR_UPSTREAM, 0);
if (old_state==DLG_STATE_EARLY)
if_update_stat(dlg_enable_stats, early_dlgs, -1);
if (unref) dlg_unref(dlg, unref);
if_update_stat(dlg_enable_stats, active_dlgs, 1);
goto done;
}
if(new_state==DLG_STATE_DELETED && old_state!=DLG_STATE_DELETED) {
/* set end time */
dlg->end_ts = (unsigned int)(time(0));
}
if ( new_state==DLG_STATE_DELETED
&& (old_state==DLG_STATE_UNCONFIRMED
|| old_state==DLG_STATE_EARLY) ) {
LM_DBG("dialog %p failed (negative reply)\n", dlg);
/* dialog setup not completed (3456XX) */
run_dlg_callbacks( DLGCB_FAILED, dlg, req, rpl, DLG_DIR_UPSTREAM, 0);
if(dlg_wait_ack==1)
dlg_set_tm_waitack(t, dlg);
/* do unref */
if (unref)
dlg_unref(dlg, unref);
if (old_state==DLG_STATE_EARLY)
if_update_stat(dlg_enable_stats, early_dlgs, -1);
if_update_stat(dlg_enable_stats, failed_dlgs, 1);
goto done;
}
if (unref) dlg_unref(dlg, unref);
done:
if(dlg_enable_dmq && (dlg->iflags & DLG_IFLAG_DMQ_SYNC) && new_state>old_state) {
dlg_dmq_replicate_action(DLG_DMQ_STATE, dlg, 0, 0);
}
done_early:
/* unref due to dlg_get_by_iuid() */
dlg_release(dlg);
return;
}
/*!
* \brief Helper function that run dialog callbacks on forwarded requests
* \see dlg_seq_up_onreply
* \see dlg_seq_down_onreply
* \param t transaction, unused
* \param type type of the callback, should be TMCB_RESPONSE_FWDED
* \param param saved dialog structure inside the callback
* \param direction direction of the request
*/
static void dlg_seq_onreply_helper(struct cell* t, int type,
struct tmcb_params *param, const int direction)
{
dlg_cell_t *dlg = NULL;
dlg_iuid_t *iuid = NULL;
if (shutdown_done)
return;
iuid = (dlg_iuid_t*)(*param->param);
dlg = dlg_get_by_iuid(iuid);
if (dlg==0)
return;
if (type==TMCB_RESPONSE_FWDED)
{
run_dlg_callbacks( DLGCB_RESPONSE_WITHIN,
dlg,
param->req,
param->rpl,
direction,
0);
}
dlg_release(dlg);
return;
}
/*!
* \brief Run dialog callbacks on forwarded requests in upstream direction
* \see dlg_seq_onreply_helper
* \param t transaction, unused
* \param type type of the callback, should be TMCB_RESPONSE_FWDED
* \param param saved dialog structure inside the callback
*/
static void dlg_seq_up_onreply(struct cell* t, int type, struct tmcb_params *param)
{
dlg_seq_onreply_helper(t, type, param, DLG_DIR_UPSTREAM);
}
/*!
* \brief Run dialog callbacks on forwarded requests in downstream direction
* \see dlg_seq_onreply_helper
* \param t transaction, unused
* \param type type of the callback, should be TMCB_RESPONSE_FWDED
* \param param saved dialog structure inside the callback
*/
static void dlg_seq_down_onreply(struct cell* t, int type, struct tmcb_params *param)
{
dlg_seq_onreply_helper(t, type, param, DLG_DIR_DOWNSTREAM);
}
/*!
* \brief Return the timeout for a dialog
* \param req SIP message
* \return value from timeout AVP if present or default timeout
*/
inline static int get_dlg_timeout(struct sip_msg *req)
{
pv_value_t pv_val;
if( timeout_avp ) {
if ( pv_get_spec_value( req, timeout_avp, &pv_val)==0 &&
pv_val.flags&PV_VAL_INT && pv_val.ri>0 ) {
return pv_val.ri;
}
LM_DBG("invalid AVP value, using default timeout\n");
}
return default_timeout;
}
/*!
* \brief Helper function to get the necessary content from SIP message
* \param req SIP request
* \param callid found callid
* \param ftag found from tag
* \param ttag found to tag
* \param with_ttag flag set if to tag must be found for success
* \return 0 on success, -1 on failure
*/
static inline int pre_match_parse( struct sip_msg *req, str *callid,
str *ftag, str *ttag, int with_ttag)
{
if (parse_headers(req,HDR_CALLID_F|HDR_TO_F,0)<0 || !req->callid ||
!req->to ) {
LM_ERR("bad request or missing CALLID/TO hdr :-/\n");
return -1;
}
if (get_to(req)->tag_value.len==0) {
if (with_ttag == 1) {
/* out of dialog request with preloaded Route headers; ignore. */
return -1;
} else {
ttag->s = NULL;
ttag->len = 0;
}
} else {
*ttag = get_to(req)->tag_value;
}
if (parse_from_header(req)<0 || get_from(req)->tag_value.len==0) {
LM_ERR("failed to get From header\n");
return -1;
}
/* callid */
*callid = req->callid->body;
trim(callid);
/* from tag */
*ftag = get_from(req)->tag_value;
return 0;
}
/*!
* \brief Sync dialog from tm callback (another wrapper)
* \param t transaction, unused
* \param type type of the entered callback
* \param param saved dialog structure in the callback
*/
static void dlg_on_send(struct cell* t, int type, struct tmcb_params *param)
{
dlg_cell_t *dlg = NULL;
dlg_iuid_t *iuid = NULL;
LM_DBG("dialog_on_send CB\n");
iuid = (dlg_iuid_t*)(*param->param);
if (iuid==NULL)
return;
dlg = dlg_get_by_iuid(iuid);
if(dlg==NULL)
return;
/* sync over dmq */
if (dlg_enable_dmq) {
dlg_dmq_replicate_action(DLG_DMQ_UPDATE, dlg, 1, 0);
}
/* unref by 2: 1 set when adding in tm cb, 1 set by dlg_get_by_iuid() */
dlg_unref(dlg, 1);
}
/*!
* \brief Function that is registered as TM callback and called on requests
* \see dlg_new_dialog
* \param t transaction, used to created the dialog
* \param type type of the entered callback
* \param param saved dialog structure in the callback
*/
void dlg_onreq(struct cell* t, int type, struct tmcb_params *param)
{
sip_msg_t *req = param->req;
dlg_cell_t *dlg = NULL;
dlg_iuid_t *iuid = NULL;
if(req->first_line.u.request.method_value == METHOD_BYE) {
_dlg_ctx.t = 1;
return;
}
if(req->first_line.u.request.method_value != METHOD_INVITE)
return;
dlg = dlg_get_ctx_dialog();
if (dlg!=NULL) {
if (!initial_cbs_inscript) {
if (spiral_detected == 1)
run_dlg_callbacks( DLGCB_SPIRALED, dlg,
req, NULL, DLG_DIR_DOWNSTREAM, 0);
else if (spiral_detected == 0)
run_create_callbacks(dlg, req);
}
}
if (dlg==NULL) {
if((req->flags&dlg_flag_mask)!=dlg_flag_mask)
return;
LM_DBG("dialog creation on config flag\n");
dlg_new_dialog(req, t, 1);
dlg = dlg_get_ctx_dialog();
}
if (dlg!=NULL) {
LM_DBG("dialog added to tm callbacks\n");
dlg_set_tm_callbacks(t, req, dlg, spiral_detected);
_dlg_ctx.t = 1;
dlg_release(dlg);
}
if (dlg_enable_dmq) {
iuid = dlg_get_iuid_shm_clone(dlg);
if(iuid==NULL)
{
LM_ERR("failed to create dialog unique id clone\n");
} else {
/* register callback for when the request is sent */
if ( d_tmb.register_tmcb(req, t, TMCB_REQUEST_FWDED,
dlg_on_send,
(void*)iuid, dlg_iuid_sfree)<0 ) {
LM_ERR("failed to register TMCB_REQUEST_FWDED\n");
shm_free(iuid);
}
}
}
}
/*!
* \brief Unreference a new dialog, helper function for dlg_onreq
* \see dlg_onreq
* \param dialog unreferenced dialog
*/
#if 0
static void unref_new_dialog(void *iuid)
{
struct tmcb_params p;
memset(&p, 0, sizeof(struct tmcb_params));
p.param = (void*)&iuid;
dlg_onreply(0, TMCB_DESTROY, &p);
}
#endif
/*!
* \brief Create a new dialog from a sip message
*
* Create a new dialog from a SIP message, register a callback
* to keep track of the dialog with help of the tm module.
* This function is either called from the request callback, or
* from the dlg_manage function in the configuration script.
* \see dlg_onreq
* \see w_dlg_manage
* \param req SIP message
* \param t transaction
* \param run_initial_cbs if set zero, initial callbacks are not executed
* \return 0 on success, -1 on failure
*/
int dlg_new_dialog(sip_msg_t *req, struct cell *t, const int run_initial_cbs)
{
dlg_cell_t *dlg;
str s;
str callid;
str ftag;
str ttag;
str req_uri;
unsigned int dir;
dlg = dlg_get_ctx_dialog();
if(dlg != NULL) {
dlg_release(dlg);
return -1;
}
if(req->first_line.u.request.method_value != METHOD_INVITE)
return -1;
if(pre_match_parse( req, &callid, &ftag, &ttag, 0)<0) {
LM_WARN("pre-matching failed\n");
return -1;
}
if(ttag.s!=0 && ttag.len!=0)
return -1;
if(pv_printf_s(req, ruri_param_model, &req_uri)<0) {
LM_ERR("error - cannot print the r-uri format\n");
return -1;
}
trim(&req_uri);
dir = DLG_DIR_NONE;
/* search dialog by SIP attributes
* - hash table slot is left locked */
dlg = dlg_search(&callid, &ftag, &ttag, &dir);
if(dlg) {
if (detect_spirals) {
if (spiral_detected == 1) {
dlg_hash_release(&callid);
return 0;
}
if ( dlg->state != DLG_STATE_DELETED ) {
LM_DBG("Callid '%.*s' found, must be a spiraled request\n",
callid.len, callid.s);
spiral_detected = 1;
if (run_initial_cbs)
run_dlg_callbacks( DLGCB_SPIRALED, dlg, req, NULL,
DLG_DIR_DOWNSTREAM, 0);
/* set ctx dlg id shortcuts */
_dlg_ctx.iuid.h_entry = dlg->h_entry;
_dlg_ctx.iuid.h_id = dlg->h_id;
/* search_dlg() has incremented the ref count by 1 */
dlg_release(dlg);
dlg_hash_release(&callid);
return 0;
}
dlg_release(dlg);
}
}
spiral_detected = 0;
dlg = build_new_dlg (&callid /*callid*/,
&(get_from(req)->uri) /*from uri*/,
&(get_to(req)->uri) /*to uri*/,
&ftag/*from_tag*/,
&req_uri /*r-uri*/ );
if (dlg==0) {
dlg_hash_release(&callid);
LM_ERR("failed to create new dialog\n");
return -1;
}
/* save caller's tag, cseq, contact and record route*/
if (populate_leg_info(dlg, req, t, DLG_CALLER_LEG,
&(get_from(req)->tag_value)) !=0) {
dlg_hash_release(&callid);
LM_ERR("could not add further info to the dialog\n");
shm_free(dlg);
return -1;
}
/* Populate initial varlist: */
dlg->vars = get_local_varlist_pointer(req, 1);
/* after dlg_search() slot was kept locked */
link_dlg(dlg, 0, 1);
/* unlock after dlg_search() */
dlg_hash_release(&callid);
dlg->lifetime = get_dlg_timeout(req);
s.s = _dlg_ctx.to_route_name;
s.len = strlen(s.s);
dlg_set_toroute(dlg, &s);
dlg->sflags |= _dlg_ctx.flags;
dlg->iflags |= _dlg_ctx.iflags;
if (dlg_send_bye!=0 || _dlg_ctx.to_bye!=0)
dlg->iflags |= DLG_IFLAG_TIMEOUTBYE;
if (run_initial_cbs) run_create_callbacks( dlg, req);
/* first INVITE seen (dialog created, unconfirmed) */
if ( seq_match_mode!=SEQ_MATCH_NO_ID &&
add_dlg_rr_param( req, dlg->h_entry, dlg->h_id)<0 ) {
LM_ERR("failed to add RR param\n");
goto error;
}
if_update_stat( dlg_enable_stats, processed_dlgs, 1);
_dlg_ctx.cpid = my_pid();
_dlg_ctx.iuid.h_entry = dlg->h_entry;
_dlg_ctx.iuid.h_id = dlg->h_id;
set_current_dialog(req, dlg);
return 0;
error:
if (!spiral_detected)
dlg_unref(dlg, 1); // undo ref regarding linking
return -1;
}
/*!
* \brief add dlg structure to tm callbacks
* \param t current transaction
* \param req current sip request
* \param dlg current dialog
* \param smode if the sip request was spiraled
* \return 0 on success, -1 on failure
*/
int dlg_set_tm_callbacks(tm_cell_t *t, sip_msg_t *req, dlg_cell_t *dlg,
int smode)
{
dlg_iuid_t *iuid = NULL;
if(t==NULL)
return -1;
if(smode==0) {
iuid = dlg_get_iuid_shm_clone(dlg);
if(iuid==NULL)
{
LM_ERR("failed to create dialog unique id clone\n");
goto error;
}
if ( d_tmb.register_tmcb( req, t,
TMCB_RESPONSE_IN|TMCB_RESPONSE_READY|TMCB_RESPONSE_FWDED|TMCB_ON_FAILURE,
dlg_onreply, (void*)iuid, dlg_iuid_sfree)<0 ) {
LM_ERR("failed to register TMCB\n");
goto error;
}
}
dlg->dflags |= DLG_FLAG_TM;
return 0;
error:
dlg_iuid_sfree(iuid);
return -1;
}
/*!
* \brief add dlg structure to tm callbacks to wait for negative ACK
* \param t current transaction
* \param dlg current dialog
* \return 0 on success, -1 on failure
*/
int dlg_set_tm_waitack(tm_cell_t *t, dlg_cell_t *dlg)
{
dlg_iuid_t *iuid = NULL;
if(t==NULL)
return -1;
LM_DBG("registering TMCB to wait for negative ACK\n");
iuid = dlg_get_iuid_shm_clone(dlg);
if(iuid==NULL)
{
LM_ERR("failed to create dialog unique id clone\n");
goto error;
}
dlg_ref(dlg, 1);