redis_client.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2011 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio 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. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/time.h>
  28. #include <stdarg.h>
  29. #include "../../mem/mem.h"
  30. #include "../../dprint.h"
  31. #include "../../hashes.h"
  32. #include "../../ut.h"
  33. #include "redis_client.h"
  34. #define redisCommandNR(a...) (int)({ void *__tmp; __tmp = redisCommand(a); if (__tmp) freeReplyObject(__tmp); __tmp ? 0 : -1;})
  35. static redisc_server_t *_redisc_srv_list=NULL;
  36. static redisc_reply_t *_redisc_rpl_list=NULL;
  37. extern int init_without_redis;
  38. /**
  39. *
  40. */
  41. int redisc_init(void)
  42. {
  43. char *addr, *pass, *unix_sock_path = NULL;
  44. unsigned int port, db;
  45. redisc_server_t *rsrv=NULL;
  46. param_t *pit = NULL;
  47. struct timeval tv;
  48. tv.tv_sec = 1;
  49. tv.tv_usec = 0;
  50. if(_redisc_srv_list==NULL)
  51. {
  52. LM_ERR("no redis servers defined\n");
  53. return -1;
  54. }
  55. for(rsrv=_redisc_srv_list; rsrv; rsrv=rsrv->next)
  56. {
  57. addr = "127.0.0.1";
  58. port = 6379;
  59. db = 0;
  60. pass = NULL;
  61. for (pit = rsrv->attrs; pit; pit=pit->next)
  62. {
  63. if(pit->name.len==4 && strncmp(pit->name.s, "unix", 4)==0) {
  64. unix_sock_path = pit->body.s;
  65. unix_sock_path[pit->body.len] = '\0';
  66. } else if(pit->name.len==4 && strncmp(pit->name.s, "addr", 4)==0) {
  67. addr = pit->body.s;
  68. addr[pit->body.len] = '\0';
  69. } else if(pit->name.len==4 && strncmp(pit->name.s, "port", 4)==0) {
  70. if(str2int(&pit->body, &port) < 0)
  71. port = 6379;
  72. } else if(pit->name.len==2 && strncmp(pit->name.s, "db", 2)==0) {
  73. if(str2int(&pit->body, &db) < 0)
  74. db = 0;
  75. } else if(pit->name.len==4 && strncmp(pit->name.s, "pass", 4)==0) {
  76. pass = pit->body.s;
  77. pass[pit->body.len] = '\0';
  78. }
  79. }
  80. if(unix_sock_path != NULL) {
  81. LM_DBG("Connecting to unix socket: %s\n", unix_sock_path);
  82. rsrv->ctxRedis = redisConnectUnixWithTimeout(unix_sock_path, tv);
  83. } else {
  84. rsrv->ctxRedis = redisConnectWithTimeout(addr, port, tv);
  85. }
  86. if(!rsrv->ctxRedis)
  87. goto err;
  88. if (rsrv->ctxRedis->err)
  89. goto err2;
  90. if ((pass != NULL) && redisc_check_auth(rsrv, pass))
  91. goto err2;
  92. if (redisCommandNR(rsrv->ctxRedis, "PING"))
  93. goto err2;
  94. if (redisCommandNR(rsrv->ctxRedis, "SELECT %i", db))
  95. goto err2;
  96. }
  97. return 0;
  98. err2:
  99. if (unix_sock_path != NULL) {
  100. LM_ERR("error communicating with redis server [%.*s] (unix:%s db:%d): %s\n",
  101. rsrv->sname->len, rsrv->sname->s, unix_sock_path, db, rsrv->ctxRedis->errstr);
  102. } else {
  103. LM_ERR("error communicating with redis server [%.*s] (%s:%d/%d): %s\n",
  104. rsrv->sname->len, rsrv->sname->s, addr, port, db, rsrv->ctxRedis->errstr);
  105. }
  106. if (init_without_redis==1)
  107. {
  108. LM_WARN("failed to initialize redis connections, but initializing module anyway.\n");
  109. return 0;
  110. }
  111. return -1;
  112. err:
  113. if (unix_sock_path != NULL) {
  114. LM_ERR("failed to connect to redis server [%.*s] (unix:%s db:%d)\n",
  115. rsrv->sname->len, rsrv->sname->s, unix_sock_path, db);
  116. } else {
  117. LM_ERR("failed to connect to redis server [%.*s] (%s:%d/%d)\n",
  118. rsrv->sname->len, rsrv->sname->s, addr, port, db);
  119. }
  120. if (init_without_redis==1)
  121. {
  122. LM_WARN("failed to initialize redis connections, but initializing module anyway.\n");
  123. return 0;
  124. }
  125. return -1;
  126. }
  127. /**
  128. *
  129. */
  130. int redisc_destroy(void)
  131. {
  132. redisc_reply_t *rpl, *next_rpl;
  133. redisc_server_t *rsrv=NULL;
  134. redisc_server_t *rsrv1=NULL;
  135. rpl = _redisc_rpl_list;
  136. while(rpl != NULL)
  137. {
  138. next_rpl = rpl->next;
  139. if(rpl->rplRedis)
  140. freeReplyObject(rpl->rplRedis);
  141. if(rpl->rname.s != NULL)
  142. pkg_free(rpl->rname.s);
  143. pkg_free(rpl);
  144. rpl = next_rpl;
  145. }
  146. _redisc_rpl_list = NULL;
  147. if(_redisc_srv_list==NULL)
  148. return -1;
  149. rsrv=_redisc_srv_list;
  150. while(rsrv!=NULL)
  151. {
  152. rsrv1 = rsrv;
  153. rsrv=rsrv->next;
  154. if(rsrv1->ctxRedis!=NULL)
  155. redisFree(rsrv1->ctxRedis);
  156. free_params(rsrv1->attrs);
  157. pkg_free(rsrv1);
  158. }
  159. _redisc_srv_list = NULL;
  160. return 0;
  161. }
  162. /**
  163. *
  164. */
  165. int redisc_add_server(char *spec)
  166. {
  167. param_t *pit=NULL;
  168. param_hooks_t phooks;
  169. redisc_server_t *rsrv=NULL;
  170. str s;
  171. s.s = spec;
  172. s.len = strlen(spec);
  173. if(s.s[s.len-1]==';')
  174. s.len--;
  175. if (parse_params(&s, CLASS_ANY, &phooks, &pit)<0)
  176. {
  177. LM_ERR("failed parsing params value\n");
  178. goto error;
  179. }
  180. rsrv = (redisc_server_t*)pkg_malloc(sizeof(redisc_server_t));
  181. if(rsrv==NULL)
  182. {
  183. LM_ERR("no more pkg\n");
  184. goto error;
  185. }
  186. memset(rsrv, 0, sizeof(redisc_server_t));
  187. rsrv->attrs = pit;
  188. for (pit = rsrv->attrs; pit; pit=pit->next)
  189. {
  190. if(pit->name.len==4 && strncmp(pit->name.s, "name", 4)==0) {
  191. rsrv->sname = &pit->body;
  192. rsrv->hname = get_hash1_raw(rsrv->sname->s, rsrv->sname->len);
  193. break;
  194. }
  195. }
  196. if(rsrv->sname==NULL)
  197. {
  198. LM_ERR("no server name\n");
  199. goto error;
  200. }
  201. rsrv->next = _redisc_srv_list;
  202. _redisc_srv_list = rsrv;
  203. return 0;
  204. error:
  205. if(pit!=NULL)
  206. free_params(pit);
  207. if(rsrv!=NULL)
  208. pkg_free(rsrv);
  209. return -1;
  210. }
  211. /**
  212. *
  213. */
  214. redisc_server_t *redisc_get_server(str *name)
  215. {
  216. redisc_server_t *rsrv=NULL;
  217. unsigned int hname;
  218. hname = get_hash1_raw(name->s, name->len);
  219. rsrv=_redisc_srv_list;
  220. while(rsrv!=NULL)
  221. {
  222. if(rsrv->hname==hname && rsrv->sname->len==name->len
  223. && strncmp(rsrv->sname->s, name->s, name->len)==0)
  224. return rsrv;
  225. rsrv=rsrv->next;
  226. }
  227. return NULL;
  228. }
  229. /**
  230. *
  231. */
  232. int redisc_reconnect_server(redisc_server_t *rsrv)
  233. {
  234. char *addr, *pass, *unix_sock_path = NULL;
  235. unsigned int port, db;
  236. param_t *pit = NULL;
  237. struct timeval tv;
  238. tv.tv_sec = 1;
  239. tv.tv_usec = 0;
  240. addr = "127.0.0.1";
  241. port = 6379;
  242. db = 0;
  243. pass = NULL;
  244. for (pit = rsrv->attrs; pit; pit=pit->next)
  245. {
  246. if(pit->name.len==4 && strncmp(pit->name.s, "unix", 4)==0) {
  247. unix_sock_path = pit->body.s;
  248. unix_sock_path[pit->body.len] = '\0';
  249. } else if(pit->name.len==4 && strncmp(pit->name.s, "addr", 4)==0) {
  250. addr = pit->body.s;
  251. addr[pit->body.len] = '\0';
  252. } else if(pit->name.len==4 && strncmp(pit->name.s, "port", 4)==0) {
  253. if(str2int(&pit->body, &port) < 0)
  254. port = 6379;
  255. } else if(pit->name.len==2 && strncmp(pit->name.s, "db", 2)==0) {
  256. if(str2int(&pit->body, &db) < 0)
  257. db = 0;
  258. } else if(pit->name.len==4 && strncmp(pit->name.s, "pass", 4)==0) {
  259. pass = pit->body.s;
  260. pass[pit->body.len] = '\0';
  261. }
  262. }
  263. if(rsrv->ctxRedis!=NULL) {
  264. redisFree(rsrv->ctxRedis);
  265. rsrv->ctxRedis = NULL;
  266. }
  267. if(unix_sock_path != NULL) {
  268. rsrv->ctxRedis = redisConnectUnixWithTimeout(unix_sock_path, tv);
  269. } else {
  270. rsrv->ctxRedis = redisConnectWithTimeout(addr, port, tv);
  271. }
  272. if(!rsrv->ctxRedis)
  273. goto err;
  274. if (rsrv->ctxRedis->err)
  275. goto err2;
  276. if ((pass != NULL) && redisc_check_auth(rsrv, pass))
  277. goto err2;
  278. if (redisCommandNR(rsrv->ctxRedis, "PING"))
  279. goto err2;
  280. if (redisCommandNR(rsrv->ctxRedis, "SELECT %i", db))
  281. goto err2;
  282. return 0;
  283. err2:
  284. if (unix_sock_path != NULL) {
  285. LM_ERR("error communicating with redis server [%.*s] (unix:%s db:%d): %s\n",
  286. rsrv->sname->len, rsrv->sname->s, unix_sock_path, db, rsrv->ctxRedis->errstr);
  287. } else {
  288. LM_ERR("error communicating with redis server [%.*s] (%s:%d/%d): %s\n",
  289. rsrv->sname->len, rsrv->sname->s, addr, port, db, rsrv->ctxRedis->errstr);
  290. }
  291. err:
  292. if (unix_sock_path != NULL) {
  293. LM_ERR("failed to connect to redis server [%.*s] (unix:%s db:%d)\n",
  294. rsrv->sname->len, rsrv->sname->s, unix_sock_path, db);
  295. } else {
  296. LM_ERR("failed to connect to redis server [%.*s] (%s:%d/%d)\n",
  297. rsrv->sname->len, rsrv->sname->s, addr, port, db);
  298. }
  299. return -1;
  300. }
  301. /**
  302. *
  303. */
  304. int redisc_exec(str *srv, str *res, str *cmd, ...)
  305. {
  306. redisc_server_t *rsrv=NULL;
  307. redisc_reply_t *rpl;
  308. char c;
  309. va_list ap, ap2;
  310. va_start(ap, cmd);
  311. va_copy(ap2, ap);
  312. rsrv = redisc_get_server(srv);
  313. if(srv==NULL || cmd==NULL || res==NULL)
  314. {
  315. LM_ERR("invalid parameters");
  316. goto error_exec;
  317. }
  318. if(srv->len==0 || res->len==0 || cmd->len==0)
  319. {
  320. LM_ERR("invalid parameters");
  321. goto error_exec;
  322. }
  323. if(rsrv==NULL)
  324. {
  325. LM_ERR("no redis server found: %.*s\n", srv->len, srv->s);
  326. goto error_exec;
  327. }
  328. if(rsrv->ctxRedis==NULL)
  329. {
  330. LM_ERR("no redis context for server: %.*s\n", srv->len, srv->s);
  331. goto error_exec;
  332. }
  333. rpl = redisc_get_reply(res);
  334. if(rpl==NULL)
  335. {
  336. LM_ERR("no redis reply id found: %.*s\n", res->len, res->s);
  337. goto error_exec;
  338. }
  339. if(rpl->rplRedis!=NULL)
  340. {
  341. /* clean up previous redis reply */
  342. freeReplyObject(rpl->rplRedis);
  343. rpl->rplRedis = NULL;
  344. }
  345. c = cmd->s[cmd->len];
  346. cmd->s[cmd->len] = '\0';
  347. rpl->rplRedis = redisvCommand(rsrv->ctxRedis, cmd->s, ap );
  348. if(rpl->rplRedis == NULL)
  349. {
  350. /* null reply, reconnect and try again */
  351. if(rsrv->ctxRedis->err)
  352. {
  353. LM_ERR("Redis error: %s\n", rsrv->ctxRedis->errstr);
  354. }
  355. if(redisc_reconnect_server(rsrv)==0)
  356. {
  357. rpl->rplRedis = redisvCommand(rsrv->ctxRedis, cmd->s, ap2);
  358. } else {
  359. LM_ERR("unable to reconnect to redis server: %.*s\n", srv->len, srv->s);
  360. cmd->s[cmd->len] = c;
  361. goto error_exec;
  362. }
  363. }
  364. cmd->s[cmd->len] = c;
  365. va_end(ap);
  366. va_end(ap2);
  367. return 0;
  368. error_exec:
  369. va_end(ap);
  370. va_end(ap2);
  371. return -1;
  372. }
  373. /**
  374. * Executes a redis command.
  375. * Command is coded using a vector of strings, and a vector of lenghts.
  376. *
  377. * @param rsrv Pointer to a redis_server_t structure.
  378. * @param argc number of elements in the command vector.
  379. * @param argv vector of zero terminated strings forming the command.
  380. * @param argvlen vector of command string lenghts or NULL.
  381. * @return redisReply structure or NULL if there was an error.
  382. */
  383. void * redisc_exec_argv(redisc_server_t *rsrv, int argc, const char **argv, const size_t *argvlen)
  384. {
  385. redisReply *res=NULL;
  386. if(rsrv==NULL || rsrv->ctxRedis==NULL)
  387. {
  388. LM_ERR("no redis context found for server %.*s\n",
  389. rsrv->sname->len, rsrv->sname->s);
  390. return NULL;
  391. }
  392. if(argc<=0)
  393. {
  394. LM_ERR("invalid parameters\n");
  395. return NULL;
  396. }
  397. if(argv==NULL || *argv==NULL)
  398. {
  399. LM_ERR("invalid parameters\n");
  400. return NULL;
  401. }
  402. res = redisCommandArgv(rsrv->ctxRedis, argc, argv, argvlen);
  403. if(res)
  404. {
  405. return res;
  406. }
  407. /* null reply, reconnect and try again */
  408. if(rsrv->ctxRedis->err)
  409. {
  410. LM_ERR("Redis error: %s\n", rsrv->ctxRedis->errstr);
  411. }
  412. if(redisc_reconnect_server(rsrv)==0)
  413. {
  414. res = redisCommandArgv(rsrv->ctxRedis, argc, argv, argvlen);
  415. }
  416. else
  417. {
  418. LM_ERR("Unable to reconnect to server: %.*s\n",
  419. rsrv->sname->len, rsrv->sname->s);
  420. return NULL;
  421. }
  422. return res;
  423. }
  424. /**
  425. *
  426. */
  427. redisc_reply_t *redisc_get_reply(str *name)
  428. {
  429. redisc_reply_t *rpl;
  430. unsigned int hid;
  431. hid = get_hash1_raw(name->s, name->len);
  432. for(rpl=_redisc_rpl_list; rpl; rpl=rpl->next) {
  433. if(rpl->hname==hid && rpl->rname.len==name->len
  434. && strncmp(rpl->rname.s, name->s, name->len)==0)
  435. return rpl;
  436. }
  437. /* not found - add a new one */
  438. rpl = (redisc_reply_t*)pkg_malloc(sizeof(redisc_reply_t));
  439. if(rpl==NULL)
  440. {
  441. LM_ERR("no more pkg\n");
  442. return NULL;
  443. }
  444. memset(rpl, 0, sizeof(redisc_reply_t));
  445. rpl->hname = hid;
  446. rpl->rname.s = (char*)pkg_malloc(name->len+1);
  447. if(rpl->rname.s==NULL)
  448. {
  449. LM_ERR("no more pkg.\n");
  450. pkg_free(rpl);
  451. return NULL;
  452. }
  453. strncpy(rpl->rname.s, name->s, name->len);
  454. rpl->rname.len = name->len;
  455. rpl->rname.s[name->len] = '\0';
  456. rpl->next = _redisc_rpl_list;
  457. _redisc_rpl_list = rpl;
  458. return rpl;
  459. }
  460. /**
  461. *
  462. */
  463. int redisc_free_reply(str *name)
  464. {
  465. redisc_reply_t *rpl;
  466. unsigned int hid;
  467. if(name==NULL || name->len==0) {
  468. LM_ERR("invalid parameters");
  469. return -1;
  470. }
  471. hid = get_hash1_raw(name->s, name->len);
  472. rpl = _redisc_rpl_list;
  473. while(rpl) {
  474. if(rpl->hname==hid && rpl->rname.len==name->len
  475. && strncmp(rpl->rname.s, name->s, name->len)==0) {
  476. if(rpl->rplRedis) {
  477. freeReplyObject(rpl->rplRedis);
  478. rpl->rplRedis = NULL;
  479. }
  480. return 0;
  481. }
  482. rpl = rpl->next;
  483. }
  484. /* reply entry not found. */
  485. return -1;
  486. }
  487. int redisc_check_auth(redisc_server_t *rsrv, char *pass)
  488. {
  489. redisReply *reply;
  490. int retval = 0;
  491. reply = redisCommand(rsrv->ctxRedis, "AUTH %s", pass);
  492. if (reply->type == REDIS_REPLY_ERROR) {
  493. LM_ERR("Redis authentication error\n");
  494. retval = -1;
  495. }
  496. freeReplyObject(reply);
  497. return retval;
  498. }