corex_mod.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2011 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Permission to use, copy, modify, and distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "../../sr_module.h"
  26. #include "../../dprint.h"
  27. #include "../../ut.h"
  28. #include "corex_lib.h"
  29. #include "corex_rpc.h"
  30. #include "corex_var.h"
  31. #include "corex_nio.h"
  32. MODULE_VERSION
  33. static int nio_intercept = 0;
  34. static int w_append_branch(sip_msg_t *msg, char *su, char *sq);
  35. static int w_send(sip_msg_t *msg, char *su, char *sq);
  36. static int w_send_tcp(sip_msg_t *msg, char *su, char *sq);
  37. static int w_send_data(sip_msg_t *msg, char *suri, char *sdata);
  38. static int w_msg_iflag_set(sip_msg_t *msg, char *pflag, char *p2);
  39. static int w_msg_iflag_reset(sip_msg_t *msg, char *pflag, char *p2);
  40. static int w_msg_iflag_is_set(sip_msg_t *msg, char *pflag, char *p2);
  41. int corex_alias_subdomains_param(modparam_t type, void *val);
  42. static int mod_init(void);
  43. static int child_init(int);
  44. static void mod_destroy(void);
  45. static pv_export_t mod_pvs[] = {
  46. { {"cfg", (sizeof("cfg")-1)}, PVT_OTHER, pv_get_cfg, 0,
  47. pv_parse_cfg_name, 0, 0, 0 },
  48. { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
  49. };
  50. static cmd_export_t cmds[]={
  51. {"append_branch", (cmd_function)w_append_branch, 0, 0,
  52. 0, REQUEST_ROUTE | FAILURE_ROUTE },
  53. {"append_branch", (cmd_function)w_append_branch, 1, fixup_spve_null,
  54. 0, REQUEST_ROUTE | FAILURE_ROUTE },
  55. {"append_branch", (cmd_function)w_append_branch, 2, fixup_spve_spve,
  56. 0, REQUEST_ROUTE | FAILURE_ROUTE },
  57. {"send", (cmd_function)w_send, 0, 0,
  58. 0, REQUEST_ROUTE | FAILURE_ROUTE },
  59. {"send", (cmd_function)w_send, 1, fixup_spve_spve,
  60. 0, REQUEST_ROUTE | FAILURE_ROUTE },
  61. {"send_tcp", (cmd_function)w_send_tcp, 0, 0,
  62. 0, REQUEST_ROUTE | FAILURE_ROUTE },
  63. {"send_tcp", (cmd_function)w_send_tcp, 1, fixup_spve_null,
  64. 0, REQUEST_ROUTE | FAILURE_ROUTE },
  65. {"send_data", (cmd_function)w_send_data, 2, fixup_spve_spve,
  66. 0, ANY_ROUTE },
  67. {"is_incoming", (cmd_function)nio_check_incoming, 0, 0,
  68. 0, ANY_ROUTE },
  69. {"msg_iflag_set", (cmd_function)w_msg_iflag_set, 1, fixup_spve_null,
  70. 0, ANY_ROUTE },
  71. {"msg_iflag_reset", (cmd_function)w_msg_iflag_reset, 1, fixup_spve_null,
  72. 0, ANY_ROUTE },
  73. {"msg_iflag_is_set", (cmd_function)w_msg_iflag_is_set, 1, fixup_spve_null,
  74. 0, ANY_ROUTE },
  75. {0, 0, 0, 0, 0, 0}
  76. };
  77. static param_export_t params[]={
  78. {"alias_subdomains", STR_PARAM|USE_FUNC_PARAM,
  79. (void*)corex_alias_subdomains_param},
  80. {"network_io_intercept", INT_PARAM, &nio_intercept},
  81. {"min_msg_len", INT_PARAM, &nio_min_msg_len},
  82. {"msg_avp", PARAM_STR, &nio_msg_avp_param},
  83. {0, 0, 0}
  84. };
  85. struct module_exports exports = {
  86. "corex",
  87. DEFAULT_DLFLAGS, /* dlopen flags */
  88. cmds,
  89. params,
  90. 0,
  91. 0, /* exported MI functions */
  92. mod_pvs, /* exported pseudo-variables */
  93. 0, /* extra processes */
  94. mod_init, /* module initialization function */
  95. 0, /* response function */
  96. mod_destroy, /* destroy function */
  97. child_init /* per child init function */
  98. };
  99. /**
  100. * init module function
  101. */
  102. static int mod_init(void)
  103. {
  104. if(corex_init_rpc()<0)
  105. {
  106. LM_ERR("failed to register RPC commands\n");
  107. return -1;
  108. }
  109. if(corex_register_check_self()<0)
  110. {
  111. LM_ERR("failed to register check self callback\n");
  112. return -1;
  113. }
  114. if((nio_intercept > 0) && (nio_intercept_init() < 0))
  115. {
  116. LM_ERR("failed to register network io intercept callback\n");
  117. return -1;
  118. }
  119. return 0;
  120. }
  121. /**
  122. * @brief Initialize async module children
  123. */
  124. static int child_init(int rank)
  125. {
  126. if (rank!=PROC_MAIN)
  127. return 0;
  128. return 0;
  129. }
  130. /**
  131. * destroy module function
  132. */
  133. static void mod_destroy(void)
  134. {
  135. }
  136. /**
  137. * config wrapper for append branch
  138. */
  139. static int w_append_branch(sip_msg_t *msg, char *su, char *sq)
  140. {
  141. if(corex_append_branch(msg, (gparam_t*)su, (gparam_t*)sq) < 0)
  142. return -1;
  143. return 1;
  144. }
  145. /**
  146. * config wrapper for send() and send_tcp()
  147. */
  148. static int w_send(sip_msg_t *msg, char *su, char *sq)
  149. {
  150. if(corex_send(msg, (gparam_t*)su, PROTO_UDP) < 0)
  151. return -1;
  152. return 1;
  153. }
  154. static int w_send_tcp(sip_msg_t *msg, char *su, char *sq)
  155. {
  156. if(corex_send(msg, (gparam_t*)su, PROTO_TCP) < 0)
  157. return -1;
  158. return 1;
  159. }
  160. static int w_send_data(sip_msg_t *msg, char *suri, char *sdata)
  161. {
  162. str uri;
  163. str data;
  164. if (fixup_get_svalue(msg, (gparam_t*)suri, &uri))
  165. {
  166. LM_ERR("cannot get the destination parameter\n");
  167. return -1;
  168. }
  169. if (fixup_get_svalue(msg, (gparam_t*)sdata, &data))
  170. {
  171. LM_ERR("cannot get the destination parameter\n");
  172. return -1;
  173. }
  174. if(corex_send_data(&uri, &data) < 0)
  175. return -1;
  176. return 1;
  177. }
  178. int corex_alias_subdomains_param(modparam_t type, void *val)
  179. {
  180. if(val==NULL)
  181. goto error;
  182. return corex_add_alias_subdomains((char*)val);
  183. error:
  184. return -1;
  185. }
  186. typedef struct _msg_iflag_name {
  187. str name;
  188. int value;
  189. } msg_iflag_name_t;
  190. static msg_iflag_name_t _msg_iflag_list[] = {
  191. { str_init("USE_UAC_FROM"), FL_USE_UAC_FROM },
  192. { str_init("USE_UAC_TO"), FL_USE_UAC_TO },
  193. { str_init("UAC_AUTH"), FL_UAC_AUTH },
  194. { {0, 0}, 0 }
  195. };
  196. /**
  197. *
  198. */
  199. static int msg_lookup_flag(str *fname)
  200. {
  201. int i;
  202. for(i=0; _msg_iflag_list[i].name.len>0; i++) {
  203. if(fname->len==_msg_iflag_list[i].name.len
  204. && strncasecmp(_msg_iflag_list[i].name.s, fname->s,
  205. fname->len)==0) {
  206. return _msg_iflag_list[i].value;
  207. }
  208. }
  209. return -1;
  210. }
  211. /**
  212. *
  213. */
  214. static int w_msg_iflag_set(sip_msg_t *msg, char *pflag, char *p2)
  215. {
  216. int fv;
  217. str fname;
  218. if (fixup_get_svalue(msg, (gparam_t*)pflag, &fname))
  219. {
  220. LM_ERR("cannot get the msg flag name parameter\n");
  221. return -1;
  222. }
  223. fv = msg_lookup_flag(&fname);
  224. if(fv==1) {
  225. LM_ERR("unsupported flag name [%.*s]\n", fname.len, fname.s);
  226. return -1;
  227. }
  228. msg->msg_flags |= fv;
  229. return 1;
  230. }
  231. /**
  232. *
  233. */
  234. static int w_msg_iflag_reset(sip_msg_t *msg, char *pflag, char *p2)
  235. {
  236. int fv;
  237. str fname;
  238. if (fixup_get_svalue(msg, (gparam_t*)pflag, &fname))
  239. {
  240. LM_ERR("cannot get the msg flag name parameter\n");
  241. return -1;
  242. }
  243. fv = msg_lookup_flag(&fname);
  244. if(fv<0) {
  245. LM_ERR("unsupported flag name [%.*s]\n", fname.len, fname.s);
  246. return -1;
  247. }
  248. msg->msg_flags &= ~fv;
  249. return 1;
  250. }
  251. /**
  252. *
  253. */
  254. static int w_msg_iflag_is_set(sip_msg_t *msg, char *pflag, char *p2)
  255. {
  256. int fv;
  257. str fname;
  258. if (fixup_get_svalue(msg, (gparam_t*)pflag, &fname))
  259. {
  260. LM_ERR("cannot get the msg flag name parameter\n");
  261. return -1;
  262. }
  263. fv = msg_lookup_flag(&fname);
  264. if(fv<0) {
  265. LM_ERR("unsupported flag name [%.*s]\n", fname.len, fname.s);
  266. return -1;
  267. }
  268. if(msg->msg_flags & fv)
  269. return 1;
  270. return -2;
  271. }