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)
75 * 2007-08-37 In case of DNS failover the new SIP message is constructed
76 * from the message buffer of the failed branch instead of
77 * applying the lumps again, because the per-branch lumps are not saved,
78 * thus, are not available. Set reparse_on_dns_failover to 0 to
79 * revert the change. (Miklos)
85 #include "../../dprint.h"
86 #include "../../config.h"
87 #include "../../parser/parser_f.h"
89 #include "../../timer.h"
90 #include "../../hash_func.h"
91 #include "../../globals.h"
92 #include "../../cfg_core.h"
93 #include "../../mem/mem.h"
94 #include "../../dset.h"
95 #include "../../action.h"
96 #include "../../data_lump.h"
97 #include "../../onsend.h"
98 #include "../../compiler_opt.h"
101 #include "t_msgbuilder.h"
103 #include "t_cancel.h"
104 #include "t_lookup.h"
106 #include "fix_lumps.h"
108 #ifdef USE_DNS_FAILOVER
109 #include "../../dns_cache.h"
110 #include "../../cfg_core.h" /* cfg_get(core, core_cfg, use_dns_failover) */
111 #include "../../msg_translator.h"
112 #include "lw_parser.h"
114 #ifdef USE_DST_BLACKLIST
115 #include "../../dst_blacklist.h"
117 #include "../../select_buf.h" /* reset_static_buffer() */
119 /* cancel hop by hop */
120 #define E2E_CANCEL_HOP_BY_HOP
122 static int goto_on_branch = 0, branch_route = 0;
124 void t_on_branch( unsigned int go_to )
126 struct cell *t = get_t();
128 /* in MODE_REPLY and MODE_ONFAILURE T will be set to current transaction;
129 * in MODE_REQUEST T will be set only if the transaction was already
130 * created; if not -> use the static variable */
131 if (!t || t==T_UNDEFINED ) {
132 goto_on_branch=go_to;
134 get_t()->on_branch = go_to;
138 unsigned int get_on_branch(void)
140 return goto_on_branch;
144 static char *print_uac_request( struct cell *t, struct sip_msg *i_req,
145 int branch, str *uri, unsigned int *len, struct dest_info* dst)
149 struct lump* add_rm_backup, *body_lumps_backup;
150 struct sip_uri parsed_uri_bak;
151 int parsed_uri_ok_bak, uri_backed_up;
153 struct run_act_ctx ra_ctx;
156 msg_uri_bak.s=0; /* kill warnings */
161 /* ... we calculate branch ... */
162 if (!t_calc_branch(t, branch, i_req->add_to_branch_s,
163 &i_req->add_to_branch_len ))
165 LOG(L_ERR, "ERROR: print_uac_request: branch computation failed\n");
169 /* ... update uri ... */
170 msg_uri=GET_RURI(i_req);
171 if ((msg_uri->s!=uri->s) || (msg_uri->len!=uri->len)){
172 msg_uri_bak=i_req->new_uri;
173 parsed_uri_ok_bak=i_req->parsed_uri_ok;
174 parsed_uri_bak=i_req->parsed_uri;
176 i_req->parsed_uri_ok=0;
180 add_rm_backup = i_req->add_rm;
181 body_lumps_backup = i_req->body_lumps;
182 i_req->add_rm = dup_lump_list(i_req->add_rm);
183 i_req->body_lumps = dup_lump_list(i_req->body_lumps);
185 if (unlikely(branch_route)) {
186 reset_static_buffer();
187 /* run branch_route actions if provided */
188 init_run_actions_ctx(&ra_ctx);
189 if (run_actions(&ra_ctx, branch_rt.rlist[branch_route], i_req) < 0) {
190 LOG(L_ERR, "ERROR: print_uac_request: Error in run_actions\n");
194 /* run the specific callbacks for this transaction */
195 if (unlikely(has_tran_tmcbs(t, TMCB_REQUEST_FWDED)))
196 run_trans_callbacks( TMCB_REQUEST_FWDED , t, i_req, 0,
199 /* ... and build it now */
200 buf=build_req_buf_from_sip_req( i_req, len, dst);
202 if (buf[*len-1]==0) {
203 LOG(L_ERR, "ERROR: print_uac_request: sanity check failed\n");
208 LOG(L_ERR, "ERROR: print_uac_request: no pkg_mem\n");
209 ser_error=E_OUT_OF_MEM;
213 shbuf=(char *)shm_malloc(*len);
215 ser_error=E_OUT_OF_MEM;
216 LOG(L_ERR, "ERROR: print_uac_request: no shmem\n");
219 memcpy( shbuf, buf, *len );
224 /* Delete the duplicated lump lists, this will also delete
225 * all lumps created here, such as lumps created in per-branch
226 * routing sections, Via, and Content-Length headers created in
227 * build_req_buf_from_sip_req
229 free_duped_lump_list(i_req->add_rm);
230 free_duped_lump_list(i_req->body_lumps);
231 /* Restore the lists from backups */
232 i_req->add_rm = add_rm_backup;
233 i_req->body_lumps = body_lumps_backup;
234 /* restore the new_uri from the backup */
236 i_req->new_uri=msg_uri_bak;
237 i_req->parsed_uri=parsed_uri_bak;
238 i_req->parsed_uri_ok=parsed_uri_ok_bak;
245 #ifdef USE_DNS_FAILOVER
246 /* Similar to print_uac_request(), but this function uses the outgoing message buffer of
247 the failed branch to construt the new message in case of DNS failover.
249 WARNING: only the first VIA header is replaced in the buffer, the rest
250 of the message is untuched, thus, the send socket is corrected only in the VIA HF.
252 static char *print_uac_request_from_buf( struct cell *t, struct sip_msg *i_req,
253 int branch, str *uri, unsigned int *len, struct dest_info* dst,
254 char *buf, short buf_len)
258 char *via, *old_via_begin, *old_via_end;
259 unsigned int via_len;
263 /* ... we calculate branch ... */
264 if (!t_calc_branch(t, branch, i_req->add_to_branch_s,
265 &i_req->add_to_branch_len ))
267 LOG(L_ERR, "ERROR: print_uac_request_from_buf: branch computation failed\n");
270 branch_str.s = i_req->add_to_branch_s;
271 branch_str.len = i_req->add_to_branch_len;
273 /* find the beginning of the first via header in the buffer */
274 old_via_begin = lw_find_via(buf, buf+buf_len);
275 if (!old_via_begin) {
276 LOG(L_ERR, "ERROR: print_uac_request_from_buf: beginning of via header not found\n");
279 /* find the end of the first via header in the buffer */
280 old_via_end = lw_next_line(old_via_begin, buf+buf_len);
282 LOG(L_ERR, "ERROR: print_uac_request_from_buf: end of via header not found\n");
286 /* create the new VIA HF */
287 via = create_via_hf(&via_len, i_req, dst, &branch_str);
289 LOG(L_ERR, "ERROR: print_uac_request_from_buf: via building failed\n");
293 /* allocate memory for the new buffer */
294 *len = buf_len + via_len - (old_via_end - old_via_begin);
295 shbuf=(char *)shm_malloc(*len);
297 ser_error=E_OUT_OF_MEM;
298 LOG(L_ERR, "ERROR: print_uac_request_from_buf: no shmem\n");
302 /* construct the new buffer */
303 memcpy(shbuf, buf, old_via_begin-buf);
304 memcpy(shbuf+(old_via_begin-buf), via, via_len);
305 memcpy(shbuf+(old_via_begin-buf)+via_len, old_via_end, (buf+buf_len)-old_via_end);
308 if (shbuf[*len-1]==0) {
309 LOG(L_ERR, "ERROR: print_uac_request_from_buf: sanity check failed\n");
321 /* introduce a new uac, which is blind -- it only creates the
322 data structures and starts FR timer, but that's it; it does
323 not print messages and send anything anywhere; that is good
324 for FIFO apps -- the transaction must look operationally
325 and FR must be ticking, whereas the request is "forwarded"
326 using a non-SIP way and will be replied the same way
328 int add_blind_uac( /*struct cell *t*/ )
330 unsigned short branch;
334 if (t==T_UNDEFINED || !t ) {
335 LOG(L_ERR, "ERROR: add_blind_uac: no transaction context\n");
339 branch=t->nr_of_outgoings;
340 if (branch==MAX_BRANCHES) {
341 LOG(L_ERR, "ERROR: add_blind_uac: "
342 "maximum number of branches exceeded\n");
345 /* make sure it will be replied */
346 t->flags |= T_NOISY_CTIMER_FLAG;
347 membar_write(); /* to allow lockless which_cancel() we want to be sure
348 all the writes finished before updating branch number*/
349 t->nr_of_outgoings=(branch+1);
350 /* start FR timer -- protocol set by default to PROTO_NONE,
351 which means retransmission timer will not be started
353 if (start_retr(&t->uac[branch].request)!=0)
354 LOG(L_CRIT, "BUG: add_blind_uac: start retr failed for %p\n",
355 &t->uac[branch].request);
356 /* we are on a timer -- don't need to put on wait on script
361 return 1; /* success */
364 /* introduce a new uac to transaction; returns its branch id (>=0)
365 or error (<0); it doesn't send a message yet -- a reply to it
366 might interfere with the processes of adding multiple branches;
367 On error returns <0 & sets ser_error to the same value
369 int add_uac( struct cell *t, struct sip_msg *request, str *uri, str* next_hop,
370 struct proxy_l *proxy, int proto )
374 unsigned short branch;
378 branch=t->nr_of_outgoings;
379 if (branch==MAX_BRANCHES) {
380 LOG(L_ERR, "ERROR: add_uac: maximum number of branches exceeded\n");
381 ret=ser_error=E_TOO_MANY_BRANCHES;
385 /* check existing buffer -- rewriting should never occur */
386 if (t->uac[branch].request.buffer) {
387 LOG(L_CRIT, "ERROR: add_uac: buffer rewrite attempt\n");
392 /* check DNS resolution */
394 /* dst filled from the proxy */
395 init_dest_info(&t->uac[branch].request.dst);
396 t->uac[branch].request.dst.proto=get_proto(proto, proxy->proto);
397 proxy2su(&t->uac[branch].request.dst.to, proxy);
398 /* fill dst send_sock */
399 t->uac[branch].request.dst.send_sock =
400 get_send_socket( request, &t->uac[branch].request.dst.to,
401 t->uac[branch].request.dst.proto);
403 #ifdef USE_DNS_FAILOVER
404 if (uri2dst(&t->uac[branch].dns_h, &t->uac[branch].request.dst,
405 request, next_hop?next_hop:uri, proto) == 0)
407 /* dst filled from the uri & request (send_socket) */
408 if (uri2dst(&t->uac[branch].request.dst, request,
409 next_hop ? next_hop: uri, proto)==0)
412 ret=ser_error=E_BAD_ADDRESS;
417 /* check if send_sock is ok */
418 if (t->uac[branch].request.dst.send_sock==0) {
419 LOG(L_ERR, "ERROR: add_uac: can't fwd to af %d, proto %d "
420 " (no corresponding listening socket)\n",
421 t->uac[branch].request.dst.to.s.sa_family,
422 t->uac[branch].request.dst.proto );
423 ret=ser_error=E_NO_SOCKET;
427 /* now message printing starts ... */
428 shbuf=print_uac_request( t, request, branch, uri,
429 &len, &t->uac[branch].request.dst);
431 ret=ser_error=E_OUT_OF_MEM;
435 /* things went well, move ahead and install new buffer! */
436 t->uac[branch].request.buffer=shbuf;
437 t->uac[branch].request.buffer_len=len;
438 t->uac[branch].uri.s=t->uac[branch].request.buffer+
439 request->first_line.u.request.method.len+1;
440 t->uac[branch].uri.len=uri->len;
441 membar_write(); /* to allow lockless ops (e.g. which_cancel()) we want
442 to be sure everything above is fully written before
443 updating branches no. */
444 t->nr_of_outgoings=(branch+1);
448 proxy_mark(proxy, 1);
460 #ifdef USE_DNS_FAILOVER
461 /* Similar to add_uac(), but this function uses the outgoing message buffer of
462 the failed branch to construt the new message in case of DNS failover.
464 static int add_uac_from_buf( struct cell *t, struct sip_msg *request, str *uri, int proto,
465 char *buf, short buf_len)
469 unsigned short branch;
473 branch=t->nr_of_outgoings;
474 if (branch==MAX_BRANCHES) {
475 LOG(L_ERR, "ERROR: add_uac_from_buf: maximum number of branches exceeded\n");
476 ret=ser_error=E_TOO_MANY_BRANCHES;
480 /* check existing buffer -- rewriting should never occur */
481 if (t->uac[branch].request.buffer) {
482 LOG(L_CRIT, "ERROR: add_uac_from_buf: buffer rewrite attempt\n");
487 if (uri2dst(&t->uac[branch].dns_h, &t->uac[branch].request.dst,
488 request, uri, proto) == 0)
490 ret=ser_error=E_BAD_ADDRESS;
494 /* check if send_sock is ok */
495 if (t->uac[branch].request.dst.send_sock==0) {
496 LOG(L_ERR, "ERROR: add_uac_from_buf: can't fwd to af %d, proto %d "
497 " (no corresponding listening socket)\n",
498 t->uac[branch].request.dst.to.s.sa_family,
499 t->uac[branch].request.dst.proto );
500 ret=ser_error=E_NO_SOCKET;
504 /* now message printing starts ... */
505 shbuf=print_uac_request_from_buf( t, request, branch, uri,
506 &len, &t->uac[branch].request.dst,
509 ret=ser_error=E_OUT_OF_MEM;
513 /* things went well, move ahead and install new buffer! */
514 t->uac[branch].request.buffer=shbuf;
515 t->uac[branch].request.buffer_len=len;
516 t->uac[branch].uri.s=t->uac[branch].request.buffer+
517 request->first_line.u.request.method.len+1;
518 t->uac[branch].uri.len=uri->len;
519 membar_write(); /* to allow lockless ops (e.g. which_cancel()) we want
520 to be sure everything above is fully written before
521 updating branches no. */
522 t->nr_of_outgoings=(branch+1);
531 /* introduce a new uac to transaction, based on old_uac and a possible
532 * new ip address (if the dns name resolves to more ips). If no more
533 * ips are found => returns -1.
534 * returns its branch id (>=0)
535 or error (<0) and sets ser_error if needed; it doesn't send a message
537 might interfere with the processes of adding multiple branches
538 if lock_replies is 1 replies will be locked for t until the new branch
539 is added (to prevent add branches races). Use 0 if the reply lock is
540 already held, e.g. in failure route/handlers (WARNING: using 1 in a
541 failure route will cause a deadlock).
543 int add_uac_dns_fallback( struct cell *t, struct sip_msg* msg,
544 struct ua_client* old_uac,
550 if (cfg_get(core, core_cfg, use_dns_failover) &&
551 !((t->flags & T_DONT_FORK) || uac_dont_fork(old_uac)) &&
552 dns_srv_handle_next(&old_uac->dns_h, 0)){
554 /* use reply lock to guarantee nobody is adding a branch
555 * in the same time */
557 /* check again that we can fork */
558 if ((t->flags & T_DONT_FORK) || uac_dont_fork(old_uac)){
560 DBG("add_uac_dns_fallback: no forking on => no new"
565 if (t->nr_of_outgoings >= MAX_BRANCHES){
566 LOG(L_ERR, "ERROR: add_uac_dns_fallback: maximum number of "
567 "branches exceeded\n");
570 ret=ser_error=E_TOO_MANY_BRANCHES;
573 /* copy the dns handle into the new uac */
574 dns_srv_handle_cpy(&t->uac[t->nr_of_outgoings].dns_h,
577 if (cfg_get(tm, tm_cfg, reparse_on_dns_failover))
578 /* Reuse the old buffer and only replace the via header.
579 * The drowback is that the send_socket is not corrected
580 * in the rest of the message, only in the VIA HF (Miklos) */
581 ret=add_uac_from_buf(t, msg, &old_uac->uri,
582 old_uac->request.dst.proto,
583 old_uac->request.buffer,
584 old_uac->request.buffer_len);
586 /* add_uac will use dns_h => next_hop will be ignored.
587 * Unfortunately we can't reuse the old buffer, the branch id
588 * must be changed and the send_socket might be different =>
589 * re-create the whole uac */
590 ret=add_uac(t, msg, &old_uac->uri, 0, 0,
591 old_uac->request.dst.proto);
594 /* failed, delete the copied dns_h */
595 dns_srv_handle_put(&t->uac[t->nr_of_outgoings].dns_h);
606 int e2e_cancel_branch( struct sip_msg *cancel_msg, struct cell *t_cancel,
607 struct cell *t_invite, int branch )
614 if (t_cancel->uac[branch].request.buffer) {
615 LOG(L_CRIT, "ERROR: e2e_cancel_branch: buffer rewrite attempt\n");
619 if (t_invite->uac[branch].request.buffer==0){
620 /* inactive / deleted branch */
623 t_invite->uac[branch].request.flags|=F_RB_CANCELED;
625 /* note -- there is a gap in proxy stats -- we don't update
626 proxy stats with CANCEL (proxy->ok, proxy->tx, etc.)
630 if (cfg_get(tm, tm_cfg, reparse_invite)) {
631 /* buffer is built localy from the INVITE which was sent out */
632 if (cancel_msg->add_rm || cancel_msg->body_lumps) {
633 LOG(L_WARN, "WARNING: e2e_cancel_branch: CANCEL is built locally, "
634 "thus lumps are not applied to the message!\n");
636 shbuf=build_local_reparse( t_invite, branch, &len, CANCEL, CANCEL_LEN, &t_invite->to);
639 /* buffer is constructed from the received CANCEL with applying lumps */
640 shbuf=print_uac_request( t_cancel, cancel_msg, branch,
641 &t_invite->uac[branch].uri, &len,
642 &t_invite->uac[branch].request.dst);
646 LOG(L_ERR, "ERROR: e2e_cancel_branch: printing e2e cancel failed\n");
647 ret=ser_error=E_OUT_OF_MEM;
652 t_cancel->uac[branch].request.dst=t_invite->uac[branch].request.dst;
653 t_cancel->uac[branch].request.buffer=shbuf;
654 t_cancel->uac[branch].request.buffer_len=len;
655 t_cancel->uac[branch].uri.s=t_cancel->uac[branch].request.buffer+
656 cancel_msg->first_line.u.request.method.len+1;
657 t_cancel->uac[branch].uri.len=t_invite->uac[branch].uri.len;
668 void e2e_cancel( struct sip_msg *cancel_msg,
669 struct cell *t_cancel, struct cell *t_invite )
671 branch_bm_t cancel_bm;
672 #ifndef E2E_CANCEL_HOP_BY_HOP
678 struct tmcb_params tmcb;
683 if (unlikely(has_tran_tmcbs(t_invite, TMCB_E2ECANCEL_IN))){
684 INIT_TMCB_PARAMS(tmcb, cancel_msg, 0, cancel_msg->REQ_METHOD);
685 run_trans_callbacks_internal(&t_invite->tmcb_hl, TMCB_E2ECANCEL_IN,
688 /* first check if there are any branches */
689 if (t_invite->nr_of_outgoings==0){
690 t_invite->flags|=T_CANCELED;
691 /* no branches yet => force a reply to the invite */
692 t_reply( t_invite, t_invite->uas.request, 487, CANCELED );
693 DBG("DEBUG: e2e_cancel: e2e cancel -- no more pending branches\n");
694 t_reply( t_cancel, cancel_msg, 200, CANCEL_DONE );
698 /* determine which branches to cancel ... */
699 which_cancel( t_invite, &cancel_bm );
700 #ifdef E2E_CANCEL_HOP_BY_HOP
701 /* we don't need to set t_cancel label to be the same as t_invite if
702 * we do hop by hop cancel. The cancel transaction will have a different
703 * label, but this is not a problem since this transaction is only used to
704 * send a reply back. The cancels sent upstream will be part of the invite
705 * transaction (local_cancel retr. bufs) and they will be generated with
706 * the same via as the invite.
707 * Note however that setting t_cancel label the same as t_invite will work
708 * too (the upstream cancel replies will properly match the t_invite
709 * transaction and will not match the t_cancel because t_cancel will always
710 * have 0 branches and we check for the branch number in
711 * t_reply_matching() ).
713 for (i=0; i<t_invite->nr_of_outgoings; i++)
714 if (cancel_bm & (1<<i)) {
715 /* it's safe to get the reply lock since e2e_cancel is
716 * called with the cancel as the "current" transaction so
717 * at most t_cancel REPLY_LOCK is held in this process =>
718 * no deadlock possibility */
719 ret=cancel_branch(t_invite, i, cfg_get(tm,tm_cfg, cancel_b_flags));
720 if (ret<0) cancel_bm &= ~(1<<i);
721 if (ret<lowest_error) lowest_error=ret;
723 #else /* ! E2E_CANCEL_HOP_BY_HOP */
724 /* fix label -- it must be same for reply matching (the label is part of
725 * the generated via branch for the cancels sent upstream and if it
726 * would be different form the one in the INVITE the transactions would not
728 t_cancel->label=t_invite->label;
729 t_cancel->nr_of_outgoings=t_invite->nr_of_outgoings;
730 /* ... and install CANCEL UACs */
731 for (i=0; i<t_invite->nr_of_outgoings; i++)
732 if ((cancel_bm & (1<<i)) && (t_invite->uac[i].last_received>=100)) {
733 ret=e2e_cancel_branch(cancel_msg, t_cancel, t_invite, i);
734 if (ret<0) cancel_bm &= ~(1<<i);
735 if (ret<lowest_error) lowest_error=ret;
739 for (i = 0; i < t_cancel->nr_of_outgoings; i++) {
740 if (cancel_bm & (1 << i)) {
741 if (t_invite->uac[i].last_received>=100){
742 /* Provisional reply received on this branch, send CANCEL */
743 /* we do need to stop the retr. timers if the request is not
744 * an invite and since the stop_rb_retr() cost is lower then
745 * the invite check we do it always --andrei */
746 stop_rb_retr(&t_invite->uac[i].request);
747 if (SEND_BUFFER(&t_cancel->uac[i].request) == -1) {
748 LOG(L_ERR, "ERROR: e2e_cancel: send failed\n");
752 if (unlikely(has_tran_tmcbs(t_cancel, TMCB_REQUEST_SENT)))
753 run_onsend_callbacks(TMCB_REQUEST_SENT,
754 &t_cancel->uac[i].request,
755 cancel_msg, 0, TMCB_LOCAL_F);
758 if (start_retr( &t_cancel->uac[i].request )!=0)
759 LOG(L_CRIT, "BUG: e2e_cancel: failed to start retr."
760 " for %p\n", &t_cancel->uac[i].request);
762 /* No provisional response received, stop
763 * retransmission timers */
764 if (!(cfg_get(tm, tm_cfg, cancel_b_flags) &
765 F_CANCEL_B_FORCE_RETR))
766 stop_rb_retr(&t_invite->uac[i].request);
767 /* no need to stop fr, it will be stoped by relay_reply
768 * put_on_wait -- andrei */
769 /* Generate faked reply */
770 if (cfg_get(tm, tm_cfg, cancel_b_flags) &
771 F_CANCEL_B_FAKE_REPLY){
772 LOCK_REPLIES(t_invite);
773 if (relay_reply(t_invite, FAKED_REPLY, i, 487, &tmp_bm) ==
781 #endif /*E2E_CANCEL_HOP_BY_HOP */
783 /* if error occurred, let it know upstream (final reply
784 will also move the transaction on wait state
786 if (lowest_error<0) {
787 LOG(L_ERR, "ERROR: cancel error\n");
788 /* if called from failure_route, make sure that the unsafe version
789 * is called (we are already holding the reply mutex for the cancel
792 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
793 t_reply_unsafe( t_cancel, cancel_msg, 500, "cancel error");
795 t_reply( t_cancel, cancel_msg, 500, "cancel error");
796 } else if (cancel_bm) {
797 /* if there are pending branches, let upstream know we
800 DBG("DEBUG: e2e_cancel: e2e cancel proceeding\n");
801 /* if called from failure_route, make sure that the unsafe version
802 * is called (we are already hold the reply mutex for the cancel
805 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
806 t_reply_unsafe( t_cancel, cancel_msg, 200, CANCELING );
808 t_reply( t_cancel, cancel_msg, 200, CANCELING );
810 /* if the transaction exists, but there are no more pending
811 branches, tell upstream we're done
813 DBG("DEBUG: e2e_cancel: e2e cancel -- no more pending branches\n");
814 /* if called from failure_route, make sure that the unsafe version
815 * is called (we are already hold the reply mutex for the cancel
818 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
819 t_reply_unsafe( t_cancel, cancel_msg, 200, CANCEL_DONE );
821 t_reply( t_cancel, cancel_msg, 200, CANCEL_DONE );
827 /* sends one uac/branch buffer and fallbacks to other ips if
828 * the destination resolves to several addresses
829 * Takes care of starting timers a.s.o. (on send success)
830 * returns: -2 on error, -1 on drop, current branch id on success,
831 * new branch id on send error/blacklist, when failover is possible
832 * (ret>=0 && ret!=branch)
833 * if lock_replies is 1, the replies for t will be locked when adding
834 * new branches (to prevent races). Use 0 from failure routes or other
835 * places where the reply lock is already held, to avoid deadlocks. */
836 int t_send_branch( struct cell *t, int branch, struct sip_msg* p_msg ,
837 struct proxy_l * proxy, int lock_replies)
839 struct ip_addr ip; /* debugging */
841 struct ua_client* uac;
845 if (run_onsend(p_msg, &uac->request.dst, uac->request.buffer,
846 uac->request.buffer_len)==0){
847 /* disable the current branch: set a "fake" timeout
848 * reply code but don't set uac->reply, to avoid overriding
849 * a higly unlikely, perfectly timed fake reply (to a message
851 * (code=final reply && reply==0 => t_pick_branch won't ever pick it)*/
852 uac->last_received=408;
853 su2ip_addr(&ip, &uac->request.dst.to);
854 DBG("t_send_branch: onsend_route dropped msg. to %s:%d (%d)\n",
855 ip_addr2a(&ip), su_getport(&uac->request.dst.to),
856 uac->request.dst.proto);
857 #ifdef USE_DNS_FAILOVER
858 /* if the destination resolves to more ips, add another
860 if (cfg_get(core, core_cfg, use_dns_failover)){
861 ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
863 su2ip_addr(&ip, &uac->request.dst.to);
864 DBG("t_send_branch: send on branch %d failed "
865 "(onsend_route), trying another ip %s:%d (%d)\n",
866 branch, ip_addr2a(&ip),
867 su_getport(&uac->request.dst.to),
868 uac->request.dst.proto);
869 /* success, return new branch */
873 #endif /* USE_DNS_FAILOVER*/
874 return -1; /* drop, try next branch */
876 #ifdef USE_DST_BLACKLIST
877 if (cfg_get(core, core_cfg, use_dst_blacklist)
879 && (p_msg->REQ_METHOD & cfg_get(tm, tm_cfg, tm_blst_methods_lookup))
881 if (dst_is_blacklisted(&uac->request.dst, p_msg)){
882 su2ip_addr(&ip, &uac->request.dst.to);
883 DBG("t_send_branch: blacklisted destination: %s:%d (%d)\n",
884 ip_addr2a(&ip), su_getport(&uac->request.dst.to),
885 uac->request.dst.proto);
886 /* disable the current branch: set a "fake" timeout
887 * reply code but don't set uac->reply, to avoid overriding
888 * a higly unlikely, perfectly timed fake reply (to a message
889 * we never sent). (code=final reply && reply==0 =>
890 * t_pick_branch won't ever pick it)*/
891 uac->last_received=408;
892 #ifdef USE_DNS_FAILOVER
893 /* if the destination resolves to more ips, add another
895 if (cfg_get(core, core_cfg, use_dns_failover)){
896 ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
898 su2ip_addr(&ip, &uac->request.dst.to);
899 DBG("t_send_branch: send on branch %d failed (blacklist),"
900 " trying another ip %s:%d (%d)\n", branch,
901 ip_addr2a(&ip), su_getport(&uac->request.dst.to),
902 uac->request.dst.proto);
903 /* success, return new branch */
907 #endif /* USE_DNS_FAILOVER*/
908 return -1; /* don't send */
911 #endif /* USE_DST_BLACKLIST */
912 if (SEND_BUFFER( &uac->request)==-1) {
913 /* disable the current branch: set a "fake" timeout
914 * reply code but don't set uac->reply, to avoid overriding
915 * a highly unlikely, perfectly timed fake reply (to a message
917 * (code=final reply && reply==0 => t_pick_branch won't ever pick it)*/
918 uac->last_received=408;
919 su2ip_addr(&ip, &uac->request.dst.to);
920 DBG("t_send_branch: send to %s:%d (%d) failed\n",
921 ip_addr2a(&ip), su_getport(&uac->request.dst.to),
922 uac->request.dst.proto);
923 #ifdef USE_DST_BLACKLIST
924 if (cfg_get(core, core_cfg, use_dst_blacklist))
925 dst_blacklist_add(BLST_ERR_SEND, &uac->request.dst, p_msg);
927 #ifdef USE_DNS_FAILOVER
928 /* if the destination resolves to more ips, add another
930 if (cfg_get(core, core_cfg, use_dns_failover)){
931 ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
933 /* success, return new branch */
934 DBG("t_send_branch: send on branch %d failed, adding another"
935 " branch with another ip\n", branch);
940 LOG(L_ERR, "ERROR: t_send_branch: sending request on branch %d "
942 if (proxy) { proxy->errors++; proxy->ok=0; }
946 if (unlikely(has_tran_tmcbs(t, TMCB_REQUEST_SENT)))
947 run_onsend_callbacks(TMCB_REQUEST_SENT, &uac->request, p_msg, 0,0);
949 /* start retr. only if the send succeeded */
950 if (start_retr( &uac->request )!=0){
951 LOG(L_CRIT, "BUG: t_send_branch: retr. already started for %p\n",
962 * 1 - forward successful
963 * -1 - error during forward
965 int t_forward_nonack( struct cell *t, struct sip_msg* p_msg ,
966 struct proxy_l * proxy, int proto)
968 int branch_ret, lowest_ret;
970 branch_bm_t added_branches;
973 struct cell *t_invite;
978 struct socket_info* si, *backup_si;
981 /* make -Wall happy */
984 if (t->flags & T_CANCELED){
985 DBG("t_forward_non_ack: no forwarding on a canceled transaction\n");
986 ser_error=E_CANCELED;
989 if (p_msg->REQ_METHOD==METHOD_CANCEL) {
990 t_invite=t_lookupOriginalT( p_msg );
991 if (t_invite!=T_NULL_CELL) {
992 e2e_cancel( p_msg, t, t_invite );
994 /* it should be set to REQ_RPLD by e2e_cancel, which should
995 * send a final reply */
1001 backup_si = p_msg->force_send_socket;
1002 /* if no more specific error code is known, use this */
1003 lowest_ret=E_UNSPEC;
1004 /* branches added */
1006 /* branch to begin with */
1007 first_branch=t->nr_of_outgoings;
1010 /* tell add_uac that it should run branch route actions */
1011 branch_route = t->on_branch;
1012 /* reset the flag before running the actions (so that it
1013 * could be set again in branch_route if needed
1020 /* on first-time forwarding, use current uri, later only what
1021 is in additional branches (which may be continuously refilled
1023 if (first_branch==0) {
1025 branch_ret=add_uac( t, p_msg, GET_RURI(p_msg), GET_NEXT_HOP(p_msg),
1028 added_branches |= 1<<branch_ret;
1030 lowest_ret=MIN_int(lowest_ret, branch_ret);
1033 init_branch_iterator();
1034 while((current_uri.s=next_branch( ¤t_uri.len, &q, &dst_uri.s, &dst_uri.len, &si))) {
1036 p_msg->force_send_socket = si;
1037 branch_ret=add_uac( t, p_msg, ¤t_uri,
1038 (dst_uri.len) ? (&dst_uri) : ¤t_uri,
1040 /* pick some of the errors in case things go wrong;
1041 note that picking lowest error is just as good as
1042 any other algorithm which picks any other negative
1045 added_branches |= 1<<branch_ret;
1047 lowest_ret=MIN_int(lowest_ret, branch_ret);
1049 /* consume processed branches */
1052 p_msg->force_send_socket = backup_si;
1054 /* don't forget to clear all branches processed so far */
1056 /* things went wrong ... no new branch has been fwd-ed at all */
1057 if (added_branches==0) {
1059 LOG(L_ERR, "ERROR: t_forward_nonack: no branches for"
1061 /* either failed to add branches, or there were no more branches
1063 ser_error=MIN_int(lowest_ret, E_CFG);
1066 LOG(L_ERR, "ERROR: t_forward_nonack: failure to add branches\n");
1067 ser_error=lowest_ret;
1070 ser_error=0; /* clear branch adding errors */
1071 /* send them out now */
1073 lock_replies= ! ((rmode==MODE_ONFAILURE) && (t==get_t()));
1074 for (i=first_branch; i<t->nr_of_outgoings; i++) {
1075 if (added_branches & (1<<i)) {
1077 branch_ret=t_send_branch(t, i, p_msg , proxy, lock_replies);
1078 if (branch_ret>=0){ /* some kind of success */
1079 if (branch_ret==i) /* success */
1081 else /* new branch added */
1082 added_branches |= 1<<branch_ret;
1086 if (success_branch<=0) {
1087 /* return always E_SEND for now
1088 * (the real reason could be: denied by onsend routes, blacklisted,
1089 * send failed or any of the errors listed before + dns failed
1090 * when attempting dns failover) */
1092 /* else return the last error (?) */
1093 /* the caller should take care and delete the transaction */
1096 ser_error=0; /* clear branch send errors, we have overall success */
1103 /* cancel handling/forwarding function
1104 * CANCELs with no matching transaction are handled in function of
1105 * the unmatched_cancel config var: they are either forwarded statefully,
1106 * statelessly or dropped.
1108 * 1 - forward successful
1109 * 0 - error, but do not reply
1110 * <0 - error during forward
1111 * it also sets *tran if a transaction was created
1113 int t_forward_cancel(struct sip_msg* p_msg , struct proxy_l * proxy, int proto,
1116 struct cell* t_invite;
1120 struct dest_info dst;
1122 unsigned short port;
1126 /* handle cancels for which no transaction was created yet */
1127 if (cfg_get(tm, tm_cfg, unmatched_cancel)==UM_CANCEL_STATEFULL){
1128 /* create cancel transaction */
1129 new_tran=t_newtran(p_msg);
1130 if (new_tran<=0 && new_tran!=E_SCRIPT){
1132 /* retransmission => do nothing */
1135 /* some error => return it or DROP */
1136 ret=(ser_error==E_BAD_VIA && reply_to_via) ? 0: new_tran;
1140 ret=t_forward_nonack(t, p_msg, proxy, proto);
1144 t_invite=t_lookupOriginalT( p_msg );
1145 if (t_invite!=T_NULL_CELL) {
1146 /* create cancel transaction */
1147 new_tran=t_newtran(p_msg);
1148 if (new_tran<=0 && new_tran!=E_SCRIPT){
1150 /* retransmission => do nothing */
1153 /* some error => return it or DROP */
1154 ret=(ser_error==E_BAD_VIA && reply_to_via) ? 0: new_tran;
1159 e2e_cancel( p_msg, t, t_invite );
1163 }else /* no coresponding INVITE transaction */
1164 if (cfg_get(tm, tm_cfg, unmatched_cancel)==UM_CANCEL_DROP){
1165 DBG("t_forward_nonack: non matching cancel dropped\n");
1166 ret=1; /* do nothing -> drop */
1169 /* UM_CANCEL_STATELESS -> stateless forward */
1170 DBG( "SER: forwarding CANCEL statelessly \n");
1172 init_dest_info(&dst);
1174 if (get_uri_send_info(GET_NEXT_HOP(p_msg), &host,
1175 &port, &dst.proto, &comp)!=0){
1182 /* dst->send_sock not set, but forward_request
1183 * will take care of it */
1184 ret=forward_request(p_msg, &host, port, &dst);
1187 init_dest_info(&dst);
1188 dst.proto=get_proto(proto, proxy->proto);
1189 proxy2su(&dst.to, proxy);
1190 /* dst->send_sock not set, but forward_request
1191 * will take care of it */
1192 ret=forward_request( p_msg , 0, 0, &dst) ;
1202 /* Relays a CANCEL request if a corresponding INVITE transaction
1203 * can be found. The function is supposed to be used at the very
1204 * beginning of the script with reparse_invite=1 module parameter.
1207 * 0: the CANCEL was successfully relayed
1208 * (or error occured but reply cannot be sent) => DROP
1209 * 1: no corresponding INVITE transaction exisis
1210 * <0: corresponding INVITE transaction exisis but error occured
1212 int t_relay_cancel(struct sip_msg* p_msg)
1214 struct cell* t_invite;
1219 t_invite=t_lookupOriginalT( p_msg );
1220 if (t_invite!=T_NULL_CELL) {
1221 /* create cancel transaction */
1222 new_tran=t_newtran(p_msg);
1223 if (new_tran<=0 && new_tran!=E_SCRIPT){
1225 /* retransmission => DROP,
1226 t_newtran() takes care about it */
1229 /* some error => return it or DROP */
1230 ret=(ser_error==E_BAD_VIA && reply_to_via) ? 0: new_tran;
1235 e2e_cancel( p_msg, t, t_invite );
1237 /* return 0 to stop the script processing */
1242 /* no corresponding INVITE trasaction found */
1249 /* WARNING: doesn't work from failure route (deadlock, uses t_relay_to which
1250 * is failure route unsafe) */
1251 int t_replicate(struct sip_msg *p_msg, struct proxy_l *proxy, int proto )
1253 /* this is a quite horrible hack -- we just take the message
1254 as is, including Route-s, Record-route-s, and Vias ,
1255 forward it downstream and prevent replies received
1256 from relaying by setting the replication/local_trans bit;
1258 nevertheless, it should be good enough for the primary
1259 customer of this function, REGISTER replication
1261 if we want later to make it thoroughly, we need to
1262 introduce delete lumps for all the header fields above
1264 return t_relay_to(p_msg, proxy, proto, 1 /* replicate */);
1267 /* fixup function for reparse_on_dns_failover modparam */
1268 int reparse_on_dns_failover_fixup(void *handle, str *name, void **val)
1270 #ifdef USE_DNS_FAILOVER
1271 if ((int)(long)(*val) && mhomed) {
1272 LOG(L_WARN, "WARNING: reparse_on_dns_failover_fixup:"
1273 "reparse_on_dns_failover is enabled on a "
1274 "multihomed host -- check the readme of tm module!\n");