matrix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * $Id: matrix.c 4978 2008-09-23 14:25:02Z henningw $
  3. *
  4. * Copyright (C) 2007 1&1 Internet AG
  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. #include <string.h>
  23. #include "../../mem/shm_mem.h"
  24. #include "../../sr_module.h"
  25. #include "../../lib/kmi/mi.h"
  26. #include "../../mem/mem.h"
  27. #include "../../usr_avp.h"
  28. #include "../../locking.h"
  29. #include "../../error.h"
  30. #include "../../ut.h"
  31. #include "../../mod_fix.h"
  32. #include "db_matrix.h"
  33. MODULE_VERSION
  34. #define MAXCOLS 1000
  35. str matrix_db_url = str_init(DEFAULT_RODB_URL);
  36. /**
  37. * Generic parameter that holds a string, an int or an pseudo-variable
  38. * @todo replace this with gparam_t
  39. */
  40. struct multiparam_t {
  41. enum {
  42. MP_INT,
  43. MP_STR,
  44. MP_AVP,
  45. MP_PVE,
  46. } type;
  47. union {
  48. int n;
  49. str s;
  50. struct {
  51. unsigned short flags;
  52. int_str name;
  53. } a;
  54. pv_elem_t *p;
  55. } u;
  56. };
  57. /* ---- fixup functions: */
  58. static int matrix_fixup(void** param, int param_no);
  59. /* ---- exported commands: */
  60. static int lookup_matrix(struct sip_msg *msg, struct multiparam_t *_first, struct multiparam_t *_second, struct multiparam_t *_dstavp);
  61. /* ---- module init functions: */
  62. static int mod_init(void);
  63. static int child_init(int rank);
  64. static int mi_child_init(void);
  65. static void mod_destroy(void);
  66. /* --- fifo functions */
  67. struct mi_root * mi_reload_matrix(struct mi_root* cmd, void* param); /* usage: kamctl fifo reload_matrix */
  68. static cmd_export_t cmds[]={
  69. { "matrix", (cmd_function)lookup_matrix, 3, matrix_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE },
  70. { 0, 0, 0, 0, 0, 0}
  71. };
  72. static param_export_t params[] = {
  73. matrix_DB_URL
  74. matrix_DB_TABLE
  75. matrix_DB_COLS
  76. { 0, 0, 0}
  77. };
  78. /* Exported MI functions */
  79. static mi_export_t mi_cmds[] = {
  80. { "reload_matrix", mi_reload_matrix, MI_NO_INPUT_FLAG, 0, mi_child_init },
  81. { 0, 0, 0, 0, 0}
  82. };
  83. struct module_exports exports= {
  84. "matrix",
  85. DEFAULT_DLFLAGS,
  86. cmds,
  87. params,
  88. 0,
  89. mi_cmds,
  90. 0,
  91. 0,
  92. mod_init,
  93. 0,
  94. mod_destroy,
  95. child_init
  96. };
  97. struct first_t {
  98. struct first_t *next;
  99. int id;
  100. short int second_list[MAXCOLS+1];
  101. };
  102. struct matrix_t {
  103. struct first_t *head;
  104. };
  105. static gen_lock_t *lock = NULL;
  106. static struct matrix_t *matrix = NULL;
  107. /**
  108. * fixes the module functions' parameters if it is a phone number.
  109. * supports string, pseudo-variables and AVPs.
  110. *
  111. * @param param the parameter
  112. *
  113. * @return 0 on success, -1 on failure
  114. */
  115. static int mp_fixup(void ** param) {
  116. pv_spec_t avp_spec;
  117. struct multiparam_t *mp;
  118. str s;
  119. mp = (struct multiparam_t *)pkg_malloc(sizeof(struct multiparam_t));
  120. if (mp == NULL) {
  121. LM_ERR("out of pkg memory\n");
  122. return -1;
  123. }
  124. memset(mp, 0, sizeof(struct multiparam_t));
  125. s.s = (char *)(*param);
  126. s.len = strlen(s.s);
  127. if (s.s[0]!='$') {
  128. /* This is string */
  129. mp->type=MP_STR;
  130. mp->u.s=s;
  131. }
  132. else {
  133. /* This is a pseudo-variable */
  134. if (pv_parse_spec(&s, &avp_spec)==0) {
  135. LM_ERR("pv_parse_spec failed for '%s'\n", (char *)(*param));
  136. pkg_free(mp);
  137. return -1;
  138. }
  139. if (avp_spec.type==PVT_AVP) {
  140. /* This is an AVP - could be an id or name */
  141. mp->type=MP_AVP;
  142. if(pv_get_avp_name(0, &(avp_spec.pvp), &(mp->u.a.name), &(mp->u.a.flags))!=0) {
  143. LM_ERR("Invalid AVP definition <%s>\n", (char *)(*param));
  144. pkg_free(mp);
  145. return -1;
  146. }
  147. } else {
  148. mp->type=MP_PVE;
  149. if(pv_parse_format(&s, &(mp->u.p))<0) {
  150. LM_ERR("pv_parse_format failed for '%s'\n", (char *)(*param));
  151. pkg_free(mp);
  152. return -1;
  153. }
  154. }
  155. }
  156. *param = (void*)mp;
  157. return 0;
  158. }
  159. /**
  160. * fixes the module functions' parameters in case of AVP names.
  161. *
  162. * @param param the parameter
  163. *
  164. * @return 0 on success, -1 on failure
  165. */
  166. static int avp_name_fixup(void ** param) {
  167. pv_spec_t avp_spec;
  168. struct multiparam_t *mp;
  169. str s;
  170. s.s = (char *)(*param);
  171. s.len = strlen(s.s);
  172. if (s.len <= 0) return -1;
  173. if (pv_parse_spec(&s, &avp_spec)==0 || avp_spec.type!=PVT_AVP) {
  174. LM_ERR("Malformed or non AVP definition <%s>\n", (char *)(*param));
  175. return -1;
  176. }
  177. mp = (struct multiparam_t *)pkg_malloc(sizeof(struct multiparam_t));
  178. if (mp == NULL) {
  179. LM_ERR("out of pkg memory\n");
  180. return -1;
  181. }
  182. memset(mp, 0, sizeof(struct multiparam_t));
  183. mp->type=MP_AVP;
  184. if(pv_get_avp_name(0, &(avp_spec.pvp), &(mp->u.a.name), &(mp->u.a.flags))!=0) {
  185. LM_ERR("Invalid AVP definition <%s>\n", (char *)(*param));
  186. pkg_free(mp);
  187. return -1;
  188. }
  189. *param = (void*)mp;
  190. return 0;
  191. }
  192. static int matrix_fixup(void** param, int param_no)
  193. {
  194. if (param_no == 1) {
  195. /* source id */
  196. if (mp_fixup(param) < 0) {
  197. LM_ERR("cannot fixup parameter %d\n", param_no);
  198. return -1;
  199. }
  200. }
  201. else if (param_no == 2) {
  202. /* destination id */
  203. if (mp_fixup(param) < 0) {
  204. LM_ERR("cannot fixup parameter %d\n", param_no);
  205. return -1;
  206. }
  207. }
  208. else if (param_no == 3) {
  209. /* destination avp name */
  210. if (avp_name_fixup(param) < 0) {
  211. LM_ERR("cannot fixup parameter %d\n", param_no);
  212. return -1;
  213. }
  214. }
  215. return 0;
  216. }
  217. static void matrix_clear(void)
  218. {
  219. struct first_t *srcitem;
  220. if (matrix) {
  221. while (matrix->head) {
  222. srcitem = matrix->head;
  223. matrix->head = srcitem->next;
  224. shm_free(srcitem);
  225. }
  226. }
  227. }
  228. static int matrix_insert(int first, short int second, int res)
  229. {
  230. struct first_t *srcitem;
  231. int i;
  232. if ((second<0) || (second>MAXCOLS)) {
  233. LM_ERR("invalid second value %d\n", second);
  234. return -1;
  235. }
  236. LM_DBG("searching for %d, %d\n", first, second);
  237. if (matrix) {
  238. srcitem = matrix->head;
  239. while (srcitem) {
  240. if (srcitem->id == first) {
  241. srcitem->second_list[second] = res;
  242. LM_DBG("inserted (%d, %d, %d)", first, second, res);
  243. return 0;
  244. }
  245. srcitem = srcitem->next;
  246. }
  247. /* not found */
  248. srcitem = shm_malloc(sizeof(struct first_t));
  249. if (srcitem == NULL) {
  250. LM_ERR("out of shared memory.");
  251. return -1;
  252. }
  253. memset(srcitem, 0, sizeof(struct first_t));
  254. /* Mark all new cells as empty */
  255. for (i=0; i<=MAXCOLS; i++) srcitem->second_list[i] = -1;
  256. srcitem->next = matrix->head;
  257. srcitem->id = first;
  258. srcitem->second_list[second] = res;
  259. matrix->head = srcitem;
  260. }
  261. LM_DBG("inserted new row for (%d, %d, %d)", first, second, res);
  262. return 0;
  263. }
  264. /* Returns the res id if the matrix contains an entry for the given indices, -1 otherwise.
  265. */
  266. static int internal_lookup(int first, short int second)
  267. {
  268. struct first_t *item;
  269. if ((second<0) || (second>MAXCOLS)) {
  270. LM_ERR("invalid second value %d\n", second);
  271. return -1;
  272. }
  273. if (matrix) {
  274. item = matrix->head;
  275. while (item) {
  276. if (item->id == first) {
  277. return item->second_list[second];
  278. }
  279. item = item->next;
  280. }
  281. }
  282. return -1;
  283. }
  284. static int lookup_matrix(struct sip_msg *msg, struct multiparam_t *_srctree, struct multiparam_t *_second, struct multiparam_t *_dstavp)
  285. {
  286. int first;
  287. int second;
  288. struct usr_avp *avp;
  289. int_str avp_val;
  290. switch (_srctree->type) {
  291. case MP_INT:
  292. first = _srctree->u.n;
  293. break;
  294. case MP_AVP:
  295. avp = search_first_avp(_srctree->u.a.flags, _srctree->u.a.name, &avp_val, 0);
  296. if (!avp) {
  297. LM_ERR("cannot find srctree AVP\n");
  298. return -1;
  299. }
  300. if ((avp->flags&AVP_VAL_STR)) {
  301. LM_ERR("cannot process string value in srctree AVP\n");
  302. return -1;
  303. }
  304. else first = avp_val.n;
  305. break;
  306. default:
  307. LM_ERR("invalid srctree type\n");
  308. return -1;
  309. }
  310. switch (_second->type) {
  311. case MP_INT:
  312. second = _second->u.n;
  313. break;
  314. case MP_AVP:
  315. avp = search_first_avp(_second->u.a.flags, _second->u.a.name, &avp_val, 0);
  316. if (!avp) {
  317. LM_ERR("cannot find second_value AVP\n");
  318. return -1;
  319. }
  320. if ((avp->flags&AVP_VAL_STR)) {
  321. LM_ERR("cannot process string value in second_value AVP\n");
  322. return -1;
  323. }
  324. else second = avp_val.n;
  325. break;
  326. default:
  327. LM_ERR("invalid second_value type\n");
  328. return -1;
  329. }
  330. /* critical section start: avoids dirty reads when updating d-tree */
  331. lock_get(lock);
  332. avp_val.n=internal_lookup(first, second);
  333. /* critical section end */
  334. lock_release(lock);
  335. if (avp_val.n<0) {
  336. LM_INFO("lookup failed\n");
  337. return -1;
  338. }
  339. /* set avp ! */
  340. if (add_avp(_dstavp->u.a.flags, _dstavp->u.a.name, avp_val)<0) {
  341. LM_ERR("add AVP failed\n");
  342. return -1;
  343. }
  344. LM_INFO("result from lookup: %d\n", avp_val.n);
  345. return 1;
  346. }
  347. /**
  348. * Rebuild matrix using database entries
  349. * \return negative on failure, positive on success, indicating the number of matrix entries
  350. */
  351. static int db_reload_matrix(void)
  352. {
  353. db_key_t columns[3] = { &matrix_first_col, &matrix_second_col, &matrix_res_col };
  354. db1_res_t *res;
  355. int i;
  356. int n = 0;
  357. if (matrix_dbf.use_table(matrix_dbh, &matrix_table) < 0) {
  358. LM_ERR("cannot use table '%.*s'.\n", matrix_table.len, matrix_table.s);
  359. return -1;
  360. }
  361. if (matrix_dbf.query(matrix_dbh, NULL, NULL, NULL, columns, 0, 3, NULL, &res) < 0) {
  362. LM_ERR("error while executing query.\n");
  363. return -1;
  364. }
  365. /* critical section start: avoids dirty reads when updating d-tree */
  366. lock_get(lock);
  367. matrix_clear();
  368. if (RES_COL_N(res) > 2) {
  369. for(i = 0; i < RES_ROW_N(res); i++) {
  370. if ((!RES_ROWS(res)[i].values[0].nul) && (!RES_ROWS(res)[i].values[1].nul)) {
  371. if ((RES_ROWS(res)[i].values[0].type == DB1_INT) &&
  372. (RES_ROWS(res)[i].values[1].type == DB1_INT) &&
  373. (RES_ROWS(res)[i].values[2].type == DB1_INT)) {
  374. matrix_insert(RES_ROWS(res)[i].values[0].val.int_val, RES_ROWS(res)[i].values[1].val.int_val, RES_ROWS(res)[i].values[2].val.int_val);
  375. n++;
  376. }
  377. else {
  378. LM_ERR("got invalid result type from query.\n");
  379. }
  380. }
  381. }
  382. }
  383. /* critical section end */
  384. lock_release(lock);
  385. matrix_dbf.free_result(matrix_dbh, res);
  386. LM_INFO("loaded %d matrix entries.", n);
  387. return n;
  388. }
  389. static int init_shmlock(void)
  390. {
  391. lock = lock_alloc();
  392. if (!lock) {
  393. LM_CRIT("cannot allocate memory for lock.\n");
  394. return -1;
  395. }
  396. if (lock_init(lock) == 0) {
  397. LM_CRIT("cannot initialize lock.\n");
  398. return -1;
  399. }
  400. return 0;
  401. }
  402. static void destroy_shmlock(void)
  403. {
  404. if (lock) {
  405. lock_destroy(lock);
  406. lock_dealloc((void *)lock);
  407. lock = NULL;
  408. }
  409. }
  410. struct mi_root * mi_reload_matrix(struct mi_root* cmd, void* param)
  411. {
  412. struct mi_root * tmp = NULL;
  413. if(db_reload_matrix() >= 0) {
  414. tmp = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  415. } else {
  416. tmp = init_mi_tree( 500, "cannot reload matrix", 24);
  417. }
  418. return tmp;
  419. }
  420. static int init_matrix(void)
  421. {
  422. matrix = shm_malloc(sizeof(struct matrix_t));
  423. if (!matrix) {
  424. LM_ERR("out of shared memory\n");
  425. return -1;
  426. }
  427. memset(matrix, 0, sizeof(struct matrix_t));
  428. if (db_reload_matrix() < 0) {
  429. LM_ERR("cannot populate matrix\n");
  430. return -1;
  431. }
  432. return 0;
  433. }
  434. static void destroy_matrix(void)
  435. {
  436. if (matrix) {
  437. matrix_clear();
  438. shm_free(matrix);
  439. }
  440. }
  441. static int mod_init(void)
  442. {
  443. if(register_mi_mod(exports.name, mi_cmds)!=0)
  444. {
  445. LM_ERR("failed to register MI commands\n");
  446. return -1;
  447. }
  448. if (init_shmlock() != 0) return -1;
  449. if (matrix_db_init() != 0) return -1;
  450. if (matrix_db_open() != 0) return -1;
  451. if (init_matrix() != 0) return -1;
  452. matrix_db_close();
  453. return 0;
  454. }
  455. static int child_init(int rank)
  456. {
  457. if(rank==PROC_INIT || rank==PROC_TCP_MAIN)
  458. return 0;
  459. if (matrix_db_open() != 0) return -1;
  460. return 0;
  461. }
  462. static int mi_child_init(void)
  463. {
  464. if (matrix_db_open() != 0) return -1;
  465. return 0;
  466. }
  467. static void mod_destroy(void)
  468. {
  469. destroy_matrix();
  470. destroy_shmlock();
  471. matrix_db_close();
  472. }