kex_mod.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * Copyright (C) 2009
  3. *
  4. * This file is part of SIP-Router.org, a free SIP server.
  5. *
  6. * SIP-Router is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*!
  21. * @defgroup kex KEX :: Kamailio Extensions
  22. */
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include "../../sr_module.h"
  27. #include "../../dprint.h"
  28. #include "../../forward.h"
  29. #include "../../flags.h"
  30. #include "../../dset.h"
  31. #include "../../mod_fix.h"
  32. #include "../../parser/parse_uri.h"
  33. #include "../../lib/srutils/sruid.h"
  34. #include "flags.h"
  35. #include "km_core.h"
  36. #include "mi_core.h"
  37. #include "core_stats.h"
  38. #include "pkg_stats.h"
  39. MODULE_VERSION
  40. /** parameters */
  41. /** module functions */
  42. int w_is_myself(struct sip_msg *msg, char *uri, str *s2);
  43. int w_setdebug(struct sip_msg *msg, char *level, str *s2);
  44. int w_resetdebug(struct sip_msg *msg, char *uri, str *s2);
  45. static int mod_init(void);
  46. static int child_init(int rank);
  47. static void destroy(void);
  48. static sruid_t _kex_sruid;
  49. static int pv_get_sruid_val(struct sip_msg *msg, pv_param_t *param,
  50. pv_value_t *res);
  51. static pv_export_t mod_pvs[] = {
  52. { {"sruid", sizeof("sruid")-1}, PVT_OTHER, pv_get_sruid_val, 0,
  53. 0, 0, 0, 0 },
  54. { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
  55. };
  56. static cmd_export_t cmds[]={
  57. {"setsflag", (cmd_function)w_setsflag, 1,fixup_igp_null,
  58. 0, ANY_ROUTE },
  59. {"resetsflag", (cmd_function)w_resetsflag, 1,fixup_igp_null,
  60. 0, ANY_ROUTE },
  61. {"issflagset", (cmd_function)w_issflagset, 1,fixup_igp_null,
  62. 0, ANY_ROUTE },
  63. {"setbflag", (cmd_function)w_setbflag, 1,fixup_igp_null,
  64. 0, ANY_ROUTE },
  65. {"setbflag", (cmd_function)w_setbflag, 2,fixup_igp_igp,
  66. 0, ANY_ROUTE },
  67. {"resetbflag", (cmd_function)w_resetbflag, 1,fixup_igp_null,
  68. 0, ANY_ROUTE },
  69. {"resetbflag", (cmd_function)w_resetbflag, 2,fixup_igp_igp,
  70. 0, ANY_ROUTE },
  71. {"isbflagset", (cmd_function)w_isbflagset, 1,fixup_igp_null,
  72. 0, ANY_ROUTE },
  73. {"isbflagset", (cmd_function)w_isbflagset, 2,fixup_igp_igp,
  74. 0, ANY_ROUTE },
  75. {"setdsturi", (cmd_function)w_setdsturi, 1, 0,
  76. 0, ANY_ROUTE },
  77. {"resetdsturi", (cmd_function)w_resetdsturi, 0, 0,
  78. 0, ANY_ROUTE },
  79. {"isdsturiset", (cmd_function)w_isdsturiset, 0, 0,
  80. 0, ANY_ROUTE },
  81. {"pv_printf", (cmd_function)w_pv_printf, 2, pv_printf_fixup,
  82. 0, ANY_ROUTE },
  83. {"avp_printf", (cmd_function)w_pv_printf, 2, pv_printf_fixup,
  84. 0, ANY_ROUTE },
  85. {"is_myself", (cmd_function)w_is_myself, 1, fixup_spve_null,
  86. 0, ANY_ROUTE },
  87. {"setdebug", (cmd_function)w_setdebug, 1, fixup_igp_null,
  88. 0, ANY_ROUTE },
  89. {"resetdebug", (cmd_function)w_resetdebug, 0, 0,
  90. 0, ANY_ROUTE },
  91. {0,0,0,0,0,0}
  92. };
  93. static param_export_t params[]={
  94. {0,0,0}
  95. };
  96. /** module exports */
  97. struct module_exports exports= {
  98. "kex",
  99. DEFAULT_DLFLAGS, /* dlopen flags */
  100. cmds,
  101. params,
  102. 0, /* exported statistics */
  103. 0, /* exported MI functions */
  104. mod_pvs, /* exported pseudo-variables */
  105. 0, /* extra processes */
  106. mod_init, /* module initialization function */
  107. 0,
  108. (destroy_function) destroy,
  109. child_init /* per-child init function */
  110. };
  111. /**
  112. * init module function
  113. */
  114. static int mod_init(void)
  115. {
  116. if(sruid_init(&_kex_sruid, '-', NULL, 0)<0)
  117. return -1;
  118. if(init_mi_core()<0)
  119. return -1;
  120. #ifdef STATISTICS
  121. if(register_core_stats()<0)
  122. return -1;
  123. if(register_mi_stats()<0)
  124. return -1;
  125. #endif
  126. register_pkg_proc_stats();
  127. pkg_proc_stats_init_rpc();
  128. return 0;
  129. }
  130. /**
  131. *
  132. */
  133. static int child_init(int rank)
  134. {
  135. LM_DBG("rank is (%d)\n", rank);
  136. if(sruid_init(&_kex_sruid, '-', NULL, 0)<0)
  137. return -1;
  138. if (rank==PROC_INIT)
  139. return pkg_proc_stats_init();
  140. return pkg_proc_stats_myinit(rank);
  141. }
  142. /**
  143. * destroy function
  144. */
  145. static void destroy(void)
  146. {
  147. pkg_proc_stats_destroy();
  148. return;
  149. }
  150. /**
  151. *
  152. */
  153. int w_is_myself(struct sip_msg *msg, char *uri, str *s2)
  154. {
  155. int ret;
  156. str suri;
  157. struct sip_uri puri;
  158. if(fixup_get_svalue(msg, (gparam_p)uri, &suri)!=0)
  159. {
  160. LM_ERR("cannot get the URI parameter\n");
  161. return -1;
  162. }
  163. if(suri.len>4 && (strncmp(suri.s, "sip:", 4)==0
  164. || strncmp(suri.s, "sips:", 5)==0))
  165. {
  166. if(parse_uri(suri.s, suri.len, &puri)!=0)
  167. {
  168. LM_ERR("failed to parse uri [%.*s]\n", suri.len, suri.s);
  169. return -1;
  170. }
  171. ret = check_self(&puri.host, (puri.port.s)?puri.port_no:0,
  172. (puri.transport_val.s)?puri.proto:0);
  173. } else {
  174. ret = check_self(&suri, 0, 0);
  175. }
  176. if(ret!=1)
  177. return -1;
  178. return 1;
  179. }
  180. int w_setdebug(struct sip_msg *msg, char *level, str *s2)
  181. {
  182. int lval=0;
  183. if(fixup_get_ivalue(msg, (gparam_p)level, &lval)!=0)
  184. {
  185. LM_ERR("no debug level value\n");
  186. return -1;
  187. }
  188. set_local_debug_level(lval);
  189. return 1;
  190. }
  191. int w_resetdebug(struct sip_msg *msg, char *uri, str *s2)
  192. {
  193. reset_local_debug_level();
  194. return 1;
  195. }
  196. static int pv_get_sruid_val(struct sip_msg *msg, pv_param_t *param,
  197. pv_value_t *res)
  198. {
  199. if(res==NULL)
  200. return -1;
  201. if(sruid_next(&_kex_sruid)<0)
  202. return pv_get_null(msg, param, res);
  203. return pv_get_strval(msg, param, res, &_kex_sruid.uid);
  204. }