/* If a proper integer is passed that is >= -1, then newValue will be set to * val, and 0 returned. Otherwise -1 is returned. */ static int set_if_valid_threshold(modparam_t type, void *val, char *varStr, int *newVal) { if (val==0) { LM_ERR("%s called with a null value!\n", varStr); return -1; } if (PARAM_TYPE_MASK(type) != INT_PARAM) { LM_ERR("%s called with type %d instead of %d!\n", varStr, type, INT_PARAM); return -1; } int new_threshold = (int)(long)(int *)val; if (new_threshold < -1) { LM_ERR("%s called with an invalid threshold=%d!\n", varStr, new_threshold); return -1; } *newVal = new_threshold; return 0; }
/*! * \brief Find a parameter with given type * \param mod module * \param name parameter name * \param type_mask parameter mask * \param param_type parameter type * \return parameter address in memory, if there is no such parameter, NULL is returned */ void* find_param_export(struct sr_module* mod, char* name, modparam_t type_mask, modparam_t *param_type) { param_export_t* param; if (!mod) return 0; for(param = mod->exports.params ;param && param->name ; param++) { if ((strcmp(name, param->name) == 0) && ((param->type & PARAM_TYPE_MASK(type_mask)) != 0)) { LM_DBG("found <%s> in module %s [%s]\n", name, mod->exports.name, mod->path); *param_type = param->type; return param->param_pointer; } } LM_DBG("parameter <%s> not found in module <%s>\n", name, mod->exports.name); return 0; }
/* * parse timeout value in syntax: nnn.mmm (sec/ms) */ static int set_tv(unsigned type, const char* val, struct timeval* tv) { char *eptr; unsigned long s, ms; double dv; if (PARAM_TYPE_MASK(type) != STR_PARAM) { LM_ERR("type of parameter is no STR\n"); return -1; } if (!val || !*val) { LM_ERR("empty parameter\n"); return -1; } errno = 0; dv = strtod(val, &eptr); if (*eptr) { LM_ERR("invalid parameter string\n"); return -2; } if ( errno || dv > (double)MAX_TIMEOUT_S || (dv && dv < ((double)MIN_TIMEOUT_MS)/1000)) { LM_ERR("value must be between 0.%u and %u.0\n", MIN_TIMEOUT_MS, MAX_TIMEOUT_S); return -3; } s = (unsigned)dv; dv -= (double)s; ms = (unsigned)(dv * 1000); tv->tv_sec = (time_t)s; tv->tv_usec = (suseconds_t)ms; return 0; }
int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val) { struct sr_module* t; param_export_t* param; regex_t preg; int mod_found, param_found, len; char* reg; int n; len = strlen(regex); reg = pkg_malloc(len + 2 + 2 + 1); if (reg == 0) { LM_ERR("no pkg memory left\n"); return -1; } reg[0] = '^'; reg[1] = '('; memcpy(reg + 2, regex, len); reg[len + 2] = ')'; reg[len + 3] = '$'; reg[len + 4] = '\0'; if (regcomp(&preg, reg, REG_EXTENDED | REG_NOSUB | REG_ICASE)) { LM_ERR("failed to compile regular expression\n"); pkg_free(reg); return -2; } mod_found = 0; for(t = modules; t; t = t->next) { if (regexec(&preg, t->exports->name, 0, 0, 0) == 0) { LM_DBG("%s matches module %s\n",regex, t->exports->name); mod_found = 1; param_found = 0; for(param=t->exports->params;param && param->name ; param++) { if (strcmp(name, param->name) == 0) { param_found = 1; if (PARAM_TYPE_MASK(param->type) == type) { LM_DBG("found <%s> in module %s [%s]\n", name, t->exports->name, t->path); if (param->type&USE_FUNC_PARAM) { n = ((param_func_t)(param->param_pointer))(type, val ); if (n<0) return -4; } else { switch(type) { case STR_PARAM: *((char**)(param->param_pointer)) = strdup((char*)val); break; case INT_PARAM: *((int*)(param->param_pointer)) = (int)(long)val; break; } } break; } } } if (!param || !param->name) { if (param_found) LM_ERR("type mismatch for parameter <%s> in module <%s>\n", name, t->exports->name); else LM_ERR("parameter <%s> not found in module <%s>\n", name, t->exports->name); regfree(&preg); pkg_free(reg); return -3; } } } regfree(&preg); if (!mod_found) { LM_ERR("no module matching %s found\n", regex); pkg_free(reg); return -4; } pkg_free(reg); return 0; }
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; void *ptr, *val2; modparam_t param_type; str s; if (!regex) { LM_ERR("Invalid mod parameter value\n"); return -5; } if (!name) { LM_ERR("Invalid name parameter value\n"); return -6; } len = strlen(regex); reg = pkg_malloc(len + 2 + 1); if (reg == 0) { LM_ERR("No memory left\n"); return -1; } reg[0] = '^'; memcpy(reg + 1, regex, len); reg[len + 1] = '$'; reg[len + 2] = '\0'; if (regcomp(&preg, reg, REG_EXTENDED | REG_NOSUB | REG_ICASE)) { LM_ERR("Error while compiling regular expression\n"); pkg_free(reg); return -2; } mod_found = 0; for(t = modules; t; t = t->next) { if (regexec(&preg, t->exports.name, 0, 0, 0) == 0) { DBG("set_mod_param_regex: '%s' matches module '%s'\n", regex, t->exports.name); mod_found = 1; /* PARAM_STR (PARAM_STRING) may be assigned also to PARAM_STRING(PARAM_STR) so let get both module param */ ptr = find_param_export(t, name, type | ((type & (PARAM_STR|PARAM_STRING))?PARAM_STR|PARAM_STRING:0), ¶m_type); if (ptr) { /* type casting */ if (type == PARAM_STRING && PARAM_TYPE_MASK(param_type) == PARAM_STR) { s.s = (char*)val; s.len = s.s ? strlen(s.s) : 0; val2 = &s; } else if (type == PARAM_STR && PARAM_TYPE_MASK(param_type) == PARAM_STRING) { s = *(str*)val; val2 = s.s; /* zero terminator expected */ } else { val2 = val; } DBG("set_mod_param_regex: found <%s> in module %s [%s]\n", name, t->exports.name, t->path); if (param_type & PARAM_USE_FUNC) { if ( ((param_func_t)(ptr))(param_type, val2) < 0) { regfree(&preg); pkg_free(reg); return -4; } } else { switch(PARAM_TYPE_MASK(param_type)) { case PARAM_STRING: *((char**)ptr) = pkg_malloc(strlen((char*)val2)+1); if (!*((char**)ptr)) { LM_ERR("No memory left\n"); regfree(&preg); pkg_free(reg); return -1; } strcpy(*((char**)ptr), (char*)val2); break; case PARAM_STR: ((str*)ptr)->s = pkg_malloc(((str*)val2)->len+1); if (!((str*)ptr)->s) { LM_ERR("No memory left\n"); regfree(&preg); pkg_free(reg); 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; break; case PARAM_INT: *((int*)ptr) = (int)(long)val2; break; } } } else { LM_ERR("parameter <%s> of type <%d> not found in module <%s>\n", name, type, t->exports.name); regfree(&preg); pkg_free(reg); return -3; } } } regfree(&preg); pkg_free(reg); if (!mod_found) { LM_ERR("No module matching <%s> found\n", regex); return -4; } return 0; }