1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
|
/*
* $Id: matrix.c 4978 2008-09-23 14:25:02Z henningw $
*
* Copyright (C) 2007 1&1 Internet AG
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include "../../mem/shm_mem.h"
#include "../../sr_module.h"
#include "../../lib/kmi/mi.h"
#include "../../mem/mem.h"
#include "../../usr_avp.h"
#include "../../locking.h"
#include "../../error.h"
#include "../../ut.h"
#include "../../mod_fix.h"
#include "db_matrix.h"
MODULE_VERSION
#define MAXCOLS 1000
str matrix_db_url = str_init(DEFAULT_RODB_URL);
/**
* Generic parameter that holds a string, an int or an pseudo-variable
* @todo replace this with gparam_t
*/
struct multiparam_t {
enum {
MP_INT,
MP_STR,
MP_AVP,
MP_PVE,
} type;
union {
int n;
str s;
struct {
unsigned short flags;
int_str name;
} a;
pv_elem_t *p;
} u;
};
/* ---- fixup functions: */
static int matrix_fixup(void** param, int param_no);
/* ---- exported commands: */
static int lookup_matrix(struct sip_msg *msg, struct multiparam_t *_first, struct multiparam_t *_second, struct multiparam_t *_dstavp);
/* ---- module init functions: */
static int mod_init(void);
static int child_init(int rank);
static int mi_child_init(void);
static void mod_destroy(void);
/* --- fifo functions */
struct mi_root * mi_reload_matrix(struct mi_root* cmd, void* param); /* usage: kamctl fifo reload_matrix */
static cmd_export_t cmds[]={
{ "matrix", (cmd_function)lookup_matrix, 3, matrix_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE },
{ 0, 0, 0, 0, 0, 0}
};
static param_export_t params[] = {
matrix_DB_URL
matrix_DB_TABLE
matrix_DB_COLS
{ 0, 0, 0}
};
/* Exported MI functions */
static mi_export_t mi_cmds[] = {
{ "reload_matrix", mi_reload_matrix, MI_NO_INPUT_FLAG, 0, mi_child_init },
{ 0, 0, 0, 0, 0}
};
struct module_exports exports= {
"matrix",
DEFAULT_DLFLAGS,
cmds,
params,
0,
mi_cmds,
0,
0,
mod_init,
0,
mod_destroy,
child_init
};
struct first_t {
struct first_t *next;
int id;
short int second_list[MAXCOLS+1];
};
struct matrix_t {
struct first_t *head;
};
static gen_lock_t *lock = NULL;
static struct matrix_t *matrix = NULL;
/**
* fixes the module functions' parameters if it is a phone number.
* supports string, pseudo-variables and AVPs.
*
* @param param the parameter
*
* @return 0 on success, -1 on failure
*/
static int mp_fixup(void ** param) {
pv_spec_t avp_spec;
struct multiparam_t *mp;
str s;
mp = (struct multiparam_t *)pkg_malloc(sizeof(struct multiparam_t));
if (mp == NULL) {
LM_ERR("out of pkg memory\n");
return -1;
}
memset(mp, 0, sizeof(struct multiparam_t));
s.s = (char *)(*param);
s.len = strlen(s.s);
if (s.s[0]!='$') {
/* This is string */
mp->type=MP_STR;
mp->u.s=s;
}
else {
/* This is a pseudo-variable */
if (pv_parse_spec(&s, &avp_spec)==0) {
LM_ERR("pv_parse_spec failed for '%s'\n", (char *)(*param));
pkg_free(mp);
return -1;
}
if (avp_spec.type==PVT_AVP) {
/* This is an AVP - could be an id or name */
mp->type=MP_AVP;
if(pv_get_avp_name(0, &(avp_spec.pvp), &(mp->u.a.name), &(mp->u.a.flags))!=0) {
LM_ERR("Invalid AVP definition <%s>\n", (char *)(*param));
pkg_free(mp);
return -1;
}
} else {
mp->type=MP_PVE;
if(pv_parse_format(&s, &(mp->u.p))<0) {
LM_ERR("pv_parse_format failed for '%s'\n", (char *)(*param));
pkg_free(mp);
return -1;
}
}
}
*param = (void*)mp;
return 0;
}
/**
* fixes the module functions' parameters in case of AVP names.
*
* @param param the parameter
*
* @return 0 on success, -1 on failure
*/
static int avp_name_fixup(void ** param) {
pv_spec_t avp_spec;
struct multiparam_t *mp;
str s;
s.s = (char *)(*param);
s.len = strlen(s.s);
if (s.len <= 0) return -1;
if (pv_parse_spec(&s, &avp_spec)==0 || avp_spec.type!=PVT_AVP) {
LM_ERR("Malformed or non AVP definition <%s>\n", (char *)(*param));
return -1;
}
mp = (struct multiparam_t *)pkg_malloc(sizeof(struct multiparam_t));
if (mp == NULL) {
LM_ERR("out of pkg memory\n");
return -1;
}
memset(mp, 0, sizeof(struct multiparam_t));
mp->type=MP_AVP;
if(pv_get_avp_name(0, &(avp_spec.pvp), &(mp->u.a.name), &(mp->u.a.flags))!=0) {
LM_ERR("Invalid AVP definition <%s>\n", (char *)(*param));
pkg_free(mp);
return -1;
}
*param = (void*)mp;
return 0;
}
static int matrix_fixup(void** param, int param_no)
{
if (param_no == 1) {
/* source id */
if (mp_fixup(param) < 0) {
LM_ERR("cannot fixup parameter %d\n", param_no);
return -1;
}
}
else if (param_no == 2) {
/* destination id */
if (mp_fixup(param) < 0) {
LM_ERR("cannot fixup parameter %d\n", param_no);
return -1;
}
}
else if (param_no == 3) {
/* destination avp name */
if (avp_name_fixup(param) < 0) {
LM_ERR("cannot fixup parameter %d\n", param_no);
return -1;
}
}
return 0;
}
static void matrix_clear(void)
{
struct first_t *srcitem;
if (matrix) {
while (matrix->head) {
srcitem = matrix->head;
matrix->head = srcitem->next;
shm_free(srcitem);
}
}
}
static int matrix_insert(int first, short int second, int res)
{
struct first_t *srcitem;
int i;
if ((second<0) || (second>MAXCOLS)) {
LM_ERR("invalid second value %d\n", second);
return -1;
}
LM_DBG("searching for %d, %d\n", first, second);
if (matrix) {
srcitem = matrix->head;
while (srcitem) {
if (srcitem->id == first) {
srcitem->second_list[second] = res;
LM_DBG("inserted (%d, %d, %d)", first, second, res);
return 0;
}
srcitem = srcitem->next;
}
/* not found */
srcitem = shm_malloc(sizeof(struct first_t));
if (srcitem == NULL) {
LM_ERR("out of shared memory.");
return -1;
}
memset(srcitem, 0, sizeof(struct first_t));
/* Mark all new cells as empty */
for (i=0; i<=MAXCOLS; i++) srcitem->second_list[i] = -1;
srcitem->next = matrix->head;
srcitem->id = first;
srcitem->second_list[second] = res;
matrix->head = srcitem;
}
LM_DBG("inserted new row for (%d, %d, %d)", first, second, res);
return 0;
}
/* Returns the res id if the matrix contains an entry for the given indices, -1 otherwise.
*/
static int internal_lookup(int first, short int second)
{
struct first_t *item;
if ((second<0) || (second>MAXCOLS)) {
LM_ERR("invalid second value %d\n", second);
return -1;
}
if (matrix) {
item = matrix->head;
while (item) {
if (item->id == first) {
return item->second_list[second];
}
item = item->next;
}
}
return -1;
}
static int lookup_matrix(struct sip_msg *msg, struct multiparam_t *_srctree, struct multiparam_t *_second, struct multiparam_t *_dstavp)
{
int first;
int second;
struct usr_avp *avp;
int_str avp_val;
switch (_srctree->type) {
case MP_INT:
first = _srctree->u.n;
break;
case MP_AVP:
avp = search_first_avp(_srctree->u.a.flags, _srctree->u.a.name, &avp_val, 0);
if (!avp) {
LM_ERR("cannot find srctree AVP\n");
return -1;
}
if ((avp->flags&AVP_VAL_STR)) {
LM_ERR("cannot process string value in srctree AVP\n");
return -1;
}
else first = avp_val.n;
break;
default:
LM_ERR("invalid srctree type\n");
return -1;
}
switch (_second->type) {
case MP_INT:
second = _second->u.n;
break;
case MP_AVP:
avp = search_first_avp(_second->u.a.flags, _second->u.a.name, &avp_val, 0);
if (!avp) {
LM_ERR("cannot find second_value AVP\n");
return -1;
}
if ((avp->flags&AVP_VAL_STR)) {
LM_ERR("cannot process string value in second_value AVP\n");
return -1;
}
else second = avp_val.n;
break;
default:
LM_ERR("invalid second_value type\n");
return -1;
}
/* critical section start: avoids dirty reads when updating d-tree */
lock_get(lock);
avp_val.n=internal_lookup(first, second);
/* critical section end */
lock_release(lock);
if (avp_val.n<0) {
LM_INFO("lookup failed\n");
return -1;
}
/* set avp ! */
if (add_avp(_dstavp->u.a.flags, _dstavp->u.a.name, avp_val)<0) {
LM_ERR("add AVP failed\n");
return -1;
}
LM_INFO("result from lookup: %d\n", avp_val.n);
return 1;
}
/**
* Rebuild matrix using database entries
* \return negative on failure, positive on success, indicating the number of matrix entries
*/
static int db_reload_matrix(void)
{
db_key_t columns[3] = { &matrix_first_col, &matrix_second_col, &matrix_res_col };
db1_res_t *res;
int i;
int n = 0;
if (matrix_dbf.use_table(matrix_dbh, &matrix_table) < 0) {
LM_ERR("cannot use table '%.*s'.\n", matrix_table.len, matrix_table.s);
return -1;
}
if (matrix_dbf.query(matrix_dbh, NULL, NULL, NULL, columns, 0, 3, NULL, &res) < 0) {
LM_ERR("error while executing query.\n");
return -1;
}
/* critical section start: avoids dirty reads when updating d-tree */
lock_get(lock);
matrix_clear();
if (RES_COL_N(res) > 2) {
for(i = 0; i < RES_ROW_N(res); i++) {
if ((!RES_ROWS(res)[i].values[0].nul) && (!RES_ROWS(res)[i].values[1].nul)) {
if ((RES_ROWS(res)[i].values[0].type == DB1_INT) &&
(RES_ROWS(res)[i].values[1].type == DB1_INT) &&
(RES_ROWS(res)[i].values[2].type == DB1_INT)) {
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);
n++;
}
else {
LM_ERR("got invalid result type from query.\n");
}
}
}
}
/* critical section end */
lock_release(lock);
matrix_dbf.free_result(matrix_dbh, res);
LM_INFO("loaded %d matrix entries.", n);
return n;
}
static int init_shmlock(void)
{
lock = lock_alloc();
if (!lock) {
LM_CRIT("cannot allocate memory for lock.\n");
return -1;
}
if (lock_init(lock) == 0) {
LM_CRIT("cannot initialize lock.\n");
return -1;
}
return 0;
}
static void destroy_shmlock(void)
{
if (lock) {
lock_destroy(lock);
lock_dealloc((void *)lock);
lock = NULL;
}
}
struct mi_root * mi_reload_matrix(struct mi_root* cmd, void* param)
{
struct mi_root * tmp = NULL;
if(db_reload_matrix() >= 0) {
tmp = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
} else {
tmp = init_mi_tree( 500, "cannot reload matrix", 24);
}
return tmp;
}
static int init_matrix(void)
{
matrix = shm_malloc(sizeof(struct matrix_t));
if (!matrix) {
LM_ERR("out of shared memory\n");
return -1;
}
memset(matrix, 0, sizeof(struct matrix_t));
if (db_reload_matrix() < 0) {
LM_ERR("cannot populate matrix\n");
return -1;
}
return 0;
}
static void destroy_matrix(void)
{
if (matrix) {
matrix_clear();
shm_free(matrix);
}
}
static int mod_init(void)
{
if(register_mi_mod(exports.name, mi_cmds)!=0)
{
LM_ERR("failed to register MI commands\n");
return -1;
}
if (init_shmlock() != 0) return -1;
if (matrix_db_init() != 0) return -1;
if (matrix_db_open() != 0) return -1;
if (init_matrix() != 0) return -1;
matrix_db_close();
return 0;
}
static int child_init(int rank)
{
if(rank==PROC_INIT || rank==PROC_TCP_MAIN)
return 0;
if (matrix_db_open() != 0) return -1;
return 0;
}
static int mi_child_init(void)
{
if (matrix_db_open() != 0) return -1;
return 0;
}
static void mod_destroy(void)
{
destroy_matrix();
destroy_shmlock();
matrix_db_close();
}
|