-
Notifications
You must be signed in to change notification settings - Fork 909
/
t_reply.c
3083 lines (2800 loc) · 91.9 KB
/
t_reply.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) 2001-2003 FhG Fokus
*
* 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
*
*/
/** Defines:
* TM_ONREPLY_FINAL_DROP_OK - allows dropping the final reply
* from the tm onreply_routes, but comes with a small performance
* hit (extra unlock()/lock() for each final reply when a onreply
* route is set).
*/
#include <sys/time.h>
#ifdef EXTRA_DEBUG
#include <assert.h>
#endif
#include "../../core/comp_defs.h"
#include "../../core/hash_func.h"
#include "../../core/dprint.h"
#include "../../core/config.h"
#include "../../core/parser/parser_f.h"
#include "../../core/parser/parse_to.h"
#include "../../core/ut.h"
#include "../../core/timer.h"
#include "../../core/error.h"
#include "../../core/action.h"
#include "../../core/script_cb.h"
#include "../../core/dset.h"
#include "../../core/tags.h"
#include "../../core/route.h"
#include "../../core/data_lump.h"
#include "../../core/data_lump_rpl.h"
#include "../../core/usr_avp.h"
#include "../../core/usr_avp.h"
#include "../../core/atomic_ops.h" /* membar_write() */
#include "../../core/compiler_opt.h"
#ifdef USE_DST_BLACKLIST
#include "../../core/dst_blacklist.h"
#endif
#ifdef USE_DNS_FAILOVER
#include "../../core/dns_cache.h"
#include "../../core/cfg_core.h" /* cfg_get(core, core_cfg, use_dns_failover) */
#endif
#include "config.h"
#include "h_table.h"
#include "t_hooks.h"
#include "t_funcs.h"
#include "t_reply.h"
#include "t_cancel.h"
#include "t_msgbuilder.h"
#include "t_lookup.h"
#include "t_fwd.h"
#include "../../core/fix_lumps.h"
#include "../../core/sip_msg_clone.h"
#include "../../core/sr_compat.h"
#include "../../core/receive.h"
#include "../../core/onsend.h"
#include "../../core/kemi.h"
#include "t_stats.h"
#include "uac.h"
#ifdef NO_TM_ONREPLY_FINAL_DROP_OK
#undef TM_ONREPLY_FINAL_DROP_OK
#endif
/* private place where we create to-tags for replies */
/* janakj: made public, I need to access this value to store it in dialogs */
char tm_tags[TOTAG_VALUE_LEN];
/* bogdan: pack tm_tag buffer and len into a str to pass them to
* build_res_buf_from_sip_req() */
static str tm_tag = {tm_tags,TOTAG_VALUE_LEN};
char *tm_tag_suffix;
/* where to go if there is no positive reply (>=300) */
static int goto_on_failure=0;
/* where to go if a failure is returned on a branch */
static int goto_on_branch_failure=0;
/* where to go on receipt of reply */
static int goto_on_reply=0;
/* where to go on receipt of reply without transaction context */
int goto_on_sl_reply=0;
extern str on_sl_reply_name;
extern str _tm_event_callback_lres_sent;
extern unsigned long tm_exec_time_check;
/* remap 503 response code to 500 */
extern int tm_remap_503_500;
/* send path and flags in 3xx class reply */
int tm_rich_redirect = 0;
/* how to deal with winning branch reply selection in failure_route
* can be overwritten per transaction with t_drop_replies(...)
* Values:
* - 0 - all branches are kept (default, and default ser 2.1.x behaviour)
* - 1 - all branches are discarded
* - 2 - braches of last step of serial forking are discarded
* - 3 - all branches are discarded if a new leg of serial forking
* is started (default kamailio 1.5.x behaviour)
*/
int failure_reply_mode = 3;
/* responses priority (used by t_pick_branch)
* 0xx is used only for the initial value (=> should have no chance to be
* selected => the highest value); 1xx is not used */
static unsigned short resp_class_prio[]={
32000, /* 0-99, special */
11000, /* 1xx, special, should never be used */
0, /* 2xx, high priority (not used, 2xx are immediately
* forwarded and t_pick_branch will never be called if
* a 2xx was received) */
3000, /* 3xx */
4000, /* 4xx */
5000, /* 5xx */
1000 /* 6xx, highest priority */
};
/* How to prioritize faked replies
* The value will be added to the default prio
* - 0 disabled
* - < 0 increase prio
* - > 0 decrease prio
*/
int faked_reply_prio = 0;
int t_get_reply_totag(struct sip_msg *msg, str *totag)
{
if(msg==NULL || totag==NULL) {
return -1;
}
calc_crc_suffix(msg, tm_tag_suffix);
*totag = tm_tag;
return 1;
}
static int picked_branch = -1;
/*! \brief returns the picked branch */
int t_get_picked_branch(void)
{
return picked_branch;
}
/* we store the reply_route # in private memory which is
* then processed during t_relay; we cannot set this value
* before t_relay creates transaction context or after
* t_relay when a reply may arrive after we set this
* value; that's why we do it how we do it, i.e.,
* *inside* t_relay using hints stored in private memory
* before t_relay is called
*/
void t_on_failure( unsigned int go_to )
{
struct cell *t = get_t();
/* in REPLY_ROUTE and FAILURE_ROUTE T will be set to current transaction;
* in REQUEST_ROUTE T will be set only if the transaction was already
* created; if not -> use the static variable */
if (!t || t==T_UNDEFINED )
goto_on_failure=go_to;
else
t->on_failure = go_to;
}
void t_on_branch_failure( unsigned int go_to )
{
struct cell *t = get_t();
/* in REPLY_ROUTE and FAILURE_ROUTE T will be set to current transaction;
* in REQUEST_ROUTE T will be set only if the transaction was already
* created; if not -> use the static variable */
if (!t || t==T_UNDEFINED )
goto_on_branch_failure=go_to;
else
t->on_branch_failure = go_to;
}
void t_on_reply( unsigned int go_to )
{
struct cell *t = get_t();
/* in REPLY_ROUTE and FAILURE_ROUTE T will be set to current transaction;
* in REQUEST_ROUTE T will be set only if the transaction was already
* created; if not -> use the static variable */
if (!t || t==T_UNDEFINED )
goto_on_reply=go_to;
else
t->on_reply = go_to;
}
unsigned int get_on_failure()
{
return goto_on_failure;
}
unsigned int get_on_branch_failure()
{
return goto_on_branch_failure;
}
unsigned int get_on_reply()
{
return goto_on_reply;
}
void tm_init_tags()
{
init_tags(tm_tags, &tm_tag_suffix,
"SER-TM/tags", TM_TAG_SEPARATOR );
}
/* returns 0 if the message was previously acknowledged
* (i.e., no E2EACK callback is needed) and one if the
* callback shall be executed */
int unmatched_totag(struct cell *t, struct sip_msg *ack)
{
struct totag_elem *i;
str *tag;
if (parse_headers(ack, HDR_TO_F,0)==-1 ||
!ack->to ) {
LM_ERR("To header is invalid\n");
return 1;
}
tag=&get_to(ack)->tag_value;
i=t->fwded_totags;
while(i){
membar_depends(); /* make sure we don't see some old i content
(needed on CPUs like Alpha) */
if (i->tag.len==tag->len
&& memcmp(i->tag.s, tag->s, tag->len)==0) {
LM_DBG("totag for e2e ACK found: %d\n", i->acked);
/* mark totag as acked and return 1 if this was the first ack
* and 0 otherwise */
return (atomic_get_and_set_int(&i->acked, 1)==0);
}
i=i->next;
}
/* surprising: to-tag never sighted before */
return 1;
}
static inline void update_local_tags(struct cell *trans,
struct bookmark *bm, char *dst_buffer,
char *src_buffer /* to which bm refers */)
{
if (bm->to_tag_val.s) {
trans->uas.local_totag.s=bm->to_tag_val.s-src_buffer+dst_buffer;
trans->uas.local_totag.len=bm->to_tag_val.len;
}
}
/* append a newly received tag from a 200/INVITE to
* transaction's set; (only safe if called from within
* a REPLY_LOCK); it returns 1 if such a to tag already
* exists
*/
inline static int update_totag_set(struct cell *t, struct sip_msg *ok)
{
struct totag_elem *i, *n;
str *tag;
char *s;
if (!ok->to || !ok->to->parsed) {
LM_ERR("to header not parsed\n");
return 0;
}
tag=&get_to(ok)->tag_value;
if (!tag->s) {
LM_DBG("no tag in to header\n");
return 0;
}
for (i=t->fwded_totags; i; i=i->next) {
if (i->tag.len==tag->len
&& i->tag.s
&& memcmp(i->tag.s, tag->s, tag->len) ==0 ){
/* to tag already recorded */
LM_DBG("to-tag retransmission\n");
return 1;
}
}
/* that's a new to-tag -- record it */
n=(struct totag_elem*) shm_malloc(sizeof(struct totag_elem));
s=(char *)shm_malloc(tag->len);
if (!s || !n) {
SHM_MEM_ERROR;
if (n) shm_free(n);
if (s) shm_free(s);
return 0;
}
memset(n, 0, sizeof(struct totag_elem));
memcpy(s, tag->s, tag->len);
n->tag.s=s;n->tag.len=tag->len;
n->next=t->fwded_totags;
membar_write(); /* make sure all the changes to n are visible on all cpus
* before we update t->fwded_totags. This is needed for
* three reasons: the compiler might reorder some of the
* writes, the cpu/cache could also reorder them with
* respect to the visibility on other cpus
* (e.g. some of the changes to n could be visible on
* another cpu _after_ seeing t->fwded_totags=n) and
* the "readers" (unmatched_tags()) do not use locks and
* can be called simultaneously on another cpu.*/
t->fwded_totags=n;
LM_DBG("new totag [%.*s]\n", tag->len, tag->s);
return 0;
}
/*
* Build an ACK to a negative reply
*/
static char *build_ack(struct sip_msg* rpl,struct cell *trans,int branch,
unsigned int *ret_len)
{
str to;
if (parse_headers(rpl,HDR_TO_F, 0)==-1 || !rpl->to ) {
LM_ERR("cannot generate a HBH ACK if key HFs in reply missing\n");
return NULL;
}
to.s=rpl->to->name.s;
to.len=rpl->to->len;
if (cfg_get(tm, tm_cfg, reparse_invite)) {
/* build the ACK from the INVITE which was sent out */
return build_local_reparse( trans, branch, ret_len,
ACK, ACK_LEN, &to
, 0
);
} else {
/* build the ACK from the reveived INVITE */
return build_local( trans, branch, ret_len,
ACK, ACK_LEN, &to
, 0
);
}
}
/*
* The function builds an ACK to 200 OK of local transactions, honoring the
* route set.
* The destination to which the message should be sent will be returned
* in the dst parameter.
* returns 0 on error and a pkg_malloc'ed buffer with length in ret_len
* and intended destination in dst on success.
*/
static char *build_local_ack(struct sip_msg* rpl, struct cell *trans,
int branch, unsigned int *ret_len,
struct dest_info* dst)
{
struct retr_buf *local_ack, *old_lack;
/* do we have the ACK cache, previously build? */
if ((local_ack = trans->uac[0].local_ack) && local_ack->buffer_len) {
LM_DBG("reusing ACK retr. buffer.\n");
*ret_len = local_ack->buffer_len;
*dst = local_ack->dst;
return local_ack->buffer;
}
/* the ACK will be built (and cached) by the AS (ack_local_uac()) */
if (trans->flags & T_NO_AUTO_ACK)
return NULL;
if (! (local_ack = local_ack_rb(rpl, trans, branch, /*hdrs*/NULL,
/*body*/NULL))) {
LM_ERR("failed to build local ACK retransmission buffer (T@%p).\n",
trans);
return NULL;
}
/* set the new buffer, but only if not already set (concurrent 2xx) */
/* a memory write barrier is needed to make sure the local_ack
* content is fully written, before we try to add it to the transaction
* -- andrei */
membar_write_atomic_op();
if ((old_lack = (struct retr_buf *)atomic_cmpxchg_long(
(void *)&trans->uac[0].local_ack, 0, (long)local_ack))) {
/* buffer already set: trash current and use the winning one */
LM_INFO("concurrent 2xx to local INVITE detected (T@%p).\n", trans);
free_local_ack(local_ack);
local_ack = old_lack;
}
*ret_len = local_ack->buffer_len;
*dst = local_ack->dst;
return local_ack->buffer;
}
inline static void start_final_repl_retr( struct cell *t )
{
if (unlikely(!is_local(t) && t->uas.request->REQ_METHOD==METHOD_INVITE )){
/* crank timers for negative replies */
if (t->uas.status>=300) {
if (start_retr(&t->uas.response)!=0)
LM_CRIT("BUG: start retr failed for %p\n", &t->uas.response);
return;
}
/* local UAS retransmits too */
if (t->relayed_reply_branch==-2 && t->uas.status>=200) {
/* we retransmit 200/INVs regardless of transport --
* even if TCP used, UDP could be used upstream and
* loose the 200, which is not retransmitted by proxies
*/
if (force_retr( &t->uas.response )!=0)
LM_CRIT("BUG: force retr failed for %p\n", &t->uas.response);
return;
}
}
}
static int _tm_local_response_sent_lookup = 0;
static int _reply_light( struct cell *trans, char* buf, unsigned int len,
unsigned int code,
char *to_tag, unsigned int to_tag_len, int lock,
struct bookmark *bm )
{
struct retr_buf *rb;
unsigned int buf_len;
struct cancel_info cancel_data;
struct tmcb_params onsend_params;
int rt, backup_rt;
struct run_act_ctx ctx;
struct run_act_ctx *bctx;
struct sip_msg pmsg;
sr_kemi_eng_t *keng = NULL;
str evname = str_init("tm:local-response-sent");
init_cancel_info(&cancel_data);
if (!buf)
{
LM_ERR("response building failed\n");
/* determine if there are some branches to be canceled */
if ( is_invite(trans) ) {
prepare_to_cancel(trans, &cancel_data.cancel_bitmap, 0);
}
/* and clean-up, including cancellations, if needed */
goto error;
}
if (lock) LOCK_REPLIES( trans );
if (trans->uas.status>=200) {
LM_INFO("can't generate %d reply when a final %d was sent out\n",
code, trans->uas.status);
goto error2;
}
rb = & trans->uas.response;
rb->rbtype=code;
trans->uas.status = code;
if(len<=0) {
LM_ERR("invalid new buffer len\n");
goto error3;
}
buf_len = rb->buffer ? len : len + REPLY_OVERBUFFER_LEN;
rb->buffer = (char*)shm_resize( rb->buffer, buf_len );
/* puts the reply's buffer to uas.response */
if (! rb->buffer ) {
LM_ERR("cannot allocate shmem buffer\n");
goto error3;
}
update_local_tags(trans, bm, rb->buffer, buf);
rb->buffer_len = len ;
memcpy( rb->buffer , buf , len );
/* needs to be protected too because what timers are set depends
* on current transactions status */
/* t_update_timers_after_sending_reply( rb ); */
update_reply_stats( code );
trans->relayed_reply_branch=-2;
t_stats_rpl_generated();
t_stats_rpl_sent();
if (lock) UNLOCK_REPLIES( trans );
/* do UAC cleanup procedures in case we generated
* a final answer whereas there are pending UACs */
if (code>=200) {
if (unlikely(is_local(trans))) {
if(unlikely(has_tran_tmcbs(trans, TMCB_LOCAL_COMPLETED)))
run_trans_callbacks(TMCB_LOCAL_COMPLETED, trans,
0, FAKED_REPLY, code);
} else {
if(unlikely(has_tran_tmcbs(trans, TMCB_RESPONSE_READY))) {
run_trans_callbacks_with_buf(TMCB_RESPONSE_READY, rb,
trans->uas.request, FAKED_REPLY, TMCB_NONE_F);
}
}
cleanup_uac_timers( trans );
if (is_invite(trans)){
prepare_to_cancel(trans, &cancel_data.cancel_bitmap, 0);
cancel_data.reason.cause=code;
cancel_uacs( trans, &cancel_data, F_CANCEL_B_KILL );
}
start_final_repl_retr( trans );
}
if (code==100) {
if(unlikely(has_tran_tmcbs(trans, TMCB_REQUEST_PENDING)))
run_trans_callbacks_with_buf(TMCB_REQUEST_PENDING, rb,
trans->uas.request, FAKED_REPLY, TMCB_NONE_F);
}
/* send it out */
/* first check if we managed to resolve topmost Via
* -- if not yet, don't try to retransmit
*/
/*
* response.dst.send_sock might be unset if the process that created
* the original transaction has not finished initialising the
* retransmission buffer (see t_newtran/ init_rb).
* If reply_to_via is set and via contains a host name (and not an ip)
* the chances for this increase a lot.
*/
if (unlikely(!trans->uas.response.dst.send_sock)) {
LM_ERR("no resolved dst to send reply to\n");
} else {
if (likely(SEND_PR_BUFFER( rb, buf, len )>=0)){
if (unlikely(code>=200 && !is_local(trans) &&
has_tran_tmcbs(trans, TMCB_RESPONSE_OUT)) ){
INIT_TMCB_ONSEND_PARAMS(onsend_params, trans->uas.request,
FAKED_REPLY, rb, &rb->dst,
buf, len, TMCB_LOCAL_F, rb->branch, code);
run_trans_callbacks_off_params(TMCB_RESPONSE_OUT, trans,
&onsend_params);
}
if (unlikely(has_tran_tmcbs(trans, TMCB_RESPONSE_SENT))){
INIT_TMCB_ONSEND_PARAMS(onsend_params, trans->uas.request,
FAKED_REPLY, rb, &rb->dst,
buf, len, TMCB_LOCAL_F, rb->branch, code);
run_trans_callbacks_off_params(TMCB_RESPONSE_SENT, trans,
&onsend_params);
}
if(_tm_event_callback_lres_sent.len>0
&& _tm_event_callback_lres_sent.s!=NULL) {
keng = sr_kemi_eng_get();
}
rt = -1;
if(likely(keng==NULL)) {
if(_tm_local_response_sent_lookup == 0) {
rt = route_lookup(&event_rt, "tm:local-response");
_tm_local_response_sent_lookup = 1;
}
}
if ((rt >= 0 && event_rt.rlist[rt] != NULL) || (keng != NULL)
|| sr_event_enabled(SREV_SIP_REPLY_OUT)) {
if (likely(build_sip_msg_from_buf(&pmsg, buf, len,
inc_msg_no()) == 0)) {
struct onsend_info onsnd_info;
onsnd_info.to=&(trans->uas.response.dst.to);
onsnd_info.send_sock=trans->uas.response.dst.send_sock;
onsnd_info.buf=buf;
onsnd_info.len=len;
if(sr_event_enabled(SREV_SIP_REPLY_OUT)) {
sr_event_param_t evp;
memset(&evp, 0, sizeof(sr_event_param_t));
evp.obuf.s = buf;
evp.obuf.len = len;
evp.rcv = &trans->uas.request->rcv;
evp.dst = &trans->uas.response.dst;
evp.req = trans->uas.request;
evp.rpl = &pmsg;
evp.rplcode = code;
evp.mode = 2;
sr_event_exec(SREV_SIP_REPLY_OUT, &evp);
}
p_onsend=&onsnd_info;
backup_rt = get_route_type();
set_route_type(LOCAL_ROUTE);
init_run_actions_ctx(&ctx);
if(rt >= 0 && event_rt.rlist[rt] != NULL) {
run_top_route(event_rt.rlist[rt], &pmsg, 0);
} else if(keng != NULL) {
bctx = sr_kemi_act_ctx_get();
sr_kemi_act_ctx_set(&ctx);
(void)sr_kemi_route(keng, &pmsg, EVENT_ROUTE,
&_tm_event_callback_lres_sent, &evname);
sr_kemi_act_ctx_set(bctx);
}
set_route_type(backup_rt);
p_onsend=0;
free_sip_msg(&pmsg);
} else {
LM_ERR("failed to build sip msg structure\n");
}
}
}
LM_DBG("reply sent out - buf=%p: %.20s... shmem=%p: %.20s\n",
buf, buf, rb->buffer, rb->buffer );
}
if (code>=200) {
/* start wait timer after finishing with t so that this function can
* be safely called from a fr_timer which allows quick timer dels
* (timer_allow_del()) (there's no chance of having the wait handler
* executed while we still need t) --andrei */
put_on_wait(trans);
}
pkg_free( buf ) ;
LM_DBG("finished\n");
return 1;
error3:
prepare_to_cancel(trans, &cancel_data.cancel_bitmap, 0);
error2:
if (lock) UNLOCK_REPLIES( trans );
pkg_free ( buf );
error:
/* do UAC cleanup */
cleanup_uac_timers( trans );
if ( is_invite(trans) && cancel_data.cancel_bitmap )
cancel_uacs( trans, &cancel_data, F_CANCEL_B_KILL);
/* we did not succeed -- put the transaction on wait */
put_on_wait(trans);
return -1;
}
/* send a UAS reply
* returns 1 if everything was OK or -1 for error
*/
static int _reply( struct cell *trans, struct sip_msg* p_msg,
unsigned int code, str *reason, int lock )
{
unsigned int len;
char * buf, *dset;
struct bookmark bm;
int dset_len;
if (code>=200) set_kr(REQ_RPLD);
/* compute the buffer in private memory prior to entering lock;
* create to-tag if needed */
/* if that is a redirection message, dump current message set to it */
if (code>=300 && code<400) {
dset=print_dset(p_msg, &dset_len, tm_rich_redirect);
if (dset) {
add_lump_rpl(p_msg, dset, dset_len, LUMP_RPL_HDR);
}
}
if (code>=180 && p_msg->to
&& (get_to(p_msg)->tag_value.s==0
|| get_to(p_msg)->tag_value.len==0)) {
calc_crc_suffix( p_msg, tm_tag_suffix );
buf = build_res_buf_from_sip_req(code, reason, &tm_tag, p_msg,
&len, &bm);
return _reply_light( trans, buf, len, code,
tm_tag.s, TOTAG_VALUE_LEN, lock, &bm);
} else {
buf = build_res_buf_from_sip_req(code, reason, 0 /*no to-tag*/,
p_msg, &len, &bm);
return _reply_light(trans,buf,len,code,
0, 0, /* no to-tag */lock, &bm);
}
}
/**
* structure to backup attributes for faked env
*/
typedef struct tm_faked_env {
int backup_route_type;
struct cell *backup_t;
int backup_branch;
msg_ctx_id_t ctxid;
avp_list_t* backup_user_from;
avp_list_t* backup_user_to;
avp_list_t* backup_domain_from;
avp_list_t* backup_domain_to;
avp_list_t* backup_uri_from;
avp_list_t* backup_uri_to;
sr_xavp_t **backup_xavps;
sr_xavp_t **backup_xavus;
sr_xavp_t **backup_xavis;
struct socket_info* backup_si;
struct lump *backup_add_rm;
struct lump *backup_body_lumps;
struct lump_rpl *backup_reply_lump;
} tm_faked_env_t;
#define TM_FAKED_ENV_SIZE 8
/**
* stack of faked environments
*/
static tm_faked_env_t _tm_faked_env[TM_FAKED_ENV_SIZE];
static int _tm_faked_env_idx = -1;
/** create or restore a "fake environment" for running a failure_route,
* OR an "async environment" depending on is_async_value (0=std failure-faked, 1=async)
* if msg is set -> it will fake the env. vars conforming with the msg; if NULL
* the env. will be restore to original.
* Side-effect: mark_ruri_consumed() for faked env only.
*/
int faked_env(struct cell *t, struct sip_msg *msg, int is_async_env)
{
if (msg) {
if(_tm_faked_env_idx+1>=TM_FAKED_ENV_SIZE) {
LM_ERR("too many faked environments on stack\n");
return -1;
}
_tm_faked_env_idx++;
/* remember we are back in request processing, but process
* a shmem-ed replica of the request; advertise it in route type;
* for example t_reply needs to know that
*/
_tm_faked_env[_tm_faked_env_idx].backup_route_type = get_route_type();
if (is_async_env) {
set_route_type(t->async_backup.backup_route);
if (t->async_backup.ruri_new) {
ruri_mark_new();
}
} else {
set_route_type(FAILURE_ROUTE);
/* don't bother backing up ruri state, since failure route
* is called either on reply or on timer and in both cases
* the ruri should not be used again for forking */
ruri_mark_consumed(); /* in failure route we assume ruri
* should not be used again for forking */
}
/* also, tm actions look in beginning whether transaction is
* set -- whether we are called from a reply-processing
* or a timer process, we need to set current transaction;
* otherwise the actions would attempt to look the transaction
* up (unnecessary overhead, refcounting)
*/
/* backup */
_tm_faked_env[_tm_faked_env_idx].backup_t = get_t();
_tm_faked_env[_tm_faked_env_idx].backup_branch = get_t_branch();
_tm_faked_env[_tm_faked_env_idx].ctxid.msgid = tm_global_ctx_id.msgid;
_tm_faked_env[_tm_faked_env_idx].ctxid.pid = tm_global_ctx_id.pid;
/* fake transaction and message id */
tm_global_ctx_id.msgid = msg->id;
tm_global_ctx_id.pid = msg->pid;
if (is_async_env) {
set_t(t, t->async_backup.backup_branch);
} else {
set_t(t, T_BR_UNDEFINED);
}
/* make available the avp list from transaction */
_tm_faked_env[_tm_faked_env_idx].backup_uri_from
= set_avp_list(AVP_TRACK_FROM | AVP_CLASS_URI,
&t->uri_avps_from);
_tm_faked_env[_tm_faked_env_idx].backup_uri_to
= set_avp_list(AVP_TRACK_TO | AVP_CLASS_URI,
&t->uri_avps_to);
_tm_faked_env[_tm_faked_env_idx].backup_user_from
= set_avp_list(AVP_TRACK_FROM | AVP_CLASS_USER,
&t->user_avps_from);
_tm_faked_env[_tm_faked_env_idx].backup_user_to
= set_avp_list(AVP_TRACK_TO | AVP_CLASS_USER,
&t->user_avps_to);
_tm_faked_env[_tm_faked_env_idx].backup_domain_from
= set_avp_list(AVP_TRACK_FROM | AVP_CLASS_DOMAIN,
&t->domain_avps_from);
_tm_faked_env[_tm_faked_env_idx].backup_domain_to
= set_avp_list(AVP_TRACK_TO | AVP_CLASS_DOMAIN,
&t->domain_avps_to);
_tm_faked_env[_tm_faked_env_idx].backup_xavps
= xavp_set_list(&t->xavps_list);
_tm_faked_env[_tm_faked_env_idx].backup_xavus
= xavu_set_list(&t->xavus_list);
_tm_faked_env[_tm_faked_env_idx].backup_xavis
= xavi_set_list(&t->xavis_list);
/* set default send address to the saved value */
_tm_faked_env[_tm_faked_env_idx].backup_si = bind_address;
bind_address = t->uac[0].request.dst.send_sock;
/* backup lump lists */
_tm_faked_env[_tm_faked_env_idx].backup_add_rm
= t->uas.request->add_rm;
_tm_faked_env[_tm_faked_env_idx].backup_body_lumps
= t->uas.request->body_lumps;
_tm_faked_env[_tm_faked_env_idx].backup_reply_lump
= t->uas.request->reply_lump;
} else {
if(_tm_faked_env_idx<0) {
LM_ERR("no faked environments on stack\n");
return -1;
}
/* restore original environment */
set_t(_tm_faked_env[_tm_faked_env_idx].backup_t,
_tm_faked_env[_tm_faked_env_idx].backup_branch);
tm_global_ctx_id.msgid = _tm_faked_env[_tm_faked_env_idx].ctxid.msgid;
tm_global_ctx_id.pid = _tm_faked_env[_tm_faked_env_idx].ctxid.pid;
set_route_type(_tm_faked_env[_tm_faked_env_idx].backup_route_type);
/* restore original avp list */
set_avp_list(AVP_TRACK_FROM | AVP_CLASS_USER,
_tm_faked_env[_tm_faked_env_idx].backup_user_from);
set_avp_list(AVP_TRACK_TO | AVP_CLASS_USER,
_tm_faked_env[_tm_faked_env_idx].backup_user_to);
set_avp_list(AVP_TRACK_FROM | AVP_CLASS_DOMAIN,
_tm_faked_env[_tm_faked_env_idx].backup_domain_from);
set_avp_list(AVP_TRACK_TO | AVP_CLASS_DOMAIN,
_tm_faked_env[_tm_faked_env_idx].backup_domain_to);
set_avp_list(AVP_TRACK_FROM | AVP_CLASS_URI,
_tm_faked_env[_tm_faked_env_idx].backup_uri_from);
set_avp_list(AVP_TRACK_TO | AVP_CLASS_URI,
_tm_faked_env[_tm_faked_env_idx].backup_uri_to);
xavp_set_list(_tm_faked_env[_tm_faked_env_idx].backup_xavps);
xavu_set_list(_tm_faked_env[_tm_faked_env_idx].backup_xavus);
xavi_set_list(_tm_faked_env[_tm_faked_env_idx].backup_xavis);
bind_address = _tm_faked_env[_tm_faked_env_idx].backup_si;
/* restore lump lists */
if(t!=NULL) {
t->uas.request->add_rm
= _tm_faked_env[_tm_faked_env_idx].backup_add_rm;
t->uas.request->body_lumps
= _tm_faked_env[_tm_faked_env_idx].backup_body_lumps;
t->uas.request->reply_lump
= _tm_faked_env[_tm_faked_env_idx].backup_reply_lump;
}
_tm_faked_env_idx--;
}
return 0;
}
/**
* helper function to clone back to pkg fields that can change in fake_req
*/
int fake_req_clone_str_helper(str *src, str *dst, char *txt)
{
/* src string can change -- make a private copy */
if (src->s!=0 && src->len!=0) {
dst->s=pkg_malloc(src->len+1);
if (!dst->s) {
PKG_MEM_ERROR;
return -1;
}
dst->len=src->len;
memcpy(dst->s, src->s, dst->len);
dst->s[dst->len]=0;
}else{
/* in case src->len==0, but src->s!=0 (extra safety) */
dst->s = 0;
}
return 0;
}
/**
* fake a private sip message using transaction's shared memory message
*/
struct sip_msg * fake_req(struct sip_msg *shmem_msg, int extra_flags,
struct ua_client *uac, int *len)
{
struct sip_msg *faked_req;
/* make a clone so eventual new parsed headers in pkg are not visible
* to other processes -- other attributes should be already parsed,
* available in the req structure and propagated by cloning */
faked_req = sip_msg_shm_clone(shmem_msg, len, 1);
if(faked_req==NULL) {
LM_ERR("failed to clone the request\n");
return NULL;
}
/* if we set msg_id to something different from current's message
* id, the first t_fork will properly clean new branch URIs */
faked_req->id=shmem_msg->id-1;
/* msg->parsed_uri_ok must be reset since msg_parsed_uri is
* not cloned (and cannot be cloned) */
faked_req->parsed_uri_ok = 0;
faked_req->msg_flags|=extra_flags; /* set the extra tm flags */
/* path_vec was cloned in shm and can change -- make a private copy */
if(fake_req_clone_str_helper(&shmem_msg->path_vec, &faked_req->path_vec,
"path_vec")<0) {
goto error00;
}
/* dst_uri was cloned in shm and can change -- make a private copy */
if(fake_req_clone_str_helper(&shmem_msg->dst_uri, &faked_req->dst_uri,
"dst_uri")<0) {
goto error01;
}
/* new_uri was cloned in shm and can change -- make a private copy */
if(fake_req_clone_str_helper(&shmem_msg->new_uri, &faked_req->new_uri,
"new_uri")<0) {
goto error02;
}
if(uac) setbflagsval(0, uac->branch_flags);
else setbflagsval(0, 0);
return faked_req;
error02:
if (faked_req->dst_uri.s) {
pkg_free(faked_req->dst_uri.s);
faked_req->dst_uri.s = 0;
faked_req->dst_uri.len = 0;
}
error01:
if (faked_req->path_vec.s) {
pkg_free(faked_req->path_vec.s);
faked_req->path_vec.s = 0;
faked_req->path_vec.len = 0;
}
error00:
shm_free(faked_req);
return NULL;
}
void free_faked_req(struct sip_msg *faked_req, int len)
{
struct hdr_field *hdr;
void *mstart = faked_req;
void *mend =((char *) faked_req) + len;
reset_new_uri(faked_req);
reset_dst_uri(faked_req);
/* free all types of lump that were added in failure handlers */
del_nonshm_lump( &(faked_req->add_rm) );
del_nonshm_lump( &(faked_req->body_lumps) );
del_nonshm_lump_rpl( &(faked_req->reply_lump) );
/* free header's parsed structures that were added by failure handlers */
for( hdr=faked_req->headers ; hdr ; hdr=hdr->next ) {
if ( hdr->parsed && hdr_allocs_parse(hdr) &&
(hdr->parsed<mstart || hdr->parsed>=mend)) {
/* header parsed filed doesn't point inside fake memory
* chunck -> it was added by failure funcs.-> free it as pkg */
LM_DBG("removing hdr->parsed %d\n", hdr->type);
clean_hdr_field(hdr);
hdr->parsed = 0;
}
}
/* free parsed body added by failure handlers */
if (faked_req->body) {
if(faked_req->body->free)
faked_req->body->free(&faked_req->body);
faked_req->body = 0;
}
/* free sip_msg_t fileds that can be set in pkg */
reset_path_vector(faked_req);
reset_instance(faked_req);
reset_ruid(faked_req);
reset_ua(faked_req);
msg_ldata_reset(faked_req);
/* free shared block */
shm_free(faked_req);
}
/* return 1 if failure_route was processed
* 0 - if unable to process failure_route
* -1 - if execution was long and transaction is gone
*/
int run_failure_handlers(tm_cell_t *t, struct sip_msg *rpl,
int code, int extra_flags)
{
struct sip_msg *faked_req;
int faked_req_len = 0;