Message lumps are saved to shared memory when t_relay() is called
[sip-router] / modules / tm / t_fwd.c
1 /*
2  * $Id$
3  *
4  *
5  * Copyright (C) 2001-2003 FhG Fokus
6  *
7  * This file is part of ser, a free SIP server.
8  *
9  * ser is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version
13  *
14  * For a license to use the ser software under conditions
15  * other than those described here, or to purchase support for this
16  * software, please contact iptel.org by e-mail at the following addresses:
17  *    info@iptel.org
18  *
19  * ser is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License 
25  * along with this program; if not, write to the Free Software 
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28 /*
29  * History:
30  * -------
31  *  2003-02-13  proto support added (andrei)
32  *  2003-02-24  s/T_NULL/T_NULL_CELL/ to avoid redefinition conflict w/
33  *              nameser_compat.h (andrei)
34  *  2003-03-01  kr set through a function now (jiri)
35  *  2003-03-06  callbacks renamed; "blind UAC" introduced, which makes
36  *              transaction behave as if it was forwarded even if it was
37  *              not -- good for local UAS, like VM (jiri)
38  *  2003-03-19  replaced all the mallocs/frees w/ pkg_malloc/pkg_free (andrei)
39  *  2003-03-30  we now watch downstream delivery and if it fails, send an
40  *              error message upstream (jiri)
41  *  2003-04-14  use protocol from uri (jiri)
42  *  2003-12-04  global TM callbacks switched to per transaction callbacks
43  *              (bogdan)
44  *  2004-02-13: t->is_invite and t->local replaced with flags (bogdan)
45  *  2005-08-04  msg->parsed_uri and parsed_uri_ok are no saved & restored
46  *               before & after handling the branches (andrei)
47  *  2005-12-11  onsend_route support added for forwarding (andrei)
48  *  2006-01-27  t_forward_no_ack will return error if a forward on an 
49  *              already canceled transaction is attempted (andrei)
50  *  2006-02-07  named routes support (andrei)
51  *  2006-04-18  add_uac simplified + switched to struct dest_info (andrei)
52  *  2006-04-20  pint_uac_request uses now struct dest_info (andrei)
53  *  2006-08-11  dns failover support (andrei)
54  *              t_forward_non_ack won't start retransmission on send errors
55  *               anymore (WARNING: callers should release/kill the transaction
56  *               if error is returned) (andrei)
57  *  2006-09-15  e2e_cancel uses t_reply_unsafe when called from the 
58  *               failure_route and replying to a cancel (andrei)
59  *  2006-10-10  e2e_cancel update for the new/modified 
60  *               which_cancel()/should_cancel() (andrei)
61  *  2006-10-11  don't fork a new branch if the transaction or branch was
62  *               canceled, or a 6xx was received
63  *              stop retr. timers fix on cancel for non-invites     (andrei)
64  *  2006-11-20  new_uri is no longer saved/restore across add_uac calls, since
65  *              print_uac_request is now uri safe (andrei)
66  *  2007-03-15  TMCB_ONSEND hooks added (andrei)
67  *  2007-05-02  added t_forward_cancel(unmatched_cancel) (andrei)
68  *  2007-05-24  added TMCB_E2ECANCEL_IN hook support (andrei)
69  *  2007-05-28: e2e_cancel_branch() constructs the CANCEL from the
70  *              outgoing INVITE instead of applying the lumps to the
71  *              incomming one. (it can be disabled with reparse_invite=0) (Miklos)
72  *              t_relay_cancel() introduced -- can be used to relay CANCELs
73  *              at the beginning of the script. (Miklos)
74  * 2007-06-04  running transaction are canceled hop by hop (andrei)
75  *  2007-08-37  In case of DNS failover the new SIP message is constructed
76  *              from the message buffer of the failed branch instead of
77  *              applying the lumps again, because the per-branch lumps are not saved,
78  *              thus, are not available. Set reparse_on_dns_failover to 0 to
79  *              revert the change. (Miklos)
80  */
81
82 #include "defs.h"
83
84
85 #include "../../dprint.h"
86 #include "../../config.h"
87 #include "../../parser/parser_f.h"
88 #include "../../ut.h"
89 #include "../../timer.h"
90 #include "../../hash_func.h"
91 #include "../../globals.h"
92 #include "../../cfg_core.h"
93 #include "../../mem/mem.h"
94 #include "../../dset.h"
95 #include "../../action.h"
96 #include "../../data_lump.h"
97 #include "../../onsend.h"
98 #include "../../compiler_opt.h"
99 #include "t_funcs.h"
100 #include "t_hooks.h"
101 #include "t_msgbuilder.h"
102 #include "ut.h"
103 #include "t_cancel.h"
104 #include "t_lookup.h"
105 #include "t_fwd.h"
106 #include "fix_lumps.h"
107 #include "config.h"
108 #ifdef USE_DNS_FAILOVER
109 #include "../../dns_cache.h"
110 #include "../../cfg_core.h" /* cfg_get(core, core_cfg, use_dns_failover) */
111 #include "../../msg_translator.h"
112 #include "lw_parser.h"
113 #endif
114 #ifdef USE_DST_BLACKLIST
115 #include "../../dst_blacklist.h"
116 #endif
117 #include "../../select_buf.h" /* reset_static_buffer() */
118 #ifdef POSTPONE_MSG_CLONING
119 #include "../../atomic_ops.h" /* membar_depends() */
120 #endif
121
122 /* cancel hop by hop */
123 #define E2E_CANCEL_HOP_BY_HOP
124
125 static int goto_on_branch = 0, branch_route = 0;
126
127 void t_on_branch( unsigned int go_to )
128 {
129         struct cell *t = get_t();
130
131        /* in MODE_REPLY and MODE_ONFAILURE T will be set to current transaction;
132         * in MODE_REQUEST T will be set only if the transaction was already
133         * created; if not -> use the static variable */
134         if (!t || t==T_UNDEFINED ) {
135                 goto_on_branch=go_to;
136         } else {
137                 get_t()->on_branch = go_to;
138         }
139 }
140
141 unsigned int get_on_branch(void)
142 {
143         return goto_on_branch;
144 }
145
146
147 static char *print_uac_request( struct cell *t, struct sip_msg *i_req,
148         int branch, str *uri, unsigned int *len, struct dest_info* dst)
149 {
150         char *buf, *shbuf;
151         str* msg_uri;
152         struct lump* add_rm_backup, *body_lumps_backup;
153         struct sip_uri parsed_uri_bak;
154         int parsed_uri_ok_bak, uri_backed_up;
155         str msg_uri_bak;
156         struct run_act_ctx ra_ctx;
157
158         shbuf=0;
159         msg_uri_bak.s=0; /* kill warnings */
160         msg_uri_bak.len=0;
161         parsed_uri_ok_bak=0;
162         uri_backed_up=0;
163
164         /* ... we calculate branch ... */       
165         if (!t_calc_branch(t, branch, i_req->add_to_branch_s,
166                         &i_req->add_to_branch_len ))
167         {
168                 LOG(L_ERR, "ERROR: print_uac_request: branch computation failed\n");
169                 goto error00;
170         }
171
172         /* ... update uri ... */
173         msg_uri=GET_RURI(i_req);
174         if ((msg_uri->s!=uri->s) || (msg_uri->len!=uri->len)){
175                 msg_uri_bak=i_req->new_uri;
176                 parsed_uri_ok_bak=i_req->parsed_uri_ok;
177                 parsed_uri_bak=i_req->parsed_uri;
178                 i_req->new_uri=*uri;
179                 i_req->parsed_uri_ok=0;
180                 uri_backed_up=1;
181         }
182
183 #ifdef POSTPONE_MSG_CLONING
184         /* lumps can be set outside of the lock, make sure that we read
185          * the up-to-date values */
186         membar_depends();
187 #endif
188         add_rm_backup = i_req->add_rm;
189         body_lumps_backup = i_req->body_lumps;
190         i_req->add_rm = dup_lump_list(i_req->add_rm);
191         i_req->body_lumps = dup_lump_list(i_req->body_lumps);
192
193         if (unlikely(branch_route)) {
194                 reset_static_buffer();
195                      /* run branch_route actions if provided */
196                 init_run_actions_ctx(&ra_ctx);
197                 if (run_actions(&ra_ctx, branch_rt.rlist[branch_route], i_req) < 0) {
198                         LOG(L_ERR, "ERROR: print_uac_request: Error in run_actions\n");
199                }
200         }
201
202         /* run the specific callbacks for this transaction */
203         if (unlikely(has_tran_tmcbs(t, TMCB_REQUEST_FWDED)))
204                 run_trans_callbacks( TMCB_REQUEST_FWDED , t, i_req, 0,
205                                                                 -i_req->REQ_METHOD);
206
207         /* ... and build it now */
208         buf=build_req_buf_from_sip_req( i_req, len, dst);
209 #ifdef DBG_MSG_QA
210         if (buf[*len-1]==0) {
211                 LOG(L_ERR, "ERROR: print_uac_request: sanity check failed\n");
212                 abort();
213         }
214 #endif
215         if (!buf) {
216                 LOG(L_ERR, "ERROR: print_uac_request: no pkg_mem\n"); 
217                 ser_error=E_OUT_OF_MEM;
218                 goto error01;
219         }
220
221         shbuf=(char *)shm_malloc(*len);
222         if (!shbuf) {
223                 ser_error=E_OUT_OF_MEM;
224                 LOG(L_ERR, "ERROR: print_uac_request: no shmem\n");
225                 goto error02;
226         }
227         memcpy( shbuf, buf, *len );
228
229 error02:
230         pkg_free( buf );
231 error01:
232              /* Delete the duplicated lump lists, this will also delete
233               * all lumps created here, such as lumps created in per-branch
234               * routing sections, Via, and Content-Length headers created in
235               * build_req_buf_from_sip_req
236               */
237         free_duped_lump_list(i_req->add_rm);
238         free_duped_lump_list(i_req->body_lumps);
239              /* Restore the lists from backups */
240         i_req->add_rm = add_rm_backup;
241         i_req->body_lumps = body_lumps_backup;
242         /* restore the new_uri from the backup */
243         if (uri_backed_up){
244                 i_req->new_uri=msg_uri_bak;
245                 i_req->parsed_uri=parsed_uri_bak;
246                 i_req->parsed_uri_ok=parsed_uri_ok_bak;
247         }
248
249  error00:
250         return shbuf;
251 }
252
253 #ifdef USE_DNS_FAILOVER
254 /* Similar to print_uac_request(), but this function uses the outgoing message buffer of
255    the failed branch to construt the new message in case of DNS failover.
256
257    WARNING: only the first VIA header is replaced in the buffer, the rest
258    of the message is untuched, thus, the send socket is corrected only in the VIA HF.
259 */
260 static char *print_uac_request_from_buf( struct cell *t, struct sip_msg *i_req,
261         int branch, str *uri, unsigned int *len, struct dest_info* dst,
262         char *buf, short buf_len)
263 {
264         char *shbuf;
265         str branch_str;
266         char *via, *old_via_begin, *old_via_end;
267         unsigned int via_len;
268
269         shbuf=0;
270
271         /* ... we calculate branch ... */       
272         if (!t_calc_branch(t, branch, i_req->add_to_branch_s,
273                         &i_req->add_to_branch_len ))
274         {
275                 LOG(L_ERR, "ERROR: print_uac_request_from_buf: branch computation failed\n");
276                 goto error00;
277         }
278         branch_str.s = i_req->add_to_branch_s;
279         branch_str.len = i_req->add_to_branch_len;
280
281         /* find the beginning of the first via header in the buffer */
282         old_via_begin = lw_find_via(buf, buf+buf_len);
283         if (!old_via_begin) {
284                 LOG(L_ERR, "ERROR: print_uac_request_from_buf: beginning of via header not found\n");
285                 goto error00;
286         }
287         /* find the end of the first via header in the buffer */
288         old_via_end = lw_next_line(old_via_begin, buf+buf_len);
289         if (!old_via_end) {
290                 LOG(L_ERR, "ERROR: print_uac_request_from_buf: end of via header not found\n");
291                 goto error00;
292         }
293
294         /* create the new VIA HF */
295         via = create_via_hf(&via_len, i_req, dst, &branch_str);
296         if (!via) {
297                 LOG(L_ERR, "ERROR: print_uac_request_from_buf: via building failed\n");
298                 goto error00;
299         }
300
301         /* allocate memory for the new buffer */
302         *len = buf_len + via_len - (old_via_end - old_via_begin);
303         shbuf=(char *)shm_malloc(*len);
304         if (!shbuf) {
305                 ser_error=E_OUT_OF_MEM;
306                 LOG(L_ERR, "ERROR: print_uac_request_from_buf: no shmem\n");
307                 goto error01;
308         }
309
310         /* construct the new buffer */
311         memcpy(shbuf, buf, old_via_begin-buf);
312         memcpy(shbuf+(old_via_begin-buf), via, via_len);
313         memcpy(shbuf+(old_via_begin-buf)+via_len, old_via_end, (buf+buf_len)-old_via_end);
314
315 #ifdef DBG_MSG_QA
316         if (shbuf[*len-1]==0) {
317                 LOG(L_ERR, "ERROR: print_uac_request_from_buf: sanity check failed\n");
318                 abort();
319         }
320 #endif
321
322 error01:
323         pkg_free(via);
324 error00:
325         return shbuf;
326 }
327 #endif
328
329 /* introduce a new uac, which is blind -- it only creates the
330    data structures and starts FR timer, but that's it; it does
331    not print messages and send anything anywhere; that is good
332    for FIFO apps -- the transaction must look operationally
333    and FR must be ticking, whereas the request is "forwarded"
334    using a non-SIP way and will be replied the same way
335 */
336 int add_blind_uac( /*struct cell *t*/ )
337 {
338         unsigned short branch;
339         struct cell *t;
340
341         t=get_t();
342         if (t==T_UNDEFINED || !t ) {
343                 LOG(L_ERR, "ERROR: add_blind_uac: no transaction context\n");
344                 return -1;
345         }
346
347         branch=t->nr_of_outgoings;      
348         if (branch==MAX_BRANCHES) {
349                 LOG(L_ERR, "ERROR: add_blind_uac: "
350                         "maximum number of branches exceeded\n");
351                 return -1;
352         }
353         /* make sure it will be replied */
354         t->flags |= T_NOISY_CTIMER_FLAG;
355         membar_write(); /* to allow lockless which_cancel() we want to be sure 
356                                            all the writes finished before updating branch number*/
357         t->nr_of_outgoings=(branch+1);
358         /* start FR timer -- protocol set by default to PROTO_NONE,
359        which means retransmission timer will not be started
360     */
361         if (start_retr(&t->uac[branch].request)!=0)
362                 LOG(L_CRIT, "BUG: add_blind_uac: start retr failed for %p\n",
363                                 &t->uac[branch].request);
364         /* we are on a timer -- don't need to put on wait on script
365            clean-up     
366         */
367         set_kr(REQ_FWDED); 
368
369         return 1; /* success */
370 }
371
372 /* introduce a new uac to transaction; returns its branch id (>=0)
373    or error (<0); it doesn't send a message yet -- a reply to it
374    might interfere with the processes of adding multiple branches;
375    On error returns <0 & sets ser_error to the same value
376 */
377 int add_uac( struct cell *t, struct sip_msg *request, str *uri, str* next_hop,
378         struct proxy_l *proxy, int proto )
379 {
380
381         int ret;
382         unsigned short branch;
383         char *shbuf;
384         unsigned int len;
385
386         branch=t->nr_of_outgoings;
387         if (branch==MAX_BRANCHES) {
388                 LOG(L_ERR, "ERROR: add_uac: maximum number of branches exceeded\n");
389                 ret=ser_error=E_TOO_MANY_BRANCHES;
390                 goto error;
391         }
392
393         /* check existing buffer -- rewriting should never occur */
394         if (t->uac[branch].request.buffer) {
395                 LOG(L_CRIT, "ERROR: add_uac: buffer rewrite attempt\n");
396                 ret=ser_error=E_BUG;
397                 goto error;
398         }
399
400         /* check DNS resolution */
401         if (proxy){
402                 /* dst filled from the proxy */
403                 init_dest_info(&t->uac[branch].request.dst);
404                 t->uac[branch].request.dst.proto=get_proto(proto, proxy->proto);
405                 proxy2su(&t->uac[branch].request.dst.to, proxy);
406                 /* fill dst send_sock */
407                 t->uac[branch].request.dst.send_sock =
408                 get_send_socket( request, &t->uac[branch].request.dst.to,
409                                                                 t->uac[branch].request.dst.proto);
410         }else {
411 #ifdef USE_DNS_FAILOVER
412                 if (uri2dst(&t->uac[branch].dns_h, &t->uac[branch].request.dst,
413                                         request, next_hop?next_hop:uri, proto) == 0)
414 #else
415                 /* dst filled from the uri & request (send_socket) */
416                 if (uri2dst(&t->uac[branch].request.dst, request,
417                                                 next_hop ? next_hop: uri, proto)==0)
418 #endif
419                 {
420                         ret=ser_error=E_BAD_ADDRESS;
421                         goto error;
422                 }
423         }
424         
425         /* check if send_sock is ok */
426         if (t->uac[branch].request.dst.send_sock==0) {
427                 LOG(L_ERR, "ERROR: add_uac: can't fwd to af %d, proto %d "
428                         " (no corresponding listening socket)\n",
429                         t->uac[branch].request.dst.to.s.sa_family, 
430                         t->uac[branch].request.dst.proto );
431                 ret=ser_error=E_NO_SOCKET;
432                 goto error01;
433         }
434
435         /* now message printing starts ... */
436         shbuf=print_uac_request( t, request, branch, uri, 
437                                                         &len, &t->uac[branch].request.dst);
438         if (!shbuf) {
439                 ret=ser_error=E_OUT_OF_MEM;
440                 goto error01;
441         }
442
443         /* things went well, move ahead and install new buffer! */
444         t->uac[branch].request.buffer=shbuf;
445         t->uac[branch].request.buffer_len=len;
446         t->uac[branch].uri.s=t->uac[branch].request.buffer+
447                 request->first_line.u.request.method.len+1;
448         t->uac[branch].uri.len=uri->len;
449         membar_write(); /* to allow lockless ops (e.g. which_cancel()) we want
450                                            to be sure everything above is fully written before
451                                            updating branches no. */
452         t->nr_of_outgoings=(branch+1);
453
454         /* update stats */
455         if (proxy){
456                 proxy_mark(proxy, 1);
457         }
458         /* done! */
459         ret=branch;
460                 
461 error01:
462 error:
463         return ret;
464 }
465
466
467
468 #ifdef USE_DNS_FAILOVER
469 /* Similar to add_uac(), but this function uses the outgoing message buffer of
470    the failed branch to construt the new message in case of DNS failover.
471 */
472 static int add_uac_from_buf( struct cell *t, struct sip_msg *request, str *uri, int proto,
473                         char *buf, short buf_len)
474 {
475
476         int ret;
477         unsigned short branch;
478         char *shbuf;
479         unsigned int len;
480
481         branch=t->nr_of_outgoings;
482         if (branch==MAX_BRANCHES) {
483                 LOG(L_ERR, "ERROR: add_uac_from_buf: maximum number of branches exceeded\n");
484                 ret=ser_error=E_TOO_MANY_BRANCHES;
485                 goto error;
486         }
487
488         /* check existing buffer -- rewriting should never occur */
489         if (t->uac[branch].request.buffer) {
490                 LOG(L_CRIT, "ERROR: add_uac_from_buf: buffer rewrite attempt\n");
491                 ret=ser_error=E_BUG;
492                 goto error;
493         }
494
495         if (uri2dst(&t->uac[branch].dns_h, &t->uac[branch].request.dst,
496                                 request, uri, proto) == 0)
497         {
498                 ret=ser_error=E_BAD_ADDRESS;
499                 goto error;
500         }
501         
502         /* check if send_sock is ok */
503         if (t->uac[branch].request.dst.send_sock==0) {
504                 LOG(L_ERR, "ERROR: add_uac_from_buf: can't fwd to af %d, proto %d "
505                         " (no corresponding listening socket)\n",
506                         t->uac[branch].request.dst.to.s.sa_family, 
507                         t->uac[branch].request.dst.proto );
508                 ret=ser_error=E_NO_SOCKET;
509                 goto error;
510         }
511
512         /* now message printing starts ... */
513         shbuf=print_uac_request_from_buf( t, request, branch, uri, 
514                                                         &len, &t->uac[branch].request.dst,
515                                                         buf, buf_len);
516         if (!shbuf) {
517                 ret=ser_error=E_OUT_OF_MEM;
518                 goto error;
519         }
520
521         /* things went well, move ahead and install new buffer! */
522         t->uac[branch].request.buffer=shbuf;
523         t->uac[branch].request.buffer_len=len;
524         t->uac[branch].uri.s=t->uac[branch].request.buffer+
525                 request->first_line.u.request.method.len+1;
526         t->uac[branch].uri.len=uri->len;
527         membar_write(); /* to allow lockless ops (e.g. which_cancel()) we want
528                                            to be sure everything above is fully written before
529                                            updating branches no. */
530         t->nr_of_outgoings=(branch+1);
531
532         /* done! */
533         ret=branch;
534                 
535 error:
536         return ret;
537 }
538
539 /* introduce a new uac to transaction, based on old_uac and a possible
540  *  new ip address (if the dns name resolves to more ips). If no more
541  *   ips are found => returns -1.
542  *  returns its branch id (>=0)
543    or error (<0) and sets ser_error if needed; it doesn't send a message 
544    yet -- a reply to it
545    might interfere with the processes of adding multiple branches
546    if lock_replies is 1 replies will be locked for t until the new branch
547    is added (to prevent add branches races). Use 0 if the reply lock is
548    already held, e.g. in failure route/handlers (WARNING: using 1 in a 
549    failure route will cause a deadlock).
550 */
551 int add_uac_dns_fallback( struct cell *t, struct sip_msg* msg, 
552                                                                         struct ua_client* old_uac,
553                                                                         int lock_replies)
554 {
555         int ret;
556         
557         ret=-1;
558         if (cfg_get(core, core_cfg, use_dns_failover) && 
559                         !((t->flags & T_DONT_FORK) || uac_dont_fork(old_uac)) &&
560                         dns_srv_handle_next(&old_uac->dns_h, 0)){
561                         if (lock_replies){
562                                 /* use reply lock to guarantee nobody is adding a branch
563                                  * in the same time */
564                                 LOCK_REPLIES(t);
565                                 /* check again that we can fork */
566                                 if ((t->flags & T_DONT_FORK) || uac_dont_fork(old_uac)){
567                                         UNLOCK_REPLIES(t);
568                                         DBG("add_uac_dns_fallback: no forking on => no new"
569                                                         " branches\n");
570                                         return ret;
571                                 }
572                         }
573                         if (t->nr_of_outgoings >= MAX_BRANCHES){
574                                 LOG(L_ERR, "ERROR: add_uac_dns_fallback: maximum number of "
575                                                         "branches exceeded\n");
576                                 if (lock_replies)
577                                         UNLOCK_REPLIES(t);
578                                         ret=ser_error=E_TOO_MANY_BRANCHES;
579                                 return ret;
580                         }
581                         /* copy the dns handle into the new uac */
582                         dns_srv_handle_cpy(&t->uac[t->nr_of_outgoings].dns_h,
583                                                                 &old_uac->dns_h);
584
585                         if (cfg_get(tm, tm_cfg, reparse_on_dns_failover))
586                                 /* Reuse the old buffer and only replace the via header.
587                                  * The drowback is that the send_socket is not corrected
588                                  * in the rest of the message, only in the VIA HF (Miklos) */
589                                 ret=add_uac_from_buf(t,  msg, &old_uac->uri, 
590                                                         old_uac->request.dst.proto,
591                                                         old_uac->request.buffer,
592                                                         old_uac->request.buffer_len);
593                         else
594                                 /* add_uac will use dns_h => next_hop will be ignored.
595                                  * Unfortunately we can't reuse the old buffer, the branch id
596                                  *  must be changed and the send_socket might be different =>
597                                  *  re-create the whole uac */
598                                 ret=add_uac(t,  msg, &old_uac->uri, 0, 0, 
599                                                         old_uac->request.dst.proto);
600
601                         if (ret<0){
602                                 /* failed, delete the copied dns_h */
603                                 dns_srv_handle_put(&t->uac[t->nr_of_outgoings].dns_h);
604                         }
605                         if (lock_replies){
606                                 UNLOCK_REPLIES(t);
607                         }
608         }
609         return ret;
610 }
611
612 #endif
613
614 int e2e_cancel_branch( struct sip_msg *cancel_msg, struct cell *t_cancel, 
615         struct cell *t_invite, int branch )
616 {
617         int ret;
618         char *shbuf;
619         unsigned int len;
620
621         ret=-1;
622         if (t_cancel->uac[branch].request.buffer) {
623                 LOG(L_CRIT, "ERROR: e2e_cancel_branch: buffer rewrite attempt\n");
624                 ret=ser_error=E_BUG;
625                 goto error;
626         }
627         if (t_invite->uac[branch].request.buffer==0){
628                 /* inactive / deleted  branch */
629                 goto error;
630         }
631         t_invite->uac[branch].request.flags|=F_RB_CANCELED;
632
633         /* note -- there is a gap in proxy stats -- we don't update 
634            proxy stats with CANCEL (proxy->ok, proxy->tx, etc.)
635         */
636
637         /* print */
638         if (cfg_get(tm, tm_cfg, reparse_invite)) {
639                 /* buffer is built localy from the INVITE which was sent out */
640 #ifdef POSTPONE_MSG_CLONING
641                 /* lumps can be set outside of the lock, make sure that we read
642                  * the up-to-date values */
643                 membar_depends();
644 #endif
645                 if (cancel_msg->add_rm || cancel_msg->body_lumps) {
646                         LOG(L_WARN, "WARNING: e2e_cancel_branch: CANCEL is built locally, "
647                         "thus lumps are not applied to the message!\n");
648                 }
649                 shbuf=build_local_reparse( t_invite, branch, &len, CANCEL, CANCEL_LEN, &t_invite->to);
650
651         } else {
652                 /* buffer is constructed from the received CANCEL with applying lumps */
653                 shbuf=print_uac_request( t_cancel, cancel_msg, branch, 
654                                                         &t_invite->uac[branch].uri, &len, 
655                                                         &t_invite->uac[branch].request.dst);
656         }
657
658         if (!shbuf) {
659                 LOG(L_ERR, "ERROR: e2e_cancel_branch: printing e2e cancel failed\n");
660                 ret=ser_error=E_OUT_OF_MEM;
661                 goto error;
662         }
663         
664         /* install buffer */
665         t_cancel->uac[branch].request.dst=t_invite->uac[branch].request.dst;
666         t_cancel->uac[branch].request.buffer=shbuf;
667         t_cancel->uac[branch].request.buffer_len=len;
668         t_cancel->uac[branch].uri.s=t_cancel->uac[branch].request.buffer+
669                 cancel_msg->first_line.u.request.method.len+1;
670         t_cancel->uac[branch].uri.len=t_invite->uac[branch].uri.len;
671         
672
673         /* success */
674         ret=1;
675
676
677 error:
678         return ret;
679 }
680
681 void e2e_cancel( struct sip_msg *cancel_msg, 
682         struct cell *t_cancel, struct cell *t_invite )
683 {
684         branch_bm_t cancel_bm;
685 #ifndef E2E_CANCEL_HOP_BY_HOP
686         branch_bm_t tmp_bm;
687 #endif
688         int i;
689         int lowest_error;
690         int ret;
691         struct tmcb_params tmcb;
692
693         cancel_bm=0;
694         lowest_error=0;
695
696         if (unlikely(has_tran_tmcbs(t_invite, TMCB_E2ECANCEL_IN))){
697                 INIT_TMCB_PARAMS(tmcb, cancel_msg, 0, cancel_msg->REQ_METHOD);
698                 run_trans_callbacks_internal(&t_invite->tmcb_hl, TMCB_E2ECANCEL_IN, 
699                                                                                 t_invite, &tmcb);
700         }
701         /* first check if there are any branches */
702         if (t_invite->nr_of_outgoings==0){
703                 t_invite->flags|=T_CANCELED;
704                 /* no branches yet => force a reply to the invite */
705                 t_reply( t_invite, t_invite->uas.request, 487, CANCELED );
706                 DBG("DEBUG: e2e_cancel: e2e cancel -- no more pending branches\n");
707                 t_reply( t_cancel, cancel_msg, 200, CANCEL_DONE );
708                 return;
709         }
710         
711         /* determine which branches to cancel ... */
712         which_cancel( t_invite, &cancel_bm );
713 #ifdef E2E_CANCEL_HOP_BY_HOP
714         /* we don't need to set t_cancel label to be the same as t_invite if
715          * we do hop by hop cancel. The cancel transaction will have a different 
716          * label, but this is not a problem since this transaction is only used to
717          * send a reply back. The cancels sent upstream will be part of the invite
718          * transaction (local_cancel retr. bufs) and they will be generated with
719          * the same via as the invite.
720          * Note however that setting t_cancel label the same as t_invite will work
721          * too (the upstream cancel replies will properly match the t_invite
722          * transaction and will not match the t_cancel because t_cancel will always
723          * have 0 branches and we check for the branch number in 
724          * t_reply_matching() ).
725          */
726         for (i=0; i<t_invite->nr_of_outgoings; i++)
727                 if (cancel_bm & (1<<i)) {
728                         /* it's safe to get the reply lock since e2e_cancel is
729                          * called with the cancel as the "current" transaction so
730                          * at most t_cancel REPLY_LOCK is held in this process =>
731                          * no deadlock possibility */
732                         ret=cancel_branch(t_invite, i, cfg_get(tm,tm_cfg, cancel_b_flags));
733                         if (ret<0) cancel_bm &= ~(1<<i);
734                         if (ret<lowest_error) lowest_error=ret;
735                 }
736 #else /* ! E2E_CANCEL_HOP_BY_HOP */
737         /* fix label -- it must be same for reply matching (the label is part of
738          * the generated via branch for the cancels sent upstream and if it
739          * would be different form the one in the INVITE the transactions would not
740          * match */
741         t_cancel->label=t_invite->label;
742         t_cancel->nr_of_outgoings=t_invite->nr_of_outgoings;
743         /* ... and install CANCEL UACs */
744         for (i=0; i<t_invite->nr_of_outgoings; i++)
745                 if ((cancel_bm & (1<<i)) && (t_invite->uac[i].last_received>=100)) {
746                         ret=e2e_cancel_branch(cancel_msg, t_cancel, t_invite, i);
747                         if (ret<0) cancel_bm &= ~(1<<i);
748                         if (ret<lowest_error) lowest_error=ret;
749                 }
750
751         /* send them out */
752         for (i = 0; i < t_cancel->nr_of_outgoings; i++) {
753                 if (cancel_bm & (1 << i)) {
754                         if (t_invite->uac[i].last_received>=100){
755                                 /* Provisional reply received on this branch, send CANCEL */
756                                 /* we do need to stop the retr. timers if the request is not 
757                                  * an invite and since the stop_rb_retr() cost is lower then
758                                  * the invite check we do it always --andrei */
759                                 stop_rb_retr(&t_invite->uac[i].request);
760                                 if (SEND_BUFFER(&t_cancel->uac[i].request) == -1) {
761                                         LOG(L_ERR, "ERROR: e2e_cancel: send failed\n");
762                                 }
763 #ifdef TMCB_ONSEND
764                                 else{
765                                         if (unlikely(has_tran_tmcbs(t_cancel, TMCB_REQUEST_SENT)))
766                                                 run_onsend_callbacks(TMCB_REQUEST_SENT, 
767                                                                                                 &t_cancel->uac[i].request,
768                                                                                                 cancel_msg, 0, TMCB_LOCAL_F);
769                                 }
770 #endif
771                                 if (start_retr( &t_cancel->uac[i].request )!=0)
772                                         LOG(L_CRIT, "BUG: e2e_cancel: failed to start retr."
773                                                         " for %p\n", &t_cancel->uac[i].request);
774                         } else {
775                                 /* No provisional response received, stop
776                                  * retransmission timers */
777                                 if (!(cfg_get(tm, tm_cfg, cancel_b_flags) & 
778                                                         F_CANCEL_B_FORCE_RETR))
779                                         stop_rb_retr(&t_invite->uac[i].request);
780                                 /* no need to stop fr, it will be stoped by relay_reply
781                                  * put_on_wait -- andrei */
782                                 /* Generate faked reply */
783                                 if (cfg_get(tm, tm_cfg, cancel_b_flags) &
784                                                 F_CANCEL_B_FAKE_REPLY){
785                                         LOCK_REPLIES(t_invite);
786                                         if (relay_reply(t_invite, FAKED_REPLY, i, 487, &tmp_bm) == 
787                                                         RPS_ERROR) {
788                                                 lowest_error = -1;
789                                         }
790                                 }
791                         }
792                 }
793         }
794 #endif /*E2E_CANCEL_HOP_BY_HOP */
795
796         /* if error occurred, let it know upstream (final reply
797            will also move the transaction on wait state
798         */
799         if (lowest_error<0) {
800                 LOG(L_ERR, "ERROR: cancel error\n");
801                 /* if called from failure_route, make sure that the unsafe version
802                  * is called (we are already holding the reply mutex for the cancel
803                  * transaction).
804                  */
805                 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
806                         t_reply_unsafe( t_cancel, cancel_msg, 500, "cancel error");
807                 else
808                         t_reply( t_cancel, cancel_msg, 500, "cancel error");
809         } else if (cancel_bm) {
810                 /* if there are pending branches, let upstream know we
811                    are working on it
812                 */
813                 DBG("DEBUG: e2e_cancel: e2e cancel proceeding\n");
814                 /* if called from failure_route, make sure that the unsafe version
815                  * is called (we are already hold the reply mutex for the cancel
816                  * transaction).
817                  */
818                 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
819                         t_reply_unsafe( t_cancel, cancel_msg, 200, CANCELING );
820                 else
821                         t_reply( t_cancel, cancel_msg, 200, CANCELING );
822         } else {
823                 /* if the transaction exists, but there are no more pending
824                    branches, tell upstream we're done
825                 */
826                 DBG("DEBUG: e2e_cancel: e2e cancel -- no more pending branches\n");
827                 /* if called from failure_route, make sure that the unsafe version
828                  * is called (we are already hold the reply mutex for the cancel
829                  * transaction).
830                  */
831                 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
832                         t_reply_unsafe( t_cancel, cancel_msg, 200, CANCEL_DONE );
833                 else
834                         t_reply( t_cancel, cancel_msg, 200, CANCEL_DONE );
835         }
836 }
837
838
839
840 /* sends one uac/branch buffer and fallbacks to other ips if
841  *  the destination resolves to several addresses
842  *  Takes care of starting timers a.s.o. (on send success)
843  *  returns: -2 on error, -1 on drop,  current branch id on success,
844  *   new branch id on send error/blacklist, when failover is possible
845  *    (ret>=0 && ret!=branch)
846  *    if lock_replies is 1, the replies for t will be locked when adding
847  *     new branches (to prevent races). Use 0 from failure routes or other
848  *     places where the reply lock is already held, to avoid deadlocks. */
849 int t_send_branch( struct cell *t, int branch, struct sip_msg* p_msg ,
850                                         struct proxy_l * proxy, int lock_replies)
851 {
852         struct ip_addr ip; /* debugging */
853         int ret;
854         struct ua_client* uac;
855         
856         uac=&t->uac[branch];
857         ret=branch;
858         if (run_onsend(p_msg,   &uac->request.dst, uac->request.buffer,
859                                         uac->request.buffer_len)==0){
860                 /* disable the current branch: set a "fake" timeout
861                  *  reply code but don't set uac->reply, to avoid overriding 
862                  *  a higly unlikely, perfectly timed fake reply (to a message
863                  *   we never sent).
864                  * (code=final reply && reply==0 => t_pick_branch won't ever pick it)*/
865                         uac->last_received=408;
866                         su2ip_addr(&ip, &uac->request.dst.to);
867                         DBG("t_send_branch: onsend_route dropped msg. to %s:%d (%d)\n",
868                                                         ip_addr2a(&ip), su_getport(&uac->request.dst.to),
869                                                         uac->request.dst.proto);
870 #ifdef USE_DNS_FAILOVER
871                         /* if the destination resolves to more ips, add another
872                          *  branch/uac */
873                         if (cfg_get(core, core_cfg, use_dns_failover)){
874                                 ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
875                                 if (ret>=0){
876                                         su2ip_addr(&ip, &uac->request.dst.to);
877                                         DBG("t_send_branch: send on branch %d failed "
878                                                         "(onsend_route), trying another ip %s:%d (%d)\n",
879                                                         branch, ip_addr2a(&ip),
880                                                         su_getport(&uac->request.dst.to),
881                                                         uac->request.dst.proto);
882                                         /* success, return new branch */
883                                         return ret;
884                                 }
885                         }
886 #endif /* USE_DNS_FAILOVER*/
887                 return -1; /* drop, try next branch */
888         }
889 #ifdef USE_DST_BLACKLIST
890         if (cfg_get(core, core_cfg, use_dst_blacklist)
891                 && p_msg
892                 && (p_msg->REQ_METHOD & cfg_get(tm, tm_cfg, tm_blst_methods_lookup))
893         ){
894                 if (dst_is_blacklisted(&uac->request.dst, p_msg)){
895                         su2ip_addr(&ip, &uac->request.dst.to);
896                         DBG("t_send_branch: blacklisted destination: %s:%d (%d)\n",
897                                                         ip_addr2a(&ip), su_getport(&uac->request.dst.to),
898                                                         uac->request.dst.proto);
899                         /* disable the current branch: set a "fake" timeout
900                          *  reply code but don't set uac->reply, to avoid overriding 
901                          *  a higly unlikely, perfectly timed fake reply (to a message
902                          *   we never sent).  (code=final reply && reply==0 => 
903                          *   t_pick_branch won't ever pick it)*/
904                         uac->last_received=408;
905 #ifdef USE_DNS_FAILOVER
906                         /* if the destination resolves to more ips, add another
907                          *  branch/uac */
908                         if (cfg_get(core, core_cfg, use_dns_failover)){
909                                 ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
910                                 if (ret>=0){
911                                         su2ip_addr(&ip, &uac->request.dst.to);
912                                         DBG("t_send_branch: send on branch %d failed (blacklist),"
913                                                         " trying another ip %s:%d (%d)\n", branch,
914                                                         ip_addr2a(&ip), su_getport(&uac->request.dst.to),
915                                                         uac->request.dst.proto);
916                                         /* success, return new branch */
917                                         return ret;
918                                 }
919                         }
920 #endif /* USE_DNS_FAILOVER*/
921                         return -1; /* don't send */
922                 }
923         }
924 #endif /* USE_DST_BLACKLIST */
925         if (SEND_BUFFER( &uac->request)==-1) {
926                 /* disable the current branch: set a "fake" timeout
927                  *  reply code but don't set uac->reply, to avoid overriding 
928                  *  a highly unlikely, perfectly timed fake reply (to a message
929                  *  we never sent).
930                  * (code=final reply && reply==0 => t_pick_branch won't ever pick it)*/
931                 uac->last_received=408;
932                 su2ip_addr(&ip, &uac->request.dst.to);
933                 DBG("t_send_branch: send to %s:%d (%d) failed\n",
934                                                         ip_addr2a(&ip), su_getport(&uac->request.dst.to),
935                                                         uac->request.dst.proto);
936 #ifdef USE_DST_BLACKLIST
937                 if (cfg_get(core, core_cfg, use_dst_blacklist))
938                         dst_blacklist_add(BLST_ERR_SEND, &uac->request.dst, p_msg);
939 #endif
940 #ifdef USE_DNS_FAILOVER
941                 /* if the destination resolves to more ips, add another
942                  *  branch/uac */
943                 if (cfg_get(core, core_cfg, use_dns_failover)){
944                         ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
945                         if (ret>=0){
946                                 /* success, return new branch */
947                                 DBG("t_send_branch: send on branch %d failed, adding another"
948                                                 " branch with another ip\n", branch);
949                                 return ret;
950                         }
951                 }
952 #endif
953                 LOG(L_ERR, "ERROR: t_send_branch: sending request on branch %d "
954                                 "failed\n", branch);
955                 if (proxy) { proxy->errors++; proxy->ok=0; }
956                 return -2;
957         } else {
958 #ifdef TMCB_ONSEND
959                 if (unlikely(has_tran_tmcbs(t, TMCB_REQUEST_SENT)))
960                         run_onsend_callbacks(TMCB_REQUEST_SENT, &uac->request, p_msg, 0,0);
961 #endif
962                 /* start retr. only if the send succeeded */
963                 if (start_retr( &uac->request )!=0){
964                         LOG(L_CRIT, "BUG: t_send_branch: retr. already started for %p\n",
965                                         &uac->request);
966                         return -2;
967                 }
968         }
969         return ret;
970 }
971
972
973
974 /* function returns:
975  *       1 - forward successful
976  *      -1 - error during forward
977  */
978 int t_forward_nonack( struct cell *t, struct sip_msg* p_msg , 
979         struct proxy_l * proxy, int proto)
980 {
981         int branch_ret, lowest_ret;
982         str current_uri;
983         branch_bm_t     added_branches;
984         int first_branch;
985         int i, q;
986         struct cell *t_invite;
987         int success_branch;
988         int try_new;
989         int lock_replies;
990         str dst_uri;
991         struct socket_info* si, *backup_si;
992         
993
994         /* make -Wall happy */
995         current_uri.s=0;
996
997         if (t->flags & T_CANCELED){
998                 DBG("t_forward_non_ack: no forwarding on a canceled transaction\n");
999                 ser_error=E_CANCELED;
1000                 return -1;
1001         }
1002         if (p_msg->REQ_METHOD==METHOD_CANCEL) { 
1003                 t_invite=t_lookupOriginalT(  p_msg );
1004                 if (t_invite!=T_NULL_CELL) {
1005                         e2e_cancel( p_msg, t, t_invite );
1006                         UNREF(t_invite);
1007                         /* it should be set to REQ_RPLD by e2e_cancel, which should
1008                          * send a final reply */
1009                         set_kr(REQ_FWDED);
1010                         return 1;
1011                 }
1012         }
1013
1014         backup_si = p_msg->force_send_socket;
1015         /* if no more specific error code is known, use this */
1016         lowest_ret=E_UNSPEC;
1017         /* branches added */
1018         added_branches=0;
1019         /* branch to begin with */
1020         first_branch=t->nr_of_outgoings;
1021
1022         if (t->on_branch) {
1023                 /* tell add_uac that it should run branch route actions */
1024                 branch_route = t->on_branch;
1025                 /* reset the flag before running the actions (so that it
1026                  * could be set again in branch_route if needed
1027                  */
1028                 t_on_branch(0);
1029         } else {
1030                 branch_route = 0;
1031         }
1032         
1033         /* on first-time forwarding, use current uri, later only what
1034            is in additional branches (which may be continuously refilled
1035         */
1036         if (first_branch==0) {
1037 #ifdef POSTPONE_MSG_CLONING
1038                 /* update the shmem-ized msg with the lumps */
1039                 if ((rmode == MODE_REQUEST) &&
1040                         save_msg_lumps(t->uas.request, p_msg)) {
1041                                 LOG(L_ERR, "ERROR: t_forward_nonack: "
1042                                         "failed to save the message lumps\n");
1043                                 return -1;
1044                         }
1045 #endif
1046                 try_new=1;
1047                 branch_ret=add_uac( t, p_msg, GET_RURI(p_msg), GET_NEXT_HOP(p_msg),
1048                                                         proxy, proto );
1049                 if (branch_ret>=0) 
1050                         added_branches |= 1<<branch_ret;
1051                 else
1052                         lowest_ret=MIN_int(lowest_ret, branch_ret);
1053         } else try_new=0;
1054
1055         init_branch_iterator();
1056         while((current_uri.s=next_branch( &current_uri.len, &q, &dst_uri.s, &dst_uri.len, &si))) {
1057                 try_new++;
1058                 p_msg->force_send_socket = si;
1059                 branch_ret=add_uac( t, p_msg, &current_uri, 
1060                                     (dst_uri.len) ? (&dst_uri) : &current_uri, 
1061                                     proxy, proto);
1062                 /* pick some of the errors in case things go wrong;
1063                    note that picking lowest error is just as good as
1064                    any other algorithm which picks any other negative
1065                    branch result */
1066                 if (branch_ret>=0) 
1067                         added_branches |= 1<<branch_ret;
1068                 else
1069                         lowest_ret=MIN_int(lowest_ret, branch_ret);
1070         }
1071         /* consume processed branches */
1072         clear_branches();
1073
1074         p_msg->force_send_socket = backup_si;
1075
1076         /* don't forget to clear all branches processed so far */
1077
1078         /* things went wrong ... no new branch has been fwd-ed at all */
1079         if (added_branches==0) {
1080                 if (try_new==0) {
1081                         LOG(L_ERR, "ERROR: t_forward_nonack: no branches for"
1082                                                 " forwarding\n");
1083                         /* either failed to add branches, or there were no more branches
1084                         */
1085                         ser_error=MIN_int(lowest_ret, E_CFG);
1086                         return -1;
1087                 }
1088                 LOG(L_ERR, "ERROR: t_forward_nonack: failure to add branches\n");
1089                 ser_error=lowest_ret;
1090                 return lowest_ret;
1091         }
1092         ser_error=0; /* clear branch adding errors */
1093         /* send them out now */
1094         success_branch=0;
1095         lock_replies= ! ((rmode==MODE_ONFAILURE) && (t==get_t()));
1096         for (i=first_branch; i<t->nr_of_outgoings; i++) {
1097                 if (added_branches & (1<<i)) {
1098                         
1099                         branch_ret=t_send_branch(t, i, p_msg , proxy, lock_replies);
1100                         if (branch_ret>=0){ /* some kind of success */
1101                                 if (branch_ret==i) /* success */
1102                                         success_branch++;
1103                                 else /* new branch added */
1104                                         added_branches |= 1<<branch_ret;
1105                         }
1106                 }
1107         }
1108         if (success_branch<=0) {
1109                 /* return always E_SEND for now
1110                  * (the real reason could be: denied by onsend routes, blacklisted,
1111                  *  send failed or any of the errors listed before + dns failed
1112                  *  when attempting dns failover) */
1113                 ser_error=E_SEND;
1114                 /* else return the last error (?) */
1115                 /* the caller should take care and delete the transaction */
1116                 return -1;
1117         }
1118         ser_error=0; /* clear branch send errors, we have overall success */
1119         set_kr(REQ_FWDED);
1120         return 1;
1121 }
1122
1123
1124
1125 /* cancel handling/forwarding function
1126  * CANCELs with no matching transaction are handled in function of
1127  * the unmatched_cancel config var: they are either forwarded statefully,
1128  * statelessly or dropped.
1129  * function returns:
1130  *       1 - forward successful
1131  *       0 - error, but do not reply 
1132  *      <0 - error during forward
1133  * it also sets *tran if a transaction was created
1134  */
1135 int t_forward_cancel(struct sip_msg* p_msg , struct proxy_l * proxy, int proto,
1136                                                 struct cell** tran)
1137 {
1138         struct cell* t_invite;
1139         struct cell* t;
1140         int ret;
1141         int new_tran;
1142         struct dest_info dst;
1143         str host;
1144         unsigned short port;
1145         short comp;
1146         
1147         t=0;
1148         /* handle cancels for which no transaction was created yet */
1149         if (cfg_get(tm, tm_cfg, unmatched_cancel)==UM_CANCEL_STATEFULL){
1150                 /* create cancel transaction */
1151                 new_tran=t_newtran(p_msg);
1152                 if (new_tran<=0 && new_tran!=E_SCRIPT){
1153                         if (new_tran==0)
1154                                  /* retransmission => do nothing */
1155                                 ret=1;
1156                         else
1157                                 /* some error => return it or DROP */
1158                                 ret=(ser_error==E_BAD_VIA && reply_to_via) ? 0: new_tran;
1159                         goto end;
1160                 }
1161                 t=get_t();
1162                 ret=t_forward_nonack(t, p_msg, proxy, proto);
1163                 goto end;
1164         }
1165         
1166         t_invite=t_lookupOriginalT(  p_msg );
1167         if (t_invite!=T_NULL_CELL) {
1168                 /* create cancel transaction */
1169                 new_tran=t_newtran(p_msg);
1170                 if (new_tran<=0 && new_tran!=E_SCRIPT){
1171                         if (new_tran==0)
1172                                  /* retransmission => do nothing */
1173                                 ret=1;
1174                         else
1175                                 /* some error => return it or DROP */
1176                                 ret=(ser_error==E_BAD_VIA && reply_to_via) ? 0: new_tran;
1177                         UNREF(t_invite);
1178                         goto end;
1179                 }
1180                 t=get_t();
1181                 e2e_cancel( p_msg, t, t_invite );
1182                 UNREF(t_invite);
1183                 ret=1;
1184                 goto end;
1185         }else /* no coresponding INVITE transaction */
1186              if (cfg_get(tm, tm_cfg, unmatched_cancel)==UM_CANCEL_DROP){
1187                                 DBG("t_forward_nonack: non matching cancel dropped\n");
1188                                 ret=1; /* do nothing -> drop */
1189                                 goto end;
1190                  }else{
1191                         /* UM_CANCEL_STATELESS -> stateless forward */
1192                                 DBG( "SER: forwarding CANCEL statelessly \n");
1193                                 if (proxy==0) {
1194                                         init_dest_info(&dst);
1195                                         dst.proto=proto;
1196                                         if (get_uri_send_info(GET_NEXT_HOP(p_msg), &host,
1197                                                                 &port, &dst.proto, &comp)!=0){
1198                                                 ret=E_BAD_ADDRESS;
1199                                                 goto end;
1200                                         }
1201 #ifdef USE_COMP
1202                                         dst.comp=comp;
1203 #endif
1204                                         /* dst->send_sock not set, but forward_request 
1205                                          * will take care of it */
1206                                         ret=forward_request(p_msg, &host, port, &dst);
1207                                         goto end;
1208                                 } else {
1209                                         init_dest_info(&dst);
1210                                         dst.proto=get_proto(proto, proxy->proto);
1211                                         proxy2su(&dst.to, proxy);
1212                                         /* dst->send_sock not set, but forward_request 
1213                                          * will take care of it */
1214                                         ret=forward_request( p_msg , 0, 0, &dst) ;
1215                                         goto end;
1216                                 }
1217                 }
1218 end:
1219         if (tran)
1220                 *tran=t;
1221         return ret;
1222 }
1223
1224 /* Relays a CANCEL request if a corresponding INVITE transaction
1225  * can be found. The function is supposed to be used at the very
1226  * beginning of the script with reparse_invite=1 module parameter.
1227  *
1228  * return value:
1229  *    0: the CANCEL was successfully relayed
1230  *       (or error occured but reply cannot be sent) => DROP
1231  *    1: no corresponding INVITE transaction exisis
1232  *   <0: corresponding INVITE transaction exisis but error occured
1233  */
1234 int t_relay_cancel(struct sip_msg* p_msg)
1235 {
1236         struct cell* t_invite;
1237         struct cell* t;
1238         int ret;
1239         int new_tran;
1240
1241         t_invite=t_lookupOriginalT(  p_msg );
1242         if (t_invite!=T_NULL_CELL) {
1243                 /* create cancel transaction */
1244                 new_tran=t_newtran(p_msg);
1245                 if (new_tran<=0 && new_tran!=E_SCRIPT){
1246                         if (new_tran==0)
1247                                 /* retransmission => DROP,
1248                                 t_newtran() takes care about it */
1249                                 ret=0;
1250                         else
1251                                 /* some error => return it or DROP */
1252                                 ret=(ser_error==E_BAD_VIA && reply_to_via) ? 0: new_tran;
1253                         UNREF(t_invite);
1254                         goto end;
1255                 }
1256                 t=get_t();
1257                 e2e_cancel( p_msg, t, t_invite );
1258                 UNREF(t_invite);
1259                 /* return 0 to stop the script processing */
1260                 ret=0;
1261                 goto end;
1262
1263         } else {
1264                 /* no corresponding INVITE trasaction found */
1265                 ret=1;
1266         }
1267 end:
1268         return ret;
1269 }
1270
1271 /* WARNING: doesn't work from failure route (deadlock, uses t_relay_to which
1272  *  is failure route unsafe) */
1273 int t_replicate(struct sip_msg *p_msg,  struct proxy_l *proxy, int proto )
1274 {
1275         /* this is a quite horrible hack -- we just take the message
1276            as is, including Route-s, Record-route-s, and Vias ,
1277            forward it downstream and prevent replies received
1278            from relaying by setting the replication/local_trans bit;
1279
1280                 nevertheless, it should be good enough for the primary
1281                 customer of this function, REGISTER replication
1282
1283                 if we want later to make it thoroughly, we need to
1284                 introduce delete lumps for all the header fields above
1285         */
1286         return t_relay_to(p_msg, proxy, proto, 1 /* replicate */);
1287 }
1288
1289 /* fixup function for reparse_on_dns_failover modparam */
1290 int reparse_on_dns_failover_fixup(void *handle, str *name, void **val)
1291 {
1292 #ifdef USE_DNS_FAILOVER
1293         if ((int)(long)(*val) && mhomed) {
1294                 LOG(L_WARN, "WARNING: reparse_on_dns_failover_fixup:"
1295                 "reparse_on_dns_failover is enabled on a "
1296                 "multihomed host -- check the readme of tm module!\n");
1297         }
1298 #endif
1299         return 0;
1300 }