mqueue_mod.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2010 Elena-Ramona Modroiu (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * This file 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. *
  14. * This file is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "../../sr_module.h"
  29. #include "../../dprint.h"
  30. #include "../../ut.h"
  31. #include "../../pvar.h"
  32. #include "../../mod_fix.h"
  33. #include "../../lib/kmi/mi.h"
  34. #include "../../parser/parse_param.h"
  35. #include "../../shm_init.h"
  36. #include "mqueue_api.h"
  37. #include "api.h"
  38. MODULE_VERSION
  39. static int mod_init(void);
  40. static void mod_destroy(void);
  41. static int w_mq_fetch(struct sip_msg* msg, char* mq, char* str2);
  42. static int w_mq_size(struct sip_msg *msg, char *mq, char *str2);
  43. static int w_mq_add(struct sip_msg* msg, char* mq, char* key, char* val);
  44. static int w_mq_pv_free(struct sip_msg* msg, char* mq, char* str2);
  45. int mq_param(modparam_t type, void *val);
  46. static int fixup_mq_add(void** param, int param_no);
  47. static int bind_mq(mq_api_t* api);
  48. static struct mi_root *mq_mi_get_size(struct mi_root *, void *);
  49. static pv_export_t mod_pvs[] = {
  50. { {"mqk", sizeof("mqk")-1}, PVT_OTHER, pv_get_mqk, 0,
  51. pv_parse_mq_name, 0, 0, 0 },
  52. { {"mqv", sizeof("mqv")-1}, PVT_OTHER, pv_get_mqv, 0,
  53. pv_parse_mq_name, 0, 0, 0 },
  54. { {"mq_size", sizeof("mq_size")-1}, PVT_OTHER, pv_get_mq_size, 0,
  55. pv_parse_mq_name, 0, 0, 0 },
  56. { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
  57. };
  58. static mi_export_t mi_cmds[] = {
  59. { "mq_get_size", mq_mi_get_size, 0, 0, 0},
  60. { 0, 0, 0, 0, 0}
  61. };
  62. static cmd_export_t cmds[]={
  63. {"mq_fetch", (cmd_function)w_mq_fetch, 1, fixup_spve_null,
  64. 0, ANY_ROUTE},
  65. {"mq_add", (cmd_function)w_mq_add, 3, fixup_mq_add,
  66. 0, ANY_ROUTE},
  67. {"mq_pv_free", (cmd_function)w_mq_pv_free, 1, fixup_spve_null,
  68. 0, ANY_ROUTE},
  69. {"mq_size", (cmd_function) w_mq_size, 1, fixup_spve_null,
  70. 0, ANY_ROUTE},
  71. {"bind_mq", (cmd_function)bind_mq, 1, 0,
  72. 0, ANY_ROUTE},
  73. {0, 0, 0, 0, 0, 0}
  74. };
  75. static param_export_t params[]={
  76. {"mqueue", PARAM_STRING|USE_FUNC_PARAM, (void*)mq_param},
  77. {0, 0, 0}
  78. };
  79. struct module_exports exports = {
  80. "mqueue",
  81. DEFAULT_DLFLAGS, /* dlopen flags */
  82. cmds,
  83. params,
  84. 0,
  85. mi_cmds, /* exported MI functions */
  86. mod_pvs, /* exported pseudo-variables */
  87. 0, /* extra processes */
  88. mod_init, /* module initialization function */
  89. 0, /* response function */
  90. mod_destroy, /* destroy function */
  91. 0 /* per child init function */
  92. };
  93. /**
  94. * init module function
  95. */
  96. static int mod_init(void)
  97. {
  98. if(!mq_head_defined())
  99. LM_WARN("no mqueue defined\n");
  100. if(register_mi_mod(exports.name, mi_cmds) != 0) {
  101. LM_ERR("failed to register MI commands\n");
  102. return 1;
  103. }
  104. return 0;
  105. }
  106. /**
  107. * destroy module function
  108. */
  109. static void mod_destroy(void)
  110. {
  111. mq_destroy();
  112. }
  113. static int w_mq_fetch(struct sip_msg* msg, char* mq, char* str2)
  114. {
  115. int ret;
  116. str q;
  117. if(fixup_get_svalue(msg, (gparam_t*)mq, &q)<0)
  118. {
  119. LM_ERR("cannot get the queue\n");
  120. return -1;
  121. }
  122. ret = mq_head_fetch(&q);
  123. if(ret<0)
  124. return ret;
  125. return 1;
  126. }
  127. static int w_mq_size(struct sip_msg *msg, char *mq, char *str2)
  128. {
  129. int ret;
  130. str q;
  131. if(fixup_get_svalue(msg, (gparam_t *) mq, &q) < 0) {
  132. LM_ERR("cannot get queue parameter\n");
  133. return -1;
  134. }
  135. ret = _mq_get_csize(&q);
  136. if(ret < 0)
  137. LM_ERR("mqueue %.*s not found\n", q.len, q.s);
  138. if(ret<=0) ret--;
  139. return ret;
  140. }
  141. static int w_mq_add(struct sip_msg* msg, char* mq, char* key, char* val)
  142. {
  143. str q;
  144. str qkey;
  145. str qval;
  146. if(fixup_get_svalue(msg, (gparam_t*)mq, &q)<0)
  147. {
  148. LM_ERR("cannot get the queue\n");
  149. return -1;
  150. }
  151. if(fixup_get_svalue(msg, (gparam_t*)key, &qkey)<0)
  152. {
  153. LM_ERR("cannot get the key\n");
  154. return -1;
  155. }
  156. if(fixup_get_svalue(msg, (gparam_t*)val, &qval)<0)
  157. {
  158. LM_ERR("cannot get the val\n");
  159. return -1;
  160. }
  161. if(mq_item_add(&q, &qkey, &qval)<0)
  162. return -1;
  163. return 1;
  164. }
  165. static int w_mq_pv_free(struct sip_msg* msg, char* mq, char* str2)
  166. {
  167. str q;
  168. if(fixup_get_svalue(msg, (gparam_t*)mq, &q)<0)
  169. {
  170. LM_ERR("cannot get the queue\n");
  171. return -1;
  172. }
  173. mq_pv_free(&q);
  174. return 1;
  175. }
  176. int mq_param(modparam_t type, void *val)
  177. {
  178. str mqs;
  179. param_t* params_list = NULL;
  180. param_hooks_t phooks;
  181. param_t *pit=NULL;
  182. str qname = {0, 0};
  183. int msize = 0;
  184. if(val==NULL)
  185. return -1;
  186. if(!shm_initialized())
  187. {
  188. LM_ERR("shm not intialized - cannot define mqueue now\n");
  189. return 0;
  190. }
  191. mqs.s = (char*)val;
  192. mqs.len = strlen(mqs.s);
  193. if(mqs.s[mqs.len-1]==';')
  194. mqs.len--;
  195. if (parse_params(&mqs, CLASS_ANY, &phooks, &params_list)<0)
  196. return -1;
  197. for (pit = params_list; pit; pit=pit->next)
  198. {
  199. if (pit->name.len==4
  200. && strncasecmp(pit->name.s, "name", 4)==0) {
  201. qname = pit->body;
  202. } else if(pit->name.len==4
  203. && strncasecmp(pit->name.s, "size", 4)==0) {
  204. str2sint(&pit->body, &msize);
  205. } else {
  206. LM_ERR("unknown param: %.*s\n", pit->name.len, pit->name.s);
  207. free_params(params_list);
  208. return -1;
  209. }
  210. }
  211. if(qname.len<=0)
  212. {
  213. LM_ERR("mqueue name not defined: %.*s\n", mqs.len, mqs.s);
  214. free_params(params_list);
  215. return -1;
  216. }
  217. if(mq_head_add(&qname, msize)<0)
  218. {
  219. LM_ERR("cannot add mqueue: %.*s\n", mqs.len, mqs.s);
  220. free_params(params_list);
  221. return -1;
  222. }
  223. free_params(params_list);
  224. return 0;
  225. }
  226. static int fixup_mq_add(void** param, int param_no)
  227. {
  228. if(param_no==1 || param_no==2 || param_no==3) {
  229. return fixup_spve_null(param, 1);
  230. }
  231. LM_ERR("invalid parameter number %d\n", param_no);
  232. return E_UNSPEC;
  233. }
  234. static int bind_mq(mq_api_t* api)
  235. {
  236. if (!api)
  237. return -1;
  238. api->add = mq_item_add;
  239. return 0;
  240. }
  241. /* Return the size of the specified mqueue */
  242. static struct mi_root *mq_mi_get_size(struct mi_root *cmd_tree,
  243. void *param)
  244. {
  245. static struct mi_node *node = NULL, *rpl = NULL;
  246. static struct mi_root *rpl_tree = NULL;
  247. static struct mi_attr *attr = NULL;
  248. str mqueue_name;
  249. int mqueue_sz = 0;
  250. char *p = NULL;
  251. int len = 0;
  252. if((node = cmd_tree->node.kids) == NULL) {
  253. return init_mi_tree(400, MI_MISSING_PARM_S,
  254. MI_MISSING_PARM_LEN);
  255. }
  256. mqueue_name = node->value;
  257. if(mqueue_name.len <= 0 || mqueue_name.s == NULL) {
  258. LM_ERR("bad mqueue name\n");
  259. return init_mi_tree(500, MI_SSTR("bad mqueue name"));
  260. }
  261. mqueue_sz = _mq_get_csize(&mqueue_name);
  262. if(mqueue_sz < 0) {
  263. LM_ERR("no such mqueue\n");
  264. return init_mi_tree(404, MI_SSTR("no such mqueue"));
  265. }
  266. rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
  267. if(rpl_tree == NULL)
  268. return 0;
  269. rpl = &rpl_tree->node;
  270. node = add_mi_node_child(rpl, MI_DUP_VALUE, "mqueue", strlen("mqueue"),
  271. NULL, 0);
  272. if(node == NULL) {
  273. free_mi_tree(rpl_tree);
  274. return NULL;
  275. }
  276. attr = add_mi_attr(node, MI_DUP_VALUE, "name", strlen("name"),
  277. mqueue_name.s, mqueue_name.len);
  278. if(attr == NULL) goto error;
  279. p = int2str((unsigned long) mqueue_sz, &len);
  280. attr = add_mi_attr(node, MI_DUP_VALUE, "size", strlen("size"),
  281. p, len);
  282. if(attr == NULL) goto error;
  283. return rpl_tree;
  284. error:
  285. free_mi_tree(rpl_tree);
  286. return NULL;
  287. }