mqueue_mod.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "../../parser/parse_param.h"
  34. #include "../../shm_init.h"
  35. #include "mqueue_api.h"
  36. MODULE_VERSION
  37. static int mod_init(void);
  38. static void mod_destroy(void);
  39. static int w_mq_fetch(struct sip_msg* msg, char* mq, char* str2);
  40. static int w_mq_add(struct sip_msg* msg, char* mq, char* key, char* val);
  41. static int w_mq_pv_free(struct sip_msg* msg, char* mq, char* str2);
  42. int mq_param(modparam_t type, void *val);
  43. static int fixup_mq_add(void** param, int param_no);
  44. static pv_export_t mod_pvs[] = {
  45. { {"mqk", sizeof("mqk")-1}, PVT_OTHER, pv_get_mqk, 0,
  46. pv_parse_mqk_name, 0, 0, 0 },
  47. { {"mqv", sizeof("mqv")-1}, PVT_OTHER, pv_get_mqv, 0,
  48. pv_parse_mqv_name, 0, 0, 0 },
  49. { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
  50. };
  51. static cmd_export_t cmds[]={
  52. {"mq_fetch", (cmd_function)w_mq_fetch, 1, fixup_str_null,
  53. 0, ANY_ROUTE},
  54. {"mq_add", (cmd_function)w_mq_add, 3, fixup_mq_add,
  55. 0, ANY_ROUTE},
  56. {"mq_pv_free", (cmd_function)w_mq_pv_free, 1, fixup_str_null,
  57. 0, ANY_ROUTE},
  58. {0, 0, 0, 0, 0, 0}
  59. };
  60. static param_export_t params[]={
  61. {"mqueue", STR_PARAM|USE_FUNC_PARAM, (void*)mq_param},
  62. {0, 0, 0}
  63. };
  64. struct module_exports exports = {
  65. "mqueue",
  66. DEFAULT_DLFLAGS, /* dlopen flags */
  67. cmds,
  68. params,
  69. 0,
  70. 0, /* exported MI functions */
  71. mod_pvs, /* exported pseudo-variables */
  72. 0, /* extra processes */
  73. mod_init, /* module initialization function */
  74. 0, /* response function */
  75. mod_destroy, /* destroy function */
  76. 0 /* per child init function */
  77. };
  78. /**
  79. * init module function
  80. */
  81. static int mod_init(void)
  82. {
  83. if(!mq_head_defined())
  84. LM_WARN("no mqueue defined\n");
  85. return 0;
  86. }
  87. /**
  88. * destroy module function
  89. */
  90. static void mod_destroy(void)
  91. {
  92. mq_destroy();
  93. }
  94. static int w_mq_fetch(struct sip_msg* msg, char* mq, char* str2)
  95. {
  96. int ret;
  97. ret = mq_head_fetch((str*)mq);
  98. if(ret<0)
  99. return ret;
  100. return 1;
  101. }
  102. static int w_mq_add(struct sip_msg* msg, char* mq, char* key, char* val)
  103. {
  104. str qkey;
  105. str qval;
  106. if(fixup_get_svalue(msg, (gparam_t*)key, &qkey)<0)
  107. {
  108. LM_ERR("cannot get the key\n");
  109. return -1;
  110. }
  111. if(fixup_get_svalue(msg, (gparam_t*)val, &qval)<0)
  112. {
  113. LM_ERR("cannot get the val\n");
  114. return -1;
  115. }
  116. if(mq_item_add((str*)mq, &qkey, &qval)<0)
  117. return -1;
  118. return 1;
  119. }
  120. static int w_mq_pv_free(struct sip_msg* msg, char* mq, char* str2)
  121. {
  122. mq_pv_free((str*)mq);
  123. return 1;
  124. }
  125. int mq_param(modparam_t type, void *val)
  126. {
  127. str mqs;
  128. param_t* params_list = NULL;
  129. param_hooks_t phooks;
  130. param_t *pit=NULL;
  131. str qname = {0, 0};
  132. int msize = 0;
  133. if(val==NULL)
  134. return -1;
  135. if(!shm_initialized())
  136. {
  137. LM_ERR("shm not intialized - cannot define mqueue now\n");
  138. return 0;
  139. }
  140. mqs.s = (char*)val;
  141. mqs.len = strlen(mqs.s);
  142. if(mqs.s[mqs.len-1]==';')
  143. mqs.len--;
  144. if (parse_params(&mqs, CLASS_ANY, &phooks, &params_list)<0)
  145. return -1;
  146. for (pit = params_list; pit; pit=pit->next)
  147. {
  148. if (pit->name.len==4
  149. && strncasecmp(pit->name.s, "name", 4)==0) {
  150. qname = pit->body;
  151. } else if(pit->name.len==4
  152. && strncasecmp(pit->name.s, "size", 4)==0) {
  153. str2sint(&pit->body, &msize);
  154. } else {
  155. LM_ERR("unknown param: %.*s\n", pit->name.len, pit->name.s);
  156. free_params(params_list);
  157. return -1;
  158. }
  159. }
  160. if(qname.len<=0)
  161. {
  162. LM_ERR("mqueue name not defined: %.*s\n", mqs.len, mqs.s);
  163. free_params(params_list);
  164. return -1;
  165. }
  166. if(mq_head_add(&qname, msize)<0)
  167. {
  168. LM_ERR("cannot add mqueue: %.*s\n", mqs.len, mqs.s);
  169. free_params(params_list);
  170. return -1;
  171. }
  172. free_params(params_list);
  173. return 0;
  174. }
  175. static int fixup_mq_add(void** param, int param_no)
  176. {
  177. if(param_no==2 || param_no==3) {
  178. return fixup_spve_null(param, 1);
  179. }
  180. if (param_no != 1) {
  181. LM_ERR("invalid parameter number %d\n", param_no);
  182. return E_UNSPEC;
  183. }
  184. return fixup_str_null(param, 1);
  185. }