mongodb_client.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 <string.h>
  25. #include <sys/time.h>
  26. #include <stdarg.h>
  27. #include "../../mem/mem.h"
  28. #include "../../dprint.h"
  29. #include "../../hashes.h"
  30. #include "../../ut.h"
  31. #include "mongodb_client.h"
  32. static mongodbc_server_t *_mongodbc_srv_list=NULL;
  33. static mongodbc_reply_t *_mongodbc_rpl_list=NULL;
  34. void mongodbc_destroy_reply(mongodbc_reply_t *rpl);
  35. /**
  36. *
  37. */
  38. int mongodbc_init(void)
  39. {
  40. mongodbc_server_t *rsrv=NULL;
  41. if(_mongodbc_srv_list==NULL)
  42. {
  43. LM_ERR("no mongodb servers defined\n");
  44. return -1;
  45. }
  46. for(rsrv=_mongodbc_srv_list; rsrv; rsrv=rsrv->next)
  47. {
  48. if(rsrv->uri==NULL || rsrv->uri->len<=0) {
  49. LM_ERR("no uri for server: %.*s\n",
  50. rsrv->sname->len, rsrv->sname->s);
  51. return -1;
  52. }
  53. rsrv->client = mongoc_client_new (rsrv->uri->s);
  54. if(rsrv->client==NULL) {
  55. LM_ERR("failed to connect to: %.*s (%.*s)\n",
  56. rsrv->sname->len, rsrv->sname->s,
  57. rsrv->uri->len, rsrv->uri->s);
  58. return -1;
  59. }
  60. }
  61. return 0;
  62. }
  63. /**
  64. *
  65. */
  66. int mongodbc_destroy(void)
  67. {
  68. mongodbc_reply_t *rpl, *next_rpl;
  69. mongodbc_server_t *rsrv=NULL;
  70. mongodbc_server_t *rsrv1=NULL;
  71. rpl = _mongodbc_rpl_list;
  72. while(rpl != NULL)
  73. {
  74. next_rpl = rpl->next;
  75. mongodbc_destroy_reply(rpl);
  76. pkg_free(rpl);
  77. rpl = next_rpl;
  78. }
  79. _mongodbc_rpl_list = NULL;
  80. if(_mongodbc_srv_list==NULL)
  81. return -1;
  82. rsrv=_mongodbc_srv_list;
  83. while(rsrv!=NULL)
  84. {
  85. rsrv1 = rsrv;
  86. rsrv=rsrv->next;
  87. if(rsrv1->client!=NULL)
  88. mongoc_client_destroy(rsrv1->client);
  89. free_params(rsrv1->attrs);
  90. pkg_free(rsrv1);
  91. }
  92. _mongodbc_srv_list = NULL;
  93. return 0;
  94. }
  95. /**
  96. *
  97. */
  98. int mongodbc_add_server(char *spec)
  99. {
  100. param_t *pit=NULL;
  101. param_hooks_t phooks;
  102. mongodbc_server_t *rsrv=NULL;
  103. str s;
  104. s.s = spec;
  105. s.len = strlen(spec);
  106. if(s.s[s.len-1]==';')
  107. s.len--;
  108. if (parse_params(&s, CLASS_ANY, &phooks, &pit)<0)
  109. {
  110. LM_ERR("failed parsing params value\n");
  111. goto error;
  112. }
  113. rsrv = (mongodbc_server_t*)pkg_malloc(sizeof(mongodbc_server_t));
  114. if(rsrv==NULL)
  115. {
  116. LM_ERR("no more pkg\n");
  117. goto error;
  118. }
  119. memset(rsrv, 0, sizeof(mongodbc_server_t));
  120. rsrv->attrs = pit;
  121. for (pit = rsrv->attrs; pit; pit=pit->next)
  122. {
  123. if(pit->name.len==4 && strncmp(pit->name.s, "name", 4)==0) {
  124. rsrv->sname = &pit->body;
  125. rsrv->hname = get_hash1_raw(rsrv->sname->s, rsrv->sname->len);
  126. } else if(pit->name.len==3 && strncmp(pit->name.s, "uri", 3)==0) {
  127. rsrv->uri = &pit->body;
  128. if(rsrv->uri->s[rsrv->uri->len]!='\0') rsrv->uri->s[rsrv->uri->len]='\0';
  129. }
  130. }
  131. if(rsrv->sname==NULL || rsrv->uri==NULL)
  132. {
  133. LM_ERR("no server name or uri\n");
  134. goto error;
  135. }
  136. LM_DBG("added server[%.*s]=%.*s\n",
  137. rsrv->sname->len, rsrv->sname->s,
  138. rsrv->uri->len, rsrv->uri->s);
  139. rsrv->next = _mongodbc_srv_list;
  140. _mongodbc_srv_list = rsrv;
  141. return 0;
  142. error:
  143. if(pit!=NULL)
  144. free_params(pit);
  145. if(rsrv!=NULL)
  146. pkg_free(rsrv);
  147. return -1;
  148. }
  149. /**
  150. *
  151. */
  152. mongodbc_server_t *mongodbc_get_server(str *name)
  153. {
  154. mongodbc_server_t *rsrv=NULL;
  155. unsigned int hname;
  156. hname = get_hash1_raw(name->s, name->len);
  157. rsrv=_mongodbc_srv_list;
  158. while(rsrv!=NULL)
  159. {
  160. if(rsrv->hname==hname && rsrv->sname->len==name->len
  161. && strncmp(rsrv->sname->s, name->s, name->len)==0)
  162. return rsrv;
  163. rsrv=rsrv->next;
  164. }
  165. return NULL;
  166. }
  167. /**
  168. *
  169. */
  170. int mongodbc_reconnect_server(mongodbc_server_t *rsrv)
  171. {
  172. mongoc_init();
  173. if(rsrv->client!=NULL)
  174. mongoc_client_destroy(rsrv->client);
  175. rsrv->client = mongoc_client_new (rsrv->uri->s);
  176. if(rsrv->client!=NULL) {
  177. LM_ERR("failed to connect to: %.*s (%.*s)\n",
  178. rsrv->sname->len, rsrv->sname->s,
  179. rsrv->uri->len, rsrv->uri->s);
  180. return -1;
  181. }
  182. return 0;
  183. }
  184. /**
  185. *
  186. */
  187. int mongodbc_exec_cmd(str *srv, str *dname, str *cname, str *cmd, str *res, int emode)
  188. {
  189. mongodbc_server_t *rsrv=NULL;
  190. mongodbc_reply_t *rpl=NULL;
  191. bson_error_t error;
  192. bson_t command;
  193. bson_t reply;
  194. const bson_t *cdoc;
  195. char c;
  196. int ret;
  197. if(srv==NULL || cmd==NULL || res==NULL)
  198. {
  199. LM_ERR("invalid parameters");
  200. goto error_exec;
  201. }
  202. if(srv->len==0 || res->len==0 || cmd->len==0)
  203. {
  204. LM_ERR("invalid parameters");
  205. goto error_exec;
  206. }
  207. rsrv = mongodbc_get_server(srv);
  208. if(rsrv==NULL)
  209. {
  210. LM_ERR("no mongodb server found: %.*s\n", srv->len, srv->s);
  211. goto error_exec;
  212. }
  213. if(rsrv->client==NULL)
  214. {
  215. if(mongodbc_reconnect_server(rsrv)<0) {
  216. LM_ERR("no mongodb context for server: %.*s\n", srv->len, srv->s);
  217. goto error_exec;
  218. }
  219. }
  220. rpl = mongodbc_get_reply(res);
  221. if(rpl==NULL)
  222. {
  223. LM_ERR("no mongodb reply id found: %.*s\n", res->len, res->s);
  224. goto error_exec;
  225. }
  226. mongodbc_destroy_reply(rpl);
  227. rpl->collection = mongoc_client_get_collection (rsrv->client, dname->s, cname->s);
  228. LM_DBG("trying to execute: [[%.*s]]\n", cmd->len, cmd->s);
  229. c = cmd->s[cmd->len];
  230. cmd->s[cmd->len] = '\0';
  231. if(!bson_init_from_json(&command, cmd->s, cmd->len, &error)) {
  232. cmd->s[cmd->len] = c;
  233. LM_ERR("Failed to run command: %s\n", error.message);
  234. goto error_exec;
  235. }
  236. cmd->s[cmd->len] = c;
  237. if(emode==0) {
  238. ret = mongoc_collection_command_simple (rpl->collection, &command, NULL, &reply, &error);
  239. if (!ret) {
  240. LM_ERR("Failed to run command: %s\n", error.message);
  241. bson_destroy (&command);
  242. goto error_exec;
  243. }
  244. bson_destroy (&command);
  245. rpl->jsonrpl.s = bson_as_json (&reply, NULL);
  246. rpl->jsonrpl.len = (rpl->jsonrpl.s)?strlen(rpl->jsonrpl.s):0;
  247. bson_destroy (&reply);
  248. } else {
  249. if(emode==1) {
  250. rpl->cursor = mongoc_collection_command (rpl->collection,
  251. MONGOC_QUERY_NONE,
  252. 0,
  253. 0,
  254. 0,
  255. &command,
  256. NULL,
  257. 0);
  258. } else {
  259. rpl->cursor = mongoc_collection_find (rpl->collection,
  260. MONGOC_QUERY_NONE,
  261. 0,
  262. 0,
  263. 0,
  264. &command,
  265. NULL,
  266. NULL);
  267. }
  268. bson_destroy (&command);
  269. if(rpl->cursor==NULL) {
  270. LM_ERR("Failed to get cursor: %s\n", error.message);
  271. goto error_exec;
  272. }
  273. if (!mongoc_cursor_next (rpl->cursor, &cdoc)) {
  274. if (mongoc_cursor_error (rpl->cursor, &error)) {
  275. LM_ERR("Cursor failure: %s\n", error.message);
  276. }
  277. goto error_exec;
  278. }
  279. rpl->jsonrpl.s = bson_as_json (cdoc, NULL);
  280. rpl->jsonrpl.len = (rpl->jsonrpl.s)?strlen(rpl->jsonrpl.s):0;
  281. }
  282. LM_DBG("command result: [[%s]]\n", (rpl->jsonrpl.s)?rpl->jsonrpl.s:"<null>");
  283. return 0;
  284. error_exec:
  285. return -1;
  286. }
  287. /**
  288. *
  289. */
  290. int mongodbc_exec_simple(str *srv, str *dname, str *cname, str *cmd, str *res)
  291. {
  292. return mongodbc_exec_cmd(srv, dname, cname, cmd, res, 0);
  293. }
  294. /**
  295. *
  296. */
  297. int mongodbc_exec(str *srv, str *dname, str *cname, str *cmd, str *res)
  298. {
  299. return mongodbc_exec_cmd(srv, dname, cname, cmd, res, 1);
  300. }
  301. /**
  302. *
  303. */
  304. int mongodbc_find(str *srv, str *dname, str *cname, str *cmd, str *res)
  305. {
  306. return mongodbc_exec_cmd(srv, dname, cname, cmd, res, 2);
  307. }
  308. /**
  309. *
  310. */
  311. mongodbc_reply_t *mongodbc_get_reply(str *name)
  312. {
  313. mongodbc_reply_t *rpl;
  314. unsigned int hid;
  315. hid = get_hash1_raw(name->s, name->len);
  316. for(rpl=_mongodbc_rpl_list; rpl; rpl=rpl->next) {
  317. if(rpl->hname==hid && rpl->rname.len==name->len
  318. && strncmp(rpl->rname.s, name->s, name->len)==0)
  319. return rpl;
  320. }
  321. /* not found - add a new one */
  322. rpl = (mongodbc_reply_t*)pkg_malloc(sizeof(mongodbc_reply_t));
  323. if(rpl==NULL)
  324. {
  325. LM_ERR("no more pkg\n");
  326. return NULL;
  327. }
  328. memset(rpl, 0, sizeof(mongodbc_reply_t));
  329. rpl->hname = hid;
  330. rpl->rname.s = (char*)pkg_malloc(name->len+1);
  331. if(rpl->rname.s==NULL)
  332. {
  333. LM_ERR("no more pkg.\n");
  334. pkg_free(rpl);
  335. return NULL;
  336. }
  337. strncpy(rpl->rname.s, name->s, name->len);
  338. rpl->rname.len = name->len;
  339. rpl->rname.s[name->len] = '\0';
  340. rpl->next = _mongodbc_rpl_list;
  341. _mongodbc_rpl_list = rpl;
  342. return rpl;
  343. }
  344. /**
  345. *
  346. */
  347. void mongodbc_destroy_reply(mongodbc_reply_t *rpl)
  348. {
  349. if(rpl->jsonrpl.s!=NULL) {
  350. bson_free(rpl->jsonrpl.s);
  351. rpl->jsonrpl.s = NULL;
  352. rpl->jsonrpl.len = 0;
  353. }
  354. if(rpl->cursor) {
  355. mongoc_cursor_destroy (rpl->cursor);
  356. rpl->cursor = NULL;
  357. }
  358. if(rpl->collection) {
  359. mongoc_collection_destroy (rpl->collection);
  360. rpl->collection = NULL;
  361. }
  362. }
  363. /**
  364. *
  365. */
  366. int mongodbc_free_reply(str *name)
  367. {
  368. mongodbc_reply_t *rpl;
  369. unsigned int hid;
  370. if(name==NULL || name->len==0) {
  371. LM_ERR("invalid parameters");
  372. return -1;
  373. }
  374. hid = get_hash1_raw(name->s, name->len);
  375. rpl = _mongodbc_rpl_list;
  376. while(rpl) {
  377. if(rpl->hname==hid && rpl->rname.len==name->len
  378. && strncmp(rpl->rname.s, name->s, name->len)==0) {
  379. mongodbc_destroy_reply(rpl);
  380. return 0;
  381. }
  382. rpl = rpl->next;
  383. }
  384. /* reply entry not found. */
  385. return -1;
  386. }
  387. /**
  388. *
  389. */
  390. int mongodbc_next_reply(str *name)
  391. {
  392. mongodbc_reply_t *rpl;
  393. unsigned int hid;
  394. bson_error_t error;
  395. const bson_t *cdoc;
  396. if(name==NULL || name->len==0) {
  397. LM_ERR("invalid parameters");
  398. return -1;
  399. }
  400. hid = get_hash1_raw(name->s, name->len);
  401. rpl = _mongodbc_rpl_list;
  402. while(rpl) {
  403. if(rpl->hname==hid && rpl->rname.len==name->len
  404. && strncmp(rpl->rname.s, name->s, name->len)==0) {
  405. break;
  406. }
  407. rpl = rpl->next;
  408. }
  409. if(!rpl) {
  410. /* reply entry not found. */
  411. return -1;
  412. }
  413. if(rpl->cursor==NULL) {
  414. LM_DBG("No active cursor for: %.*s\n", rpl->rname.len, rpl->rname.s);
  415. return -2;
  416. }
  417. if (!mongoc_cursor_next (rpl->cursor, &cdoc)) {
  418. if (mongoc_cursor_error (rpl->cursor, &error)) {
  419. LM_ERR("Cursor failure: %s\n", error.message);
  420. }
  421. return -2;
  422. }
  423. if(rpl->jsonrpl.s) {
  424. bson_free(rpl->jsonrpl.s);
  425. rpl->jsonrpl.s = NULL;
  426. rpl->jsonrpl.len = 0;
  427. }
  428. rpl->jsonrpl.s = bson_as_json (cdoc, NULL);
  429. rpl->jsonrpl.len = (rpl->jsonrpl.s)?strlen(rpl->jsonrpl.s):0;
  430. LM_DBG("next cursor result: [[%s]]\n", (rpl->jsonrpl.s)?rpl->jsonrpl.s:"<null>");
  431. return 0;
  432. }