00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 #include "defs.h"
00084
00085
00086 #include "../../dprint.h"
00087 #include "../../config.h"
00088 #include "../../parser/parser_f.h"
00089 #include "../../ut.h"
00090 #include "../../timer.h"
00091 #include "../../hash_func.h"
00092 #include "../../globals.h"
00093 #include "../../cfg_core.h"
00094 #include "../../mem/mem.h"
00095 #include "../../dset.h"
00096 #include "../../action.h"
00097 #include "../../data_lump.h"
00098 #include "../../onsend.h"
00099 #include "../../compiler_opt.h"
00100 #include "t_funcs.h"
00101 #include "t_hooks.h"
00102 #include "t_msgbuilder.h"
00103 #include "ut.h"
00104 #include "t_cancel.h"
00105 #include "t_lookup.h"
00106 #include "t_fwd.h"
00107 #include "fix_lumps.h"
00108 #include "config.h"
00109 #ifdef USE_DNS_FAILOVER
00110 #include "../../dns_cache.h"
00111 #include "../../cfg_core.h"
00112 #include "../../msg_translator.h"
00113 #include "lw_parser.h"
00114 #endif
00115 #ifdef USE_DST_BLACKLIST
00116 #include "../../dst_blacklist.h"
00117 #endif
00118 #include "../../select_buf.h"
00119 #ifdef POSTPONE_MSG_CLONING
00120 #include "../../atomic_ops.h"
00121 #endif
00122
00123 static int goto_on_branch = 0, branch_route = 0;
00124
00125 void t_on_branch( unsigned int go_to )
00126 {
00127 struct cell *t = get_t();
00128
00129
00130
00131
00132 if (!t || t==T_UNDEFINED ) {
00133 goto_on_branch=go_to;
00134 } else {
00135 get_t()->on_branch = go_to;
00136 }
00137 }
00138
00139 unsigned int get_on_branch(void)
00140 {
00141 return goto_on_branch;
00142 }
00143
00144
00145 static char *print_uac_request( struct cell *t, struct sip_msg *i_req,
00146 int branch, str *uri, unsigned int *len, struct dest_info* dst)
00147 {
00148 char *buf, *shbuf;
00149 str* msg_uri;
00150 struct lump* add_rm_backup, *body_lumps_backup;
00151 struct sip_uri parsed_uri_bak;
00152 int parsed_uri_ok_bak, uri_backed_up;
00153 str msg_uri_bak;
00154 struct run_act_ctx ra_ctx;
00155
00156 shbuf=0;
00157 msg_uri_bak.s=0;
00158 msg_uri_bak.len=0;
00159 parsed_uri_ok_bak=0;
00160 uri_backed_up=0;
00161
00162
00163 if (!t_calc_branch(t, branch, i_req->add_to_branch_s,
00164 &i_req->add_to_branch_len ))
00165 {
00166 LOG(L_ERR, "ERROR: print_uac_request: branch computation failed\n");
00167 goto error00;
00168 }
00169
00170
00171 msg_uri=GET_RURI(i_req);
00172 if ((msg_uri->s!=uri->s) || (msg_uri->len!=uri->len)){
00173 msg_uri_bak=i_req->new_uri;
00174 parsed_uri_ok_bak=i_req->parsed_uri_ok;
00175 parsed_uri_bak=i_req->parsed_uri;
00176 i_req->new_uri=*uri;
00177 i_req->parsed_uri_ok=0;
00178 uri_backed_up=1;
00179 }
00180
00181 #ifdef POSTPONE_MSG_CLONING
00182
00183
00184 membar_depends();
00185 #endif
00186 add_rm_backup = i_req->add_rm;
00187 body_lumps_backup = i_req->body_lumps;
00188 i_req->add_rm = dup_lump_list(i_req->add_rm);
00189 i_req->body_lumps = dup_lump_list(i_req->body_lumps);
00190
00191 if (unlikely(branch_route)) {
00192 reset_static_buffer();
00193
00194 init_run_actions_ctx(&ra_ctx);
00195 if (run_actions(&ra_ctx, branch_rt.rlist[branch_route], i_req) < 0) {
00196 LOG(L_ERR, "ERROR: print_uac_request: Error in run_actions\n");
00197 }
00198 }
00199
00200
00201 if (unlikely(has_tran_tmcbs(t, TMCB_REQUEST_FWDED)))
00202 run_trans_callbacks( TMCB_REQUEST_FWDED , t, i_req, 0,
00203 -i_req->REQ_METHOD);
00204
00205
00206 buf=build_req_buf_from_sip_req( i_req, len, dst);
00207 #ifdef DBG_MSG_QA
00208 if (buf[*len-1]==0) {
00209 LOG(L_ERR, "ERROR: print_uac_request: sanity check failed\n");
00210 abort();
00211 }
00212 #endif
00213 if (!buf) {
00214 LOG(L_ERR, "ERROR: print_uac_request: no pkg_mem\n");
00215 ser_error=E_OUT_OF_MEM;
00216 goto error01;
00217 }
00218
00219 shbuf=(char *)shm_malloc(*len);
00220 if (!shbuf) {
00221 ser_error=E_OUT_OF_MEM;
00222 LOG(L_ERR, "ERROR: print_uac_request: no shmem\n");
00223 goto error02;
00224 }
00225 memcpy( shbuf, buf, *len );
00226
00227 error02:
00228 pkg_free( buf );
00229 error01:
00230
00231
00232
00233
00234
00235 free_duped_lump_list(i_req->add_rm);
00236 free_duped_lump_list(i_req->body_lumps);
00237
00238 i_req->add_rm = add_rm_backup;
00239 i_req->body_lumps = body_lumps_backup;
00240
00241 if (uri_backed_up){
00242 i_req->new_uri=msg_uri_bak;
00243 i_req->parsed_uri=parsed_uri_bak;
00244 i_req->parsed_uri_ok=parsed_uri_ok_bak;
00245 }
00246
00247 error00:
00248 return shbuf;
00249 }
00250
00251 #ifdef USE_DNS_FAILOVER
00252
00253
00254
00255
00256
00257
00258 static char *print_uac_request_from_buf( struct cell *t, struct sip_msg *i_req,
00259 int branch, str *uri, unsigned int *len, struct dest_info* dst,
00260 char *buf, short buf_len)
00261 {
00262 char *shbuf;
00263 str branch_str;
00264 char *via, *old_via_begin, *old_via_end;
00265 unsigned int via_len;
00266
00267 shbuf=0;
00268
00269
00270 if (!t_calc_branch(t, branch, i_req->add_to_branch_s,
00271 &i_req->add_to_branch_len ))
00272 {
00273 LOG(L_ERR, "ERROR: print_uac_request_from_buf: branch computation failed\n");
00274 goto error00;
00275 }
00276 branch_str.s = i_req->add_to_branch_s;
00277 branch_str.len = i_req->add_to_branch_len;
00278
00279
00280 old_via_begin = lw_find_via(buf, buf+buf_len);
00281 if (!old_via_begin) {
00282 LOG(L_ERR, "ERROR: print_uac_request_from_buf: beginning of via header not found\n");
00283 goto error00;
00284 }
00285
00286 old_via_end = lw_next_line(old_via_begin, buf+buf_len);
00287 if (!old_via_end) {
00288 LOG(L_ERR, "ERROR: print_uac_request_from_buf: end of via header not found\n");
00289 goto error00;
00290 }
00291
00292
00293 via = create_via_hf(&via_len, i_req, dst, &branch_str);
00294 if (!via) {
00295 LOG(L_ERR, "ERROR: print_uac_request_from_buf: via building failed\n");
00296 goto error00;
00297 }
00298
00299
00300 *len = buf_len + via_len - (old_via_end - old_via_begin);
00301 shbuf=(char *)shm_malloc(*len);
00302 if (!shbuf) {
00303 ser_error=E_OUT_OF_MEM;
00304 LOG(L_ERR, "ERROR: print_uac_request_from_buf: no shmem\n");
00305 goto error01;
00306 }
00307
00308
00309 memcpy(shbuf, buf, old_via_begin-buf);
00310 memcpy(shbuf+(old_via_begin-buf), via, via_len);
00311 memcpy(shbuf+(old_via_begin-buf)+via_len, old_via_end, (buf+buf_len)-old_via_end);
00312
00313 #ifdef DBG_MSG_QA
00314 if (shbuf[*len-1]==0) {
00315 LOG(L_ERR, "ERROR: print_uac_request_from_buf: sanity check failed\n");
00316 abort();
00317 }
00318 #endif
00319
00320 error01:
00321 pkg_free(via);
00322 error00:
00323 return shbuf;
00324 }
00325 #endif
00326
00327
00328
00329
00330
00331
00332
00333
00334 int add_blind_uac( )
00335 {
00336 unsigned short branch;
00337 struct cell *t;
00338
00339 t=get_t();
00340 if (t==T_UNDEFINED || !t ) {
00341 LOG(L_ERR, "ERROR: add_blind_uac: no transaction context\n");
00342 return -1;
00343 }
00344
00345 branch=t->nr_of_outgoings;
00346 if (branch==MAX_BRANCHES) {
00347 LOG(L_ERR, "ERROR: add_blind_uac: "
00348 "maximum number of branches exceeded\n");
00349 return -1;
00350 }
00351
00352 t->flags |= T_NOISY_CTIMER_FLAG;
00353 membar_write();
00354
00355 t->nr_of_outgoings=(branch+1);
00356
00357
00358
00359 if (start_retr(&t->uac[branch].request)!=0)
00360 LOG(L_CRIT, "BUG: add_blind_uac: start retr failed for %p\n",
00361 &t->uac[branch].request);
00362
00363
00364
00365 set_kr(REQ_FWDED);
00366
00367 return 1;
00368 }
00369
00370
00371
00372
00373
00374
00375 int add_uac( struct cell *t, struct sip_msg *request, str *uri, str* next_hop,
00376 struct proxy_l *proxy, int proto )
00377 {
00378
00379 int ret;
00380 unsigned short branch;
00381 char *shbuf;
00382 unsigned int len;
00383
00384 branch=t->nr_of_outgoings;
00385 if (branch==MAX_BRANCHES) {
00386 LOG(L_ERR, "ERROR: add_uac: maximum number of branches exceeded\n");
00387 ret=ser_error=E_TOO_MANY_BRANCHES;
00388 goto error;
00389 }
00390
00391
00392 if (t->uac[branch].request.buffer) {
00393 LOG(L_CRIT, "ERROR: add_uac: buffer rewrite attempt\n");
00394 ret=ser_error=E_BUG;
00395 goto error;
00396 }
00397
00398
00399 if (proxy){
00400
00401 init_dest_info(&t->uac[branch].request.dst);
00402 t->uac[branch].request.dst.proto=get_proto(proto, proxy->proto);
00403 proxy2su(&t->uac[branch].request.dst.to, proxy);
00404
00405 t->uac[branch].request.dst.send_sock =
00406 get_send_socket( request, &t->uac[branch].request.dst.to,
00407 t->uac[branch].request.dst.proto);
00408 }else {
00409 #ifdef USE_DNS_FAILOVER
00410 if (uri2dst(&t->uac[branch].dns_h, &t->uac[branch].request.dst,
00411 request, next_hop?next_hop:uri, proto) == 0)
00412 #else
00413
00414 if (uri2dst(&t->uac[branch].request.dst, request,
00415 next_hop ? next_hop: uri, proto)==0)
00416 #endif
00417 {
00418 ret=ser_error=E_BAD_ADDRESS;
00419 goto error;
00420 }
00421 }
00422
00423
00424 if (t->uac[branch].request.dst.send_sock==0) {
00425 LOG(L_ERR, "ERROR: add_uac: can't fwd to af %d, proto %d "
00426 " (no corresponding listening socket)\n",
00427 t->uac[branch].request.dst.to.s.sa_family,
00428 t->uac[branch].request.dst.proto );
00429 ret=ser_error=E_NO_SOCKET;
00430 goto error01;
00431 }
00432
00433
00434 shbuf=print_uac_request( t, request, branch, uri,
00435 &len, &t->uac[branch].request.dst);
00436 if (!shbuf) {
00437 ret=ser_error=E_OUT_OF_MEM;
00438 goto error01;
00439 }
00440
00441
00442 t->uac[branch].request.buffer=shbuf;
00443 t->uac[branch].request.buffer_len=len;
00444 t->uac[branch].uri.s=t->uac[branch].request.buffer+
00445 request->first_line.u.request.method.len+1;
00446 t->uac[branch].uri.len=uri->len;
00447 membar_write();
00448
00449
00450 t->nr_of_outgoings=(branch+1);
00451
00452
00453 if (proxy){
00454 proxy_mark(proxy, 1);
00455 }
00456
00457 ret=branch;
00458
00459 error01:
00460 error:
00461 return ret;
00462 }
00463
00464
00465
00466 #ifdef USE_DNS_FAILOVER
00467
00468
00469
00470 static int add_uac_from_buf( struct cell *t, struct sip_msg *request, str *uri, int proto,
00471 char *buf, short buf_len)
00472 {
00473
00474 int ret;
00475 unsigned short branch;
00476 char *shbuf;
00477 unsigned int len;
00478
00479 branch=t->nr_of_outgoings;
00480 if (branch==MAX_BRANCHES) {
00481 LOG(L_ERR, "ERROR: add_uac_from_buf: maximum number of branches exceeded\n");
00482 ret=ser_error=E_TOO_MANY_BRANCHES;
00483 goto error;
00484 }
00485
00486
00487 if (t->uac[branch].request.buffer) {
00488 LOG(L_CRIT, "ERROR: add_uac_from_buf: buffer rewrite attempt\n");
00489 ret=ser_error=E_BUG;
00490 goto error;
00491 }
00492
00493 if (uri2dst(&t->uac[branch].dns_h, &t->uac[branch].request.dst,
00494 request, uri, proto) == 0)
00495 {
00496 ret=ser_error=E_BAD_ADDRESS;
00497 goto error;
00498 }
00499
00500
00501 if (t->uac[branch].request.dst.send_sock==0) {
00502 LOG(L_ERR, "ERROR: add_uac_from_buf: can't fwd to af %d, proto %d "
00503 " (no corresponding listening socket)\n",
00504 t->uac[branch].request.dst.to.s.sa_family,
00505 t->uac[branch].request.dst.proto );
00506 ret=ser_error=E_NO_SOCKET;
00507 goto error;
00508 }
00509
00510
00511 shbuf=print_uac_request_from_buf( t, request, branch, uri,
00512 &len, &t->uac[branch].request.dst,
00513 buf, buf_len);
00514 if (!shbuf) {
00515 ret=ser_error=E_OUT_OF_MEM;
00516 goto error;
00517 }
00518
00519
00520 t->uac[branch].request.buffer=shbuf;
00521 t->uac[branch].request.buffer_len=len;
00522 t->uac[branch].uri.s=t->uac[branch].request.buffer+
00523 request->first_line.u.request.method.len+1;
00524 t->uac[branch].uri.len=uri->len;
00525 membar_write();
00526
00527
00528 t->nr_of_outgoings=(branch+1);
00529
00530
00531 ret=branch;
00532
00533 error:
00534 return ret;
00535 }
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549 int add_uac_dns_fallback( struct cell *t, struct sip_msg* msg,
00550 struct ua_client* old_uac,
00551 int lock_replies)
00552 {
00553 int ret;
00554
00555 ret=-1;
00556 if (cfg_get(core, core_cfg, use_dns_failover) &&
00557 !((t->flags & T_DONT_FORK) || uac_dont_fork(old_uac)) &&
00558 dns_srv_handle_next(&old_uac->dns_h, 0)){
00559 if (lock_replies){
00560
00561
00562 LOCK_REPLIES(t);
00563
00564 if ((t->flags & T_DONT_FORK) || uac_dont_fork(old_uac)){
00565 UNLOCK_REPLIES(t);
00566 DBG("add_uac_dns_fallback: no forking on => no new"
00567 " branches\n");
00568 return ret;
00569 }
00570 }
00571 if (t->nr_of_outgoings >= MAX_BRANCHES){
00572 LOG(L_ERR, "ERROR: add_uac_dns_fallback: maximum number of "
00573 "branches exceeded\n");
00574 if (lock_replies)
00575 UNLOCK_REPLIES(t);
00576 ret=ser_error=E_TOO_MANY_BRANCHES;
00577 return ret;
00578 }
00579
00580 dns_srv_handle_cpy(&t->uac[t->nr_of_outgoings].dns_h,
00581 &old_uac->dns_h);
00582
00583 if (cfg_get(tm, tm_cfg, reparse_on_dns_failover))
00584
00585
00586
00587 ret=add_uac_from_buf(t, msg, &old_uac->uri,
00588 old_uac->request.dst.proto,
00589 old_uac->request.buffer,
00590 old_uac->request.buffer_len);
00591 else
00592
00593
00594
00595
00596 ret=add_uac(t, msg, &old_uac->uri, 0, 0,
00597 old_uac->request.dst.proto);
00598
00599 if (ret<0){
00600
00601 dns_srv_handle_put(&t->uac[t->nr_of_outgoings].dns_h);
00602 }
00603 if (lock_replies){
00604 UNLOCK_REPLIES(t);
00605 }
00606 }
00607 return ret;
00608 }
00609
00610 #endif
00611
00612 int e2e_cancel_branch( struct sip_msg *cancel_msg, struct cell *t_cancel,
00613 struct cell *t_invite, int branch )
00614 {
00615 int ret;
00616 char *shbuf;
00617 unsigned int len;
00618
00619 ret=-1;
00620 if (t_cancel->uac[branch].request.buffer) {
00621 LOG(L_CRIT, "ERROR: e2e_cancel_branch: buffer rewrite attempt\n");
00622 ret=ser_error=E_BUG;
00623 goto error;
00624 }
00625 if (t_invite->uac[branch].request.buffer==0){
00626
00627 goto error;
00628 }
00629 t_invite->uac[branch].request.flags|=F_RB_CANCELED;
00630
00631
00632
00633
00634
00635
00636 if (cfg_get(tm, tm_cfg, reparse_invite)) {
00637
00638 #ifdef POSTPONE_MSG_CLONING
00639
00640
00641 membar_depends();
00642 #endif
00643 if (cancel_msg->add_rm || cancel_msg->body_lumps) {
00644 LOG(L_WARN, "WARNING: e2e_cancel_branch: CANCEL is built locally, "
00645 "thus lumps are not applied to the message!\n");
00646 }
00647 shbuf=build_local_reparse( t_invite, branch, &len, CANCEL, CANCEL_LEN, &t_invite->to);
00648
00649 } else {
00650
00651 shbuf=print_uac_request( t_cancel, cancel_msg, branch,
00652 &t_invite->uac[branch].uri, &len,
00653 &t_invite->uac[branch].request.dst);
00654 }
00655
00656 if (!shbuf) {
00657 LOG(L_ERR, "ERROR: e2e_cancel_branch: printing e2e cancel failed\n");
00658 ret=ser_error=E_OUT_OF_MEM;
00659 goto error;
00660 }
00661
00662
00663 t_cancel->uac[branch].request.dst=t_invite->uac[branch].request.dst;
00664 t_cancel->uac[branch].request.buffer=shbuf;
00665 t_cancel->uac[branch].request.buffer_len=len;
00666 t_cancel->uac[branch].uri.s=t_cancel->uac[branch].request.buffer+
00667 cancel_msg->first_line.u.request.method.len+1;
00668 t_cancel->uac[branch].uri.len=t_invite->uac[branch].uri.len;
00669
00670
00671
00672 ret=1;
00673
00674
00675 error:
00676 return ret;
00677 }
00678
00679 void e2e_cancel( struct sip_msg *cancel_msg,
00680 struct cell *t_cancel, struct cell *t_invite )
00681 {
00682 branch_bm_t cancel_bm;
00683 #ifndef E2E_CANCEL_HOP_BY_HOP
00684 branch_bm_t tmp_bm;
00685 #endif
00686 int i;
00687 int lowest_error;
00688 int ret;
00689 struct tmcb_params tmcb;
00690
00691 cancel_bm=0;
00692 lowest_error=0;
00693
00694 if (unlikely(has_tran_tmcbs(t_invite, TMCB_E2ECANCEL_IN))){
00695 INIT_TMCB_PARAMS(tmcb, cancel_msg, 0, cancel_msg->REQ_METHOD);
00696 run_trans_callbacks_internal(&t_invite->tmcb_hl, TMCB_E2ECANCEL_IN,
00697 t_invite, &tmcb);
00698 }
00699
00700
00701
00702
00703
00704
00705 t_invite->flags|=T_CANCELED;
00706
00707 if (t_invite->nr_of_outgoings==0){
00708
00709 t_reply( t_invite, t_invite->uas.request, 487, CANCELED );
00710 DBG("DEBUG: e2e_cancel: e2e cancel -- no more pending branches\n");
00711 t_reply( t_cancel, cancel_msg, 200, CANCEL_DONE );
00712 return;
00713 }
00714
00715
00716 which_cancel( t_invite, &cancel_bm );
00717 #ifdef E2E_CANCEL_HOP_BY_HOP
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730 for (i=0; i<t_invite->nr_of_outgoings; i++)
00731 if (cancel_bm & (1<<i)) {
00732
00733
00734
00735
00736 ret=cancel_branch(
00737 t_invite,
00738 i,
00739 cfg_get(tm,tm_cfg, cancel_b_flags)
00740 | ((t_invite->uac[i].request.buffer==NULL)?
00741 F_CANCEL_B_FAKE_REPLY:0)
00742 );
00743 if (ret<0) cancel_bm &= ~(1<<i);
00744 if (ret<lowest_error) lowest_error=ret;
00745 }
00746 #else
00747
00748
00749
00750
00751 t_cancel->label=t_invite->label;
00752 t_cancel->nr_of_outgoings=t_invite->nr_of_outgoings;
00753
00754 for (i=0; i<t_invite->nr_of_outgoings; i++)
00755 if ((cancel_bm & (1<<i)) && (t_invite->uac[i].last_received>=100)) {
00756 ret=e2e_cancel_branch(cancel_msg, t_cancel, t_invite, i);
00757 if (ret<0) cancel_bm &= ~(1<<i);
00758 if (ret<lowest_error) lowest_error=ret;
00759 }
00760
00761
00762 for (i = 0; i < t_cancel->nr_of_outgoings; i++) {
00763 if (cancel_bm & (1 << i)) {
00764 if (t_invite->uac[i].last_received>=100){
00765
00766
00767
00768
00769 stop_rb_retr(&t_invite->uac[i].request);
00770 if (SEND_BUFFER(&t_cancel->uac[i].request) == -1) {
00771 LOG(L_ERR, "ERROR: e2e_cancel: send failed\n");
00772 }
00773 #ifdef TMCB_ONSEND
00774 else{
00775 if (unlikely(has_tran_tmcbs(t_cancel, TMCB_REQUEST_SENT)))
00776 run_onsend_callbacks(TMCB_REQUEST_SENT,
00777 &t_cancel->uac[i].request,
00778 cancel_msg, 0, TMCB_LOCAL_F);
00779 }
00780 #endif
00781 if (start_retr( &t_cancel->uac[i].request )!=0)
00782 LOG(L_CRIT, "BUG: e2e_cancel: failed to start retr."
00783 " for %p\n", &t_cancel->uac[i].request);
00784 } else {
00785
00786
00787 if (!(cfg_get(tm, tm_cfg, cancel_b_flags) &
00788 F_CANCEL_B_FORCE_RETR))
00789 stop_rb_retr(&t_invite->uac[i].request);
00790
00791
00792
00793 if (cfg_get(tm, tm_cfg, cancel_b_flags) &
00794 F_CANCEL_B_FAKE_REPLY){
00795 LOCK_REPLIES(t_invite);
00796 if (relay_reply(t_invite, FAKED_REPLY, i,
00797 487, &tmp_bm, 1) == RPS_ERROR) {
00798 lowest_error = -1;
00799 }
00800 }
00801 }
00802 }
00803 }
00804 #endif
00805
00806
00807
00808
00809 if (lowest_error<0) {
00810 LOG(L_ERR, "ERROR: cancel error\n");
00811
00812
00813
00814
00815 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
00816 t_reply_unsafe( t_cancel, cancel_msg, 500, "cancel error");
00817 else
00818 t_reply( t_cancel, cancel_msg, 500, "cancel error");
00819 } else if (cancel_bm) {
00820
00821
00822
00823 DBG("DEBUG: e2e_cancel: e2e cancel proceeding\n");
00824
00825
00826
00827
00828 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
00829 t_reply_unsafe( t_cancel, cancel_msg, 200, CANCELING );
00830 else
00831 t_reply( t_cancel, cancel_msg, 200, CANCELING );
00832 } else {
00833
00834
00835
00836 DBG("DEBUG: e2e_cancel: e2e cancel -- no more pending branches\n");
00837
00838
00839
00840
00841 if ((rmode==MODE_ONFAILURE) && (t_cancel==get_t()))
00842 t_reply_unsafe( t_cancel, cancel_msg, 200, CANCEL_DONE );
00843 else
00844 t_reply( t_cancel, cancel_msg, 200, CANCEL_DONE );
00845 }
00846 }
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859 int t_send_branch( struct cell *t, int branch, struct sip_msg* p_msg ,
00860 struct proxy_l * proxy, int lock_replies)
00861 {
00862 struct ip_addr ip;
00863 int ret;
00864 struct ua_client* uac;
00865
00866 uac=&t->uac[branch];
00867 ret=branch;
00868 if (run_onsend(p_msg, &uac->request.dst, uac->request.buffer,
00869 uac->request.buffer_len)==0){
00870
00871
00872
00873
00874
00875 uac->last_received=408;
00876 su2ip_addr(&ip, &uac->request.dst.to);
00877 DBG("t_send_branch: onsend_route dropped msg. to %s:%d (%d)\n",
00878 ip_addr2a(&ip), su_getport(&uac->request.dst.to),
00879 uac->request.dst.proto);
00880 #ifdef USE_DNS_FAILOVER
00881
00882
00883 if (cfg_get(core, core_cfg, use_dns_failover)){
00884 ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
00885 if (ret>=0){
00886 su2ip_addr(&ip, &uac->request.dst.to);
00887 DBG("t_send_branch: send on branch %d failed "
00888 "(onsend_route), trying another ip %s:%d (%d)\n",
00889 branch, ip_addr2a(&ip),
00890 su_getport(&uac->request.dst.to),
00891 uac->request.dst.proto);
00892
00893 return ret;
00894 }
00895 }
00896 #endif
00897 return -1;
00898 }
00899 #ifdef USE_DST_BLACKLIST
00900 if (cfg_get(core, core_cfg, use_dst_blacklist)
00901 && p_msg
00902 && (p_msg->REQ_METHOD & cfg_get(tm, tm_cfg, tm_blst_methods_lookup))
00903 ){
00904 if (dst_is_blacklisted(&uac->request.dst, p_msg)){
00905 su2ip_addr(&ip, &uac->request.dst.to);
00906 DBG("t_send_branch: blacklisted destination: %s:%d (%d)\n",
00907 ip_addr2a(&ip), su_getport(&uac->request.dst.to),
00908 uac->request.dst.proto);
00909
00910
00911
00912
00913
00914 uac->last_received=408;
00915 #ifdef USE_DNS_FAILOVER
00916
00917
00918 if (cfg_get(core, core_cfg, use_dns_failover)){
00919 ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
00920 if (ret>=0){
00921 su2ip_addr(&ip, &uac->request.dst.to);
00922 DBG("t_send_branch: send on branch %d failed (blacklist),"
00923 " trying another ip %s:%d (%d)\n", branch,
00924 ip_addr2a(&ip), su_getport(&uac->request.dst.to),
00925 uac->request.dst.proto);
00926
00927 return ret;
00928 }
00929 }
00930 #endif
00931 return -1;
00932 }
00933 }
00934 #endif
00935 if (SEND_BUFFER( &uac->request)==-1) {
00936
00937
00938
00939
00940
00941 uac->last_received=408;
00942 su2ip_addr(&ip, &uac->request.dst.to);
00943 DBG("t_send_branch: send to %s:%d (%d) failed\n",
00944 ip_addr2a(&ip), su_getport(&uac->request.dst.to),
00945 uac->request.dst.proto);
00946 #ifdef USE_DST_BLACKLIST
00947 if (cfg_get(core, core_cfg, use_dst_blacklist))
00948 dst_blacklist_add(BLST_ERR_SEND, &uac->request.dst, p_msg);
00949 #endif
00950 #ifdef USE_DNS_FAILOVER
00951
00952
00953 if (cfg_get(core, core_cfg, use_dns_failover)){
00954 ret=add_uac_dns_fallback(t, p_msg, uac, lock_replies);
00955 if (ret>=0){
00956
00957 DBG("t_send_branch: send on branch %d failed, adding another"
00958 " branch with another ip\n", branch);
00959 return ret;
00960 }
00961 }
00962 #endif
00963 LOG(L_ERR, "ERROR: t_send_branch: sending request on branch %d "
00964 "failed\n", branch);
00965 if (proxy) { proxy->errors++; proxy->ok=0; }
00966 return -2;
00967 } else {
00968 #ifdef TMCB_ONSEND
00969 if (unlikely(has_tran_tmcbs(t, TMCB_REQUEST_SENT)))
00970 run_onsend_callbacks(TMCB_REQUEST_SENT, &uac->request, p_msg, 0,0);
00971 #endif
00972
00973 if (start_retr( &uac->request )!=0){
00974 LOG(L_CRIT, "BUG: t_send_branch: retr. already started for %p\n",
00975 &uac->request);
00976 return -2;
00977 }
00978 }
00979 return ret;
00980 }
00981
00982
00983
00984
00985
00986
00987
00988 int t_forward_nonack( struct cell *t, struct sip_msg* p_msg ,
00989 struct proxy_l * proxy, int proto)
00990 {
00991 int branch_ret, lowest_ret;
00992 str current_uri;
00993 branch_bm_t added_branches;
00994 int first_branch;
00995 int i, q;
00996 struct cell *t_invite;
00997 int success_branch;
00998 int try_new;
00999 int lock_replies;
01000 str dst_uri;
01001 struct socket_info* si, *backup_si;
01002
01003
01004
01005 current_uri.s=0;
01006
01007 if (t->flags & T_CANCELED){
01008 DBG("t_forward_non_ack: no forwarding on a canceled transaction\n");
01009 ser_error=E_CANCELED;
01010 return -1;
01011 }
01012 if (p_msg->REQ_METHOD==METHOD_CANCEL) {
01013 t_invite=t_lookupOriginalT( p_msg );
01014 if (t_invite!=T_NULL_CELL) {
01015 e2e_cancel( p_msg, t, t_invite );
01016 UNREF(t_invite);
01017
01018
01019 set_kr(REQ_FWDED);
01020 return 1;
01021 }
01022 }
01023
01024 backup_si = p_msg->force_send_socket;
01025
01026 lowest_ret=E_UNSPEC;
01027
01028 added_branches=0;
01029
01030 first_branch=t->nr_of_outgoings;
01031
01032 if (t->on_branch) {
01033
01034 branch_route = t->on_branch;
01035
01036
01037
01038 t_on_branch(0);
01039 } else {
01040 branch_route = 0;
01041 }
01042
01043
01044
01045
01046 if (first_branch==0) {
01047 #ifdef POSTPONE_MSG_CLONING
01048
01049 if ((rmode == MODE_REQUEST) &&
01050 save_msg_lumps(t->uas.request, p_msg)) {
01051 LOG(L_ERR, "ERROR: t_forward_nonack: "
01052 "failed to save the message lumps\n");
01053 return -1;
01054 }
01055 #endif
01056 try_new=1;
01057 branch_ret=add_uac( t, p_msg, GET_RURI(p_msg), GET_NEXT_HOP(p_msg),
01058 proxy, proto );
01059 if (branch_ret>=0)
01060 added_branches |= 1<<branch_ret;
01061 else
01062 lowest_ret=MIN_int(lowest_ret, branch_ret);
01063 } else try_new=0;
01064
01065 init_branch_iterator();
01066 while((current_uri.s=next_branch( ¤t_uri.len, &q, &dst_uri.s, &dst_uri.len, &si))) {
01067 try_new++;
01068 p_msg->force_send_socket = si;
01069 branch_ret=add_uac( t, p_msg, ¤t_uri,
01070 (dst_uri.len) ? (&dst_uri) : ¤t_uri,
01071 proxy, proto);
01072
01073
01074
01075
01076 if (branch_ret>=0)
01077 added_branches |= 1<<branch_ret;
01078 else
01079 lowest_ret=MIN_int(lowest_ret, branch_ret);
01080 }
01081
01082 clear_branches();
01083
01084 p_msg->force_send_socket = backup_si;
01085
01086
01087
01088
01089 if (added_branches==0) {
01090 if (try_new==0) {
01091 LOG(L_ERR, "ERROR: t_forward_nonack: no branches for"
01092 " forwarding\n");
01093
01094
01095 ser_error=MIN_int(lowest_ret, E_CFG);
01096 return -1;
01097 }
01098 LOG(L_ERR, "ERROR: t_forward_nonack: failure to add branches\n");
01099 ser_error=lowest_ret;
01100 return lowest_ret;
01101 }
01102 ser_error=0;
01103
01104 success_branch=0;
01105 lock_replies= ! ((rmode==MODE_ONFAILURE) && (t==get_t()));
01106 for (i=first_branch; i<t->nr_of_outgoings; i++) {
01107 if (added_branches & (1<<i)) {
01108
01109 branch_ret=t_send_branch(t, i, p_msg , proxy, lock_replies);
01110 if (branch_ret>=0){
01111 if (branch_ret==i)
01112 success_branch++;
01113 else
01114 added_branches |= 1<<branch_ret;
01115 }
01116 }
01117 }
01118 if (success_branch<=0) {
01119
01120
01121
01122
01123 ser_error=E_SEND;
01124
01125
01126 return -1;
01127 }
01128 ser_error=0;
01129 set_kr(REQ_FWDED);
01130 return 1;
01131 }
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145 int t_forward_cancel(struct sip_msg* p_msg , struct proxy_l * proxy, int proto,
01146 struct cell** tran)
01147 {
01148 struct cell* t_invite;
01149 struct cell* t;
01150 int ret;
01151 int new_tran;
01152 struct dest_info dst;
01153 str host;
01154 unsigned short port;
01155 short comp;
01156
01157 t=0;
01158
01159 if (cfg_get(tm, tm_cfg, unmatched_cancel)==UM_CANCEL_STATEFULL){
01160
01161 new_tran=t_newtran(p_msg);
01162 if (new_tran<=0 && new_tran!=E_SCRIPT){
01163 if (new_tran==0)
01164
01165 ret=1;
01166 else
01167
01168 ret=(ser_error==E_BAD_VIA && reply_to_via) ? 0: new_tran;
01169 goto end;
01170 }
01171 t=get_t();
01172 ret=t_forward_nonack(t, p_msg, proxy, proto);
01173 goto end;
01174 }
01175
01176 t_invite=t_lookupOriginalT( p_msg );
01177 if (t_invite!=T_NULL_CELL) {
01178
01179 new_tran=t_newtran(p_msg);
01180 if (new_tran<=0 && new_tran!=E_SCRIPT){
01181 if (new_tran==0)
01182
01183 ret=1;
01184 else
01185
01186 ret=(ser_error==E_BAD_VIA && reply_to_via) ? 0: new_tran;
01187 UNREF(t_invite);
01188 goto end;
01189 }
01190 t=get_t();
01191 e2e_cancel( p_msg, t, t_invite );
01192 UNREF(t_invite);
01193 ret=1;
01194 goto end;
01195 }else
01196 if (cfg_get(tm, tm_cfg, unmatched_cancel)==UM_CANCEL_DROP){
01197 DBG("t_forward_nonack: non matching cancel dropped\n");
01198 ret=1;
01199 goto end;
01200 }else{
01201
01202 DBG( "SER: forwarding CANCEL statelessly \n");
01203 if (proxy==0) {
01204 init_dest_info(&dst);
01205 dst.proto=proto;
01206 if (get_uri_send_info(GET_NEXT_HOP(p_msg), &host,
01207 &port, &dst.proto, &comp)!=0){
01208 ret=E_BAD_ADDRESS;
01209 goto end;
01210 }
01211 #ifdef USE_COMP
01212 dst.comp=comp;
01213 #endif
01214
01215
01216 ret=forward_request(p_msg, &host, port, &dst);
01217 goto end;
01218 } else {
01219 init_dest_info(&dst);
01220 dst.proto=get_proto(proto, proxy->proto);
01221 proxy2su(&dst.to, proxy);
01222
01223
01224 ret=forward_request( p_msg , 0, 0, &dst) ;
01225 goto end;
01226 }
01227 }
01228 end:
01229 if (tran)
01230 *tran=t;
01231 return ret;
01232 }
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244 int t_relay_cancel(struct sip_msg* p_msg)
01245 {
01246 struct cell* t_invite;
01247 struct cell* t;
01248 int ret;
01249 int new_tran;
01250
01251 t_invite=t_lookupOriginalT( p_msg );
01252 if (t_invite!=T_NULL_CELL) {
01253
01254 new_tran=t_newtran(p_msg);
01255 if (new_tran<=0 && new_tran!=E_SCRIPT){
01256 if (new_tran==0)
01257
01258
01259 ret=0;
01260 else
01261
01262 ret=(ser_error==E_BAD_VIA && reply_to_via) ? 0: new_tran;
01263 UNREF(t_invite);
01264 goto end;
01265 }
01266 t=get_t();
01267 e2e_cancel( p_msg, t, t_invite );
01268 UNREF(t_invite);
01269
01270 ret=0;
01271 goto end;
01272
01273 } else {
01274
01275 ret=1;
01276 }
01277 end:
01278 return ret;
01279 }
01280
01281
01282
01283 int t_replicate(struct sip_msg *p_msg, struct proxy_l *proxy, int proto )
01284 {
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296 return t_relay_to(p_msg, proxy, proto, 1 );
01297 }
01298
01299
01300 int reparse_on_dns_failover_fixup(void *handle, str *gname, str *name, void **val)
01301 {
01302 #ifdef USE_DNS_FAILOVER
01303 if ((int)(long)(*val) && mhomed) {
01304 LOG(L_WARN, "WARNING: reparse_on_dns_failover_fixup:"
01305 "reparse_on_dns_failover is enabled on a "
01306 "multihomed host -- check the readme of tm module!\n");
01307 }
01308 #endif
01309 return 0;
01310 }