modparam.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  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. /*!
  22. * \file
  23. * \brief Kamailio core :: Configuration parameters for modules (modparams)
  24. * \ingroup core
  25. * Module: \ref core
  26. */
  27. #include "modparam.h"
  28. #include "dprint.h"
  29. #include "mem/mem.h"
  30. #include <sys/types.h>
  31. #include <regex.h>
  32. #include <string.h>
  33. int set_mod_param(char* _mod, char* _name, modparam_t _type, void* _val)
  34. {
  35. return set_mod_param_regex(_mod, _name, _type, _val);
  36. }
  37. int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
  38. {
  39. struct sr_module* t;
  40. regex_t preg;
  41. int mod_found, len;
  42. char* reg;
  43. void *ptr, *val2;
  44. modparam_t param_type;
  45. str s;
  46. if (!regex) {
  47. LM_ERR("Invalid mod parameter value\n");
  48. return -5;
  49. }
  50. if (!name) {
  51. LM_ERR("Invalid name parameter value\n");
  52. return -6;
  53. }
  54. len = strlen(regex);
  55. reg = pkg_malloc(len + 2 + 1);
  56. if (reg == 0) {
  57. LM_ERR("No memory left\n");
  58. return -1;
  59. }
  60. reg[0] = '^';
  61. memcpy(reg + 1, regex, len);
  62. reg[len + 1] = '$';
  63. reg[len + 2] = '\0';
  64. if (regcomp(&preg, reg, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
  65. LM_ERR("Error while compiling regular expression\n");
  66. pkg_free(reg);
  67. return -2;
  68. }
  69. mod_found = 0;
  70. for(t = modules; t; t = t->next) {
  71. if (regexec(&preg, t->exports.name, 0, 0, 0) == 0) {
  72. LM_DBG("'%s' matches module '%s'\n", regex, t->exports.name);
  73. mod_found = 1;
  74. /* PARAM_STR (PARAM_STRING) may be assigned also to PARAM_STRING(PARAM_STR) so let get both module param */
  75. ptr = find_param_export(t, name, type | ((type & (PARAM_STR|PARAM_STRING))?PARAM_STR|PARAM_STRING:0), &param_type);
  76. if (ptr) {
  77. /* type casting */
  78. if (type == PARAM_STRING && PARAM_TYPE_MASK(param_type) == PARAM_STR) {
  79. s.s = (char*)val;
  80. s.len = s.s ? strlen(s.s) : 0;
  81. val2 = &s;
  82. } else if (type == PARAM_STR && PARAM_TYPE_MASK(param_type) == PARAM_STRING) {
  83. s = *(str*)val;
  84. val2 = s.s; /* zero terminator expected */
  85. } else {
  86. val2 = val;
  87. }
  88. LM_DBG("found <%s> in module %s [%s]\n", name, t->exports.name, t->path);
  89. if (param_type & PARAM_USE_FUNC) {
  90. if ( ((param_func_t)(ptr))(param_type, val2) < 0) {
  91. regfree(&preg);
  92. pkg_free(reg);
  93. return -4;
  94. }
  95. }
  96. else {
  97. switch(PARAM_TYPE_MASK(param_type)) {
  98. case PARAM_STRING:
  99. *((char**)ptr) = pkg_malloc(strlen((char*)val2)+1);
  100. if (!*((char**)ptr)) {
  101. LM_ERR("No memory left\n");
  102. regfree(&preg);
  103. pkg_free(reg);
  104. return -1;
  105. }
  106. strcpy(*((char**)ptr), (char*)val2);
  107. break;
  108. case PARAM_STR:
  109. ((str*)ptr)->s = pkg_malloc(((str*)val2)->len+1);
  110. if (!((str*)ptr)->s) {
  111. LM_ERR("No memory left\n");
  112. regfree(&preg);
  113. pkg_free(reg);
  114. return -1;
  115. }
  116. memcpy(((str*)ptr)->s, ((str*)val2)->s, ((str*)val2)->len);
  117. ((str*)ptr)->len = ((str*)val2)->len;
  118. ((str*)ptr)->s[((str*)ptr)->len] = 0;
  119. break;
  120. case PARAM_INT:
  121. *((int*)ptr) = (int)(long)val2;
  122. break;
  123. }
  124. }
  125. }
  126. else {
  127. LM_ERR("parameter <%s> of type <%d> not found in module <%s>\n",
  128. name, type, t->exports.name);
  129. regfree(&preg);
  130. pkg_free(reg);
  131. return -3;
  132. }
  133. }
  134. }
  135. regfree(&preg);
  136. pkg_free(reg);
  137. if (!mod_found) {
  138. LM_ERR("No module matching <%s> found\n", regex);
  139. return -4;
  140. }
  141. return 0;
  142. }