diversion.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * $Id$
  3. *
  4. * Diversion Header Field Support
  5. *
  6. * Copyright (C) 2004 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include "../../sr_module.h"
  33. #include "../../error.h"
  34. #include "../../dprint.h"
  35. #include "../../mem/mem.h"
  36. #include "../../data_lump.h"
  37. MODULE_VERSION
  38. #define DIVERSION_HF "Diversion"
  39. #define DIVERSION_HF_LEN (sizeof(DIVERSION_HF) - 1)
  40. #define DIVERSION_PREFIX DIVERSION_HF ": <"
  41. #define DIVERSION_PREFIX_LEN (sizeof(DIVERSION_PREFIX) - 1)
  42. #define DIVERSION_SUFFIX ">;reason="
  43. #define DIVERSION_SUFFIX_LEN (sizeof(DIVERSION_SUFFIX) - 1)
  44. str suffix = STR_STATIC_INIT("");
  45. int add_diversion(struct sip_msg* msg, char* r, char* s);
  46. /*
  47. * Module initialization function prototype
  48. */
  49. static int mod_init(void);
  50. /*
  51. * Exported functions
  52. */
  53. static cmd_export_t cmds[] = {
  54. {"add_diversion", add_diversion, 1, fixup_var_str_1, REQUEST_ROUTE | FAILURE_ROUTE},
  55. {0, 0, 0, 0, 0}
  56. };
  57. /*
  58. * Exported parameters
  59. */
  60. static param_export_t params[] = {
  61. {"suffix", PARAM_STR, &suffix},
  62. {0, 0, 0}
  63. };
  64. /*
  65. * Module interface
  66. */
  67. struct module_exports exports = {
  68. "diversion",
  69. cmds, /* Exported functions */
  70. 0, /* RPC methods */
  71. params, /* Exported parameters */
  72. mod_init, /* module initialization function */
  73. 0, /* response function */
  74. 0, /* destroy function */
  75. 0, /* oncancel function */
  76. 0 /* child initialization function */
  77. };
  78. static int mod_init(void)
  79. {
  80. return 0;
  81. }
  82. static inline int add_diversion_helper(struct sip_msg* msg, str* s)
  83. {
  84. char *ptr;
  85. static struct lump* anchor = 0;
  86. static int msg_id = 0;
  87. if (msg_id != msg->id) {
  88. msg_id = msg->id;
  89. anchor = 0;
  90. }
  91. if (!msg->diversion && parse_headers(msg, HDR_DIVERSION_F, 0) == -1) {
  92. LOG(L_ERR, "add_diversion_helper: Header parsing failed\n");
  93. return -1;
  94. }
  95. if (msg->diversion) {
  96. /* Insert just before the topmost Diversion header */
  97. ptr = msg->diversion->name.s;
  98. } else {
  99. /* Insert at the end */
  100. ptr = msg->unparsed;
  101. }
  102. if (!anchor) {
  103. anchor = anchor_lump(msg, ptr - msg->buf, 0, 0);
  104. if (!anchor) {
  105. LOG(L_ERR, "add_diversion_helper: Can't get anchor\n");
  106. return -2;
  107. }
  108. }
  109. if (!insert_new_lump_before(anchor, s->s, s->len, 0)) {
  110. LOG(L_ERR, "add_diversion_helper: Can't insert lump\n");
  111. return -3;
  112. }
  113. return 0;
  114. }
  115. int add_diversion(struct sip_msg* msg, char* r, char* s)
  116. {
  117. str div_hf;
  118. char *at;
  119. str* uri;
  120. str reason;
  121. if (get_str_fparam(&reason, msg, (fparam_t*)r) < 0) return -1;
  122. uri = &msg->first_line.u.request.uri;
  123. div_hf.len = DIVERSION_PREFIX_LEN + uri->len + DIVERSION_SUFFIX_LEN + reason.len + CRLF_LEN;
  124. div_hf.s = pkg_malloc(div_hf.len);
  125. if (!div_hf.s) {
  126. LOG(L_ERR, "add_diversion: No memory left\n");
  127. return -1;
  128. }
  129. at = div_hf.s;
  130. memcpy(at, DIVERSION_PREFIX, DIVERSION_PREFIX_LEN);
  131. at += DIVERSION_PREFIX_LEN;
  132. memcpy(at, uri->s, uri->len);
  133. at += uri->len;
  134. memcpy(at, DIVERSION_SUFFIX, DIVERSION_SUFFIX_LEN);
  135. at += DIVERSION_SUFFIX_LEN;
  136. memcpy(at, reason.s, reason.len);
  137. at += reason.len;
  138. memcpy(at, CRLF, CRLF_LEN);
  139. if (add_diversion_helper(msg, &div_hf) < 0) {
  140. pkg_free(div_hf.s);
  141. return -1;
  142. }
  143. return 1;
  144. }