4e653bdd64183d9f69a9326e130241d1129928f8
[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  */
67
68 #include "defs.h"
69
70
71 #include "../../dprint.h"
72 #include "../../config.h"
73 #include "../../parser/parser_f.h"
74 #include "../../ut.h"
75 #include "../../timer.h"
76 #include "../../hash_func.h"
77 #include "../../globals.h"
78 #include "../../mem/mem.h"
79 #include "../../dset.h"
80 #include "../../action.h"
81 #include "../../data_lump.h"
82 #include "../../onsend.h"
83 #include "t_funcs.h"
84 #include "t_hooks.h"
85 #include "t_msgbuilder.h"
86 #include "ut.h"
87 #include "t_cancel.h"
88 #include "t_lookup.h"
89 #include "t_fwd.h"
90 #include "fix_lumps.h"
91 #include "config.h"
92 #ifdef USE_DNS_FAILOVER
93 #include "../../dns_cache.h"
94 #endif
95 #ifdef USE_DST_BLACKLIST
96 #include "../../dst_blacklist.h"
97 #endif
98
99
100
101 static int goto_on_branch = 0, branch_route = 0;
102
103 void t_on_branch( unsigned int go_to )
104 {
105         struct cell *t = get_t();
106
107        /* in MODE_REPLY and MODE_ONFAILURE T will be set to current transaction;
108         * in MODE_REQUEST T will be set only if the transaction was already
109         * created; if not -> use the static variable */
110         if (!t || t==T_UNDEFINED ) {
111                 goto_on_branch=go_to;
112         } else {
113                 get_t()->on_branch = go_to;
114         }
115 }
116
117 unsigned int get_on_branch(void)
118 {
119         return goto_on_branch;
120 }
121
122
123 static char *print_uac_request( struct cell *t, struct sip_msg *i_req,
124         int branch, str *uri, unsigned int *len, struct dest_info* dst)
125 {
126         char *buf, *shbuf;
127         str* msg_uri;
128         struct lump* add_rm_backup, *body_lumps_backup;
129         struct sip_uri parsed_uri_bak;
130         int parsed_uri_ok_bak, uri_backed_up;
131         str msg_uri_bak;
132
133         shbuf=0;
134         msg_uri_bak.s=0; /* kill warnings */
135         msg_uri_bak.len=0;
136         parsed_uri_ok_bak=0;
137         uri_backed_up=0;
138
139         /* ... we calculate branch ... */       
140         if (!t_calc_branch(t, branch, i_req->add_to_branch_s,
141                         &i_req->add_to_branch_len ))
142         {
143                 LOG(L_ERR, "ERROR: print_uac_request: branch computation failed\n");
144                 goto error00;
145         }
146
147         /* ... update uri ... */
148         msg_uri=GET_RURI(i_req);
149         if ((msg_uri->s!=uri->s) || (msg_uri->len!=uri->len)){
150                 msg_uri_bak=i_req->new_uri;
151                 parsed_uri_ok_bak=i_req->parsed_uri_ok;
152                 parsed_uri_bak=i_req->parsed_uri;
153                 i_req->new_uri=*uri;
154                 i_req->parsed_uri_ok=0;
155                 uri_backed_up=1;
156         }
157
158         add_rm_backup = i_req->add_rm;
159         body_lumps_backup = i_req->body_lumps;
160         i_req->add_rm = dup_lump_list(i_req->add_rm);
161         i_req->body_lumps = dup_lump_list(i_req->body_lumps);
162
163         if (branch_route) {
164                      /* run branch_route actions if provided */
165                 if (run_actions(branch_rt.rlist[branch_route], i_req) < 0) {
166                         LOG(L_ERR, "ERROR: print_uac_request: Error in run_actions\n");
167                }
168         }
169
170         /* run the specific callbacks for this transaction */
171         run_trans_callbacks( TMCB_REQUEST_FWDED , t, i_req, 0, -i_req->REQ_METHOD);
172
173         /* ... and build it now */
174         buf=build_req_buf_from_sip_req( i_req, len, dst);
175 #ifdef DBG_MSG_QA
176         if (buf[*len-1]==0) {
177                 LOG(L_ERR, "ERROR: print_uac_request: sanity check failed\n");
178                 abort();
179         }
180 #endif
181         if (!buf) {
182                 LOG(L_ERR, "ERROR: print_uac_request: no pkg_mem\n"); 
183                 ser_error=E_OUT_OF_MEM;
184                 goto error01;
185         }
186
187         shbuf=(char *)shm_malloc(*len);
188         if (!shbuf) {
189                 ser_error=E_OUT_OF_MEM;
190                 LOG(L_ERR, "ERROR: print_uac_request: no shmem\n");
191                 goto error02;
192         }
193         memcpy( shbuf, buf, *len );
194
195 error02:
196         pkg_free( buf );
197 error01:
198              /* Delete the duplicated lump lists, this will also delete
199               * all lumps created here, such as lumps created in per-branch
200               * routing sections, Via, and Content-Length headers created in
201               * build_req_buf_from_sip_req
202               */
203         free_duped_lump_list(i_req->add_rm);
204         free_duped_lump_list(i_req->body_lumps);
205              /* Restore the lists from backups */
206         i_req->add_rm = add_rm_backup;
207         i_req->body_lumps = body_lumps_backup;
208         /* restore the new_uri from the backup */
209         if (uri_backed_up){
210                 i_req->new_uri=msg_uri_bak;
211                 i_req->parsed_uri=parsed_uri_bak;
212                 i_req->parsed_uri_ok=parsed_uri_ok_bak;
213         }
214
215  error00:
216         return shbuf;
217 }
218
219 /* introduce a new uac, which is blind -- it only creates the
220    data structures and starts FR timer, but that's it; it does
221    not print messages and send anything anywhere; that is good
222    for FIFO apps -- the transaction must look operationally
223    and FR must be ticking, whereas the request is "forwarded"
224    using a non-SIP way and will be replied the same way
225 */
226 int add_blind_uac( /*struct cell *t*/ )
227 {
228         unsigned short branch;
229         struct cell *t;
230
231         t=get_t();
232         if (t==T_UNDEFINED || !t ) {
233                 LOG(L_ERR, "ERROR: add_blind_uac: no transaction context\n");
234                 return -1;
235         }
236
237         branch=t->nr_of_outgoings;      
238         if (branch==MAX_BRANCHES) {
239                 LOG(L_ERR, "ERROR: add_blind_uac: "
240                         "maximum number of branches exceeded\n");
241                 return -1;
242         }
243         /* make sure it will be replied */
244         t->flags |= T_NOISY_CTIMER_FLAG;
245         t->nr_of_outgoings++;
246         /* start FR timer -- protocol set by default to PROTO_NONE,
247        which means retransmission timer will not be started
248     */
249         if (start_retr(&t->uac[branch].request)!=0)
250                 LOG(L_CRIT, "BUG: add_blind_uac: start retr failed for %p\n",
251                                 &t->uac[branch].request);
252         /* we are on a timer -- don't need to put on wait on script
253            clean-up     
254         */
255         set_kr(REQ_FWDED); 
256
257         return 1; /* success */
258 }
259
260 /* introduce a new uac to transaction; returns its branch id (>=0)
261    or error (<0); it doesn't send a message yet -- a reply to it
262    might interfere with the processes of adding multiple branches
263 */
264 int add_uac( struct cell *t, struct sip_msg *request, str *uri, str* next_hop,
265         struct proxy_l *proxy, int proto )
266 {
267
268         int ret;
269         unsigned short branch;
270         char *shbuf;
271         unsigned int len;
272
273         branch=t->nr_of_outgoings;
274         if (branch==MAX_BRANCHES) {
275                 LOG(L_ERR, "ERROR: add_uac: maximum number of branches exceeded\n");
276                 ret=E_CFG;
277                 goto error;
278         }
279
280         /* check existing buffer -- rewriting should never occur */
281         if (t->uac[branch].request.buffer) {
282                 LOG(L_CRIT, "ERROR: add_uac: buffer rewrite attempt\n");
283                 ret=ser_error=E_BUG;
284                 goto error;
285         }
286
287         /* check DNS resolution */
288         if (proxy){
289                 /* dst filled from the proxy */
290                 init_dest_info(&t->uac[branch].request.dst);
291                 t->uac[branch].request.dst.proto=get_proto(proto, proxy->proto);
292                 proxy2su(&t->uac[branch].request.dst.to, proxy);
293                 /* fill dst send_sock */
294                 t->uac[branch].request.dst.send_sock =
295                 get_send_socket( request, &t->uac[branch].request.dst.to,
296                                                                 t->uac[branch].request.dst.proto);
297         }else {
298 #ifdef USE_DNS_FAILOVER
299                 if (uri2dst(&t->uac[branch].dns_h, &t->uac[branch].request.dst,
300                                         request, next_hop?next_hop:uri, proto) == 0)
301 #else
302                 /* dst filled from the uri & request (send_socket) */
303                 if (uri2dst(&t->uac[branch].request.dst, request,
304                                                 next_hop ? next_hop: uri, proto)==0)
305 #endif
306                 {
307                         ret=E_BAD_ADDRESS;
308                         goto error;
309                 }
310         }
311         
312         /* check if send_sock is ok */
313         if (t->uac[branch].request.dst.send_sock==0) {
314                 LOG(L_ERR, "ERROR: add_uac: can't fwd to af %d, proto %d "
315                         " (no corresponding listening socket)\n",
316                         t->uac[branch].request.dst.to.s.sa_family, 
317                         t->uac[branch].request.dst.proto );
318                 ret=ser_error=E_NO_SOCKET;
319                 goto error01;
320         }
321
322         /* now message printing starts ... */
323         shbuf=print_uac_request( t, request, branch, uri, 
324                                                         &len, &t->uac[branch].request.dst);
325         if (!shbuf) {
326                 ret=ser_error=E_OUT_OF_MEM;
327                 goto error01;
328         }
329
330         /* things went well, move ahead and install new buffer! */
331         t->uac[branch].request.buffer=shbuf;
332         t->uac[branch].request.buffer_len=len;
333         t->uac[branch].uri.s=t->uac[branch].request.buffer+
334                 request->first_line.u.request.method.len+1;
335         t->uac[branch].uri.len=uri->len;
336         t->nr_of_outgoings++;
337
338         /* update stats */
339         if (proxy){
340                 proxy_mark(proxy, 1);
341         }
342         /* done! */
343         ret=branch;
344                 
345 error01:
346 error:
347         return ret;
348 }
349
350
351
352 #ifdef USE_DNS_FAILOVER
353 /* introduce a new uac to transaction, based on old_uac and a possible
354  *  new ip address (if the dns name resolves to more ips). If no more
355  *   ips are found => returns -1.
356  *  returns its branch id (>=0)
357    or error (<0); it doesn't send a message yet -- a reply to it
358    might interfere with the processes of adding multiple branches
359    if lock_replies is 1 replies will be locked for t until the new branch
360    is added (to prevent add branches races). Use 0 if the reply lock is
361    already held, e.g. in failure route/handlers (WARNING: using 1 in a 
362    failure route will cause a deadlock).
363 */
364 int add_uac_dns_fallback( struct cell *t, struct sip_msg* msg, 
365                                                                         struct ua_client* old_uac,
366                                                                         int lock_replies)
367 {
368         int ret;
369         
370         ret=-1;
371         if (use_dns_failover && 
372                         !((t->flags & T_DONT_FORK) || uac_dont_fork(old_uac)) &&
373                         dns_srv_handle_next(&old_uac->dns_h, 0)){
374                         if (lock_replies){
375                                 /* use reply lock to guarantee nobody is adding a branch
376                                  * in the same time */
377                                 LOCK_REPLIES(t);
378                                 /* check again that we can fork */
379                                 if ((t->flags & T_DONT_FORK) || uac_dont_fork(old_uac)){
380                                         UNLOCK_REPLIES(t);
381                                         DBG("add_uac_dns_fallback: no forking on => no new"
382                                                         " branches\n");
383                                         return ret;
384                                 }
385                         }
386                         if (t->nr_of_outgoings >= MAX_BRANCHES){
387                                 LOG(L_ERR, "ERROR: add_uac_dns_fallback: maximum number of "
388                                                         "branches exceeded\n");
389                                 if (lock_replies)
390                                         UNLOCK_REPLIES(t);
391                                 return E_CFG;
392                         }
393                         /* copy the dns handle into the new uac */
394                         dns_srv_handle_cpy(&t->uac[t->nr_of_outgoings].dns_h,
395                                                                 &old_uac->dns_h);
396                         /* add_uac will use dns_h => next_hop will be ignored.
397                          * Unfortunately we can't reuse the old buffer, the branch id
398                          *  must be changed and the send_socket might be different =>
399                          *  re-create the whole uac */
400                         ret=add_uac(t,  msg, &old_uac->uri, 0, 0, 
401                                                         old_uac->request.dst.proto);
402                         if (ret<0){
403                                 /* failed, delete the copied dns_h */
404                                 dns_srv_handle_put(&t->uac[t->nr_of_outgoings].dns_h);
405                         }
406                         if (lock_replies){
407                                 UNLOCK_REPLIES(t);
408                         }
409         }
410         return ret;
411 }
412
413 #endif
414
415 int e2e_cancel_branch( struct sip_msg *cancel_msg, struct cell *t_cancel, 
416         struct cell *t_invite, int branch )
417 {
418         int ret;
419         char *shbuf;
420         unsigned int len;
421
422         ret=-1;
423         if (t_cancel->uac[branch].request.buffer) {
424                 LOG(L_CRIT, "ERROR: e2e_cancel_branch: buffer rewrite attempt\n");
425                 ret=ser_error=E_BUG;
426                 goto error;
427         }
428         if (t_invite->uac[branch].request.buffer==0){
429                 /* inactive / deleted  branch */
430                 goto error;
431         }
432
433         /* note -- there is a gap in proxy stats -- we don't update 
434            proxy stats with CANCEL (proxy->ok, proxy->tx, etc.)
435         */
436
437         /* print */
438         shbuf=print_uac_request( t_cancel, cancel_msg, branch, 
439                                                         &t_invite->uac[branch].uri, &len, 
440                                                         &t_invite->uac[branch].request.dst);
441         if (!shbuf) {
442                 LOG(L_ERR, "ERROR: e2e_cancel_branch: printing e2e cancel failed\n");
443                 ret=ser_error=E_OUT_OF_MEM;
444                 goto error;
445         }
446         
447         /* install buffer */
448         t_cancel->uac[branch].request.dst=t_invite->uac[branch].request.dst;
449         t_cancel->uac[branch].request.buffer=shbuf;
450         t_cancel->uac[branch].request.buffer_len=len;
451         t_cancel->uac[branch].uri.s=t_cancel->uac[branch].request.buffer+
452                 cancel_msg->first_line.u.request.method.len+1;
453         t_cancel->uac[branch].uri.len=t_invite->uac[branch].uri.len;
454         
455
456         /* success */
457         ret=1;
458
459
460 error:
461         return ret;
462 }
463
464 void e2e_cancel( struct sip_msg *cancel_msg, 
465         struct cell *t_cancel, struct cell *t_invite )
466 {
467         branch_bm_t cancel_bm, tmp_bm;
468         int i;
469         int lowest_error;
470         int ret;
471
472         cancel_bm=0;
473         lowest_error=0;
474
475         /* first check if there are any branches */
476         if (t_invite->nr_of_outgoings==0){
477                 t_invite->flags|=T_CANCELED;
478                 /* no branches yet => force a reply to the invite */
479                 t_reply( t_invite, t_invite->uas.request, 487, CANCELED );
480                 DBG("DEBUG: e2e_cancel: e2e cancel -- no more pending branches\n");
481                 t_reply( t_cancel, cancel_msg, 200, CANCEL_DONE );
482                 return;
483         }
484         
485         /* determine which branches to cancel ... */
486         which_cancel( t_invite, &cancel_bm );
487         t_cancel->nr_of_outgoings=t_invite->nr_of_outgoings;
488         /* fix label -- it must be same for reply matching */
489         t_cancel->label=t_invite->label;
490         /* ... and install CANCEL UACs */
491         for (i=0; i<t_invite->nr_of_outgoings; i++)
492                 if ((cancel_bm & (1<<i)) && (t_invite->uac[i].last_received>=100)) {
493                         ret=e2e_cancel_branch(cancel_msg, t_cancel, t_invite, i);
494                         if (ret<0) cancel_bm &= ~(1<<i);
495                         if (ret<lowest_error) lowest_error=ret;
496                 }
497
498         /* send them out */
499         for (i = 0; i < t_cancel->nr_of_outgoings; i++) {
500                 if (cancel_bm & (1 << i)) {
501                         if (t_invite->uac[i].last_received>=100){
502                                 /* Provisional reply received on this branch, send CANCEL */
503                                 /* we do need to stop the retr. timers if the request is not 
504                                  * an invite and since the stop_rb_retr() cost is lower then
505                                  * the invite check we do it always --andrei */
506                                 stop_rb_retr(&t_invite->uac[i].request);
507                                 if (SEND_BUFFER(&t_cancel->uac[i].request) == -1) {
508                                         LOG(L_ERR, "ERROR: e2e_cancel: send failed\n");
509                                 }
510                                 if (start_retr( &t_cancel->uac[i].request )!=0)
511                                         LOG(L_CRIT, "BUG: e2e_cancel: failed to start retr."
512                                                         " for %p\n", &t_cancel->uac[i].request);
513                         } else {
514                                 /* No provisional response received, stop
515                                  * retransmission timers */
516                                 stop_rb_retr(&t_invite->uac[i].request);
517                                 /* no need to stop fr, it will be stoped by relay_reply
518                                  * put_on_wait -- andrei */
519                                 /* Generate faked reply */
520                                 LOCK_REPLIES(t_invite);
521                                 if (relay_reply(t_invite, FAKED_REPLY, i, 487, &tmp_bm) == 
522                                                 RPS_ERROR) {
523                                         lowest_error = -1;
524                                 }
525                         }
526                 }
527         }
528
529         /* if error occurred, let it know upstream (final reply
530            will also move the transaction on wait state
531         */
532         if (lowest_error<0) {
533                 LOG(L_ERR, "ERROR: cancel error\n");
534                 /* if called from failure_route, make sure that the unsafe version
535                  * is called (we are already hold the reply mutex for the cancel
536                  * transaction).
537                  */
538                 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
539                         t_reply_unsafe( t_cancel, cancel_msg, 500, "cancel error");
540                 else
541                         t_reply( t_cancel, cancel_msg, 500, "cancel error");
542         } else if (cancel_bm) {
543                 /* if there are pending branches, let upstream know we
544                    are working on it
545                 */
546                 DBG("DEBUG: e2e_cancel: e2e cancel proceeding\n");
547                 /* if called from failure_route, make sure that the unsafe version
548                  * is called (we are already hold the reply mutex for the cancel
549                  * transaction).
550                  */
551                 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
552                         t_reply_unsafe( t_cancel, cancel_msg, 200, CANCELING );
553                 else
554                         t_reply( t_cancel, cancel_msg, 200, CANCELING );
555         } else {
556                 /* if the transaction exists, but there is no more pending
557                    branch, tell upstream we're done
558                 */
559                 DBG("DEBUG: e2e_cancel: e2e cancel -- no more pending branches\n");
560                 /* if called from failure_route, make sure that the unsafe version
561                  * is called (we are already hold the reply mutex for the cancel
562                  * transaction).
563                  */
564                 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
565                         t_reply_unsafe( t_cancel, cancel_msg, 200, CANCEL_DONE );
566                 else
567                         t_reply( t_cancel, cancel_msg, 200, CANCEL_DONE );
568         }
569 }
570
571
572
573 /* sends one uac/branch buffer and fallbacks to other ips if
574  *  the destination resolves to several addresses
575  *  Takes care of starting timers a.s.o. (on send success)
576  *  returns: -2 on error, -1 on drop,  current branch id on success,
577  *   new branch id on send error/blacklist, when failover is possible
578  *    (ret>=0 && ret!=branch)
579  *    if lock_replies is 1, the replies for t will be locked when adding
580  *     new branches (to prevent races). Use 0 from failure routes or other
581  *     places where the reply lock is already held, to avoid deadlocks. */
582 int t_send_branch( struct cell *t, int branch, struct sip_msg* p_msg ,
583                                         struct proxy_l * proxy, int lock_replies)
584 {
585         struct ip_addr ip; /* debugging */
586         int ret;
587         struct ua_client* uac;
588         
589         uac=&t->uac[branch];
590         ret=branch;
591         if (run_onsend(p_msg,   &uac->request.dst, uac->request.buffer,
592                                         uac->request.buffer_len)==0){
593                 /* disable the current branch: set a "fake" timeout
594                  *  reply code but don't set uac->reply, to avoid overriding 
595                  *  a higly unlikely, perfectly timed fake reply (to a message
596                  *   we never sent).
597                  * (code=final reply && reply==0 => t_pick_branch won't ever pick it)*/
598                         uac->last_received=408;
599                         su2ip_addr(&ip, &uac->request.dst.to);
600                         DBG("t_send_branch: onsend_route dropped msg. to %s:%d (%d)\n",
601                                                         ip_addr2a(&ip), su_getport(&uac->request.dst.to),
602                                                         uac->request.dst.proto);
603 #ifdef USE_DNS_FAILOVER
604                         /* if the destination resolves to more ips, add another
605                          *  branch/uac */
606                         if (use_dns_failover){
607                                 ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
608                                 if (ret>=0){
609                                         su2ip_addr(&ip, &uac->request.dst.to);
610                                         DBG("t_send_branch: send on branch %d failed "
611                                                         "(onsend_route), trying another ip %s:%d (%d)\n",
612                                                         branch, ip_addr2a(&ip),
613                                                         su_getport(&uac->request.dst.to),
614                                                         uac->request.dst.proto);
615                                         /* success, return new branch */
616                                         return ret;
617                                 }
618                         }
619 #endif /* USE_DNS_FAILOVER*/
620                 return -1; /* drop, try next branch */
621         }
622 #ifdef USE_DST_BLACKLIST
623         if (use_dst_blacklist){
624                 if (dst_is_blacklisted(&uac->request.dst)){
625                         su2ip_addr(&ip, &uac->request.dst.to);
626                         DBG("t_send_branch: blacklisted destination: %s:%d (%d)\n",
627                                                         ip_addr2a(&ip), su_getport(&uac->request.dst.to),
628                                                         uac->request.dst.proto);
629                         /* disable the current branch: set a "fake" timeout
630                          *  reply code but don't set uac->reply, to avoid overriding 
631                          *  a higly unlikely, perfectly timed fake reply (to a message
632                          *   we never sent).  (code=final reply && reply==0 => 
633                          *   t_pick_branch won't ever pick it)*/
634                         uac->last_received=408;
635 #ifdef USE_DNS_FAILOVER
636                         /* if the destination resolves to more ips, add another
637                          *  branch/uac */
638                         if (use_dns_failover){
639                                 ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
640                                 if (ret>=0){
641                                         su2ip_addr(&ip, &uac->request.dst.to);
642                                         DBG("t_send_branch: send on branch %d failed (blacklist),"
643                                                         " trying another ip %s:%d (%d)\n", branch,
644                                                         ip_addr2a(&ip), su_getport(&uac->request.dst.to),
645                                                         uac->request.dst.proto);
646                                         /* success, return new branch */
647                                         return ret;
648                                 }
649                         }
650 #endif /* USE_DNS_FAILOVER*/
651                         return -1; /* don't send */
652                 }
653         }
654 #endif /* USE_DST_BLACKLIST */
655         if (SEND_BUFFER( &uac->request)==-1) {
656                 /* disable the current branch: set a "fake" timeout
657                  *  reply code but don't set uac->reply, to avoid overriding 
658                  *  a higly unlikely, perfectly timed fake reply (to a message
659                  *  we never sent).
660                  * (code=final reply && reply==0 => t_pick_branch won't ever pick it)*/
661                 uac->last_received=408;
662                 su2ip_addr(&ip, &uac->request.dst.to);
663                 DBG("t_send_branch: send to %s:%d (%d) failed\n",
664                                                         ip_addr2a(&ip), su_getport(&uac->request.dst.to),
665                                                         uac->request.dst.proto);
666 #ifdef USE_DST_BLACKLIST
667                 if (use_dst_blacklist)
668                         dst_blacklist_add(BLST_ERR_SEND, &uac->request.dst);
669 #endif
670 #ifdef USE_DNS_FAILOVER
671                 /* if the destination resolves to more ips, add another
672                  *  branch/uac */
673                 if (use_dns_failover){
674                         ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
675                         if (ret>=0){
676                                 /* success, return new branch */
677                                 DBG("t_send_branch: send on branch %d failed, adding another"
678                                                 " branch with another ip\n", branch);
679                                 return ret;
680                         }
681                 }
682 #endif
683                 LOG(L_ERR, "ERROR: t_send_branch: sending request on branch %d "
684                                 "failed\n", branch);
685                 if (proxy) { proxy->errors++; proxy->ok=0; }
686                 return -2;
687         } else {
688                 /* start retr. only if the send succeeded */
689                 if (start_retr( &uac->request )!=0){
690                         LOG(L_CRIT, "BUG: t_send_branch: retr. already started for %p\n",
691                                         &uac->request);
692                         return -2;
693                 }
694         }
695         return ret;
696 }
697
698
699
700 /* function returns:
701  *       1 - forward successful
702  *      -1 - error during forward
703  */
704 int t_forward_nonack( struct cell *t, struct sip_msg* p_msg , 
705         struct proxy_l * proxy, int proto)
706 {
707         int branch_ret, lowest_ret;
708         str current_uri;
709         branch_bm_t     added_branches;
710         int first_branch;
711         int i, q;
712         struct cell *t_invite;
713         int success_branch;
714         int try_new;
715         int lock_replies;
716         str dst_uri;
717         struct socket_info* si, *backup_si;
718
719         /* make -Wall happy */
720         current_uri.s=0;
721
722         set_kr(REQ_FWDED);
723
724         if (p_msg->REQ_METHOD==METHOD_CANCEL) {
725                 t_invite=t_lookupOriginalT(  p_msg );
726                 if (t_invite!=T_NULL_CELL) {
727                         e2e_cancel( p_msg, t, t_invite );
728                         UNREF(t_invite);
729                         return 1;
730                 }
731         }
732         if (t->flags & T_CANCELED){
733                 DBG("t_forward_non_ack: no forwarding on canceled branch\n");
734                 ser_error=E_CANCELED;
735                 return -1;
736         }
737
738         backup_si = p_msg->force_send_socket;
739         /* if no more specific error code is known, use this */
740         lowest_ret=E_BUG;
741         /* branches added */
742         added_branches=0;
743         /* branch to begin with */
744         first_branch=t->nr_of_outgoings;
745
746         if (t->on_branch) {
747                 /* tell add_uac that it should run branch route actions */
748                 branch_route = t->on_branch;
749                 /* reset the flag before running the actions (so that it
750                  * could be set again in branch_route if needed
751                  */
752                 t_on_branch(0);
753         } else {
754                 branch_route = 0;
755         }
756         
757         /* on first-time forwarding, use current uri, later only what
758            is in additional branches (which may be continuously refilled
759         */
760         if (first_branch==0) {
761                 try_new=1;
762                 branch_ret=add_uac( t, p_msg, GET_RURI(p_msg), GET_NEXT_HOP(p_msg), proxy, proto );
763                 if (branch_ret>=0) 
764                         added_branches |= 1<<branch_ret;
765                 else
766                         lowest_ret=branch_ret;
767         } else try_new=0;
768
769         init_branch_iterator();
770         while((current_uri.s=next_branch( &current_uri.len, &q, &dst_uri.s, &dst_uri.len, &si))) {
771                 try_new++;
772                 p_msg->force_send_socket = si;
773                 branch_ret=add_uac( t, p_msg, &current_uri, 
774                                     (dst_uri.len) ? (&dst_uri) : &current_uri, 
775                                     proxy, proto);
776                 /* pick some of the errors in case things go wrong;
777                    note that picking lowest error is just as good as
778                    any other algorithm which picks any other negative
779                    branch result */
780                 if (branch_ret>=0) 
781                         added_branches |= 1<<branch_ret;
782                 else
783                         lowest_ret=branch_ret;
784         }
785         /* consume processed branches */
786         clear_branches();
787
788         p_msg->force_send_socket = backup_si;
789
790         /* don't forget to clear all branches processed so far */
791
792         /* things went wrong ... no new branch has been fwd-ed at all */
793         if (added_branches==0) {
794                 if (try_new==0) {
795                         LOG(L_ERR, "ERROR: t_forward_nonack: no branches for forwarding\n");
796                         return -1;
797                 }
798                 LOG(L_ERR, "ERROR: t_forward_nonack: failure to add branches\n");
799                 return lowest_ret;
800         }
801
802         /* send them out now */
803         success_branch=0;
804         lock_replies= ! ((rmode==MODE_ONFAILURE) && (t==get_t()));
805         for (i=first_branch; i<t->nr_of_outgoings; i++) {
806                 if (added_branches & (1<<i)) {
807                         
808                         branch_ret=t_send_branch(t, i, p_msg , proxy, lock_replies);
809                         if (branch_ret>=0){ /* some kind of success */
810                                 if (branch_ret==i) /* success */
811                                         success_branch++;
812                                 else /* new branch added */
813                                         added_branches |= 1<<branch_ret;
814                         }
815                 }
816         }
817         if (success_branch<=0) {
818                 ser_error=E_SEND;
819                 /* the caller should take care and delete the transaction */
820                 return -1;
821         }
822         return 1;
823 }
824
825
826
827 /* WARNING: doesn't work from failure route (deadlock, uses t_relay_to which
828  *  is failure route unsafe) */
829 int t_replicate(struct sip_msg *p_msg,  struct proxy_l *proxy, int proto )
830 {
831         /* this is a quite horrible hack -- we just take the message
832            as is, including Route-s, Record-route-s, and Vias ,
833            forward it downstream and prevent replies received
834            from relaying by setting the replication/local_trans bit;
835
836                 nevertheless, it should be good enough for the primary
837                 customer of this function, REGISTER replication
838
839                 if we want later to make it thoroughly, we need to
840                 introduce delete lumps for all the header fields above
841         */
842         return t_relay_to(p_msg, proxy, proto, 1 /* replicate */);
843 }