ndb_mongodb_mod.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**
  2. * Copyright (C) 2014 Daniel-Constantin Mierla (asipto.com)
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio 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. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <stdlib.h>
  24. #include <ctype.h>
  25. #include "../../sr_module.h"
  26. #include "../../mem/mem.h"
  27. #include "../../dprint.h"
  28. #include "../../mod_fix.h"
  29. #include "../../trim.h"
  30. #include "mongodb_client.h"
  31. MODULE_VERSION
  32. /** parameters */
  33. int mongodb_srv_param(modparam_t type, void *val);
  34. static int w_mongodb_find(sip_msg_t* msg, char* ssrv, char *sdname, char *scname,
  35. char* scmd, char* sres);
  36. static int w_mongodb_cmd_simple(sip_msg_t* msg, char* ssrv, char *sdname, char *scname,
  37. char* scmd, char* sres);
  38. static int w_mongodb_cmd(sip_msg_t* msg, char* ssrv, char *sdname, char *scname,
  39. char* scmd, char* sres);
  40. static int fixup_mongodb_cmd(void** param, int param_no);
  41. static int w_mongodb_free_reply(struct sip_msg* msg, char* res);
  42. static int w_mongodb_next_reply(struct sip_msg* msg, char* res);
  43. static void mod_destroy(void);
  44. static int child_init(int rank);
  45. static int pv_get_mongodb(struct sip_msg *msg, pv_param_t *param,
  46. pv_value_t *res);
  47. static int pv_parse_mongodb_name(pv_spec_p sp, str *in);
  48. static pv_export_t mod_pvs[] = {
  49. { {"mongodb", sizeof("mongodb")-1}, PVT_OTHER, pv_get_mongodb, 0,
  50. pv_parse_mongodb_name, 0, 0, 0 },
  51. { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
  52. };
  53. static cmd_export_t cmds[]={
  54. {"mongodb_find", (cmd_function)w_mongodb_find, 5, fixup_mongodb_cmd,
  55. 0, ANY_ROUTE},
  56. {"mongodb_cmd_simple", (cmd_function)w_mongodb_cmd_simple, 5, fixup_mongodb_cmd,
  57. 0, ANY_ROUTE},
  58. {"mongodb_cmd", (cmd_function)w_mongodb_cmd, 5, fixup_mongodb_cmd,
  59. 0, ANY_ROUTE},
  60. {"mongodb_free", (cmd_function)w_mongodb_free_reply, 1, fixup_spve_null,
  61. 0, ANY_ROUTE},
  62. {"mongodb_next", (cmd_function)w_mongodb_next_reply, 1, fixup_spve_null,
  63. 0, ANY_ROUTE},
  64. {0, 0, 0, 0, 0, 0}
  65. };
  66. static param_export_t params[]={
  67. {"server", PARAM_STRING|USE_FUNC_PARAM, (void*)mongodb_srv_param},
  68. {0, 0, 0}
  69. };
  70. struct module_exports exports = {
  71. "ndb_mongodb",
  72. DEFAULT_DLFLAGS, /* dlopen flags */
  73. cmds,
  74. params,
  75. 0,
  76. 0, /* exported MI functions */
  77. mod_pvs, /* exported pseudo-variables */
  78. 0, /* extra processes */
  79. 0, /* module initialization function */
  80. 0, /* response function */
  81. mod_destroy, /* destroy function */
  82. child_init /* per child init function */
  83. };
  84. /* each child get a new connection to the database */
  85. static int child_init(int rank)
  86. {
  87. /* skip child init for non-worker process ranks */
  88. if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  89. return 0;
  90. if(mongodbc_init()<0)
  91. {
  92. LM_ERR("failed to initialize mongodb connections\n");
  93. return -1;
  94. }
  95. return 0;
  96. }
  97. /**
  98. *
  99. */
  100. static void mod_destroy(void)
  101. {
  102. LM_DBG("cleaning up\n");
  103. mongodbc_destroy();
  104. }
  105. /**
  106. *
  107. */
  108. static int w_mongodb_do_cmd(sip_msg_t* msg, char* ssrv, char *sdname, char *scname,
  109. char* scmd, char* sres, int ctype)
  110. {
  111. int ret;
  112. str s[5];
  113. if(fixup_get_svalue(msg, (gparam_t*)ssrv, &s[0])!=0)
  114. {
  115. LM_ERR("no mongodb server name\n");
  116. return -1;
  117. }
  118. if(fixup_get_svalue(msg, (gparam_t*)sdname, &s[1])!=0)
  119. {
  120. LM_ERR("no mongodb database name\n");
  121. return -1;
  122. }
  123. if(fixup_get_svalue(msg, (gparam_t*)scname, &s[2])!=0)
  124. {
  125. LM_ERR("no mongodb collection name\n");
  126. return -1;
  127. }
  128. if(fixup_get_svalue(msg, (gparam_t*)scmd, &s[3])!=0)
  129. {
  130. LM_ERR("no mongodb command\n");
  131. return -1;
  132. }
  133. if(fixup_get_svalue(msg, (gparam_t*)sres, &s[4])!=0)
  134. {
  135. LM_ERR("no mongodb reply name\n");
  136. return -1;
  137. }
  138. if(ctype==0) {
  139. ret = mongodbc_exec_simple(&s[0], &s[1], &s[2], &s[3], &s[4]);
  140. } else if(ctype==1) {
  141. ret = mongodbc_exec(&s[0], &s[1], &s[2], &s[3], &s[4]);
  142. } else {
  143. ret = mongodbc_find(&s[0], &s[1], &s[2], &s[3], &s[4]);
  144. }
  145. if(ret<0)
  146. return -1;
  147. return 1;
  148. }
  149. /**
  150. *
  151. */
  152. static int w_mongodb_cmd_simple(sip_msg_t* msg, char* ssrv, char *sdname, char *scname,
  153. char* scmd, char* sres)
  154. {
  155. return w_mongodb_do_cmd(msg, ssrv, sdname, scname, scmd, sres, 0);
  156. }
  157. /**
  158. *
  159. */
  160. static int w_mongodb_cmd(sip_msg_t* msg, char* ssrv, char *sdname, char *scname,
  161. char* scmd, char* sres)
  162. {
  163. return w_mongodb_do_cmd(msg, ssrv, sdname, scname, scmd, sres, 1);
  164. }
  165. /**
  166. *
  167. */
  168. static int w_mongodb_find(sip_msg_t* msg, char* ssrv, char *sdname, char *scname,
  169. char* scmd, char* sres)
  170. {
  171. return w_mongodb_do_cmd(msg, ssrv, sdname, scname, scmd, sres, 2);
  172. }
  173. /**
  174. *
  175. */
  176. static int fixup_mongodb_cmd(void** param, int param_no)
  177. {
  178. return fixup_spve_null(param, 1);
  179. }
  180. /**
  181. *
  182. */
  183. static int w_mongodb_free_reply(struct sip_msg* msg, char* res)
  184. {
  185. str name;
  186. if(fixup_get_svalue(msg, (gparam_t*)res, &name)!=0)
  187. {
  188. LM_ERR("no mongodb reply name\n");
  189. return -1;
  190. }
  191. if(mongodbc_free_reply(&name)<0)
  192. return -1;
  193. return 1;
  194. }
  195. /**
  196. *
  197. */
  198. static int w_mongodb_next_reply(struct sip_msg* msg, char* res)
  199. {
  200. str name;
  201. if(fixup_get_svalue(msg, (gparam_t*)res, &name)!=0)
  202. {
  203. LM_ERR("no mongodb reply name\n");
  204. return -1;
  205. }
  206. if(mongodbc_next_reply(&name)<0)
  207. return -1;
  208. return 1;;
  209. }
  210. /**
  211. *
  212. */
  213. int mongodb_srv_param(modparam_t type, void *val)
  214. {
  215. return mongodbc_add_server((char*)val);
  216. }
  217. /**
  218. *
  219. */
  220. static int pv_parse_mongodb_name(pv_spec_p sp, str *in)
  221. {
  222. mongodbc_pv_t *rpv=NULL;
  223. str pvs;
  224. int i;
  225. if(in->s==NULL || in->len<=0)
  226. return -1;
  227. rpv = (mongodbc_pv_t*)pkg_malloc(sizeof(mongodbc_pv_t));
  228. if(rpv==NULL)
  229. return -1;
  230. memset(rpv, 0, sizeof(mongodbc_pv_t));
  231. pvs = *in;
  232. trim(&pvs);
  233. rpv->rname.s = pvs.s;
  234. for(i=0; i<pvs.len-2; i++)
  235. {
  236. if(isspace(pvs.s[i]) || pvs.s[i]=='=') {
  237. rpv->rname.len = i;
  238. break;
  239. }
  240. }
  241. rpv->rname.len = i;
  242. if(rpv->rname.len==0)
  243. goto error_var;
  244. while(i<pvs.len-2 && isspace(pvs.s[i]))
  245. i++;
  246. if(pvs.s[i]!='=')
  247. goto error_var;
  248. if(pvs.s[i+1]!='>')
  249. goto error_var;
  250. i += 2;
  251. while(i<pvs.len && isspace(pvs.s[i]))
  252. i++;
  253. if(i>=pvs.len)
  254. goto error_key;
  255. rpv->rkey.s = pvs.s + i;
  256. rpv->rkey.len = pvs.len - i;
  257. if(rpv->rkey.len>=5 && strncmp(rpv->rkey.s, "value", 5)==0) {
  258. rpv->rkeyid = 1;
  259. } else if(rpv->rkey.len>=4 && strncmp(rpv->rkey.s, "type", 4)==0) {
  260. rpv->rkeyid = 0;
  261. } else if(rpv->rkey.len==4 && strncmp(rpv->rkey.s, "info", 4)==0) {
  262. rpv->rkeyid = 2;
  263. } else if(rpv->rkey.len==4 && strncmp(rpv->rkey.s, "size", 4)==0) {
  264. rpv->rkeyid = 3;
  265. } else {
  266. goto error_key;
  267. }
  268. sp->pvp.pvn.u.dname = (void*)rpv;
  269. sp->pvp.pvn.type = PV_NAME_OTHER;
  270. return 0;
  271. error_var:
  272. LM_ERR("invalid var spec [%.*s]\n", in->len, in->s);
  273. pkg_free(rpv);
  274. return -1;
  275. error_key:
  276. LM_ERR("invalid key spec in [%.*s]\n", in->len, in->s);
  277. pkg_free(rpv);
  278. return -1;
  279. }
  280. /**
  281. *
  282. */
  283. static int pv_get_mongodb(struct sip_msg *msg, pv_param_t *param,
  284. pv_value_t *res)
  285. {
  286. mongodbc_pv_t *rpv;
  287. rpv = (mongodbc_pv_t*)param->pvn.u.dname;
  288. if(rpv->reply==NULL)
  289. {
  290. rpv->reply = mongodbc_get_reply(&rpv->rname);
  291. if(rpv->reply==NULL)
  292. return pv_get_null(msg, param, res);
  293. }
  294. switch(rpv->rkeyid) {
  295. case 1:
  296. /* value */
  297. if(rpv->reply->jsonrpl.s==NULL)
  298. return pv_get_null(msg, param, res);
  299. return pv_get_strval(msg, param, res, &rpv->reply->jsonrpl);
  300. case 2:
  301. /* info */
  302. return pv_get_null(msg, param, res);
  303. case 3:
  304. /* size */
  305. return pv_get_null(msg, param, res);
  306. case 0:
  307. /* type */
  308. return pv_get_sintval(msg, param, res, 0);
  309. default:
  310. /* We do nothing. */
  311. return pv_get_null(msg, param, res);
  312. }
  313. }