diversion.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * $Id$
  3. *
  4. * Diversion Header Field Support
  5. *
  6. * Copyright (C) 2004 FhG Fokus
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio 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. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include "../../sr_module.h"
  28. #include "../../error.h"
  29. #include "../../dprint.h"
  30. #include "../../mem/mem.h"
  31. #include "../../data_lump.h"
  32. #include "../../mod_fix.h"
  33. MODULE_VERSION
  34. #define DIVERSION_HF "Diversion"
  35. #define DIVERSION_HF_LEN (sizeof(DIVERSION_HF) - 1)
  36. #define DIVERSION_PREFIX DIVERSION_HF ": <"
  37. #define DIVERSION_PREFIX_LEN (sizeof(DIVERSION_PREFIX) - 1)
  38. #define DIVERSION_SUFFIX ">;reason="
  39. #define DIVERSION_SUFFIX_LEN (sizeof(DIVERSION_SUFFIX) - 1)
  40. str suffix = {"", 0};
  41. int add_diversion(struct sip_msg* msg, char* r, char* u);
  42. /*
  43. * Module initialization function prototype
  44. */
  45. static int mod_init(void);
  46. /*
  47. * Exported functions
  48. */
  49. static cmd_export_t cmds[] = {
  50. {"add_diversion", (cmd_function)add_diversion, 1, fixup_spve_null,
  51. 0, REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  52. {"add_diversion", (cmd_function)add_diversion, 2, fixup_spve_spve,
  53. 0, REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  54. {0, 0, 0, 0, 0, 0}
  55. };
  56. /*
  57. * Exported parameters
  58. */
  59. static param_export_t params[] = {
  60. {"suffix", STR_PARAM, &suffix.s},
  61. {0, 0, 0}
  62. };
  63. /*
  64. * Module interface
  65. */
  66. struct module_exports exports = {
  67. "diversion",
  68. DEFAULT_DLFLAGS, /* dlopen flags */
  69. cmds, /* Exported functions */
  70. params, /* Exported parameters */
  71. 0, /* exported statistics */
  72. 0, /* exported MI functions */
  73. 0, /* exported pseudo-variables */
  74. 0, /* extra processes */
  75. mod_init, /* module initialization function */
  76. 0, /* response function */
  77. 0, /* destroy function */
  78. 0 /* child initialization function */
  79. };
  80. static int mod_init(void)
  81. {
  82. suffix.len = strlen(suffix.s);
  83. return 0;
  84. }
  85. static inline int add_diversion_helper(struct sip_msg* msg, str* s)
  86. {
  87. char *ptr;
  88. int is_ref;
  89. struct lump* anchor = 0;
  90. if (!msg->diversion && parse_headers(msg, HDR_DIVERSION_F, 0) == -1) {
  91. LM_ERR("header parsing failed\n");
  92. return -1;
  93. }
  94. if (msg->diversion) {
  95. /* Insert just before the topmost Diversion header */
  96. ptr = msg->diversion->name.s;
  97. } else {
  98. /* Insert at the end */
  99. ptr = msg->unparsed;
  100. }
  101. anchor = anchor_lump2(msg, ptr - msg->buf, 0, 0, &is_ref);
  102. if (!anchor) {
  103. LM_ERR("can't get anchor\n");
  104. return -2;
  105. }
  106. if (!insert_new_lump_before(anchor, s->s, s->len, 0)) {
  107. LM_ERR("can't insert lump\n");
  108. return -3;
  109. }
  110. return 0;
  111. }
  112. int add_diversion(struct sip_msg* msg, char* r, char* u)
  113. {
  114. str div_hf;
  115. char *at;
  116. str uri;
  117. str reason;
  118. if(fixup_get_svalue(msg, (gparam_t*)r, &reason)<0)
  119. {
  120. LM_ERR("cannot get the script\n");
  121. return -1;
  122. }
  123. if(u==NULL) {
  124. uri = msg->first_line.u.request.uri;
  125. } else {
  126. if(fixup_get_svalue(msg, (gparam_t*)u, &uri)<0)
  127. {
  128. LM_ERR("cannot get the uri parameter\n");
  129. return -1;
  130. }
  131. }
  132. div_hf.len = DIVERSION_PREFIX_LEN + uri.len + DIVERSION_SUFFIX_LEN + reason.len + CRLF_LEN;
  133. div_hf.s = pkg_malloc(div_hf.len);
  134. if (!div_hf.s) {
  135. LM_ERR("no pkg memory left\n");
  136. return -1;
  137. }
  138. at = div_hf.s;
  139. memcpy(at, DIVERSION_PREFIX, DIVERSION_PREFIX_LEN);
  140. at += DIVERSION_PREFIX_LEN;
  141. memcpy(at, uri.s, uri.len);
  142. at += uri.len;
  143. memcpy(at, DIVERSION_SUFFIX, DIVERSION_SUFFIX_LEN);
  144. at += DIVERSION_SUFFIX_LEN;
  145. memcpy(at, reason.s, reason.len);
  146. at += reason.len;
  147. memcpy(at, CRLF, CRLF_LEN);
  148. if (add_diversion_helper(msg, &div_hf) < 0) {
  149. pkg_free(div_hf.s);
  150. return -1;
  151. }
  152. return 1;
  153. }