5 * Copyright (C) 2001-2003 FhG Fokus
7 * This file is part of ser, a free SIP server.
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
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:
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.
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
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
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)
80 #include "../../dprint.h"
81 #include "../../config.h"
82 #include "../../parser/parser_f.h"
84 #include "../../timer.h"
85 #include "../../hash_func.h"
86 #include "../../globals.h"
87 #include "../../cfg_core.h"
88 #include "../../mem/mem.h"
89 #include "../../dset.h"
90 #include "../../action.h"
91 #include "../../data_lump.h"
92 #include "../../onsend.h"
93 #include "../../compiler_opt.h"
96 #include "t_msgbuilder.h"
101 #include "fix_lumps.h"
103 #ifdef USE_DNS_FAILOVER
104 #include "../../dns_cache.h"
105 #include "../../cfg_core.h" /* cfg_get(core, core_cfg, use_dns_failover) */
107 #ifdef USE_DST_BLACKLIST
108 #include "../../dst_blacklist.h"
110 #include "../../select_buf.h" /* reset_static_buffer() */
112 /* cancel hop by hop */
113 #define E2E_CANCEL_HOP_BY_HOP
115 static int goto_on_branch = 0, branch_route = 0;
117 void t_on_branch( unsigned int go_to )
119 struct cell *t = get_t();
121 /* in MODE_REPLY and MODE_ONFAILURE T will be set to current transaction;
122 * in MODE_REQUEST T will be set only if the transaction was already
123 * created; if not -> use the static variable */
124 if (!t || t==T_UNDEFINED ) {
125 goto_on_branch=go_to;
127 get_t()->on_branch = go_to;
131 unsigned int get_on_branch(void)
133 return goto_on_branch;
137 static char *print_uac_request( struct cell *t, struct sip_msg *i_req,
138 int branch, str *uri, unsigned int *len, struct dest_info* dst)
142 struct lump* add_rm_backup, *body_lumps_backup;
143 struct sip_uri parsed_uri_bak;
144 int parsed_uri_ok_bak, uri_backed_up;
146 struct run_act_ctx ra_ctx;
149 msg_uri_bak.s=0; /* kill warnings */
154 /* ... we calculate branch ... */
155 if (!t_calc_branch(t, branch, i_req->add_to_branch_s,
156 &i_req->add_to_branch_len ))
158 LOG(L_ERR, "ERROR: print_uac_request: branch computation failed\n");
162 /* ... update uri ... */
163 msg_uri=GET_RURI(i_req);
164 if ((msg_uri->s!=uri->s) || (msg_uri->len!=uri->len)){
165 msg_uri_bak=i_req->new_uri;
166 parsed_uri_ok_bak=i_req->parsed_uri_ok;
167 parsed_uri_bak=i_req->parsed_uri;
169 i_req->parsed_uri_ok=0;
173 add_rm_backup = i_req->add_rm;
174 body_lumps_backup = i_req->body_lumps;
175 i_req->add_rm = dup_lump_list(i_req->add_rm);
176 i_req->body_lumps = dup_lump_list(i_req->body_lumps);
178 if (unlikely(branch_route)) {
179 reset_static_buffer();
180 /* run branch_route actions if provided */
181 init_run_actions_ctx(&ra_ctx);
182 if (run_actions(&ra_ctx, branch_rt.rlist[branch_route], i_req) < 0) {
183 LOG(L_ERR, "ERROR: print_uac_request: Error in run_actions\n");
187 /* run the specific callbacks for this transaction */
188 if (unlikely(has_tran_tmcbs(t, TMCB_REQUEST_FWDED)))
189 run_trans_callbacks( TMCB_REQUEST_FWDED , t, i_req, 0,
192 /* ... and build it now */
193 buf=build_req_buf_from_sip_req( i_req, len, dst);
195 if (buf[*len-1]==0) {
196 LOG(L_ERR, "ERROR: print_uac_request: sanity check failed\n");
201 LOG(L_ERR, "ERROR: print_uac_request: no pkg_mem\n");
202 ser_error=E_OUT_OF_MEM;
206 shbuf=(char *)shm_malloc(*len);
208 ser_error=E_OUT_OF_MEM;
209 LOG(L_ERR, "ERROR: print_uac_request: no shmem\n");
212 memcpy( shbuf, buf, *len );
217 /* Delete the duplicated lump lists, this will also delete
218 * all lumps created here, such as lumps created in per-branch
219 * routing sections, Via, and Content-Length headers created in
220 * build_req_buf_from_sip_req
222 free_duped_lump_list(i_req->add_rm);
223 free_duped_lump_list(i_req->body_lumps);
224 /* Restore the lists from backups */
225 i_req->add_rm = add_rm_backup;
226 i_req->body_lumps = body_lumps_backup;
227 /* restore the new_uri from the backup */
229 i_req->new_uri=msg_uri_bak;
230 i_req->parsed_uri=parsed_uri_bak;
231 i_req->parsed_uri_ok=parsed_uri_ok_bak;
238 /* introduce a new uac, which is blind -- it only creates the
239 data structures and starts FR timer, but that's it; it does
240 not print messages and send anything anywhere; that is good
241 for FIFO apps -- the transaction must look operationally
242 and FR must be ticking, whereas the request is "forwarded"
243 using a non-SIP way and will be replied the same way
245 int add_blind_uac( /*struct cell *t*/ )
247 unsigned short branch;
251 if (t==T_UNDEFINED || !t ) {
252 LOG(L_ERR, "ERROR: add_blind_uac: no transaction context\n");
256 branch=t->nr_of_outgoings;
257 if (branch==MAX_BRANCHES) {
258 LOG(L_ERR, "ERROR: add_blind_uac: "
259 "maximum number of branches exceeded\n");
262 /* make sure it will be replied */
263 t->flags |= T_NOISY_CTIMER_FLAG;
264 membar_write(); /* to allow lockless which_cancel() we want to be sure
265 all the writes finished before updating branch number*/
266 t->nr_of_outgoings=(branch+1);
267 /* start FR timer -- protocol set by default to PROTO_NONE,
268 which means retransmission timer will not be started
270 if (start_retr(&t->uac[branch].request)!=0)
271 LOG(L_CRIT, "BUG: add_blind_uac: start retr failed for %p\n",
272 &t->uac[branch].request);
273 /* we are on a timer -- don't need to put on wait on script
278 return 1; /* success */
281 /* introduce a new uac to transaction; returns its branch id (>=0)
282 or error (<0); it doesn't send a message yet -- a reply to it
283 might interfere with the processes of adding multiple branches;
284 On error returns <0 & sets ser_error to the same value
286 int add_uac( struct cell *t, struct sip_msg *request, str *uri, str* next_hop,
287 struct proxy_l *proxy, int proto )
291 unsigned short branch;
295 branch=t->nr_of_outgoings;
296 if (branch==MAX_BRANCHES) {
297 LOG(L_ERR, "ERROR: add_uac: maximum number of branches exceeded\n");
298 ret=ser_error=E_TOO_MANY_BRANCHES;
302 /* check existing buffer -- rewriting should never occur */
303 if (t->uac[branch].request.buffer) {
304 LOG(L_CRIT, "ERROR: add_uac: buffer rewrite attempt\n");
309 /* check DNS resolution */
311 /* dst filled from the proxy */
312 init_dest_info(&t->uac[branch].request.dst);
313 t->uac[branch].request.dst.proto=get_proto(proto, proxy->proto);
314 proxy2su(&t->uac[branch].request.dst.to, proxy);
315 /* fill dst send_sock */
316 t->uac[branch].request.dst.send_sock =
317 get_send_socket( request, &t->uac[branch].request.dst.to,
318 t->uac[branch].request.dst.proto);
320 #ifdef USE_DNS_FAILOVER
321 if (uri2dst(&t->uac[branch].dns_h, &t->uac[branch].request.dst,
322 request, next_hop?next_hop:uri, proto) == 0)
324 /* dst filled from the uri & request (send_socket) */
325 if (uri2dst(&t->uac[branch].request.dst, request,
326 next_hop ? next_hop: uri, proto)==0)
329 ret=ser_error=E_BAD_ADDRESS;
334 /* check if send_sock is ok */
335 if (t->uac[branch].request.dst.send_sock==0) {
336 LOG(L_ERR, "ERROR: add_uac: can't fwd to af %d, proto %d "
337 " (no corresponding listening socket)\n",
338 t->uac[branch].request.dst.to.s.sa_family,
339 t->uac[branch].request.dst.proto );
340 ret=ser_error=E_NO_SOCKET;
344 /* now message printing starts ... */
345 shbuf=print_uac_request( t, request, branch, uri,
346 &len, &t->uac[branch].request.dst);
348 ret=ser_error=E_OUT_OF_MEM;
352 /* things went well, move ahead and install new buffer! */
353 t->uac[branch].request.buffer=shbuf;
354 t->uac[branch].request.buffer_len=len;
355 t->uac[branch].uri.s=t->uac[branch].request.buffer+
356 request->first_line.u.request.method.len+1;
357 t->uac[branch].uri.len=uri->len;
358 membar_write(); /* to allow lockless ops (e.g. which_cancel()) we want
359 to be sure everything above is fully written before
360 updating branches no. */
361 t->nr_of_outgoings=(branch+1);
365 proxy_mark(proxy, 1);
377 #ifdef USE_DNS_FAILOVER
378 /* introduce a new uac to transaction, based on old_uac and a possible
379 * new ip address (if the dns name resolves to more ips). If no more
380 * ips are found => returns -1.
381 * returns its branch id (>=0)
382 or error (<0) and sets ser_error if needed; it doesn't send a message
384 might interfere with the processes of adding multiple branches
385 if lock_replies is 1 replies will be locked for t until the new branch
386 is added (to prevent add branches races). Use 0 if the reply lock is
387 already held, e.g. in failure route/handlers (WARNING: using 1 in a
388 failure route will cause a deadlock).
390 int add_uac_dns_fallback( struct cell *t, struct sip_msg* msg,
391 struct ua_client* old_uac,
397 if (cfg_get(core, core_cfg, use_dns_failover) &&
398 !((t->flags & T_DONT_FORK) || uac_dont_fork(old_uac)) &&
399 dns_srv_handle_next(&old_uac->dns_h, 0)){
401 /* use reply lock to guarantee nobody is adding a branch
402 * in the same time */
404 /* check again that we can fork */
405 if ((t->flags & T_DONT_FORK) || uac_dont_fork(old_uac)){
407 DBG("add_uac_dns_fallback: no forking on => no new"
412 if (t->nr_of_outgoings >= MAX_BRANCHES){
413 LOG(L_ERR, "ERROR: add_uac_dns_fallback: maximum number of "
414 "branches exceeded\n");
417 ret=ser_error=E_TOO_MANY_BRANCHES;
420 /* copy the dns handle into the new uac */
421 dns_srv_handle_cpy(&t->uac[t->nr_of_outgoings].dns_h,
423 /* add_uac will use dns_h => next_hop will be ignored.
424 * Unfortunately we can't reuse the old buffer, the branch id
425 * must be changed and the send_socket might be different =>
426 * re-create the whole uac */
427 ret=add_uac(t, msg, &old_uac->uri, 0, 0,
428 old_uac->request.dst.proto);
430 /* failed, delete the copied dns_h */
431 dns_srv_handle_put(&t->uac[t->nr_of_outgoings].dns_h);
442 int e2e_cancel_branch( struct sip_msg *cancel_msg, struct cell *t_cancel,
443 struct cell *t_invite, int branch )
450 if (t_cancel->uac[branch].request.buffer) {
451 LOG(L_CRIT, "ERROR: e2e_cancel_branch: buffer rewrite attempt\n");
455 if (t_invite->uac[branch].request.buffer==0){
456 /* inactive / deleted branch */
459 t_invite->uac[branch].request.flags|=F_RB_CANCELED;
461 /* note -- there is a gap in proxy stats -- we don't update
462 proxy stats with CANCEL (proxy->ok, proxy->tx, etc.)
466 if (cfg_get(tm, tm_cfg, reparse_invite)) {
467 /* buffer is built localy from the INVITE which was sent out */
468 if (cancel_msg->add_rm || cancel_msg->body_lumps) {
469 LOG(L_WARN, "WARNING: e2e_cancel_branch: CANCEL is built locally, "
470 "thus lumps are not applied to the message!\n");
472 shbuf=build_local_reparse( t_invite, branch, &len, CANCEL, CANCEL_LEN, &t_invite->to);
475 /* buffer is constructed from the received CANCEL with applying lumps */
476 shbuf=print_uac_request( t_cancel, cancel_msg, branch,
477 &t_invite->uac[branch].uri, &len,
478 &t_invite->uac[branch].request.dst);
482 LOG(L_ERR, "ERROR: e2e_cancel_branch: printing e2e cancel failed\n");
483 ret=ser_error=E_OUT_OF_MEM;
488 t_cancel->uac[branch].request.dst=t_invite->uac[branch].request.dst;
489 t_cancel->uac[branch].request.buffer=shbuf;
490 t_cancel->uac[branch].request.buffer_len=len;
491 t_cancel->uac[branch].uri.s=t_cancel->uac[branch].request.buffer+
492 cancel_msg->first_line.u.request.method.len+1;
493 t_cancel->uac[branch].uri.len=t_invite->uac[branch].uri.len;
504 void e2e_cancel( struct sip_msg *cancel_msg,
505 struct cell *t_cancel, struct cell *t_invite )
507 branch_bm_t cancel_bm;
508 #ifndef E2E_CANCEL_HOP_BY_HOP
514 struct tmcb_params tmcb;
519 if (unlikely(has_tran_tmcbs(t_invite, TMCB_E2ECANCEL_IN))){
520 INIT_TMCB_PARAMS(tmcb, cancel_msg, 0, cancel_msg->REQ_METHOD);
521 run_trans_callbacks_internal(&t_invite->tmcb_hl, TMCB_E2ECANCEL_IN,
524 /* first check if there are any branches */
525 if (t_invite->nr_of_outgoings==0){
526 t_invite->flags|=T_CANCELED;
527 /* no branches yet => force a reply to the invite */
528 t_reply( t_invite, t_invite->uas.request, 487, CANCELED );
529 DBG("DEBUG: e2e_cancel: e2e cancel -- no more pending branches\n");
530 t_reply( t_cancel, cancel_msg, 200, CANCEL_DONE );
534 /* determine which branches to cancel ... */
535 which_cancel( t_invite, &cancel_bm );
536 #ifdef E2E_CANCEL_HOP_BY_HOP
537 /* we don't need to set t_cancel label to be the same as t_invite if
538 * we do hop by hop cancel. The cancel transaction will have a different
539 * label, but this is not a problem since this transaction is only used to
540 * send a reply back. The cancels sent upstream will be part of the invite
541 * transaction (local_cancel retr. bufs) and they will be generated with
542 * the same via as the invite.
543 * Note however that setting t_cancel label the same as t_invite will work
544 * too (the upstream cancel replies will properly match the t_invite
545 * transaction and will not match the t_cancel because t_cancel will always
546 * have 0 branches and we check for the branch number in
547 * t_reply_matching() ).
549 for (i=0; i<t_invite->nr_of_outgoings; i++)
550 if (cancel_bm & (1<<i)) {
551 /* it's safe to get the reply lock since e2e_cancel is
552 * called with the cancel as the "current" transaction so
553 * at most t_cancel REPLY_LOCK is held in this process =>
554 * no deadlock possibility */
555 ret=cancel_branch(t_invite, i, cfg_get(tm,tm_cfg, cancel_b_flags));
556 if (ret<0) cancel_bm &= ~(1<<i);
557 if (ret<lowest_error) lowest_error=ret;
559 #else /* ! E2E_CANCEL_HOP_BY_HOP */
560 /* fix label -- it must be same for reply matching (the label is part of
561 * the generated via branch for the cancels sent upstream and if it
562 * would be different form the one in the INVITE the transactions would not
564 t_cancel->label=t_invite->label;
565 t_cancel->nr_of_outgoings=t_invite->nr_of_outgoings;
566 /* ... and install CANCEL UACs */
567 for (i=0; i<t_invite->nr_of_outgoings; i++)
568 if ((cancel_bm & (1<<i)) && (t_invite->uac[i].last_received>=100)) {
569 ret=e2e_cancel_branch(cancel_msg, t_cancel, t_invite, i);
570 if (ret<0) cancel_bm &= ~(1<<i);
571 if (ret<lowest_error) lowest_error=ret;
575 for (i = 0; i < t_cancel->nr_of_outgoings; i++) {
576 if (cancel_bm & (1 << i)) {
577 if (t_invite->uac[i].last_received>=100){
578 /* Provisional reply received on this branch, send CANCEL */
579 /* we do need to stop the retr. timers if the request is not
580 * an invite and since the stop_rb_retr() cost is lower then
581 * the invite check we do it always --andrei */
582 stop_rb_retr(&t_invite->uac[i].request);
583 if (SEND_BUFFER(&t_cancel->uac[i].request) == -1) {
584 LOG(L_ERR, "ERROR: e2e_cancel: send failed\n");
588 if (unlikely(has_tran_tmcbs(t_cancel, TMCB_REQUEST_SENT)))
589 run_onsend_callbacks(TMCB_REQUEST_SENT,
590 &t_cancel->uac[i].request,
591 cancel_msg, 0, TMCB_LOCAL_F);
594 if (start_retr( &t_cancel->uac[i].request )!=0)
595 LOG(L_CRIT, "BUG: e2e_cancel: failed to start retr."
596 " for %p\n", &t_cancel->uac[i].request);
598 /* No provisional response received, stop
599 * retransmission timers */
600 if (!(cfg_get(tm, tm_cfg, cancel_b_flags) &
601 F_CANCEL_B_FORCE_RETR))
602 stop_rb_retr(&t_invite->uac[i].request);
603 /* no need to stop fr, it will be stoped by relay_reply
604 * put_on_wait -- andrei */
605 /* Generate faked reply */
606 if (cfg_get(tm, tm_cfg, cancel_b_flags) &
607 F_CANCEL_B_FAKE_REPLY){
608 LOCK_REPLIES(t_invite);
609 if (relay_reply(t_invite, FAKED_REPLY, i, 487, &tmp_bm) ==
617 #endif /*E2E_CANCEL_HOP_BY_HOP */
619 /* if error occurred, let it know upstream (final reply
620 will also move the transaction on wait state
622 if (lowest_error<0) {
623 LOG(L_ERR, "ERROR: cancel error\n");
624 /* if called from failure_route, make sure that the unsafe version
625 * is called (we are already holding the reply mutex for the cancel
628 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
629 t_reply_unsafe( t_cancel, cancel_msg, 500, "cancel error");
631 t_reply( t_cancel, cancel_msg, 500, "cancel error");
632 } else if (cancel_bm) {
633 /* if there are pending branches, let upstream know we
636 DBG("DEBUG: e2e_cancel: e2e cancel proceeding\n");
637 /* if called from failure_route, make sure that the unsafe version
638 * is called (we are already hold the reply mutex for the cancel
641 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
642 t_reply_unsafe( t_cancel, cancel_msg, 200, CANCELING );
644 t_reply( t_cancel, cancel_msg, 200, CANCELING );
646 /* if the transaction exists, but there are no more pending
647 branches, tell upstream we're done
649 DBG("DEBUG: e2e_cancel: e2e cancel -- no more pending branches\n");
650 /* if called from failure_route, make sure that the unsafe version
651 * is called (we are already hold the reply mutex for the cancel
654 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
655 t_reply_unsafe( t_cancel, cancel_msg, 200, CANCEL_DONE );
657 t_reply( t_cancel, cancel_msg, 200, CANCEL_DONE );
663 /* sends one uac/branch buffer and fallbacks to other ips if
664 * the destination resolves to several addresses
665 * Takes care of starting timers a.s.o. (on send success)
666 * returns: -2 on error, -1 on drop, current branch id on success,
667 * new branch id on send error/blacklist, when failover is possible
668 * (ret>=0 && ret!=branch)
669 * if lock_replies is 1, the replies for t will be locked when adding
670 * new branches (to prevent races). Use 0 from failure routes or other
671 * places where the reply lock is already held, to avoid deadlocks. */
672 int t_send_branch( struct cell *t, int branch, struct sip_msg* p_msg ,
673 struct proxy_l * proxy, int lock_replies)
675 struct ip_addr ip; /* debugging */
677 struct ua_client* uac;
681 if (run_onsend(p_msg, &uac->request.dst, uac->request.buffer,
682 uac->request.buffer_len)==0){
683 /* disable the current branch: set a "fake" timeout
684 * reply code but don't set uac->reply, to avoid overriding
685 * a higly unlikely, perfectly timed fake reply (to a message
687 * (code=final reply && reply==0 => t_pick_branch won't ever pick it)*/
688 uac->last_received=408;
689 su2ip_addr(&ip, &uac->request.dst.to);
690 DBG("t_send_branch: onsend_route dropped msg. to %s:%d (%d)\n",
691 ip_addr2a(&ip), su_getport(&uac->request.dst.to),
692 uac->request.dst.proto);
693 #ifdef USE_DNS_FAILOVER
694 /* if the destination resolves to more ips, add another
696 if (cfg_get(core, core_cfg, use_dns_failover)){
697 ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
699 su2ip_addr(&ip, &uac->request.dst.to);
700 DBG("t_send_branch: send on branch %d failed "
701 "(onsend_route), trying another ip %s:%d (%d)\n",
702 branch, ip_addr2a(&ip),
703 su_getport(&uac->request.dst.to),
704 uac->request.dst.proto);
705 /* success, return new branch */
709 #endif /* USE_DNS_FAILOVER*/
710 return -1; /* drop, try next branch */
712 #ifdef USE_DST_BLACKLIST
713 if (cfg_get(core, core_cfg, use_dst_blacklist)
715 && (p_msg->REQ_METHOD & cfg_get(tm, tm_cfg, tm_blst_methods_lookup))
717 if (dst_is_blacklisted(&uac->request.dst, p_msg)){
718 su2ip_addr(&ip, &uac->request.dst.to);
719 DBG("t_send_branch: blacklisted destination: %s:%d (%d)\n",
720 ip_addr2a(&ip), su_getport(&uac->request.dst.to),
721 uac->request.dst.proto);
722 /* disable the current branch: set a "fake" timeout
723 * reply code but don't set uac->reply, to avoid overriding
724 * a higly unlikely, perfectly timed fake reply (to a message
725 * we never sent). (code=final reply && reply==0 =>
726 * t_pick_branch won't ever pick it)*/
727 uac->last_received=408;
728 #ifdef USE_DNS_FAILOVER
729 /* if the destination resolves to more ips, add another
731 if (cfg_get(core, core_cfg, use_dns_failover)){
732 ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
734 su2ip_addr(&ip, &uac->request.dst.to);
735 DBG("t_send_branch: send on branch %d failed (blacklist),"
736 " trying another ip %s:%d (%d)\n", branch,
737 ip_addr2a(&ip), su_getport(&uac->request.dst.to),
738 uac->request.dst.proto);
739 /* success, return new branch */
743 #endif /* USE_DNS_FAILOVER*/
744 return -1; /* don't send */
747 #endif /* USE_DST_BLACKLIST */
748 if (SEND_BUFFER( &uac->request)==-1) {
749 /* disable the current branch: set a "fake" timeout
750 * reply code but don't set uac->reply, to avoid overriding
751 * a highly unlikely, perfectly timed fake reply (to a message
753 * (code=final reply && reply==0 => t_pick_branch won't ever pick it)*/
754 uac->last_received=408;
755 su2ip_addr(&ip, &uac->request.dst.to);
756 DBG("t_send_branch: send to %s:%d (%d) failed\n",
757 ip_addr2a(&ip), su_getport(&uac->request.dst.to),
758 uac->request.dst.proto);
759 #ifdef USE_DST_BLACKLIST
760 if (cfg_get(core, core_cfg, use_dst_blacklist))
761 dst_blacklist_add(BLST_ERR_SEND, &uac->request.dst, p_msg);
763 #ifdef USE_DNS_FAILOVER
764 /* if the destination resolves to more ips, add another
766 if (cfg_get(core, core_cfg, use_dns_failover)){
767 ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
769 /* success, return new branch */
770 DBG("t_send_branch: send on branch %d failed, adding another"
771 " branch with another ip\n", branch);
776 LOG(L_ERR, "ERROR: t_send_branch: sending request on branch %d "
778 if (proxy) { proxy->errors++; proxy->ok=0; }
782 if (unlikely(has_tran_tmcbs(t, TMCB_REQUEST_SENT)))
783 run_onsend_callbacks(TMCB_REQUEST_SENT, &uac->request, p_msg, 0,0);
785 /* start retr. only if the send succeeded */
786 if (start_retr( &uac->request )!=0){
787 LOG(L_CRIT, "BUG: t_send_branch: retr. already started for %p\n",
798 * 1 - forward successful
799 * -1 - error during forward
801 int t_forward_nonack( struct cell *t, struct sip_msg* p_msg ,
802 struct proxy_l * proxy, int proto)
804 int branch_ret, lowest_ret;
806 branch_bm_t added_branches;
809 struct cell *t_invite;
814 struct socket_info* si, *backup_si;
817 /* make -Wall happy */
820 if (t->flags & T_CANCELED){
821 DBG("t_forward_non_ack: no forwarding on a canceled transaction\n");
822 ser_error=E_CANCELED;
825 if (p_msg->REQ_METHOD==METHOD_CANCEL) {
826 t_invite=t_lookupOriginalT( p_msg );
827 if (t_invite!=T_NULL_CELL) {
828 e2e_cancel( p_msg, t, t_invite );
830 /* it should be set to REQ_RPLD by e2e_cancel, which should
831 * send a final reply */
837 backup_si = p_msg->force_send_socket;
838 /* if no more specific error code is known, use this */
842 /* branch to begin with */
843 first_branch=t->nr_of_outgoings;
846 /* tell add_uac that it should run branch route actions */
847 branch_route = t->on_branch;
848 /* reset the flag before running the actions (so that it
849 * could be set again in branch_route if needed
856 /* on first-time forwarding, use current uri, later only what
857 is in additional branches (which may be continuously refilled
859 if (first_branch==0) {
861 branch_ret=add_uac( t, p_msg, GET_RURI(p_msg), GET_NEXT_HOP(p_msg),
864 added_branches |= 1<<branch_ret;
866 lowest_ret=MIN_int(lowest_ret, branch_ret);
869 init_branch_iterator();
870 while((current_uri.s=next_branch( ¤t_uri.len, &q, &dst_uri.s, &dst_uri.len, &si))) {
872 p_msg->force_send_socket = si;
873 branch_ret=add_uac( t, p_msg, ¤t_uri,
874 (dst_uri.len) ? (&dst_uri) : ¤t_uri,
876 /* pick some of the errors in case things go wrong;
877 note that picking lowest error is just as good as
878 any other algorithm which picks any other negative
881 added_branches |= 1<<branch_ret;
883 lowest_ret=MIN_int(lowest_ret, branch_ret);
885 /* consume processed branches */
888 p_msg->force_send_socket = backup_si;
890 /* don't forget to clear all branches processed so far */
892 /* things went wrong ... no new branch has been fwd-ed at all */
893 if (added_branches==0) {
895 LOG(L_ERR, "ERROR: t_forward_nonack: no branches for"
897 /* either failed to add branches, or there were no more branches
899 ser_error=MIN_int(lowest_ret, E_CFG);
902 LOG(L_ERR, "ERROR: t_forward_nonack: failure to add branches\n");
903 ser_error=lowest_ret;
906 ser_error=0; /* clear branch adding errors */
907 /* send them out now */
909 lock_replies= ! ((rmode==MODE_ONFAILURE) && (t==get_t()));
910 for (i=first_branch; i<t->nr_of_outgoings; i++) {
911 if (added_branches & (1<<i)) {
913 branch_ret=t_send_branch(t, i, p_msg , proxy, lock_replies);
914 if (branch_ret>=0){ /* some kind of success */
915 if (branch_ret==i) /* success */
917 else /* new branch added */
918 added_branches |= 1<<branch_ret;
922 if (success_branch<=0) {
923 /* return always E_SEND for now
924 * (the real reason could be: denied by onsend routes, blacklisted,
925 * send failed or any of the errors listed before + dns failed
926 * when attempting dns failover) */
928 /* else return the last error (?) */
929 /* the caller should take care and delete the transaction */
932 ser_error=0; /* clear branch send errors, we have overall success */
939 /* cancel handling/forwarding function
940 * CANCELs with no matching transaction are handled in function of
941 * the unmatched_cancel config var: they are either forwarded statefully,
942 * statelessly or dropped.
944 * 1 - forward successful
945 * 0 - error, but do not reply
946 * <0 - error during forward
947 * it also sets *tran if a transaction was created
949 int t_forward_cancel(struct sip_msg* p_msg , struct proxy_l * proxy, int proto,
952 struct cell* t_invite;
956 struct dest_info dst;
962 /* handle cancels for which no transaction was created yet */
963 if (cfg_get(tm, tm_cfg, unmatched_cancel)==UM_CANCEL_STATEFULL){
964 /* create cancel transaction */
965 new_tran=t_newtran(p_msg);
966 if (new_tran<=0 && new_tran!=E_SCRIPT){
968 /* retransmission => do nothing */
971 /* some error => return it or DROP */
972 ret=(ser_error==E_BAD_VIA && reply_to_via) ? 0: new_tran;
976 ret=t_forward_nonack(t, p_msg, proxy, proto);
980 t_invite=t_lookupOriginalT( p_msg );
981 if (t_invite!=T_NULL_CELL) {
982 /* create cancel transaction */
983 new_tran=t_newtran(p_msg);
984 if (new_tran<=0 && new_tran!=E_SCRIPT){
986 /* retransmission => do nothing */
989 /* some error => return it or DROP */
990 ret=(ser_error==E_BAD_VIA && reply_to_via) ? 0: new_tran;
995 e2e_cancel( p_msg, t, t_invite );
999 }else /* no coresponding INVITE transaction */
1000 if (cfg_get(tm, tm_cfg, unmatched_cancel)==UM_CANCEL_DROP){
1001 DBG("t_forward_nonack: non matching cancel dropped\n");
1002 ret=1; /* do nothing -> drop */
1005 /* UM_CANCEL_STATELESS -> stateless forward */
1006 DBG( "SER: forwarding CANCEL statelessly \n");
1008 init_dest_info(&dst);
1010 if (get_uri_send_info(GET_NEXT_HOP(p_msg), &host,
1011 &port, &dst.proto, &comp)!=0){
1018 /* dst->send_sock not set, but forward_request
1019 * will take care of it */
1020 ret=forward_request(p_msg, &host, port, &dst);
1023 init_dest_info(&dst);
1024 dst.proto=get_proto(proto, proxy->proto);
1025 proxy2su(&dst.to, proxy);
1026 /* dst->send_sock not set, but forward_request
1027 * will take care of it */
1028 ret=forward_request( p_msg , 0, 0, &dst) ;
1038 /* Relays a CANCEL request if a corresponding INVITE transaction
1039 * can be found. The function is supposed to be used at the very
1040 * beginning of the script with reparse_invite=1 module parameter.
1043 * 0: the CANCEL was successfully relayed
1044 * (or error occured but reply cannot be sent) => DROP
1045 * 1: no corresponding INVITE transaction exisis
1046 * <0: corresponding INVITE transaction exisis but error occured
1048 int t_relay_cancel(struct sip_msg* p_msg)
1050 struct cell* t_invite;
1055 t_invite=t_lookupOriginalT( p_msg );
1056 if (t_invite!=T_NULL_CELL) {
1057 /* create cancel transaction */
1058 new_tran=t_newtran(p_msg);
1059 if (new_tran<=0 && new_tran!=E_SCRIPT){
1061 /* retransmission => DROP,
1062 t_newtran() takes care about it */
1065 /* some error => return it or DROP */
1066 ret=(ser_error==E_BAD_VIA && reply_to_via) ? 0: new_tran;
1071 e2e_cancel( p_msg, t, t_invite );
1073 /* return 0 to stop the script processing */
1078 /* no corresponding INVITE trasaction found */
1085 /* WARNING: doesn't work from failure route (deadlock, uses t_relay_to which
1086 * is failure route unsafe) */
1087 int t_replicate(struct sip_msg *p_msg, struct proxy_l *proxy, int proto )
1089 /* this is a quite horrible hack -- we just take the message
1090 as is, including Route-s, Record-route-s, and Vias ,
1091 forward it downstream and prevent replies received
1092 from relaying by setting the replication/local_trans bit;
1094 nevertheless, it should be good enough for the primary
1095 customer of this function, REGISTER replication
1097 if we want later to make it thoroughly, we need to
1098 introduce delete lumps for all the header fields above
1100 return t_relay_to(p_msg, proxy, proto, 1 /* replicate */);