modparam.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * Copyright (C) 2001-2003 FhG Fokus
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. * History:
  29. * -------
  30. * 2003-03-20 regex support in modparam (janakj)
  31. * 2004-03-12 extra flag USE_FUNC_PARAM added to modparam type -
  32. * instead of copying the param value, a func is called (bogdan)
  33. * 2005-07-01 PARAM_STRING & PARAM_STR support
  34. */
  35. #include "modparam.h"
  36. #include "dprint.h"
  37. #include "mem/mem.h"
  38. #include <sys/types.h>
  39. #include <regex.h>
  40. #include <string.h>
  41. int set_mod_param(char* _mod, char* _name, modparam_t _type, void* _val)
  42. {
  43. return set_mod_param_regex(_mod, _name, _type, _val);
  44. }
  45. int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
  46. {
  47. struct sr_module* t;
  48. regex_t preg;
  49. int mod_found, len;
  50. char* reg;
  51. void *ptr, *val2;
  52. modparam_t param_type;
  53. str s;
  54. if (!regex) {
  55. LOG(L_ERR, "set_mod_param_regex(): Invalid mod parameter value\n");
  56. return -5;
  57. }
  58. if (!name) {
  59. LOG(L_ERR, "set_mod_param_regex(): Invalid name parameter value\n");
  60. return -6;
  61. }
  62. len = strlen(regex);
  63. reg = pkg_malloc(len + 2 + 1);
  64. if (reg == 0) {
  65. LOG(L_ERR, "set_mod_param_regex(): No memory left\n");
  66. return -1;
  67. }
  68. reg[0] = '^';
  69. memcpy(reg + 1, regex, len);
  70. reg[len + 1] = '$';
  71. reg[len + 2] = '\0';
  72. if (regcomp(&preg, reg, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
  73. LOG(L_ERR, "set_mod_param_regex(): Error while compiling regular expression\n");
  74. pkg_free(reg);
  75. return -2;
  76. }
  77. mod_found = 0;
  78. for(t = modules; t; t = t->next) {
  79. if (regexec(&preg, t->exports->c.name, 0, 0, 0) == 0) {
  80. DBG("set_mod_param_regex: '%s' matches module '%s'\n",
  81. regex, t->exports->c.name);
  82. mod_found = 1;
  83. /* PARAM_STR (PARAM_STRING) may be assigned also to PARAM_STRING(PARAM_STR) so let get both module param */
  84. ptr = find_param_export(t, name, type | ((type & (PARAM_STR|PARAM_STRING))?PARAM_STR|PARAM_STRING:0), &param_type);
  85. if (ptr) {
  86. /* type casting */
  87. if (type == PARAM_STRING && PARAM_TYPE_MASK(param_type) == PARAM_STR) {
  88. s.s = (char*)val;
  89. s.len = s.s ? strlen(s.s) : 0;
  90. val2 = &s;
  91. } else if (type == PARAM_STR && PARAM_TYPE_MASK(param_type) == PARAM_STRING) {
  92. val2 = s.s; /* zero terminator expected */
  93. } else {
  94. val2 = val;
  95. }
  96. DBG("set_mod_param_regex: found <%s> in module %s [%s]\n",
  97. name, t->exports->c.name, t->path);
  98. if (param_type & PARAM_USE_FUNC) {
  99. if ( ((param_func_t)(ptr))(param_type, val2) < 0) {
  100. regfree(&preg);
  101. pkg_free(reg);
  102. return -4;
  103. }
  104. }
  105. else {
  106. switch(PARAM_TYPE_MASK(param_type)) {
  107. case PARAM_STRING:
  108. *((char**)ptr) = pkg_malloc(strlen((char*)val2)+1);
  109. if (!*((char**)ptr)) {
  110. LOG(L_ERR, "set_mod_param_regex(): No memory left\n");
  111. return -1;
  112. }
  113. strcpy(*((char**)ptr), (char*)val2);
  114. break;
  115. case PARAM_STR:
  116. ((str*)ptr)->s = pkg_malloc(((str*)val2)->len+1);
  117. if (!((str*)ptr)->s) {
  118. LOG(L_ERR, "set_mod_param_regex(): No memory left\n");
  119. return -1;
  120. }
  121. memcpy(((str*)ptr)->s, ((str*)val2)->s, ((str*)val2)->len);
  122. ((str*)ptr)->len = ((str*)val2)->len;
  123. ((str*)ptr)->s[((str*)ptr)->len] = 0;
  124. break;
  125. case PARAM_INT:
  126. *((int*)ptr) = (int)(long)val2;
  127. break;
  128. }
  129. }
  130. }
  131. else {
  132. LOG(L_ERR, "set_mod_param_regex: parameter <%s> not found in"
  133. " module <%s>\n", name, t->exports->c.name);
  134. regfree(&preg);
  135. pkg_free(reg);
  136. return -3;
  137. }
  138. }
  139. }
  140. regfree(&preg);
  141. pkg_free(reg);
  142. if (!mod_found) {
  143. LOG(L_ERR, "set_mod_param_regex: No module matching <%s> found\n", regex);
  144. return -4;
  145. }
  146. return 0;
  147. }