modparam.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. */
  32. #include "modparam.h"
  33. #include "dprint.h"
  34. #include "mem/mem.h"
  35. #include <sys/types.h>
  36. #include <regex.h>
  37. #include <string.h>
  38. int set_mod_param(char* _mod, char* _name, modparam_t _type, void* _val)
  39. {
  40. void* ptr;
  41. if (!_mod) {
  42. LOG(L_ERR, "set_mod_param(): Invalid _mod parameter value\n");
  43. return -1;
  44. }
  45. if (!_name) {
  46. LOG(L_ERR, "set_mod_param(): Invalid _name parameter value\n");
  47. return -2;
  48. }
  49. ptr = find_param_export(_mod, _name, _type);
  50. if (!ptr) {
  51. LOG(L_ERR, "set_mod_param(): Parameter not found\n");
  52. return -3;
  53. }
  54. switch(_type) {
  55. case STR_PARAM:
  56. *((char**)ptr) = strdup((char*)_val);
  57. break;
  58. case INT_PARAM:
  59. *((int*)ptr) = (int)(long)_val;
  60. break;
  61. }
  62. return 0;
  63. }
  64. int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
  65. {
  66. struct sr_module* t;
  67. param_export_t* param;
  68. regex_t preg;
  69. int mod_found, len;
  70. char* reg;
  71. len = strlen(regex);
  72. reg = pkg_malloc(len + 2 + 1);
  73. if (reg == 0) {
  74. LOG(L_ERR, "set_mod_param_regex(): No memory left\n");
  75. return -1;
  76. }
  77. reg[0] = '^';
  78. memcpy(reg + 1, regex, len);
  79. reg[len + 1] = '$';
  80. reg[len + 2] = '\0';
  81. if (regcomp(&preg, reg, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
  82. LOG(L_ERR, "set_mod_param_regex(): Error while compiling regular expression\n");
  83. pkg_free(reg);
  84. return -2;
  85. }
  86. mod_found = 0;
  87. for(t = modules; t; t = t->next) {
  88. if (regexec(&preg, t->exports->name, 0, 0, 0) == 0) {
  89. DBG("set_mod_param_regex: %s matches module %s\n", regex, t->exports->name);
  90. mod_found = 1;
  91. for(param=t->exports->params;param && param->name ; param++) {
  92. if ((strcmp(name, param->name) == 0) &&
  93. (param->type == type)) {
  94. DBG("set_mod_param_regex: found <%s> in module %s [%s]\n",
  95. name, t->exports->name, t->path);
  96. switch(type) {
  97. case STR_PARAM:
  98. *((char**)(param->param_pointer)) = strdup((char*)val);
  99. break;
  100. case INT_PARAM:
  101. *((int*)(param->param_pointer)) = (int)(long)val;
  102. break;
  103. }
  104. break;
  105. }
  106. }
  107. if (!param || !param->name) {
  108. LOG(L_ERR, "set_mod_param_regex: parameter <%s> not found in module <%s>\n",
  109. name, t->exports->name);
  110. regfree(&preg);
  111. pkg_free(reg);
  112. return -3;
  113. }
  114. }
  115. }
  116. regfree(&preg);
  117. if (!mod_found) {
  118. LOG(L_ERR, "set_mod_param_regex: No module matching %s found\n|", regex);
  119. pkg_free(reg);
  120. return -4;
  121. }
  122. pkg_free(reg);
  123. return 0;
  124. }