Coverage Report

Created: 2025-07-12 06:14

/src/opensips/modparam.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2001-2003 FhG Fokus
3
 *
4
 * This file is part of opensips, a free SIP server.
5
 *
6
 * opensips 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
 * opensips 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
 * History:
21
 * -------
22
 * 2003-03-20  regex support in modparam (janakj)
23
 * 2004-03-12  extra flag USE_FUNC_PARAM added to modparam type -
24
 *             instead of copying the param value, a func is called (bogdan)
25
 */
26
27
/*!
28
 * \file
29
 * \brief Module parameter configuration
30
 */
31
32
33
#include "modparam.h"
34
#include "dprint.h"
35
#include "mem/mem.h"
36
#include "ut.h"
37
#include <sys/types.h>
38
#include <regex.h>
39
#include <string.h>
40
41
int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
42
0
{
43
0
  struct sr_module* t;
44
0
  const param_export_t* param;
45
0
  regex_t preg;
46
0
  int mod_found, param_found, len;
47
0
  char* reg;
48
0
  int n;
49
50
0
  len = strlen(regex);
51
0
  reg = pkg_malloc(len + 2 + 2 + 1);
52
0
  if (reg == 0) {
53
0
    LM_ERR("no pkg memory left\n");
54
0
    return -1;
55
0
  }
56
0
  reg[0] = '^';
57
0
  reg[1] = '(';
58
0
  memcpy(reg + 2, regex, len);
59
0
  reg[len + 2] = ')';
60
0
  reg[len + 3] = '$';
61
0
  reg[len + 4] = '\0';
62
63
0
  if (regcomp(&preg, reg, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
64
0
    LM_ERR("failed to compile regular expression\n");
65
0
    pkg_free(reg);
66
0
    return -2;
67
0
  }
68
69
0
  mod_found = 0;
70
71
0
  for(t = modules; t; t = t->next) {
72
0
    if (regexec(&preg, t->exports->name, 0, 0, 0) == 0) {
73
0
      LM_DBG("%s matches module %s\n",regex, t->exports->name);
74
0
      mod_found = 1;
75
0
      param_found = 0;
76
77
0
      for(param=t->exports->params;param && param->name ; param++) {
78
79
0
        if (strcmp(name, param->name) == 0) {
80
0
          param_found = 1;
81
82
0
          if (PARAM_TYPE_MASK(param->type) & type) {
83
0
            LM_DBG("found <%s> in module %s [%s]\n",
84
0
              name, t->exports->name, t->path);
85
86
0
            if (param->type&USE_FUNC_PARAM) {
87
0
              n = ((param_func_t)(param->param_pointer))(type, val );
88
0
              if (n<0)
89
0
                return -4;
90
0
            } else {
91
0
              switch(type) {
92
0
                case STR_PARAM:
93
0
                  *((char**)(param->param_pointer)) =
94
0
                    strdup((char*)val);
95
0
                  break;
96
0
                case INT_PARAM:
97
0
                  *((int*)(param->param_pointer)) =
98
0
                    (int)(long)val;
99
0
                  break;
100
0
              }
101
0
            }
102
103
            /* register any module deps imposed by this parameter */
104
0
            if (add_modparam_dependencies(t, param) != 0) {
105
0
              LM_ERR("failed to add modparam dependencies!\n");
106
0
              return E_BUG;
107
0
            }
108
109
0
            break;
110
0
          }
111
0
        }
112
0
      }
113
114
0
      if (!param || !param->name) {
115
0
        if (param_found)
116
0
          LM_ERR("type mismatch for parameter <%s> in module <%s>\n",
117
0
                  name, t->exports->name);
118
0
        else
119
0
          LM_ERR("parameter <%s> not found in module <%s>\n",
120
0
                name, t->exports->name);
121
122
0
        regfree(&preg);
123
0
        pkg_free(reg);
124
0
        return -3;
125
0
      }
126
0
    }
127
0
  }
128
129
0
  regfree(&preg);
130
0
  if (!mod_found) {
131
0
    LM_ERR("no module matching %s found\n", regex);
132
0
    pkg_free(reg);
133
0
    return -4;
134
0
  }
135
136
0
  pkg_free(reg);
137
0
  return 0;
138
0
}