modparam.c
93d87145
 /*
53c7e0f1
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
6a0f4382
  * This file is part of Kamailio, a free SIP server.
7dd0b342
  *
6a0f4382
  * Kamailio is free software; you can redistribute it and/or modify
7dd0b342
  * 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
  *
6a0f4382
  * Kamailio is distributed in the hope that it will be useful,
7dd0b342
  * 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.
  *
fb851d7e
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
270255ff
  *
93d87145
  */
 
1d0661db
 /*!
  * \file
6a0f4382
  * \brief Kamailio core :: Configuration parameters for modules (modparams)
1d0661db
  * \ingroup core
  * Module: \ref core
  */
 
7dd0b342
 
ad79ca94
 #include "modparam.h"
 #include "dprint.h"
270255ff
 #include "mem/mem.h"
 #include <sys/types.h>
 #include <regex.h>
ad79ca94
 #include <string.h>
 
 int set_mod_param(char* _mod, char* _name, modparam_t _type, void* _val)
 {
6308a4ee
 	return set_mod_param_regex(_mod, _name, _type, _val);
ad79ca94
 }
270255ff
 
 int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
 {
 	struct sr_module* t;
 	regex_t preg;
 	int mod_found, len;
 	char* reg;
6308a4ee
 	void *ptr, *val2;
fb851d7e
 	modparam_t param_type;
6308a4ee
 	str s;
 
 	if (!regex) {
eadbcc18
 		LM_ERR("Invalid mod parameter value\n");
6308a4ee
 		return -5;
 	}
 	if (!name) {
eadbcc18
 		LM_ERR("Invalid name parameter value\n");
6308a4ee
 		return -6;
 	}
270255ff
 
 	len = strlen(regex);
 	reg = pkg_malloc(len + 2 + 1);
 	if (reg == 0) {
eadbcc18
 		LM_ERR("No memory left\n");
270255ff
 		return -1;
 	}
 	reg[0] = '^';
 	memcpy(reg + 1, regex, len);
 	reg[len + 1] = '$';
 	reg[len + 2] = '\0';
fb851d7e
 
270255ff
 	if (regcomp(&preg, reg, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
eadbcc18
 		LM_ERR("Error while compiling regular expression\n");
270255ff
 		pkg_free(reg);
 		return -2;
 	}
 
fb851d7e
 	mod_found = 0;
270255ff
 	for(t = modules; t; t = t->next) {
dee21fff
 		if (regexec(&preg, t->exports.name, 0, 0, 0) == 0) {
c1836c5c
 			LM_DBG("'%s' matches module '%s'\n", regex, t->exports.name);
270255ff
 			mod_found = 1;
f141bc93
 			/* PARAM_STR (PARAM_STRING) may be assigned also to PARAM_STRING(PARAM_STR) so let get both module param */
dd807399
 			ptr = find_param_export(t, name, type | ((type & (PARAM_STR|PARAM_STRING))?PARAM_STR|PARAM_STRING:0), &param_type);
fb851d7e
 			if (ptr) {
bb32b8ca
 				     /* type casting */
6308a4ee
 				if (type == PARAM_STRING && PARAM_TYPE_MASK(param_type) == PARAM_STR) {
 					s.s = (char*)val;
bb32b8ca
 					s.len = s.s ? strlen(s.s) : 0;
6308a4ee
 					val2 = &s;
 				} else if (type == PARAM_STR && PARAM_TYPE_MASK(param_type) == PARAM_STRING) {
97727e80
 					s = *(str*)val;
bb32b8ca
 					val2 = s.s;	/* zero terminator expected */
 				} else {
6308a4ee
 					val2 = val;
bb32b8ca
 				}
c1836c5c
 				LM_DBG("found <%s> in module %s [%s]\n", name, t->exports.name, t->path);
fb851d7e
 				if (param_type & PARAM_USE_FUNC) {
6308a4ee
 					if ( ((param_func_t)(ptr))(param_type, val2) < 0) {
fb851d7e
 						regfree(&preg);
 						pkg_free(reg);
 						return -4;
 					}
 				}
 				else {
 					switch(PARAM_TYPE_MASK(param_type)) {
 						case PARAM_STRING:
e5b51405
 							*((char**)ptr) = pkg_malloc(strlen((char*)val2)+1);
 							if (!*((char**)ptr)) {
eadbcc18
 								LM_ERR("No memory left\n");
b1106fb4
 								regfree(&preg);
 								pkg_free(reg);
e5b51405
 								return -1;
 							}
 							strcpy(*((char**)ptr), (char*)val2);
fb851d7e
 							break;
 
 						case PARAM_STR:
e5b51405
 							((str*)ptr)->s = pkg_malloc(((str*)val2)->len+1);
 							if (!((str*)ptr)->s) {
eadbcc18
 								LM_ERR("No memory left\n");
b1106fb4
 								regfree(&preg);
 								pkg_free(reg);
e5b51405
 								return -1;
 							}
 							memcpy(((str*)ptr)->s, ((str*)val2)->s, ((str*)val2)->len);
 							((str*)ptr)->len = ((str*)val2)->len;
 							((str*)ptr)->s[((str*)ptr)->len] = 0;
fb851d7e
 							break;
 
 						case PARAM_INT:
6308a4ee
 							*((int*)ptr) = (int)(long)val2;
fb851d7e
 							break;
270255ff
 					}
 				}
 			}
fb851d7e
 			else {
eadbcc18
 				LM_ERR("parameter <%s> of type <%d> not found in module <%s>\n",
 						name, type, t->exports.name);
270255ff
 				regfree(&preg);
 				pkg_free(reg);
 				return -3;
 			}
 		}
 	}
 
 	regfree(&preg);
fb851d7e
 	pkg_free(reg);
270255ff
 	if (!mod_found) {
eadbcc18
 		LM_ERR("No module matching <%s> found\n", regex);
270255ff
 		return -4;
 	}
 	return 0;
 }