tsilo.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2014 Federico Cabiddu ([email protected])
  5. *
  6. * This file is part of SIP-Router.org, a free SIP server.
  7. *
  8. * SIP-Router 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 <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include "../../sr_module.h"
  26. #include "../../dprint.h"
  27. #include "../../mod_fix.h"
  28. #include "../../route.h"
  29. #include "../../script_cb.h"
  30. #include "../../modules/tm/tm_load.h"
  31. #include "../../modules/registrar/api.h"
  32. #include "../../dset.h"
  33. #include "ts_hash.h"
  34. #include "ts_handlers.h"
  35. #include "ts_append.h"
  36. #include "ts_store.h"
  37. MODULE_VERSION
  38. /** TM bind **/
  39. struct tm_binds _tmb;
  40. /** REGISTRAR bind **/
  41. registrar_api_t _regapi;
  42. /** parameters */
  43. static int hash_size = 2048;
  44. /** module functions */
  45. static int mod_init(void);
  46. static void destroy(void);
  47. static int w_ts_append_to(struct sip_msg* msg, char *idx, char *lbl, char *d);
  48. static int fixup_ts_append_to(void** param, int param_no);
  49. static int w_ts_append(struct sip_msg* _msg, char *_table, char *_ruri);
  50. static int fixup_ts_append(void** param, int param_no);
  51. static int w_ts_store(struct sip_msg* msg);
  52. static cmd_export_t cmds[]={
  53. {"ts_append_to", (cmd_function)w_ts_append_to, 3,
  54. fixup_ts_append_to, REQUEST_ROUTE | FAILURE_ROUTE },
  55. {"ts_append", (cmd_function)w_ts_append, 2,
  56. fixup_ts_append, REQUEST_ROUTE | FAILURE_ROUTE },
  57. {"ts_store", (cmd_function)w_ts_store, 0,
  58. 0 , REQUEST_ROUTE | FAILURE_ROUTE },
  59. {0,0,0,0,0}
  60. };
  61. static param_export_t params[]={
  62. {"hash_size", INT_PARAM, &hash_size},
  63. {0,0,0}
  64. };
  65. /** module exports */
  66. struct module_exports exports= {
  67. "tsilo",
  68. cmds,
  69. 0, /* RPC methods */
  70. params,
  71. mod_init, /* module initialization function */
  72. 0,
  73. (destroy_function) destroy, /* destroy function */
  74. 0,
  75. 0
  76. };
  77. /**
  78. * init module function
  79. */
  80. static int mod_init(void)
  81. {
  82. unsigned int n;
  83. /* load the TM API */
  84. if (load_tm_api(&_tmb)!=0) {
  85. LM_ERR("can't load TM API\n");
  86. return -1;
  87. }
  88. /* load the REGISTRAR API */
  89. if (registrar_load_api(&_regapi) != 0) {
  90. LM_ERR("cannot load REGISTRAR API\n");
  91. return -1;
  92. }
  93. /* sanitize hash_size */
  94. if (hash_size < 1){
  95. LM_WARN("hash_size is smaller "
  96. "than 1 -> rounding from %d to 1\n",
  97. hash_size);
  98. hash_size = 1;
  99. }
  100. /* initialize the hash table */
  101. for( n=0 ; n<(8*sizeof(n)) ; n++) {
  102. if (hash_size==(1<<n))
  103. break;
  104. if (n && hash_size<(1<<n)) {
  105. LM_WARN("hash_size is not a power "
  106. "of 2 as it should be -> rounding from %d to %d (n=%d)\n",
  107. hash_size, 1<<(n-1), n);
  108. hash_size = 1<<(n-1);
  109. break;
  110. }
  111. }
  112. LM_DBG("creating table with size %d", hash_size);
  113. if ( init_ts_table(hash_size)<0 ) {
  114. LM_ERR("failed to create hash table\n");
  115. return -1;
  116. }
  117. return 0;
  118. }
  119. /**
  120. * destroy function
  121. */
  122. static void destroy(void)
  123. {
  124. destroy_ts_table();
  125. return;
  126. }
  127. /**
  128. *
  129. */
  130. static int fixup_ts_append_to(void** param, int param_no)
  131. {
  132. if (param_no==1 || param_no==2) {
  133. return fixup_igp_null(param, 1);
  134. }
  135. if (param_no==3) {
  136. if(strlen((char*)*param)<=1 && (*(char*)(*param)==0 || *(char*)(*param)=='0')) {
  137. *param = (void*)0;
  138. LM_ERR("empty table name\n");
  139. return -1;
  140. }
  141. }
  142. return 0;
  143. }
  144. static int fixup_ts_append(void** param, int param_no)
  145. {
  146. if (param_no==1) {
  147. if(strlen((char*)*param)<=1 && (*(char*)(*param)==0 || *(char*)(*param)=='0')) {
  148. *param = (void*)0;
  149. LM_ERR("empty table name\n");
  150. return -1;
  151. }
  152. }
  153. if (param_no==2 || param_no==3) {
  154. return fixup_spve_null(param, 1);
  155. }
  156. return 0;
  157. }
  158. /**
  159. *
  160. */
  161. static int w_ts_append(struct sip_msg* _msg, char *_table, char *_ruri)
  162. {
  163. str ruri = {0};
  164. if(_ruri==NULL || (fixup_get_svalue(_msg, (gparam_p)_ruri, &ruri)!=0 || ruri.len<=0)) {
  165. LM_ERR("invalid ruri parameter\n");
  166. return -1;
  167. }
  168. return ts_append(_msg, &ruri, _table);
  169. }
  170. /**
  171. *
  172. */
  173. static int w_ts_append_to(struct sip_msg* msg, char *idx, char *lbl, char *table)
  174. {
  175. unsigned int tindex;
  176. unsigned int tlabel;
  177. if(fixup_get_ivalue(msg, (gparam_p)idx, (int*)&tindex)<0) {
  178. LM_ERR("cannot get transaction index\n");
  179. return -1;
  180. }
  181. if(fixup_get_ivalue(msg, (gparam_p)lbl, (int*)&tlabel)<0) {
  182. LM_ERR("cannot get transaction label\n");
  183. return -1;
  184. }
  185. return ts_append_to(msg, tindex, tlabel, table);
  186. }
  187. /**
  188. *
  189. */
  190. static int w_ts_store(struct sip_msg* msg)
  191. {
  192. return ts_store(msg);
  193. }