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