cpl_sig.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "../../action.h"
  23. #include "../../dset.h"
  24. #include "../../modules/tm/tm_load.h"
  25. #include "loc_set.h"
  26. #include "cpl_sig.h"
  27. #include "cpl_env.h"
  28. /* forwards the msg to the given location set; if flags has set the
  29. * CPL_PROXY_DONE, all locations will be added as branches, otherwise, the
  30. * first one will set as RURI (this is ha case when this is the first proxy
  31. * of the message)
  32. * The given list of location will be freed, returning 0 instead.
  33. * Returns: 0 - OK
  34. * -1 - error */
  35. int cpl_proxy_to_loc_set( struct sip_msg *msg, struct location **locs,
  36. unsigned char flag)
  37. {
  38. struct location *foo;
  39. struct action act;
  40. struct run_act_ctx ra_ctx;
  41. int bflags;
  42. if (!*locs) {
  43. LM_ERR("empty loc set!!\n");
  44. goto error;
  45. }
  46. /* if it's the first time when this sip_msg is proxied, use the first addr
  47. * in loc_set to rewrite the RURI */
  48. if (!(flag&CPL_PROXY_DONE)) {
  49. LM_DBG("rewriting Request-URI with <%s>\n",(*locs)->addr.uri.s);
  50. /* build a new action for setting the URI */
  51. memset(&act, '\0', sizeof(act));
  52. act.type = SET_URI_T;
  53. act.val[0].type = STRING_ST;
  54. act.val[0].u.string = (*locs)->addr.uri.s;
  55. init_run_actions_ctx(&ra_ctx);
  56. /* push the action */
  57. if (do_action(&ra_ctx, &act, msg) < 0) {
  58. LM_ERR("do_action failed\n");
  59. goto error;
  60. }
  61. /* build a new action for setting the DSTURI */
  62. if((*locs)->addr.received.s && (*locs)->addr.received.len) {
  63. LM_DBG("rewriting Destination URI "
  64. "with <%s>\n",(*locs)->addr.received.s);
  65. if (set_dst_uri(msg, &(*locs)->addr.received) < 0) {
  66. LM_ERR("Error while setting the dst uri\n");
  67. goto error;
  68. }
  69. /* dst_uri changes, so it makes sense to re-use the current uri for
  70. forking */
  71. ruri_mark_new(); /* re-use uri for serial forking */
  72. }
  73. /* is the location NATED? */
  74. if ((*locs)->flags&CPL_LOC_NATED)
  75. setbflag( 0, cpl_fct.ulb.nat_flag );
  76. /* free the location and point to the next one */
  77. foo = (*locs)->next;
  78. free_location( *locs );
  79. *locs = foo;
  80. }
  81. /* add the rest of the locations as branches */
  82. while(*locs) {
  83. bflags = ((*locs)->flags&CPL_LOC_NATED) ? cpl_fct.ulb.nat_flag : 0 ;
  84. LM_DBG("appending branch <%.*s>, flags %d\n",
  85. (*locs)->addr.uri.len, (*locs)->addr.uri.s, bflags);
  86. if(append_branch(msg, &(*locs)->addr.uri,
  87. &(*locs)->addr.received, 0,
  88. Q_UNSPECIFIED, bflags, 0, 0, 0, 0, 0)==-1){
  89. LM_ERR("failed when appending branch <%s>\n",
  90. (*locs)->addr.uri.s);
  91. goto error;
  92. }
  93. /* free the location and point to the next one */
  94. foo = (*locs)->next;
  95. free_location( *locs );
  96. *locs = foo;
  97. }
  98. /* run what proxy route is set */
  99. if (cpl_env.proxy_route) {
  100. /* do not alter route type - it might be REQUEST or FAILURE */
  101. run_top_route( main_rt.rlist[cpl_env.proxy_route], msg, 0);
  102. }
  103. /* do t_forward */
  104. if (cpl_fct.tmb.t_relay(msg, 0, 0)==-1) {
  105. LM_ERR("t_relay failed !\n");
  106. goto error;
  107. }
  108. return 0;
  109. error:
  110. return -1;
  111. }