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