/
t_fwd.c
1968 lines (1834 loc) · 60.6 KB
/
t_fwd.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
*/
#include "defs.h"
#include "../../core/dprint.h"
#include "../../core/config.h"
#include "../../core/parser/parser_f.h"
#include "../../core/ut.h"
#include "../../core/timer.h"
#include "../../core/hash_func.h"
#include "../../core/globals.h"
#include "../../core/cfg_core.h"
#include "../../core/mem/mem.h"
#include "../../core/dset.h"
#include "../../core/action.h"
#include "../../core/data_lump.h"
#include "../../core/onsend.h"
#include "../../core/compiler_opt.h"
#include "../../core/route.h"
#include "../../core/script_cb.h"
#include "t_funcs.h"
#include "t_hooks.h"
#include "t_msgbuilder.h"
#include "ut.h"
#include "t_cancel.h"
#include "t_lookup.h"
#include "t_fwd.h"
#include "t_reply.h"
#include "h_table.h"
#include "../../core/fix_lumps.h"
#include "config.h"
#ifdef USE_DNS_FAILOVER
#include "../../core/dns_cache.h"
#include "../../core/cfg_core.h" /* cfg_get(core, core_cfg, use_dns_failover) */
#include "../../core/msg_translator.h"
#include "lw_parser.h"
#endif
#ifdef USE_DST_BLACKLIST
#include "../../core/dst_blacklist.h"
#endif
#include "../../core/atomic_ops.h" /* membar_depends() */
#include "../../core/kemi.h"
extern int tm_failure_exec_mode;
extern int tm_dns_reuse_rcv_socket;
static int goto_on_branch = 0, branch_route = 0;
void t_on_branch( 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=go_to;
} else {
t->on_branch = go_to;
}
}
unsigned int get_on_branch(void)
{
return goto_on_branch;
}
void set_branch_route( unsigned int on_branch)
{
branch_route = on_branch;
}
/** prepares a new branch "buffer".
* Creates the buffer used in the branch rb, fills everything needed (
* the sending information: t->uac[branch].request.dst, branch buffer, uri
* path vector a.s.o.) and runs the on_branch route.
* t->uac[branch].request.dst will be filled if next_hop !=0 with the result
* of the DNS resolution (next_hop, fproto and fsocket).
* If next_hop is 0 all the dst members except the send_flags are read-only
* (send_flags it's updated) and are supposed to be pre-filled.
*
* @param t - transaction
* @param i_req - corresponding sip_msg, must be non-null, flags might be
* be modified (on_branch route)
* @param branch - branch no
* @param uri
* @param path - path vector (list of route like destination in text form,
* e.g.: "<sip:1.2.3.4;lr>, <sip:5.6.7.8;lr>")
* @param next_hop - uri of the next hop. If non 0 it will be used
* for DNS resolution and the branch request.dst structure will
* be filled. If 0 the branch must already have
* a pre-filled valid request.dst.
* @param fsocket - forced send socket for forwarding.
* @param send_flags - special flags for sending (see SND_F_* / snd_flags_t).
* @param fproto - forced proto for forwarding. Used only if next_hop!=0.
* @param flags - 0, UAC_DNS_FAILOVER_F or UAC_SKIP_BR_DST_F for now.
*
* @return 0 on success, < 0 (ser_errror E***) on failure.
*/
static int prepare_new_uac( struct cell *t, struct sip_msg *i_req,
int branch, str *uri, str* path,
str* next_hop,
struct socket_info* fsocket,
snd_flags_t snd_flags,
int fproto, int flags,
str *instance, str *ruid,
str *location_ua)
{
char *shbuf;
struct lump* add_rm_backup, *body_lumps_backup;
struct sip_uri parsed_uri_bak;
int ret;
unsigned int len;
int parsed_uri_ok_bak, free_new_uri;
str msg_uri_bak;
str dst_uri_bak;
int dst_uri_backed_up;
str path_bak;
int free_path;
str instance_bak;
int free_instance;
str ruid_bak;
int free_ruid;
str ua_bak;
int free_ua;
int backup_route_type;
int test_dst;
snd_flags_t fwd_snd_flags_bak;
snd_flags_t rpl_snd_flags_bak;
struct socket_info *force_send_socket_bak;
struct dest_info *dst;
struct run_act_ctx ctx;
struct run_act_ctx *bctx;
sr_kemi_eng_t *keng;
shbuf=0;
ret=E_UNSPEC;
msg_uri_bak.s=0; /* kill warnings */
msg_uri_bak.len=0;
parsed_uri_ok_bak=0;
free_new_uri=0;
dst_uri_bak.s=0;
dst_uri_bak.len=0;
dst_uri_backed_up=0;
path_bak.s=0;
path_bak.len=0;
free_path=0;
instance_bak.s=0;
instance_bak.len=0;
free_instance=0;
ruid_bak.s=0;
ruid_bak.len=0;
free_ruid=0;
ua_bak.s=0;
ua_bak.len=0;
free_ua=0;
dst=&t->uac[branch].request.dst;
/* ... we calculate branch ... */
if (!t_calc_branch(t, branch, i_req->add_to_branch_s,
&i_req->add_to_branch_len ))
{
LM_ERR("branch computation failed\n");
ret=E_UNSPEC;
goto error00;
}
/* dup lumps
* TODO: clone lumps only if needed */
/* lumps can be set outside of the lock, make sure that we read
* the up-to-date values */
membar_depends();
add_rm_backup = i_req->add_rm;
body_lumps_backup = i_req->body_lumps;
if (unlikely(i_req->add_rm)){
i_req->add_rm = dup_lump_list(i_req->add_rm);
if (unlikely(i_req->add_rm==0)){
ret=E_OUT_OF_MEM;
goto error06;
}
}
if (unlikely(i_req->body_lumps)){
i_req->body_lumps = dup_lump_list(i_req->body_lumps);
if (unlikely(i_req->body_lumps==0)){
ret=E_OUT_OF_MEM;
goto error05;
}
}
/* backup uri & path: we need to change them so that build_req...()
* will use uri & path and not the ones in the original msg (i_req)
* => we must back them up so that we can restore them to the original
* value after building the send buffer */
msg_uri_bak=i_req->new_uri;
parsed_uri_bak=i_req->parsed_uri;
parsed_uri_ok_bak=i_req->parsed_uri_ok;
path_bak=i_req->path_vec;
instance_bak=i_req->instance;
ruid_bak=i_req->ruid;
ua_bak=i_req->location_ua;
if (unlikely(branch_route || has_tran_tmcbs(t, TMCB_REQUEST_FWDED))){
/* dup uris, path a.s.o. if we have a branch route or callback */
/* ... set ruri ... */
/* if uri points to new_uri, it needs to be "fixed" so that we can
* change msg->new_uri */
if (uri==&i_req->new_uri)
uri=&msg_uri_bak;
i_req->parsed_uri_ok=0;
i_req->new_uri.s=pkg_malloc(uri->len);
if (unlikely(i_req->new_uri.s==0)){
ret=E_OUT_OF_MEM;
goto error03;
}
free_new_uri=1;
memcpy(i_req->new_uri.s, uri->s, uri->len);
i_req->new_uri.len=uri->len;
/* update path_vec */
/* if path points to msg path_vec, it needs to be "fixed" so that we
* can change/update msg->path_vec */
if (path==&i_req->path_vec)
path=&path_bak;
/* zero it first so that set_path_vector will work */
i_req->path_vec.s=0;
i_req->path_vec.len=0;
if (unlikely(path)){
if (unlikely(set_path_vector(i_req, path)<0)){
ret=E_OUT_OF_MEM;
goto error03;
}
free_path=1;
}
/* update instance */
/* if instance points to msg instance, it needs to be "fixed" so that we
* can change/update msg->instance */
if (instance==&i_req->instance)
instance=&instance_bak;
/* zero it first so that set_instance will work */
i_req->instance.s=0;
i_req->instance.len=0;
if (unlikely(instance)){
if (unlikely(set_instance(i_req, instance)<0)){
ret=E_OUT_OF_MEM;
goto error03;
}
free_instance=1;
}
/* update ruid */
/* if ruid points to msg ruid, it needs to be "fixed" so that we
* can change/update msg->ruid */
if (ruid==&i_req->ruid)
ruid=&ruid_bak;
/* zero it first so that set_ruid will work */
i_req->ruid.s=0;
i_req->ruid.len=0;
if (unlikely(ruid)){
if (unlikely(set_ruid(i_req, ruid)<0)){
ret=E_OUT_OF_MEM;
goto error03;
}
free_ruid=1;
}
/* update location_ua */
/* if location_ua points to msg location_ua, it needs to be "fixed"
* so that we can change/update msg->location_ua */
if (location_ua==&i_req->location_ua)
location_ua=&ua_bak;
/* zero it first so that set_ua will work */
i_req->location_ua.s=0;
i_req->location_ua.len=0;
if (unlikely(location_ua)){
if (unlikely(set_ua(i_req, location_ua)<0)){
ret=E_OUT_OF_MEM;
goto error03;
}
free_ua=1;
}
/* backup dst uri & zero it*/
dst_uri_bak=i_req->dst_uri;
dst_uri_backed_up=1;
/* if next_hop points to dst_uri, it needs to be "fixed" so that we
* can change msg->dst_uri */
if (next_hop==&i_req->dst_uri)
next_hop=&dst_uri_bak;
/* zero it first so that set_dst_uri will work */
i_req->dst_uri.s=0;
i_req->dst_uri.len=0;
if (likely(next_hop)){
if(unlikely((flags & UAC_SKIP_BR_DST_F)==0)){
/* set dst uri to next_hop for the on_branch route */
if (unlikely(set_dst_uri(i_req, next_hop)<0)){
ret=E_OUT_OF_MEM;
goto error03;
}
}
}
if (likely(branch_route)) {
/* run branch_route actions if provided */
backup_route_type = get_route_type();
set_route_type(BRANCH_ROUTE);
tm_ctx_set_branch_index(branch);
/* no need to backup/set avp lists: the on_branch route is run
* only in the main route context (e.g. t_relay() in the main
* route) or in the failure route context (e.g. append_branch &
* t_relay()) and in both cases the avp lists are properly set
* Note: the branch route is not run on delayed dns failover
* (for that to work one would have to set branch_route prior to
* calling add_uac(...) and then reset it afterwards).
*/
if (exec_pre_script_cb(i_req, BRANCH_CB_TYPE)>0) {
/* backup ireq msg send flags and force_send_socket*/
fwd_snd_flags_bak=i_req->fwd_send_flags;;
rpl_snd_flags_bak=i_req->rpl_send_flags;
force_send_socket_bak=i_req->force_send_socket;
/* set the new values */
i_req->fwd_send_flags=snd_flags /* intial value */;
set_force_socket(i_req, fsocket);
keng = sr_kemi_eng_get();
if(unlikely(keng!=NULL)) {
bctx = sr_kemi_act_ctx_get();
init_run_actions_ctx(&ctx);
sr_kemi_act_ctx_set(&ctx);
if(sr_kemi_route(keng, i_req, BRANCH_ROUTE,
sr_kemi_cbname_lookup_idx(branch_route), NULL)<0) {
LM_ERR("error running branch route kemi callback\n");
}
sr_kemi_act_ctx_set(bctx);
} else {
if (run_top_route(branch_rt.rlist[branch_route],
i_req, &ctx) < 0) {
LM_DBG("negative return code in run_top_route\n");
}
}
/* update dst send_flags and send socket*/
snd_flags=i_req->fwd_send_flags;
fsocket=i_req->force_send_socket;
/* restore ireq_msg force_send_socket & flags */
set_force_socket(i_req, force_send_socket_bak);
i_req->fwd_send_flags=fwd_snd_flags_bak;
i_req->rpl_send_flags=rpl_snd_flags_bak;
exec_post_script_cb(i_req, BRANCH_CB_TYPE);
/* if DROP was called in cfg, don't forward, jump to end */
if (unlikely(ctx.run_flags&DROP_R_F))
{
tm_ctx_set_branch_index(T_BR_UNDEFINED);
set_route_type(backup_route_type);
/* triggered by drop in CFG */
ret=E_CFG;
goto error03;
}
}
tm_ctx_set_branch_index(T_BR_UNDEFINED);
set_route_type(backup_route_type);
}
/* run the specific callbacks for this transaction */
if (unlikely(has_tran_tmcbs(t, TMCB_REQUEST_FWDED)))
run_trans_callbacks( TMCB_REQUEST_FWDED , t, i_req, 0,
-i_req->REQ_METHOD);
if (likely( !(flags & UAC_DNS_FAILOVER_F) && i_req->dst_uri.s &&
i_req->dst_uri.len)){
/* no dns failover and non-empty dst_uri => use it as dst
* (on dns failover dns_h will be non-empty => next_hop will be
* ignored) */
next_hop=&i_req->dst_uri;
}
/* no path vector initially, but now is set after branch route and
* callbacks execution */
if(i_req->path_vec.s!=0 && free_path==0)
free_path=1;
}else{
/* no branch route and no TMCB_REQUEST_FWDED callback => set
* msg uri and path to the new values (if needed) */
if (unlikely((uri->s!=i_req->new_uri.s || uri->len!=i_req->new_uri.len)
&& (i_req->new_uri.s!=0 ||
uri->s!=i_req->first_line.u.request.uri.s ||
uri->len!=i_req->first_line.u.request.uri.len) )){
/* uri is different from i_req uri => replace i_req uri and force
* uri re-parsing */
i_req->new_uri=*uri;
i_req->parsed_uri_ok=0;
}
if (unlikely(path && (i_req->path_vec.s!=path->s ||
i_req->path_vec.len!=path->len))){
i_req->path_vec=*path;
}else if (unlikely(path==0 && i_req->path_vec.len!=0)){
i_req->path_vec.s=0;
i_req->path_vec.len=0;
}
if (unlikely(instance && (i_req->instance.s!=instance->s ||
i_req->instance.len!=instance->len))){
i_req->instance=*instance;
}else if (unlikely(instance==0 && i_req->instance.len!=0)){
i_req->instance.s=0;
i_req->instance.len=0;
}
if (unlikely(ruid && (i_req->ruid.s!=ruid->s ||
i_req->ruid.len!=ruid->len))){
i_req->ruid=*ruid;
}else if (unlikely(ruid==0 && i_req->ruid.len!=0)){
i_req->ruid.s=0;
i_req->ruid.len=0;
}
if (unlikely(location_ua && (i_req->location_ua.s!=location_ua->s ||
i_req->location_ua.len!=location_ua->len))){
i_req->location_ua=*location_ua;
}else if (unlikely(location_ua==0 && i_req->location_ua.len!=0)){
i_req->location_ua.s=0;
i_req->location_ua.len=0;
}
}
if (likely(next_hop!=0 || (flags & UAC_DNS_FAILOVER_F))){
/* next_hop present => use it for dns resolution */
#ifdef USE_DNS_FAILOVER
test_dst = (uri2dst2(&t->uac[branch].dns_h, dst, fsocket, snd_flags,
next_hop?next_hop:uri, fproto) == 0);
#else
/* dst filled from the uri & request (send_socket) */
test_dst = (uri2dst2(dst, fsocket, snd_flags,
next_hop?next_hop:uri, fproto)==0);
#endif
if (test_dst){
ret=E_BAD_ADDRESS;
goto error01;
}
}
/* else next_hop==0 =>
* no dst_uri / empty dst_uri and initial next_hop==0 =>
* dst is pre-filled with a valid dst => use the pre-filled dst */
/* Set on_reply and on_negative handlers for this branch to the handlers in the transaction */
t->uac[branch].on_reply = t->on_reply;
t->uac[branch].on_failure = t->on_failure;
t->uac[branch].on_branch_failure = t->on_branch_failure;
/* check if send_sock is ok */
if (t->uac[branch].request.dst.send_sock==0) {
LM_ERR("can't fwd to af %d, proto %d "
" (no corresponding listening socket)\n",
dst->to.s.sa_family, dst->proto );
ret=E_NO_SOCKET;
goto error01;
}
/* ... and build it now */
shbuf=build_req_buf_from_sip_req( i_req, &len, dst, BUILD_IN_SHM);
if (!shbuf) {
LM_ERR("could not build request\n");
ret=E_OUT_OF_MEM;
goto error01;
}
#ifdef DBG_MSG_QA
if (shbuf[len-1]==0) {
LM_ERR("sanity check failed\n");
abort();
}
#endif
/* things went well, move ahead and install new buffer! */
t->uac[branch].request.buffer=shbuf;
t->uac[branch].request.buffer_len=len;
t->uac[branch].uri.s=t->uac[branch].request.buffer+
i_req->first_line.u.request.method.len+1;
t->uac[branch].uri.len=GET_RURI(i_req)->len;
if (unlikely(i_req->path_vec.s && i_req->path_vec.len)){
t->uac[branch].path.s=shm_malloc(i_req->path_vec.len+1);
if (unlikely(t->uac[branch].path.s==0)) {
shm_free(shbuf);
t->uac[branch].request.buffer=0;
t->uac[branch].request.buffer_len=0;
t->uac[branch].uri.s=0;
t->uac[branch].uri.len=0;
ret=E_OUT_OF_MEM;
goto error01;
}
t->uac[branch].path.len=i_req->path_vec.len;
t->uac[branch].path.s[i_req->path_vec.len]=0;
memcpy( t->uac[branch].path.s, i_req->path_vec.s, i_req->path_vec.len);
}
if (unlikely(i_req->instance.s && i_req->instance.len)){
t->uac[branch].instance.s=shm_malloc(i_req->instance.len+1);
if (unlikely(t->uac[branch].instance.s==0)) {
shm_free(shbuf);
t->uac[branch].request.buffer=0;
t->uac[branch].request.buffer_len=0;
t->uac[branch].uri.s=0;
t->uac[branch].uri.len=0;
ret=E_OUT_OF_MEM;
goto error01;
}
t->uac[branch].instance.len=i_req->instance.len;
t->uac[branch].instance.s[i_req->instance.len]=0;
memcpy( t->uac[branch].instance.s, i_req->instance.s, i_req->instance.len);
}
if (unlikely(i_req->ruid.s && i_req->ruid.len)){
t->uac[branch].ruid.s=shm_malloc(i_req->ruid.len+1);
if (unlikely(t->uac[branch].ruid.s==0)) {
shm_free(shbuf);
t->uac[branch].request.buffer=0;
t->uac[branch].request.buffer_len=0;
t->uac[branch].uri.s=0;
t->uac[branch].uri.len=0;
ret=E_OUT_OF_MEM;
goto error01;
}
t->uac[branch].ruid.len=i_req->ruid.len;
t->uac[branch].ruid.s[i_req->ruid.len]=0;
memcpy( t->uac[branch].ruid.s, i_req->ruid.s, i_req->ruid.len);
}
if (unlikely(i_req->location_ua.s && i_req->location_ua.len)){
t->uac[branch].location_ua.s=shm_malloc(i_req->location_ua.len+1);
if (unlikely(t->uac[branch].location_ua.s==0)) {
shm_free(shbuf);
t->uac[branch].request.buffer=0;
t->uac[branch].request.buffer_len=0;
t->uac[branch].uri.s=0;
t->uac[branch].uri.len=0;
ret=E_OUT_OF_MEM;
goto error01;
}
t->uac[branch].location_ua.len=i_req->location_ua.len;
t->uac[branch].location_ua.s[i_req->location_ua.len]=0;
memcpy( t->uac[branch].location_ua.s, i_req->location_ua.s,
i_req->location_ua.len);
}
len = count_applied_lumps(i_req->add_rm, HDR_RECORDROUTE_T);
if(len==1)
t->uac[branch].flags = TM_UAC_FLAG_RR;
else if(len==2)
t->uac[branch].flags = TM_UAC_FLAG_RR|TM_UAC_FLAG_R2;
ret=0;
error01:
error03:
/* restore the new_uri & path from the backup */
if (unlikely(free_new_uri && i_req->new_uri.s)){
pkg_free(i_req->new_uri.s);
}
if (unlikely(free_path)){
reset_path_vector(i_req);
}
if (unlikely(free_instance)){
reset_instance(i_req);
}
if (unlikely(free_ruid)){
reset_ruid(i_req);
}
if (unlikely(free_ua)){
reset_ua(i_req);
}
if (dst_uri_backed_up){
reset_dst_uri(i_req); /* free dst_uri */
i_req->dst_uri=dst_uri_bak;
}
/* restore original new_uri and path values */
i_req->new_uri=msg_uri_bak;
i_req->parsed_uri=parsed_uri_bak;
i_req->parsed_uri_ok=parsed_uri_ok_bak;
i_req->path_vec=path_bak;
i_req->instance=instance_bak;
i_req->ruid=ruid_bak;
i_req->location_ua=ua_bak;
/* Delete the duplicated lump lists, this will also delete
* all lumps created here, such as lumps created in per-branch
* routing sections, Via, and Content-Length headers created in
* build_req_buf_from_sip_req().
*/
error04:
free_duped_lump_list(i_req->body_lumps);
error05:
free_duped_lump_list(i_req->add_rm);
error06:
/* Restore the lists from backups. */
i_req->add_rm = add_rm_backup;
i_req->body_lumps = body_lumps_backup;
error00:
return ret;
}
#ifdef USE_DNS_FAILOVER
/* Similar to print_uac_request(), but this function uses the outgoing message
* buffer of the failed branch to construct the new message in case of DNS
* failover.
*
* WARNING: only the first VIA header is replaced in the buffer, the rest
* of the message is untouched, thus, the send socket is corrected only in the
* VIA HF.
*/
static char *print_uac_request_from_buf( struct cell *t, struct sip_msg *i_req,
int branch, str *uri, unsigned int *len, struct dest_info* dst,
char *buf, short buf_len)
{
char *shbuf;
str branch_str;
char *via, *old_via_begin, *old_via_end;
unsigned int via_len;
shbuf=0;
/* ... we calculate branch ... */
if (!t_calc_branch(t, branch, i_req->add_to_branch_s,
&i_req->add_to_branch_len ))
{
LM_ERR("branch computation failed\n");
goto error00;
}
branch_str.s = i_req->add_to_branch_s;
branch_str.len = i_req->add_to_branch_len;
/* find the beginning of the first via header in the buffer */
old_via_begin = lw_find_via(buf, buf+buf_len);
if (!old_via_begin) {
LM_ERR("beginning of via header not found\n");
goto error00;
}
/* find the end of the first via header in the buffer */
old_via_end = lw_next_line(old_via_begin, buf+buf_len);
if (!old_via_end) {
LM_ERR("end of via header not found\n");
goto error00;
}
/* create the new VIA HF */
via = create_via_hf(&via_len, i_req, dst, &branch_str);
if (!via) {
LM_ERR("via building failed\n");
goto error00;
}
/* allocate memory for the new buffer */
*len = buf_len + via_len - (old_via_end - old_via_begin);
shbuf=(char *)shm_malloc(*len);
if (!shbuf) {
ser_error=E_OUT_OF_MEM;
LM_ERR("no shmem\n");
goto error01;
}
/* construct the new buffer */
memcpy(shbuf, buf, old_via_begin-buf);
memcpy(shbuf+(old_via_begin-buf), via, via_len);
memcpy(shbuf+(old_via_begin-buf)+via_len, old_via_end, (buf+buf_len)-old_via_end);
#ifdef DBG_MSG_QA
if (shbuf[*len-1]==0) {
LM_ERR("sanity check failed\n");
abort();
}
#endif
error01:
pkg_free(via);
error00:
return shbuf;
}
#endif
/* introduce a new uac, which is blind -- it only creates the
* data structures and starts FR timer, but that's it; it does
* not print messages and send anything anywhere; that is good
* for FIFO apps -- the transaction must look operationally
* and FR must be ticking, whereas the request is "forwarded"
* using a non-SIP way and will be replied the same way
*/
int add_blind_uac( /*struct cell *t*/ )
{
unsigned short branch;
struct cell *t;
t=get_t();
if (t==T_UNDEFINED || !t ) {
LM_ERR("no transaction context\n");
return -1;
}
branch=t->nr_of_outgoings;
if (branch==sr_dst_max_branches) {
LM_ERR("maximum number of branches exceeded\n");
return -1;
}
/* make sure it will be replied */
t->flags |= T_NOISY_CTIMER_FLAG;
membar_write(); /* to allow lockless prepare_to_cancel() we want to be sure
* all the writes finished before updating branch number*/
t->uac[branch].flags |= TM_UAC_FLAG_BLIND;
t->nr_of_outgoings=(branch+1);
t->async_backup.blind_uac = branch;
/* ^^^ whenever we create a blind UAC, lets save the current branch
* this is used in async tm processing specifically to be able to route replies
* that were possibly in response to a request forwarded on this blind UAC......
* we still want replies to be processed as if it were a normal UAC */
/* start FR timer -- protocol set by default to PROTO_NONE,
* which means retransmission timer will not be started
*/
if (start_retr(&t->uac[branch].request)!=0)
LM_CRIT("start retr failed for %p\n", &t->uac[branch].request);
/* we are on a timer -- don't need to put on wait on script clean-up */
set_kr(REQ_FWDED);
return 1; /* success */
}
/** introduce a new uac to transaction.
* It doesn't send a message yet -- a reply to it might interfere with the
* processes of adding multiple branches; On error returns <0 & sets ser_error
* to the same value.
* @param t - transaction
* @param request - corresponding sip_mst, must be non-null, flags might be
* modified (on_branch route).
* @param uri - uri used for the branch (must be non-null).
* @param next_hop - next_hop in sip uri format. If null and proxy is null
* too, the uri will be used
* @param path - path vector (list of route like destinations in sip
* uri format, e.g.: "<sip:1.2.3.4;lr>, <sip:5.6.7.8;lr>").
* @param proxy - proxy structure. If non-null it takes precedence over
* next_hop/uri and it will be used for forwarding.
* @param fsocket - forced forward send socket (can be 0).
* @param snd_flags - special send flags (see SND_F_* / snd_flags_t)
* @param proto - forced protocol for forwarding (overrides the protocol
* in next_hop/uri or proxy if != PROTO_NONE).
* @param flags - special flags passed to prepare_new_uac().
* @see prepare_new_uac().
* @returns branch id (>=0) or error (<0)
*/
int add_uac( struct cell *t, struct sip_msg *request, str *uri,
str* next_hop, str* path, struct proxy_l *proxy,
struct socket_info* fsocket, snd_flags_t snd_flags,
int proto, int flags, str *instance, str *ruid,
str *location_ua)
{
int ret;
unsigned short branch;
branch=t->nr_of_outgoings;
if (branch==sr_dst_max_branches) {
LM_ERR("maximum number of branches exceeded\n");
ret=ser_error=E_TOO_MANY_BRANCHES;
goto error;
}
/* check existing buffer -- rewriting should never occur */
if (t->uac[branch].request.buffer) {
LM_CRIT("buffer rewrite attempt\n");
ret=ser_error=E_BUG;
goto error;
}
/* check DNS resolution */
if (proxy){
/* dst filled from the proxy */
init_dest_info(&t->uac[branch].request.dst);
t->uac[branch].request.dst.proto=get_proto(proto, proxy->proto);
proxy2su(&t->uac[branch].request.dst.to, proxy);
/* fill dst send_sock */
t->uac[branch].request.dst.send_sock =
get_send_socket( request, &t->uac[branch].request.dst.to,
t->uac[branch].request.dst.proto);
if (request)
t->uac[branch].request.dst.send_flags=request->fwd_send_flags;
else
SND_FLAGS_INIT(&t->uac[branch].request.dst.send_flags);
next_hop=0;
}else {
next_hop= next_hop?next_hop:uri;
}
/* now message printing starts ... */
if (unlikely( (ret=prepare_new_uac(t, request, branch, uri, path,
next_hop, fsocket, snd_flags,
proto, flags, instance, ruid,
location_ua)) < 0)){
ser_error=ret;
goto error01;
}
getbflagsval(0, &t->uac[branch].branch_flags);
membar_write(); /* to allow lockless ops (e.g. prepare_to_cancel()) we want
* to be sure everything above is fully written before
* updating branches no. */
t->nr_of_outgoings=(branch+1);
/* update stats */
if (proxy){
proxy_mark(proxy, 1);
}
/* done! */
ret=branch;
error01:
error:
return ret;
}
#ifdef USE_DNS_FAILOVER
/* Similar to add_uac(), but this function uses the outgoing message buffer
* of the failed branch to construct the new message in case of DNS failover.
*/
static int add_uac_from_buf( struct cell *t, struct sip_msg *request,
str *uri, str* path,
struct socket_info* fsocket,
snd_flags_t send_flags,
int proto,
char *buf, short buf_len,
str *instance, str *ruid,
str *location_ua)
{
int ret;
unsigned short branch;
char *shbuf;
unsigned int len;
branch=t->nr_of_outgoings;
if (branch==sr_dst_max_branches) {
LM_ERR("maximum number of branches exceeded\n");
ret=ser_error=E_TOO_MANY_BRANCHES;
goto error;
}
/* check existing buffer -- rewriting should never occur */
if (t->uac[branch].request.buffer) {
LM_CRIT("buffer rewrite attempt\n");
ret=ser_error=E_BUG;
goto error;
}
if (uri2dst2(&t->uac[branch].dns_h, &t->uac[branch].request.dst,
fsocket, send_flags, uri, proto) == 0)
{
ret=ser_error=E_BAD_ADDRESS;
goto error;
}
/* check if send_sock is ok */
if (t->uac[branch].request.dst.send_sock==0) {
LM_ERR("can't fwd to af %d, proto %d"
" (no corresponding listening socket)\n",
t->uac[branch].request.dst.to.s.sa_family,
t->uac[branch].request.dst.proto );
ret=ser_error=E_NO_SOCKET;
goto error;
}
/* now message printing starts ... */
shbuf=print_uac_request_from_buf( t, request, branch, uri,
&len, &t->uac[branch].request.dst,
buf, buf_len);
if (!shbuf) {
ret=ser_error=E_OUT_OF_MEM;
goto error;
}
/* things went well, move ahead and install new buffer! */
t->uac[branch].request.buffer=shbuf;
t->uac[branch].request.buffer_len=len;
t->uac[branch].uri.s=t->uac[branch].request.buffer+
request->first_line.u.request.method.len+1;
t->uac[branch].uri.len=uri->len;
/* copy the path */
if (unlikely(path && path->s)){
t->uac[branch].path.s=shm_malloc(path->len+1);
if (unlikely(t->uac[branch].path.s==0)) {
shm_free(shbuf);
t->uac[branch].request.buffer=0;
t->uac[branch].request.buffer_len=0;
t->uac[branch].uri.s=0;
t->uac[branch].uri.len=0;
ret=ser_error=E_OUT_OF_MEM;
goto error;
}
t->uac[branch].path.len=path->len;
t->uac[branch].path.s[path->len]=0;
memcpy( t->uac[branch].path.s, path->s, path->len);
}
/* copy the instance */
if (unlikely(instance && instance->s)){
t->uac[branch].instance.s=shm_malloc(instance->len+1);
if (unlikely(t->uac[branch].instance.s==0)) {
shm_free(shbuf);
t->uac[branch].request.buffer=0;
t->uac[branch].request.buffer_len=0;
t->uac[branch].uri.s=0;
t->uac[branch].uri.len=0;
ret=ser_error=E_OUT_OF_MEM;
goto error;
}
t->uac[branch].instance.len=instance->len;
t->uac[branch].instance.s[instance->len]=0;
memcpy( t->uac[branch].instance.s, instance->s, instance->len);
}
/* copy the ruid */
if (unlikely(ruid && ruid->s)){
t->uac[branch].ruid.s=shm_malloc(ruid->len+1);
if (unlikely(t->uac[branch].ruid.s==0)) {
shm_free(shbuf);
t->uac[branch].request.buffer=0;
t->uac[branch].request.buffer_len=0;
t->uac[branch].uri.s=0;
t->uac[branch].uri.len=0;
ret=ser_error=E_OUT_OF_MEM;
goto error;
}
t->uac[branch].ruid.len=ruid->len;
t->uac[branch].ruid.s[ruid->len]=0;
memcpy( t->uac[branch].ruid.s, ruid->s, ruid->len);
}
/* copy the location_ua */
if (unlikely(location_ua && location_ua->s)){
t->uac[branch].location_ua.s=shm_malloc(location_ua->len+1);
if (unlikely(t->uac[branch].location_ua.s==0)) {
shm_free(shbuf);
t->uac[branch].request.buffer=0;
t->uac[branch].request.buffer_len=0;
t->uac[branch].uri.s=0;
t->uac[branch].uri.len=0;
ret=ser_error=E_OUT_OF_MEM;
goto error;
}
t->uac[branch].location_ua.len=location_ua->len;
t->uac[branch].location_ua.s[location_ua->len]=0;
memcpy( t->uac[branch].location_ua.s, location_ua->s, location_ua->len);
}
t->uac[branch].on_reply = t->on_reply;
t->uac[branch].on_failure = t->on_failure;
t->uac[branch].on_branch_failure = t->on_branch_failure;
membar_write(); /* to allow lockless ops (e.g. prepare_to_cancel()) we want
* to be sure everything above is fully written before
* updating branches no. */
t->nr_of_outgoings=(branch+1);
/* done! */
ret=branch;
error:
return ret;
}
/* introduce a new uac to transaction, based on old_uac and a possible
* new ip address (if the dns name resolves to more ips). If no more
* ips are found => returns -1.
* returns its branch id (>=0)
* or error (<0) and sets ser_error if needed; it doesn't send a message
* yet -- a reply to it
* might interfere with the processes of adding multiple branches
* if lock_replies is 1 replies will be locked for t until the new branch
* is added (to prevent add branches races). Use 0 if the reply lock is
* already held, e.g. in failure route/handlers (WARNING: using 1 in a
* failure route will cause a deadlock).
*/
int add_uac_dns_fallback(struct cell *t, struct sip_msg* msg,
struct ua_client* old_uac,
int lock_replies)
{
int ret;
ret=-1;
if (cfg_get(core, core_cfg, use_dns_failover) &&
!((t->flags & (T_DONT_FORK|T_DISABLE_FAILOVER)) ||
uac_dont_fork(old_uac)) &&
dns_srv_handle_next(&old_uac->dns_h, 0)){
if (lock_replies){