forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dlg_handlers.c
2331 lines (1942 loc) · 62.2 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) 2009-2014 OpenSIPS Solutions
* Copyright (C) 2006-2009 Voice System SRL
*
* 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:
* --------
* 2006-04-14 initial version (bogdan)
* 2006-11-28 Added support for tracking the number of early dialogs, and the
* number of failed dialogs. This involved updates to dlg_onreply()
* (Jeffrey Magder - SOMA Networks)
* 2007-03-06 syncronized state machine added for dialog state. New tranzition
* design based on events; removed num_1xx and num_2xx (bogdan)
* 2007-04-30 added dialog matching without DID (dialog ID), but based only
* on RFC3261 elements - based on an original patch submitted
* by Michel Bensoussan <michel@extricom.com> (bogdan)
* 2007-05-17 new feature: saving dialog info into a database if
* realtime update is set(ancuta)
* 2007-07-06 support for saving additional dialog info : cseq, contact,
* route_set and socket_info for both caller and callee (ancuta)
* 2007-07-10 Optimized dlg_match_mode 2 (DID_NONE), it now employs a proper
* hash table lookup and isn't dependant on the is_direction
* function (which requires an RR param like dlg_match_mode 0
* anyways.. ;) ; based on a patch from
* Tavis Paquette <tavis@galaxytelecom.net>
* and Peter Baer <pbaer@galaxytelecom.net> (bogdan)
* 2008-04-04 added direction reporting in dlg callbacks (bogdan)
* 2009-09-09 support for early dialogs added; proper handling of cseq
* while PRACK is used (bogdan)
*/
#include <string.h>
#include <time.h>
#include "../../pvar.h"
#include "../../timer.h"
#include "../../statistics.h"
#include "../../data_lump.h"
#include "../../parser/parse_to.h"
#include "../../parser/parse_cseq.h"
#include "../../parser/contact/parse_contact.h"
#include "../../parser/parse_rr.h"
#include "../../parser/parse_cseq.h"
#include "../../parser/parse_hname2.h"
#include "../../parser/parser_f.h"
#include "../tm/tm_load.h"
#include "../rr/api.h"
#include "dlg_hash.h"
#include "dlg_timer.h"
#include "dlg_cb.h"
#include "dlg_handlers.h"
#include "dlg_db_handler.h"
#include "dlg_profile.h"
#include "dlg_req_within.h"
#include "dlg_replication.h"
extern str rr_param;
static int default_timeout;
static int shutdown_done = 0;
extern int seq_match_mode;
extern struct rr_binds d_rrb;
/* statistic variables */
extern stat_var *early_dlgs;
extern stat_var *processed_dlgs;
extern stat_var *expired_dlgs;
extern stat_var *failed_dlgs;
int ctx_lastdstleg_idx = -1;
int ctx_timeout_idx = -1;
void init_dlg_handlers(int default_timeout_p)
{
default_timeout = default_timeout_p;
}
void destroy_dlg_handlers(void)
{
shutdown_done = 1;
}
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;
}
static inline void get_routing_info(struct sip_msg *msg, int is_req,
unsigned int *skip_rrs, str *contact, str *rr_set)
{
/* 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");
contact->s = NULL;
contact->len = 0;
} else {
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");
contact->s = NULL;
contact->len = 0;
} else {
*contact = ((contact_body_t *)msg->contact->parsed)->contacts->uri;
}
}
/* extract the RR parts - parse all headers as we can have multiple
RR headers in the same message */
if( parse_headers(msg,HDR_EOH_F,0)<0 ){
LM_ERR("failed to parse record route header\n");
rr_set->s = 0;
rr_set->len = 0;
} else {
if(msg->record_route){
if( print_rr_body(msg->record_route, rr_set, !is_req,
skip_rrs) != 0 ){
LM_ERR("failed to print route records \n");
rr_set->s = 0;
rr_set->len = 0;
}
} else {
rr_set->s = 0;
rr_set->len = 0;
}
}
}
/*usage: dlg: the dialog to add cseq, contact & record_route
* msg: sip message
* flag: 0-for a request(INVITE),
* 1- for a reply(200 ok)
*
* 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
*/
static int init_leg_info( struct dlg_cell *dlg, struct sip_msg *msg,
struct cell* t, str *tag,str *mangled_from,str *mangled_to)
{
unsigned int skip_recs;
str cseq;
str contact;
str rr_set;
int is_req;
is_req = (msg->first_line.type==SIP_REQUEST)?1:0;
/* extract the cseq number as string */
if (is_req) {
/* cseq */
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;
/* routing info */
skip_recs = 0;
get_routing_info(msg, is_req, &skip_recs, &contact, &rr_set);
dlg->from_rr_nb = skip_recs;
} else {
/* use the same as in invite cseq in caller leg */
cseq = dlg->legs[DLG_CALLER_LEG].inv_cseq;
if ((dlg->mod_flags & TOPOH_ONGOING) && (msg->REPLY_STATUS<200 &&
msg->REPLY_STATUS>100)) {
/* save contact && rr_set , may need need to route requests
* before the INVITE transaction terminates */
get_routing_info(msg,is_req,0,&contact,&rr_set);
} else {
/* no need to save these here, wait for final response */
rr_set.len = contact.len = 0;
rr_set.s = contact.s = NULL;
}
}
LM_DBG("route_set %.*s, contact %.*s, cseq %.*s and bind_addr %.*s\n",
rr_set.len, ZSW(rr_set.s), contact.len, ZSW(contact.s),
cseq.len, cseq.s,
msg->rcv.bind_address->sock_str.len,
msg->rcv.bind_address->sock_str.s);
if (dlg_add_leg_info( dlg, tag, &rr_set, &contact, &cseq,
msg->rcv.bind_address,mangled_from,mangled_to,0)!=0) {
LM_ERR("dlg_add_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;
}
static str extracted_to_uri;
static inline str* extract_mangled_touri(str *mangled_to_hdr)
{
struct to_body to_b;
struct hdr_field hdr;
char *tmp,*end;
if (mangled_to_hdr->len == 0 || mangled_to_hdr->s == NULL)
return NULL;
end = mangled_to_hdr->s+mangled_to_hdr->len;
tmp=parse_hname2(mangled_to_hdr->s,end,&hdr);
if (hdr.type==HDR_ERROR_T) {
LM_ERR("bad to header\n");
return NULL;
}
tmp = eat_lws_end(tmp,end);
if (tmp >= end) {
LM_ERR("empty header\n");
return NULL;
}
parse_to(tmp,end,&to_b);
if (to_b.error == PARSE_ERROR) {
LM_ERR("bad to header [%.*s]\n",mangled_to_hdr->len,mangled_to_hdr->s);
return NULL;
}
extracted_to_uri = to_b.uri;
free_to_params(&to_b);
LM_DBG("extracted to uri [%.*s]\n",extracted_to_uri.len,extracted_to_uri.s);
return &extracted_to_uri;
}
static str extracted_from_uri;
static inline str* extract_mangled_fromuri(str *mangled_from_hdr)
{
struct to_body from_b;
struct hdr_field hdr;
char *tmp,*end;
if (mangled_from_hdr->len == 0 || mangled_from_hdr->s == NULL)
return NULL;
end = mangled_from_hdr->s+mangled_from_hdr->len;
tmp=parse_hname2(mangled_from_hdr->s,end,&hdr);
if (hdr.type==HDR_ERROR_T) {
LM_ERR("bad from header\n");
return NULL;
}
tmp=eat_lws_end(tmp, end);
if (tmp >= end) {
LM_ERR("empty header\n");
return NULL;
}
parse_to(tmp,end,&from_b);
if (from_b.error == PARSE_ERROR) {
LM_ERR("bad from header [%.*s]\n",mangled_from_hdr->len,mangled_from_hdr->s);
return NULL;
}
extracted_from_uri = from_b.uri;
free_to_params(&from_b);
LM_DBG("extracted from uri [%.*s]\n",extracted_from_uri.len,extracted_from_uri.s);
return &extracted_from_uri;
}
static inline void push_reply_in_dialog(struct sip_msg *rpl, struct cell* t,
struct dlg_cell *dlg,str *mangled_from,str *mangled_to)
{
str tag,contact,rr_set;
unsigned int leg, skip_rrs,cseq_no;
/* 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) {
/* Don't print error for final replies in DLG_STATE_UNCONFIRMED */
if (!(dlg->state == DLG_STATE_UNCONFIRMED &&
rpl->first_line.u.reply.statuscode >= 300)) {
LM_ERR("[%d] reply in dlg state [%d]: missing TAG param in TO hdr\n",
rpl->first_line.u.reply.statuscode, dlg->state);
}
tag.s = 0;
tag.len = 0;
}
}
LM_DBG("%p totag in rpl is <%.*s> (%d)\n",
dlg, tag.len,tag.s,tag.len);
/* ignore provisional replies replies without totag */
if (tag.len==0 && rpl->REPLY_STATUS<200 )
return;
/* is the totag already known ?? */
for(leg=DLG_FIRST_CALLEE_LEG ; leg<dlg->legs_no[DLG_LEGS_USED] ; leg++ ) {
if ( dlg->legs[leg].tag.len==tag.len &&
strncmp(dlg->legs[leg].tag.s,tag.s,tag.len)==0 ) {
/* we have a match -> branch already known... */
LM_DBG("branch with tag <%.*s> already exists\n",tag.len,tag.s);
goto routing_info;
}
}
/* coooool :D ....new totag learned !! -> store it */
/* save callee's tag and cseq */
LM_DBG("new branch with tag <%.*s>\n",tag.len,tag.s);
if (init_leg_info( dlg, rpl, t, &tag,extract_mangled_fromuri(mangled_from),
extract_mangled_touri(mangled_to)) !=0) {
LM_ERR("could not add further info to the dialog\n");
return;
}
leg = dlg->legs_no[DLG_LEGS_USED] - 1; /* idx of last created leg */
routing_info:
/* update dlg info only if 2xx reply and if not already done so */
if (rpl->REPLY_STATUS>=200 && rpl->REPLY_STATUS<300 &&
dlg->legs_no[DLG_LEG_200OK] != leg) {
/* set this branch as primary */
if (!dlg->legs_no[DLG_LEG_200OK])
dlg->legs_no[DLG_LEG_200OK] = leg;
if (dlg->flags & DLG_FLAG_CSEQ_ENFORCE) {
/* increase all future requests going to this leg */
if (str2int(&(((struct cseq_body *)t->uas.request->cseq->parsed)->number),&cseq_no) < 0) {
LM_ERR("Failed to convert cseq to integer \n");
} else {
/* XXX - fix this */
dlg->legs[dlg->legs_no[DLG_LEG_200OK]].last_gen_cseq = cseq_no + 1;
}
}
/* update routing info */
if(dlg->mod_flags & TOPOH_ONGOING)
skip_rrs = 0; /* changed here for contact - it was 1 */
else
skip_rrs = dlg->from_rr_nb +
((t->relaied_reply_branch>=0)?
(t->uac[t->relaied_reply_branch].added_rr):0);
LM_DBG("Skipping %d ,%d, %d, %d \n",skip_rrs, dlg->from_rr_nb,t->relaied_reply_branch,t->uac[t->relaied_reply_branch].added_rr);
get_routing_info(rpl, 0, &skip_rrs, &contact, &rr_set);
dlg_update_routing( dlg, leg, &rr_set, &contact);
if( rr_set.s )
pkg_free( rr_set.s);
}
}
static void dlg_onreply(struct cell* t, int type, struct tmcb_params *param)
{
struct sip_msg *rpl,*req;
struct dlg_cell *dlg;
int new_state;
int old_state;
int unref;
int event;
str mangled_from = {0,0};
str mangled_to = {0,0};
str *req_out_buff;
dlg = (struct dlg_cell *)(*param->param);
if (shutdown_done || dlg==0)
return;
rpl = param->rpl;
req = param->req;
if (type==TMCB_RESPONSE_FWDED) {
/* this callback is under transaction lock (by TM), so it is save
to operate at write level, but we need to take care on write-read
conflicts -bogdan */
if (rpl!=FAKED_REPLY) {
if (req->msg_flags & (FL_USE_UAC_FROM | FL_USE_UAC_TO ) ) {
req_out_buff = &t->uac[d_tmb.get_branch_index()].request.buffer;
if (extract_ftc_hdrs(req_out_buff->s,req_out_buff->len,
(req->msg_flags & FL_USE_UAC_FROM )?&mangled_from:0,
(req->msg_flags & FL_USE_UAC_TO )?&mangled_to:0,0,0) != 0) {
LM_ERR("failed to extract mangled FROM and TO hdrs\n");
mangled_from.len = 0;
mangled_from.s = NULL;
mangled_to.len = 0;
mangled_to.s = NULL;
} else {
if ((req->msg_flags & FL_USE_UAC_FROM) && (mangled_from.len == 0 || mangled_from.s == NULL))
LM_CRIT("extract_ftc_hdrs ok but no from extracted : [%.*s]\n",req_out_buff->len,req_out_buff->s);
if ((req->msg_flags & FL_USE_UAC_TO) && (mangled_to.len == 0 || mangled_to.s == NULL))
LM_CRIT("extract_ftc_hdrs ok but no to extracted : [%.*s]\n",req_out_buff->len,req_out_buff->s);
}
}
push_reply_in_dialog( rpl, t, dlg,&mangled_from,&mangled_to);
} else {
LM_DBG("dialog replied from script - cannot get callee info\n");
}
/* The state does not change, but the msg is mutable in this callback*/
run_dlg_callbacks(DLGCB_RESPONSE_FWDED, dlg, rpl, DLG_DIR_UPSTREAM, 0);
return;
}
if (type==TMCB_TRANS_CANCELLED) {
/* only if we did force match the Cancel to the
* dialog before ( from the script ) */
if ( ctx_dialog_get()==NULL) {
/* reference and attached to script */
ref_dlg(dlg,1);
ctx_dialog_set(t->dialog_ctx);
}
return;
}
if (type==TMCB_RESPONSE_OUT) {
if (dlg->state == DLG_STATE_CONFIRMED_NA && dialog_replicate_cluster)
replicate_dialog_created(dlg);
return;
}
if (type==TMCB_TRANS_DELETED) {
event = DLG_EVENT_TDEL;
} else if (param->code<200) {
event = DLG_EVENT_RPL1xx;
ctx_lastdstleg_set(DLG_CALLER_LEG);
} else if (param->code<300) {
event = DLG_EVENT_RPL2xx;
ctx_lastdstleg_set(DLG_CALLER_LEG);
} else {
event = DLG_EVENT_RPL3xx;
ctx_lastdstleg_set(DLG_CALLER_LEG);
}
next_state_dlg(dlg, event, DLG_DIR_UPSTREAM, &old_state, &new_state,
&unref, DLG_CALLER_LEG, 0);
if (new_state==DLG_STATE_EARLY && old_state!=DLG_STATE_EARLY) {
run_dlg_callbacks(DLGCB_EARLY, dlg, rpl, DLG_DIR_UPSTREAM, 0);
if_update_stat(dlg_enable_stats, early_dlgs, 1);
return;
}
if (new_state==DLG_STATE_CONFIRMED_NA &&
old_state!=DLG_STATE_CONFIRMED_NA && old_state!=DLG_STATE_CONFIRMED ) {
LM_DBG("dialog %p confirmed\n",dlg);
/* set start time */
dlg->start_ts = (unsigned int)(time(0));
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->legs[DLG_CALLER_LEG].tag.len,
dlg->legs[DLG_CALLER_LEG].tag.s,
dlg->legs[callee_idx(dlg)].tag.len,
ZSW(dlg->legs[callee_idx(dlg)].tag.s));
} else {
/* reference dialog as kept in timer list */
ref_dlg(dlg,1);
}
if (dlg->flags & DLG_FLAG_PING_CALLER || dlg->flags & DLG_FLAG_PING_CALLEE) {
if (0 != insert_ping_timer( dlg)) {
LM_CRIT("Unable to insert ping 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->legs[DLG_CALLER_LEG].tag.len,
dlg->legs[DLG_CALLER_LEG].tag.s,
dlg->legs[callee_idx(dlg)].tag.len,
ZSW(dlg->legs[callee_idx(dlg)].tag.s));
} else {
/* reference dialog as kept in ping timer list */
ref_dlg(dlg,1);
}
}
if (dlg->flags & DLG_FLAG_REINVITE_PING_CALLER || dlg->flags & DLG_FLAG_REINVITE_PING_CALLEE) {
if (0 != insert_reinvite_ping_timer( dlg)) {
LM_CRIT("Unable to insert ping 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->legs[DLG_CALLER_LEG].tag.len,
dlg->legs[DLG_CALLER_LEG].tag.s,
dlg->legs[callee_idx(dlg)].tag.len,
ZSW(dlg->legs[callee_idx(dlg)].tag.s));
} else {
/* reference dialog as kept in reinvite ping timer list */
ref_dlg(dlg,1);
}
}
/* 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->flags |= DLG_FLAG_NEW;
if ( dlg_db_mode==DB_MODE_REALTIME )
update_dialog_dbinfo(dlg);
/* dialog confirmed */
run_dlg_callbacks( DLGCB_CONFIRMED, dlg, rpl, DLG_DIR_UPSTREAM, 0);
if (old_state==DLG_STATE_EARLY)
if_update_stat(dlg_enable_stats, early_dlgs, -1);
if_update_stat(dlg_enable_stats, active_dlgs, 1);
return;
}
if ( old_state!=DLG_STATE_DELETED && new_state==DLG_STATE_DELETED ) {
LM_DBG("dialog %p failed (negative reply)\n", dlg);
/*destroy linkers */
dlg_lock_dlg(dlg);
destroy_linkers(dlg->profile_links, 0);
dlg->profile_links = NULL;
dlg_unlock_dlg(dlg);
/* dialog setup not completed (3456XX) */
run_dlg_callbacks( DLGCB_FAILED, dlg, rpl, DLG_DIR_UPSTREAM, 0);
/* do unref */
if (unref)
unref_dlg(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);
return;
}
/* in any other case, check if the dialog state machine
requests to unref the dialog */
if (unref)
unref_dlg(dlg,unref);
return;
}
/* modifies the sip_msg, setting the cseq header to
* new_cseq.s + value OR
* value if new_cseq is NULL
*/
static inline int update_msg_cseq(struct sip_msg *msg,str *new_cseq,
unsigned int value)
{
int offset,len;
struct lump *tmp;
char *buf;
unsigned int loc_cseq;
str final_cseq;
str pkg_cseq;
if (!msg)
{
LM_ERR("null pointer provided\n");
return -1;
}
if(parse_headers(msg, HDR_CSEQ_F, 0) <0 )
{
LM_ERR("failed to parse headers \n");
return -1;
}
if (new_cseq == 0 || new_cseq->s == 0 || new_cseq->len == 0)
{
LM_DBG("null str provided. Using only int value for cseq\n");
final_cseq.s = int2str(value,&final_cseq.len);
}
else
{
if( str2int(new_cseq, &loc_cseq) != 0){
LM_ERR("could not convert string cseq to number\n");
return -1;
}
loc_cseq += value;
final_cseq.s = int2str(loc_cseq,&final_cseq.len);
}
buf = msg->buf;
len = ((struct cseq_body *)msg->cseq->parsed)->number.len;
offset = ((struct cseq_body *)msg->cseq->parsed)->number.s - buf;
if ((tmp = del_lump(msg,offset,len,0)) == 0)
{
LM_ERR("failed to remove the existing CSEQ\n");
return -1;
}
/* Make pkg copy of cseq */
pkg_cseq.s = pkg_malloc(final_cseq.len);
if (pkg_cseq.s == 0)
{
LM_ERR("no more pkg mem\n");
return -1;
}
pkg_cseq.len = final_cseq.len;
memcpy(pkg_cseq.s,final_cseq.s,final_cseq.len);
LM_DBG("Message CSEQ translated from [%.*s] to [%.*s]\n",
((struct cseq_body *)msg->cseq->parsed)->number.len,
((struct cseq_body *)msg->cseq->parsed)->number.s,pkg_cseq.len,
pkg_cseq.s);
if (insert_new_lump_after(tmp,pkg_cseq.s,pkg_cseq.len,0) == 0)
{
LM_ERR("failed to insert new CSEQ\n");
pkg_free(pkg_cseq.s);
return -1;
}
return 0;
}
static void dlg_update_sdp(struct dlg_leg *leg,struct sip_msg *msg)
{
str sdp;
if (get_body(msg,&sdp) < 0) {
LM_ERR("Failed to extract SDP \n");
sdp.s = NULL;
sdp.len = 0;
}
if (leg->sdp.len < sdp.len) {
leg->sdp.s = shm_realloc(leg->sdp.s,sdp.len);
if (!leg->sdp.s) {
LM_ERR("Failed to reallocate sdp \n");
return;
}
}
leg->sdp.len = sdp.len;
memcpy(leg->sdp.s,sdp.s,sdp.len);
}
static void dlg_update_callee_sdp(struct cell* t, int type,
struct tmcb_params *ps)
{
struct sip_msg *rpl,*msg;
int statuscode;
struct dlg_cell *dlg;
str buffer;
if(ps == NULL || ps->rpl == NULL) {
LM_ERR("Wrong tmcb params\n");
return;
}
if( ps->param== NULL ) {
LM_ERR("Null callback parameter\n");
return;
}
rpl = ps->rpl;
statuscode = ps->code;
dlg = *(ps->param);
if(rpl==NULL || rpl==FAKED_REPLY) {
/* we only care about actual replayed replies */
return;
}
LM_DBG("Status Code received = [%d]\n", statuscode);
if (statuscode == 200) {
buffer.s = ((str*)ps->extra1)->s;
buffer.len = ((str*)ps->extra1)->len;
msg=pkg_malloc(sizeof(struct sip_msg));
if (msg==0) {
LM_ERR("no pkg mem left for sip_msg\n");
return;
}
memset(msg,0, sizeof(struct sip_msg));
msg->buf=buffer.s;
msg->len=buffer.len;
if (parse_msg(buffer.s,buffer.len, msg)!=0) {
pkg_free(msg);
return;
}
dlg_update_sdp(&dlg->legs[callee_idx(dlg)],msg);
free_sip_msg(msg);
pkg_free(msg);
}
}
static void dlg_update_caller_sdp(struct cell* t, int type,
struct tmcb_params *ps)
{
struct sip_msg *rpl,*msg;
int statuscode;
struct dlg_cell *dlg;
str buffer;
if(ps == NULL || ps->rpl == NULL) {
LM_ERR("Wrong tmcb params\n");
return;
}
if( ps->param== NULL ) {
LM_ERR("Null callback parameter\n");
return;
}
rpl = ps->rpl;
statuscode = ps->code;
dlg = *(ps->param);
if(rpl==NULL || rpl==FAKED_REPLY) {
/* we only care about actual replayed replies */
return;
}
LM_DBG("Status Code received = [%d]\n", statuscode);
if (statuscode == 200) {
buffer.s = ((str*)ps->extra1)->s;
buffer.len = ((str*)ps->extra1)->len;
msg=pkg_malloc(sizeof(struct sip_msg));
if (msg==0) {
LM_ERR("no pkg mem left for sip_msg\n");
return;
}
memset(msg,0, sizeof(struct sip_msg));
msg->buf=buffer.s;
msg->len=buffer.len;
if (parse_msg(buffer.s,buffer.len, msg)!=0) {
pkg_free(msg);
return;
}
dlg_update_sdp(&dlg->legs[DLG_CALLER_LEG],msg);
free_sip_msg(msg);
pkg_free(msg);
}
}
static void dlg_seq_up_onreply_mod_cseq(struct cell* t, int type,
struct tmcb_params *param)
{
struct dlg_cell *dlg;
dlg = ((dlg_cseq_wrapper*)*param->param)->dlg;
if (shutdown_done || dlg==0)
return;
if (update_msg_cseq((struct sip_msg *)param->rpl,&((dlg_cseq_wrapper *)*param->param)->cseq,0) != 0)
LM_ERR("failed to update CSEQ in msg\n");
if (type==TMCB_RESPONSE_FWDED &&
(dlg->cbs.types)&DLGCB_RESPONSE_WITHIN) {
run_dlg_callbacks(DLGCB_RESPONSE_WITHIN, dlg, param->rpl,
DLG_DIR_UPSTREAM, 0);
return;
}
return;
}
static void dlg_seq_up_onreply(struct cell* t, int type,
struct tmcb_params *param)
{
struct dlg_cell *dlg;
dlg = (struct dlg_cell *)(*param->param);
if (shutdown_done || dlg==0)
return;
if (type==TMCB_RESPONSE_FWDED &&
(dlg->cbs.types)&DLGCB_RESPONSE_WITHIN) {
run_dlg_callbacks(DLGCB_RESPONSE_WITHIN, dlg, param->rpl,
DLG_DIR_UPSTREAM, 0);
return;
}
return;
}
static void dlg_seq_down_onreply_mod_cseq(struct cell* t, int type,
struct tmcb_params *param)
{
struct dlg_cell *dlg;
dlg = ((dlg_cseq_wrapper*)*param->param)->dlg;
if (shutdown_done || dlg==0)
return;
if (update_msg_cseq((struct sip_msg *)param->rpl,&((dlg_cseq_wrapper *)*param->param)->cseq,0) != 0)
LM_ERR("failed to update CSEQ in msg\n");
if (type==TMCB_RESPONSE_FWDED &&
(dlg->cbs.types)&DLGCB_RESPONSE_WITHIN) {
run_dlg_callbacks(DLGCB_RESPONSE_WITHIN, dlg, param->rpl,
DLG_DIR_DOWNSTREAM, 0);
return;
}
return;
}
static void fix_final_cseq(struct cell *t,int type,
struct tmcb_params *param)
{
str cseq;
cseq.s = (char *)(*param->param);
cseq.len = strlen(cseq.s);
if (update_msg_cseq((struct sip_msg *)param->rpl,&cseq,0) != 0)
LM_ERR("failed to update CSEQ in msg\n");
shm_free(cseq.s);
return ;
}
static void dlg_seq_down_onreply(struct cell* t, int type,
struct tmcb_params *param)
{
struct dlg_cell *dlg;
dlg = (struct dlg_cell *)(*param->param);
if (shutdown_done || dlg==0)
return;
if (type==TMCB_RESPONSE_FWDED &&
(dlg->cbs.types)&DLGCB_RESPONSE_WITHIN) {
run_dlg_callbacks(DLGCB_RESPONSE_WITHIN, dlg, param->rpl,
DLG_DIR_DOWNSTREAM, 0);
return;
}
return;
}
inline static int get_dlg_timeout(struct sip_msg *msg)
{
return (current_processing_ctx && (ctx_timeout_get()!=0)) ?
ctx_timeout_get() : default_timeout;
}
static void unreference_dialog_cseq(void *cseq_wrap)
{
/* if the dialog table is gone, it means the system is shutting down.*/
if (!d_table)
return;
dlg_cseq_wrapper *wrap = (dlg_cseq_wrapper *)cseq_wrap;
unref_dlg(wrap->dlg, 1);
shm_free(wrap);
}
void unreference_dialog(void *dialog)
{
/* if the dialog table is gone, it means the system is shutting down.*/
unref_dlg_destroy_safe((struct dlg_cell*)dialog, 1);
}
static void unreference_dialog_create(void *dialog)
{
struct tmcb_params params;
memset(¶ms, 0, sizeof(struct tmcb_params));
params.param = (void*)&dialog;
/* just a wapper */
dlg_onreply( 0, TMCB_TRANS_DELETED, ¶ms);
}
static void tmcb_unreference_dialog(struct cell* t, int type,
struct tmcb_params *param)
{
unref_dlg_destroy_safe((struct dlg_cell*)*param->param, 1);
}
static void dlg_onreply_out(struct cell* t, int type, struct tmcb_params *ps)
{
struct sip_msg *msg,*rpl;
struct dlg_cell *dlg;
str buffer,contact,sdp;
dlg = (struct dlg_cell *)(*ps->param);
rpl = ps->rpl;
if(rpl==NULL || rpl==FAKED_REPLY) {
/* we only care about actual replayed replies */
return;
}
if (ps->code == 200) {
buffer.s = ((str*)ps->extra1)->s;
buffer.len = ((str*)ps->extra1)->len;
msg=pkg_malloc(sizeof(struct sip_msg));
if (msg==0) {
LM_ERR("no pkg mem left for sip_msg\n");
return;
}
memset(msg,0, sizeof(struct sip_msg));
msg->buf=buffer.s;
msg->len=buffer.len;
if (parse_msg(buffer.s,buffer.len, msg)!=0) {
pkg_free(msg);
return;
}
/* extract SDP */
if (get_body(msg,&sdp) < 0) {
LM_ERR("Failed to extract SDP from outgoing invite \n");
sdp.s = NULL;
sdp.len = 0;
}
dlg->legs[callee_idx(dlg)].sdp.s = shm_malloc(sdp.len);
if (!dlg->legs[callee_idx(dlg)].sdp.s) {
LM_ERR("No more shm \n");
free_sip_msg(msg);
pkg_free(msg);
return;
}
dlg->legs[callee_idx(dlg)].sdp.len = sdp.len;
memcpy(dlg->legs[callee_idx(dlg)].sdp.s,sdp.s,sdp.len);
/* extract the contact address */
if (!msg->contact&&(parse_headers(msg,HDR_CONTACT_F,0)<0||!msg->contact)){
LM_ERR("There is no contact header in the outgoing 200OK \n");
} else {