cpl_sig.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser 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. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. #include "../../action.h"
  28. #include "../../dset.h"
  29. #include "../../route.h"
  30. #include "../../modules/tm/tm_load.h"
  31. #include "loc_set.h"
  32. #include "cpl_sig.h"
  33. #include "cpl_env.h"
  34. /* forwards the msg to the given location set; if flags has set the
  35. * CPL_PROXY_DONE, all locations will be added as branches, otherwise, the first
  36. * one will set as RURI (this is ha case when this is the first proxy of the
  37. * message)
  38. * The given list of location will be freed, returning 0 instead.
  39. * Returns: 0 - OK
  40. * -1 - error */
  41. int cpl_proxy_to_loc_set( struct sip_msg *msg, struct location **locs,
  42. unsigned char flag)
  43. {
  44. struct location *foo;
  45. struct action act;
  46. struct run_act_ctx ra_ctx;
  47. if (!*locs) {
  48. LOG(L_ERR,"ERROR:cpl_c:cpl_proxy_to_loc_set: empty loc set!!\n");
  49. goto error;
  50. }
  51. init_run_actions_ctx(&ra_ctx);
  52. /* if it's the first time when this sip_msg is proxied, use the first addr
  53. * in loc_set to rewrite the RURI */
  54. if (!(flag&CPL_PROXY_DONE)) {
  55. DBG("DEBUG:cpl_c:cpl_proxy_to_loc_set: rewriting Request-URI with "
  56. "<%s>\n",(*locs)->addr.uri.s);
  57. /* build a new action for setting the URI */
  58. memset(&act, 0, sizeof(act));
  59. act.type = SET_URI_T;
  60. act.val[0].type = STRING_ST;
  61. act.val[0].u.string = (*locs)->addr.uri.s;
  62. act.next = 0;
  63. /* push the action */
  64. if (do_action(&ra_ctx, &act, msg) < 0) {
  65. LOG(L_ERR,"ERROR:cpl_c:cpl_proxy_to_loc_set: do_action failed\n");
  66. goto error;
  67. }
  68. /* is the location NATED? */
  69. if ((*locs)->flags&CPL_LOC_NATED) setflag(msg,cpl_env.nat_flag);
  70. /* free the location and point to the next one */
  71. foo = (*locs)->next;
  72. free_location( *locs );
  73. *locs = foo;
  74. }
  75. /* add the rest of the locations as branches */
  76. while(*locs) {
  77. DBG("DEBUG:cpl_c:cpl_proxy_to_loc_set: appending branch "
  78. "<%.*s>\n",(*locs)->addr.uri.len,(*locs)->addr.uri.s);
  79. if(append_branch(msg, &(*locs)->addr.uri,
  80. 0, 0, Q_UNSPECIFIED, 0, 0, 0, 0)==-1){
  81. LOG(L_ERR,"ERROR:cpl_c:cpl_proxy_to_loc_set: failed when "
  82. "appending branch <%s>\n",(*locs)->addr.uri.s);
  83. goto error;
  84. }
  85. /* is the location NATED? */
  86. if ((*locs)->flags&CPL_LOC_NATED) setflag(msg,cpl_env.nat_flag);
  87. /* free the location and point to the next one */
  88. foo = (*locs)->next;
  89. free_location( *locs );
  90. *locs = foo;
  91. }
  92. /* run what proxy route is set */
  93. if (cpl_env.proxy_route) {
  94. if (run_actions(&ra_ctx, main_rt.rlist[cpl_env.proxy_route], msg)<0) {
  95. LOG(L_ERR,"ERROR:cpl_c:cpl_proxy_to_loc_set: "
  96. "Error in do_action for proxy_route\n");
  97. }
  98. }
  99. /* do t_forward */
  100. if ( flag&CPL_IS_STATEFUL ) {
  101. /* transaction exists */
  102. if (cpl_fct.tmb.t_forward_nonack(msg, 0/*no proxy*/)==-1) {
  103. LOG(L_ERR,"ERROR:cpl_c:cpl_proxy_to_loc_set: t_forward_nonack "
  104. "failed !\n");
  105. goto error;
  106. }
  107. } else {
  108. /* no transaction -> build & fwd */
  109. if (cpl_fct.tmb.t_relay(msg, 0, 0)==-1) {
  110. LOG(L_ERR,"ERROR:cpl_c:cpl_proxy_to_loc_set: t_relay failed !\n");
  111. goto error;
  112. }
  113. }
  114. return 0;
  115. error:
  116. return -1;
  117. }