-
Notifications
You must be signed in to change notification settings - Fork 587
/
Copy patht_reply.c
1904 lines (1675 loc) · 54.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) 2010-2014 OpenSIPS Solutions
* Copyright (C) 2001-2003 FhG Fokus
*
* 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:
* --------
* 2003-01-19 faked lump list created in on_reply handlers
* 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
* 2003-02-13 updated to use rb->dst (andrei)
* 2003-02-18 replaced TOTAG_LEN w/ TOTAG_VALUE_LEN (TOTAG_LEN was defined
* twice with different values!) (andrei)
* 2003-02-28 scratchpad compatibility abandoned (jiri)
* 2003-03-01 kr set through a function now (jiri)
* 2003-03-06 saving of to-tags for ACK/200 matching introduced,
* voicemail changes accepted, updated to new callback
* names (jiri)
* 2003-03-10 fixed new to tag bug/typo (if w/o {}) (andrei)
* 2003-03-16 removed _TOTAG (jiri)
* 2003-03-31 200 for INVITE/UAS resent even for UDP (jiri)
* 2003-03-31 removed msg->repl_add_rm (andrei)
* 2003-04-05 s/reply_route/failure_route, onreply_route introduced (jiri)
* 2003-04-14 local acks generated before reply processing to avoid
* delays in length reply processing (like opening TCP
* connection to an unavailable destination) (jiri)
* 2003-09-11 updates to new build_res_buf_from_sip_req() interface (bogdan)
* 2003-09-11 t_reply_with_body() reshaped to use reply_lumps +
* build_res_buf_from_sip_req() instead of
* build_res_buf_with_body_from_sip_req() (bogdan)
* 2003-11-05 flag context updated from failure/reply handlers back
* to transaction context (jiri)
* 2003-11-11: build_lump_rpl() removed, add_lump_rpl() has flags (bogdan)
* 2003-12-04 global TM callbacks switched to per transaction callbacks
* (bogdan)
* 2004-02-06: support for user pref. added - destroy_avps (bogdan)
* 2003-11-05 flag context updated from failure/reply handlers back
* to transaction context (jiri)
* 2003-11-11: build_lump_rpl() removed, add_lump_rpl() has flags (bogdan)
* 2004-02-13: t->is_invite and t->local replaced with flags (bogdan)
* 2004-02-18 fifo_t_reply imported from vm module (bogdan)
* 2004-08-23 avp list is available from failure/on_reply routes (bogdan)
* 2004-10-01 added a new param.: restart_fr_on_each_reply (andrei)
* 2005-03-01 force for statefull replies the incoming interface of
* the request (bogdan)
* 2005-03-01 local ACK sent to same address as INVITE ->
* all [build|send]_[local]_ack functions merged into
* send_ack() (bogdan)
* 2007-01-25 DNS failover at transaction level added (bogdan)
*/
#include "../../hash_func.h"
#include "../../dprint.h"
#include "../../config.h"
#include "../../parser/parser_f.h"
#include "../../parser/msg_parser.h"
#include "../../ut.h"
#include "../../timer.h"
#include "../../error.h"
#include "../../action.h"
#include "../../dset.h"
#include "../../tags.h"
#include "../../data_lump.h"
#include "../../data_lump_rpl.h"
#include "../../usr_avp.h"
#include "../../receive.h"
#include "../../msg_callbacks.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 "fix_lumps.h"
#include "t_stats.h"
#include "uac.h"
/* restart fr timer on each provisional reply, default yes */
int restart_fr_on_each_reply=1;
int onreply_avp_mode = 0;
/* disable the 6xx fork-blocking - default no (as per RFC3261) */
int disable_6xx_block = 0;
/* flag for marking minor branches */
int minor_branch_flag = -1;
char *minor_branch_flag_str = 0;
/* private place where we create to-tags for replies */
char tm_tags[TOTAG_VALUE_LEN];
static str tm_tag = {tm_tags,TOTAG_VALUE_LEN};
char *tm_tag_suffix;
static int picked_branch=-1;
/* where to go if there is no positive reply */
static struct script_route_ref *goto_on_negative = NULL;
/* where to go on receipt of reply */
static struct script_route_ref *goto_on_reply = NULL;
/* currently processed branch */
extern int _tm_branch_index;
static struct sip_msg dummy_msg;
/* 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_negative( struct script_route_ref *ref )
{
struct cell *t = get_t();
struct script_route_ref **holder;
/* in MODE_REPLY and MODE_ONFAILURE T will be set to current transaction;
* in MODE_REQUEST T will be set only if the transaction was already
* created; if not -> use the static variable */
holder = (!t || t==T_UNDEFINED ) ? &goto_on_negative : &t->on_negative ;
/* if something already set, free it first */
if (*holder)
shm_free( *holder );
*holder = ref ? dup_ref_script_route_in_shm( ref, 0) : NULL;
}
void t_on_reply( struct script_route_ref *ref )
{
struct cell *t = get_t();
struct script_route_ref **holder;
/* in MODE_REPLY and MODE_ONFAILURE T will be set to current transaction;
* in MODE_REQUEST T will be set only if the transaction was already
* created; if not -> use the static variable */
holder = (!t || t==T_UNDEFINED ) ? &goto_on_reply :
( route_type==BRANCH_ROUTE ?
&t->uac[_tm_branch_index].on_reply : &t->on_reply );
/* if something already set, free it first */
if (*holder)
shm_free( *holder );
*holder = ref ? dup_ref_script_route_in_shm( ref, 0) : NULL;
}
struct script_route_ref *get_on_negative(void)
{
struct script_route_ref *ref = goto_on_negative;
goto_on_negative = NULL;
return ref;
}
struct script_route_ref *get_on_reply(void)
{
struct script_route_ref *ref = goto_on_reply;
goto_on_reply = NULL;
return ref;
}
void tm_init_tags(void)
{
init_tags(tm_tags, &tm_tag_suffix,
"OpenSIPS-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 invalid\n");
return 1;
}
tag=&get_to(ack)->tag_value;
for (i=t->fwded_totags; i; i=i->next) {
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);
/* to-tag recorded, and an ACK has been received for it */
if (i->acked) return 0;
/* to-tag recorded, but this ACK came for the first time */
i->acked=1;
return 1;
}
}
/* 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 not parsed\n");
return 0;
}
tag=&get_to(ok)->tag_value;
if (!tag->s) {
LM_DBG("no tag in to\n");
return 0;
}
for (i=t->fwded_totags; i; i=i->next) {
if (i->tag.len==tag->len
&& memcmp(i->tag.s, tag->s, tag->len) ==0 ){
/* to tag already recorded */
#ifdef XL_DEBUG
LM_CRIT("totag retransmission\n");
#else
LM_DBG("totag retransmission\n");
#endif
return 1;
}
}
/* that's a new to-tag -- record it */
shm_lock();
n = shm_malloc_bulk(sizeof *n);
s = shm_malloc_bulk(tag->len);
shm_unlock();
if (!s || !n) {
LM_ERR("no more share memory \n");
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;
t->fwded_totags=n;
LM_DBG("new totag \n");
return 0;
}
/*
* Build and send an ACK to a negative reply
* On successful send:
* - return 0
* - populate @ack_buf, for callback purposes, which *must* be SHM freed!
*/
static int send_ack(struct sip_msg* rpl, struct cell *trans, int branch, str *ack_buf)
{
str method = str_init(ACK);
str to;
struct usr_avp **backup_list;
int rc;
if(parse_headers(rpl,is_local(trans)?HDR_EOH_F:(HDR_TO_F|HDR_FROM_F),0)==-1
|| !rpl->to || !rpl->from ) {
LM_ERR("failed to generate a HBH ACK if key HFs in reply missing\n");
goto error;
}
to.s=rpl->to->name.s;
to.len=rpl->to->len;
ack_buf->s = is_local(trans)?
build_dlg_ack(rpl, trans, branch, &to, (unsigned int*)&ack_buf->len):
build_local( trans, branch, &method, NULL, rpl, (unsigned int*)&ack_buf->len );
if (ack_buf->s==0) {
LM_ERR("failed to build ACK\n");
goto error;
}
if (trans->uac[branch].br_flags & tcp_no_new_conn_bflag)
tcp_no_new_conn = 1;
set_bavp_list(&trans->uac[branch].user_avps);
backup_list = set_avp_list( &trans->user_avps );
rc = SEND_PR_BUFFER(&trans->uac[branch].request, ack_buf->s, ack_buf->len);
set_avp_list(backup_list);
reset_bavp_list();
tcp_no_new_conn = 0;
return rc;
error:
memset(ack_buf, 0, sizeof *ack_buf);
return -1;
}
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;
branch_bm_t cancel_bitmap = 0;
str cb_s;
if (!buf)
{
LM_DBG("response building failed\n");
/* determine if there are some branches to be canceled */
if ( is_invite(trans) ) {
if (lock) LOCK_REPLIES( trans );
which_cancel(trans, &cancel_bitmap );
if (lock) UNLOCK_REPLIES( trans );
}
/* and clean-up, including cancellations, if needed */
goto error;
}
if (lock) LOCK_REPLIES( trans );
if (trans->uas.status>=200) {
LM_ERR("failed to generate %d reply when a final %d was sent out\n",
code, trans->uas.status);
goto error2;
}
if ( is_invite(trans) && code>=200 )
which_cancel(trans, &cancel_bitmap );
rb = & trans->uas.response;
rb->activ_type=code;
trans->uas.status = code;
buf_len = rb->buffer.s ? len : len + REPLY_OVERBUFFER_LEN;
rb->buffer.s = shm_realloc( rb->buffer.s, buf_len );
/* puts the reply's buffer to uas.response */
if (! rb->buffer.s ) {
LM_ERR("failed to allocate shmem buffer\n");
goto error3;
}
update_local_tags(trans, bm, rb->buffer.s, buf);
rb->buffer.len = len ;
memcpy( rb->buffer.s , buf , len );
/* needs to be protected too because what timers are set depends
on current transactions status */
/* t_update_timers_after_sending_reply( rb ); */
trans->relaied_reply_branch=-2;
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 ( is_local(trans) ) {
LM_DBG("local transaction completed\n");
if ( has_tran_tmcbs(trans, TMCB_LOCAL_COMPLETED) ) {
run_trans_callbacks( TMCB_LOCAL_COMPLETED, trans,
0, FAKED_REPLY, code);
}
} else {
/* run the PRE send callbacks */
if ( has_tran_tmcbs(trans, TMCB_RESPONSE_PRE_OUT) ) {
cb_s.s = buf;
cb_s.len = len;
set_extra_tmcb_params( &cb_s, &rb->dst);
if (lock)
run_trans_callbacks_locked( TMCB_RESPONSE_PRE_OUT, trans,
trans->uas.request, FAKED_REPLY, code);
else
run_trans_callbacks( TMCB_RESPONSE_PRE_OUT, trans,
trans->uas.request, FAKED_REPLY, code);
}
}
if (!is_hopbyhop_cancel(trans)) {
cleanup_uac_timers( trans );
if (is_invite(trans)) cancel_uacs( trans, cancel_bitmap );
/* for auth related replies, we do not do retransmission
(via set_final_timer()), but only wait for a final
reply (put_on_wait() ) - see RFC 3261 (26.3.2.4 DoS Protection) */
if ((code != 401) && (code != 407))
set_final_timer( trans );
else
put_on_wait(trans);
}
}
/* send it out : response.dst.send_sock is valid all the time now,
* as it's taken from original request -bogdan */
if (!trans->uas.response.dst.send_sock) {
LM_CRIT("send_sock is NULL\n");
}
if(trans->uas.request && trans->uas.request->flags&tcp_no_new_conn_rplflag)
tcp_no_new_conn = 1;
if(ref_script_route_is_valid(tm_local_reply_route)) {
LM_DBG("Found Local-Reply Route...\n");
memset(&dummy_msg, 0, sizeof(struct sip_msg));
dummy_msg.buf = buf;
dummy_msg.len = len;
if (parse_msg(buf, len, &dummy_msg) == 0) {
LM_DBG("Parsed Message, executing Local-Reply Route "
"with Message...\n");
run_top_route(sroutes->request[tm_local_reply_route->idx],
&dummy_msg);
}
free_sip_msg(&dummy_msg);
}
if ( SEND_PR_BUFFER( rb, buf, len )==0 ) {
LM_DBG("reply sent out. buf=%p: %.9s..., "
"shmem=%p: %.9s\n", buf, buf, rb->buffer.s, rb->buffer.s );
if (has_tran_tmcbs(trans, TMCB_MSG_SENT_OUT) ) {
cb_s.s = buf;
cb_s.len = len;
set_extra_tmcb_params( &cb_s, &rb->dst);
run_trans_callbacks( TMCB_MSG_SENT_OUT, trans,
NULL, FAKED_REPLY, code);
}
stats_trans_rpl( code, 1 /*local*/ );
}
tcp_no_new_conn = 0;
/* run the POST send callbacks */
if (code>=200&&!is_local(trans)&&has_tran_tmcbs(trans,TMCB_RESPONSE_OUT)){
cb_s.s = buf;
cb_s.len = len;
set_extra_tmcb_params( &cb_s, &rb->dst);
if (lock)
run_trans_callbacks_locked( TMCB_RESPONSE_OUT, trans,
trans->uas.request, FAKED_REPLY, code);
else
run_trans_callbacks( TMCB_RESPONSE_OUT, trans,
trans->uas.request, FAKED_REPLY, code);
}
pkg_free( buf ) ;
LM_DBG("finished\n");
return 1;
error3:
error2:
if (lock) UNLOCK_REPLIES( trans );
pkg_free ( buf );
error:
/* do UAC cleanup */
cleanup_uac_timers( trans );
if ( is_invite(trans) ) cancel_uacs( trans, cancel_bitmap );
/* 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, const str *text, 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);
if (dset) {
add_lump_rpl(p_msg, dset, dset_len, LUMP_RPL_HDR);
}
}
if (has_tran_tmcbs(trans, TMCB_LOCAL_RESPONSE)) {
run_trans_callbacks(TMCB_LOCAL_RESPONSE,
trans, p_msg, 0, code);
}
/* check if the UAS retranmission port needs to be updated */
if ( (p_msg->msg_flags ^ trans->uas.request->msg_flags) & FL_FORCE_RPORT )
su_setport( &trans->uas.response.dst.to, p_msg->rcv.src_port );
if (code>=180 && p_msg->to
&& (get_to(p_msg)->tag_value.s==0
|| get_to(p_msg)->tag_value.len==0)) {
calc_tag_suffix( p_msg, tm_tag_suffix );
buf = build_res_buf_from_sip_req(code,text, &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,text, 0 /*no to-tag*/,
p_msg, &len, &bm);
return _reply_light(trans,buf,len,code, 0, 0 /* no to-tag */,
lock, &bm);
}
}
/*if msg is set -> it will fake the env. vars conforming with the msg; if NULL
* the env. will be restore to original */
static inline void faked_env( struct cell *t,struct sip_msg *msg)
{
static struct cell *backup_t;
static struct usr_avp **backup_list;
static const struct socket_info* backup_si;
static int backup_route_type;
if (msg) {
swap_route_type( backup_route_type, FAILURE_ROUTE);
/* 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 */
backup_t = get_t();
/* fake transaction */
set_t(t);
/* make available the avp list from transaction */
backup_list = set_avp_list( &t->user_avps );
/* set default send address to the saved value */
backup_si = bind_address;
bind_address = t->uac[0].request.dst.send_sock;
} else {
/* restore original environment */
set_t(backup_t);
set_route_type( backup_route_type );
/* restore original avp list */
set_avp_list( backup_list );
bind_address = backup_si;
}
}
/* return 1 if a failure_route processes */
static inline int run_failure_handlers(struct cell *t)
{
static struct sip_msg faked_req;
struct sip_msg *shmem_msg;
struct ua_client *uac;
int on_failure_idx;
int old_route_type;
shmem_msg = t->uas.request;
uac = &t->uac[picked_branch];
/* failure_route for a local UAC? */
if (!shmem_msg || REQ_LINE(shmem_msg).method_value==METHOD_CANCEL ) {
LM_WARN("no UAC or CANCEL support (%s, %d) \n",
ref_script_route_name(t->on_negative), t->tmcb_hl.reg_types);
return 0;
}
/* don't start faking anything if we don't have to */
if ( !ref_script_route_check_and_update(t->on_negative) &&
!has_tran_tmcbs( t, TMCB_ON_FAILURE) ) {
LM_WARN("no failure route (%s/%d), callbacks (%d)\n",
ref_script_route_name(t->on_negative),
ref_script_route_idx(t->on_negative),
t->tmcb_hl.reg_types);
return 1;
}
if (!fake_req(&faked_req, shmem_msg, &t->uas, NULL)) {
LM_ERR("fake_req failed\n");
return 0;
}
/* fake also the env. conforming to the fake msg */
faked_env( t, &faked_req);
/* DONE with faking ;-) -> run the failure handlers */
if ( has_tran_tmcbs( t, TMCB_ON_FAILURE) ) {
run_trans_callbacks( TMCB_ON_FAILURE, t, &faked_req,
uac->reply, uac->last_received);
}
if (ref_script_route_is_valid(t->on_negative)) {
/* update flags in transaction if changed by callbacks */
shmem_msg->flags = faked_req.flags;
/* avoid recursion -- if failure_route forwards, and does not
* set next failure route, failure_route will not be reentered
* on failure */
on_failure_idx = t->on_negative->idx;
t_on_negative(NULL);
/* run a reply_route action if some was marked */
swap_route_type(old_route_type, FAILURE_ROUTE);
run_top_route(sroutes->failure[on_failure_idx], &faked_req);
set_route_type(old_route_type);
}
/* restore original environment and free the fake msg */
faked_env( t, 0);
free_faked_req(&faked_req,t);
/* if failure handler changed flag, update transaction context */
shmem_msg->flags = faked_req.flags;
/* branch flags do not need to be updated as this branch will be never
* used again */
return 1;
}
static inline int is_3263_failure(struct cell *t)
{
/* is is a DNS failover scenario? - according to RFC 3263
* and RFC 3261, this means 503 reply with Retr-After hdr
* or timeout with no reply */
LM_DBG("dns-failover test: branch=%d, last_recv=%d, flags=%X\n",
picked_branch, t->uac[picked_branch].last_received,
t->uac[picked_branch].flags);
switch (t->uac[picked_branch].last_received) {
case 408:
return ((t->uac[picked_branch].flags&T_UAC_HAS_RECV_REPLY)==0);
case 503:
if (t->uac[picked_branch].reply==NULL ||
t->uac[picked_branch].reply==FAKED_REPLY)
return 0;
/* we do not care about the Retry-After header in 503
* as following discussion on sip-implementers list :
* with or without a RA header, a 503 should fire DNS
* based failover - bogdan
*/
return 1;
}
return 0;
}
static inline int do_dns_failover(struct cell *t)
{
static struct sip_msg faked_req;
struct sip_msg *shmem_msg;
struct sip_msg *req;
struct ua_client *uac;
dlg_t dialog;
int ret, sip_msg_len;
uac = &t->uac[picked_branch];
/* check if the DNS resolver can get at least one new IP */
if ( get_next_su( uac->proxy, &uac->request.dst.to, 1)!=0 )
return -1;
LM_DBG("new destination available\n");
if (t->uas.request==NULL) {
if (!is_local(t)) {
LM_CRIT("BUG: proxy transaction without UAS request :-/\n");
return -1;
}
/* create the cloned SIP msg -> first create a new SIP msg */
memset( &dialog, 0, sizeof(dialog));
dialog.send_sock = uac->request.dst.send_sock;
dialog.hooks.next_hop = &uac->uri;
req = buf_to_sip_msg(uac->request.buffer.s, uac->request.buffer.len,
&dialog);
if (req==NULL) {
LM_ERR("failed to generate SIP msg from previous buffer\n");
return -1;
}
/* now do the actual cloning of the SIP message */
t->uas.request = sip_msg_cloner( req, &sip_msg_len, 1);
if (t->uas.request==NULL) {
LM_ERR("cloning failed\n");
free_sip_msg(req);
pkg_free(req);
return -1;
}
t->uas.end_request = ((char*)t->uas.request) + sip_msg_len;
/* free the actual SIP message, keep the clone only */
free_sip_msg(req);
pkg_free(req);
}
shmem_msg = t->uas.request;
if (!fake_req(&faked_req, shmem_msg, &t->uas, uac)) {
LM_ERR("fake_req failed\n");
return -1;
}
/* fake also the env. conforming to the fake msg */
faked_env( t, &faked_req);
ret = -1;
/* set info as current destination */
if ( set_ruri( &faked_req, &uac->uri)!= 0)
goto done;
setb0flags( &faked_req, uac->br_flags );
faked_req.force_send_socket = shmem_msg->force_send_socket;
/* send it out */
if (t_forward_nonack( t, &faked_req, uac->proxy,1/*reset*/,1/*locked*/)==1)
ret = 0;
done:
/* restore original environment and free the fake msg */
faked_env( t, 0);
free_faked_req(&faked_req,t);
return ret;
}
static inline int branch_prio( short ret_code, unsigned int is_cancelled)
{
int first_digit;
first_digit = ret_code / 100;
switch(first_digit){
case 1: /* 100 - 199 */
case 2: /* 200 - 299 */
return ret_code;
case 6:
return ret_code - 300; /* 300 - 399 */
case 3:
return ret_code + 100; /* 400 - 499 */
case 5:
if (ret_code==503)
return 801; /* 801 */
return ret_code + 200; /* 700 - 799 */
case 4:
switch(ret_code){
case 401:
return 500; /* 500 - 599 */
case 407:
return 501;
case 415:
return 502;
case 420:
return 503;
case 484:
return 504;
case 408:
return 800; /* 800 */
case 487:
if(is_cancelled)
return 0;
default: /* the rest of ret codes in the 4xx class */
return ret_code + 200; /* 600 - 699 */
}
default:
return ret_code + 200; /* > 900 */
}
}
/* select a branch for forwarding; returns:
* 0..X ... branch number
* -1 ... error
* -2 ... can't decide yet -- incomplete branches present
*/
static inline int t_pick_branch( struct cell *t, int *res_code, int *do_cancel)
{
#define PHONY_NO_WAIT(_t,_b) \
(_t->uac[_b].br_flags && _t->uac[_b].br_flags<=_t->nr_of_outgoings)
int lowest_b, lowest_s, b, prio;
unsigned int cancelled;
lowest_b=-1; lowest_s=999;
cancelled = was_cancelled(t);
*do_cancel = 0;
for ( b=t->first_branch; b<t->nr_of_outgoings ; b++ ) {
if ( (t->uac[b].flags & T_UAC_IS_PHONY) && (
/* skip PHONY branches if the transaction was canceled by UAC;
* a phony branch is used just to force the transaction to wait for
* more branches, but if canceled, it makes no sense to wait anymore;
* Exception - do not ignore the branch if there is reply pushed
* on that branch, like an internal timeout or so */
(t->flags & T_WAS_CANCELLED_FLAG && t->uac[b].last_received<299)
||
/* also skip a PHONY branch if it has a true setting about how
* many branches are to be waited. The max branch idx to be waited
* is stored in the 'br_flags' flags of the uac */
PHONY_NO_WAIT(t,b)
))
continue;
/* skip 'empty branches' */
if (!t->uac[b].request.buffer.s) continue;
/* there is still an unfinished UAC transaction; wait now! */
if ( t->uac[b].last_received<200 ) {
if (t->uac[b].br_flags & minor_branch_flag) {
*do_cancel = 1;
continue; /* if last branch, lowest_b remains -1 */
}
return -2;
}
/* compare against the priority of the current branch */
prio = branch_prio(t->uac[b].last_received,cancelled);
if ( (lowest_b==-1) || (prio<lowest_s) ) {
lowest_b = b;
lowest_s = prio;
}
} /* find lowest branch */
LM_DBG("picked branch %d, code %d (prio=%d)\n",
lowest_b,lowest_b==-1 ? -1 : t->uac[lowest_b].last_received,lowest_s);
*res_code=lowest_s;
return lowest_b;
}
/* Quick and harmless test to see if the transaction still has
pending branches */
static inline int tran_is_completed( struct cell *t )
{
int i;
for( i=t->first_branch ; i<t->nr_of_outgoings ; i++ )
if ( t->uac[i].last_received<200 )
return 0;
return 1;
}
/* This is the neurological point of reply processing -- called
* from within a REPLY_LOCK, t_should_relay_response decides
* how a reply shall be processed and how transaction state is
* affected.
*
* Checks if the new reply (with new_code status) should be sent or not
* based on the current
* transaction status.
* Returns - branch number (0,1,...) which should be relayed
* -1 if nothing to be relayed
*/
static enum rps t_should_relay_response( struct cell *Trans , int new_code,
int branch , int *should_store, int *should_relay,
branch_bm_t *cancel_bitmap, struct sip_msg *reply )
{
int branch_cnt;
int picked_code;
int inv_through;
int do_cancel;
/* note: this code never lets replies to CANCEL go through;
we generate always a local 200 for CANCEL; 200s are
not relayed because it's not an INVITE transaction;
>= 300 are not relayed because 200 was already sent
out
*/
LM_DBG("T_code=%d, new_code=%d\n", Trans->uas.status,new_code);
/* a final reply after 200 OK on a transaction with multi branch 200 OK */
if ( is_invite(Trans) && new_code>=200
&& Trans->flags&T_MULTI_200OK_FLAG
&& Trans->uas.status>=200 && Trans->uas.status<300) {
*should_store=0;
picked_branch=-1;
Trans->uac[branch].last_received=new_code;
if (new_code>=300) {
/* negative reply, we simply discard (no relay, no save) */
*should_relay = -1;
return RPS_DISCARDED;
}
/* 2xx reply - is this the last branch to complete?? */
if (tran_is_completed(Trans)) {
/* last branch gets also a 200OK, no more pending branches */
*should_relay = branch;
return RPS_COMPLETED;
} else {
/* 200 OK, but still having ongoing branches */
*should_relay = branch;
return RPS_RELAY;
}
}
inv_through=new_code>=200 && new_code<300 && is_invite(Trans);
/* if final response sent out, allow only INVITE 2xx */
if ( Trans->uas.status >= 200 ) {
if (inv_through) {
LM_DBG("200 OK for INVITE after final sent\n");
*should_store=0;
Trans->uac[branch].last_received=new_code;
*should_relay=branch;
return RPS_PUSHED_AFTER_COMPLETION;
}
if ( is_hopbyhop_cancel(Trans) && new_code>=200) {
*should_store=0;
*should_relay=-1;
picked_branch=-1;
return RPS_COMPLETED;
}
/* except the exception above, too late messages will
be discarded */
goto discard;
}
/* if final response received at this branch, allow only INVITE 2xx */
if (Trans->uac[branch].last_received>=200
&& !(inv_through && Trans->uac[branch].last_received<300)) {
#ifdef EXTRA_DEBUG
/* don't report on retransmissions */
if (Trans->uac[branch].last_received==new_code) {
LM_DBG("final rely retransmission\n");
} else
/* if you FR-timed-out, faked a local 408 and 487 came, don't
* report on it either */
if (Trans->uac[branch].last_received==408 && new_code==487) {
LM_DBG("487 reply came for a timed-out branch\n");
} else {
/* this looks however how a very strange status rewrite attempt;
* report on it */
LM_DBG("status rewrite by UAS: stored: %d, received: %d\n",
Trans->uac[branch].last_received, new_code );
}
#endif
goto discard;
}
/* no final response sent yet */
/* negative replies subject to fork picking */
if (new_code >=300 ) {
Trans->uac[branch].last_received=new_code;
/* also append the current reply to the transaction to
* make it available in failure routes - a kind of "fake"
* save of the final reply per branch */
Trans->uac[branch].reply = reply;
if (new_code>=600 && !disable_6xx_block) {
/* this is a winner and close all branches */
which_cancel( Trans, cancel_bitmap );
picked_branch=branch;
/* no more new branches should be added to this transaction */
Trans->flags |= T_NO_NEW_BRANCHES_FLAG;
} else {
/* if all_final return lowest */
picked_branch = t_pick_branch( Trans, &picked_code, &do_cancel);
if (picked_branch==-2) { /* branches open yet */
*should_store=1;
*should_relay=-1;
picked_branch=-1;
Trans->uac[branch].reply = 0;
return RPS_STORE;
}
if (picked_branch==-1) {
LM_CRIT("pick_branch failed (lowest==-1) for code %d\n",new_code);
Trans->uac[branch].reply = 0;
goto discard;
}
if (do_cancel) {
branch_bm_t cb = 0;