1 Star 0 Fork 0

souhoiryo / opensips

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mod_fix.c 23.55 KB
一键复制 编辑 原始数据 按行查看 历史
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
/*
*$Id$
*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* 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
*
* opensips is distributed in the hope that it will be useful,
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*!
* \file
* \brief Generic fixup functions for module function parameter.
* - \ref FixupNameFormat
*/
#include <stdio.h>
#include <stdlib.h>
#include "mem/mem.h"
#include "str.h"
#include "ut.h"
#include "error.h"
#include "pvar.h"
#include "mod_fix.h"
/*!
* \page FixupNameFormat Fixup Naming format
* NAMING FORMAT
* === fixup functions ===
* + fixup_type1_type2(...)
* - type1 - is the type the fist parameter gets converted to
* - type2 - is the type the second parameter gets converted to
* + if the parameter is missing, then use 'null'
* + if the parameter is not converted, then use 'none'
*
* === fixup free functions ===
* + free_fixup_type1_type2(...)
* - type1 and type2 are same as for fixup function
*
* === helper functions ===
* + functions to be used internaly for fixup/free functions
* + fixup_type(...)
* + fixup_free_type(...)
* - type - is the type of the parameter that gets converted to/freed
*/
/*! \brief
* helper function
* Convert char* parameter to str* parameter
*/
int fixup_str(void** param)
{
str* s;
s = (str*)pkg_malloc(sizeof(str));
if (!s) {
LM_ERR("no more pkg memory\n");
return E_UNSPEC;
}
s->s = (char*)*param;
s->len = strlen(s->s);
*param = (void*)s;
return 0;
}
/*! \brief
* - helper function
* free the str* parameter
*/
int fixup_free_str(void** param)
{
if(*param) {
pkg_free(*param);
*param = 0;
}
return 0;
}
/*! \brief
* fixup for functions that get one parameter
* - first parameter is converted to str*
*/
int fixup_str_null(void** param, int param_no)
{
if(param_no != 1)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_str(param);
}
/*! \brief
* fixup for functions that get two parameters
* - first parameter is converted to str*
* - second parameter is converted to str*
*/
int fixup_str_str(void** param, int param_no)
{
if (param_no != 1 && param_no != 2 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_str(param);
}
/*! \brief
* fixup free for functions that get one parameter
* - first parameter was converted to str*
*/
int fixup_free_str_null(void** param, int param_no)
{
if(param_no != 1)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_free_str(param);
}
/*! \brief
* fixup free for functions that get two parameters
* - first parameter was converted to str*
* - second parameter was converted to str*
*/
int fixup_free_str_str(void** param, int param_no)
{
if (param_no != 1 && param_no != 2 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_free_str(param);
}
/*! \brief
* - helper function
* Convert char* parameter to unsigned int
* - the input parameter must be pkg-allocated and will be freed by function
* (it is how it comes from the config parser)
*/
int fixup_uint(void** param)
{
unsigned int ui;
str s;
s.s = (char*)*param;
s.len = strlen(s.s);
if(str2int(&s, &ui)==0)
{
pkg_free(*param);
*param=(void *)(unsigned long)ui;
return 0;
}
LM_ERR("bad number <%s>\n", (char *)(*param));
return E_CFG;
}
/*! \brief
* fixup for functions that get one parameter
* - first parameter is converted to unsigned int
*/
int fixup_uint_null(void** param, int param_no)
{
if(param_no != 1)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_uint(param);
}
/*! \brief
* fixup for functions that get two parameters
* - first parameter is converted to unsigned int
* - second parameter is converted to unsigned int
*/
int fixup_uint_uint(void** param, int param_no)
{
if (param_no != 1 && param_no != 2 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_uint(param);
}
/*! \brief
* - helper function
* Convert char* parameter to signed int
* - the input parameter must be pkg-allocated and will be freed by function
* (it is how it comes from the config parser)
*/
int fixup_sint( void** param)
{
int si;
str s;
s.s = (char*)*param;
s.len = strlen(s.s);
if(str2sint(&s, &si)==0)
{
pkg_free(*param);
*param=(void *)(unsigned long)si;
return 0;
}
LM_ERR("bad number <%s>\n", (char *)(*param));
return E_CFG;
}
/*! \brief
* fixup for functions that get one parameter
* - first parameter is converted to signed int
*/
int fixup_sint_null(void** param, int param_no)
{
if(param_no != 1)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_sint(param);
}
/*! \brief
* fixup for functions that get two parameters
* - first parameter is converted to signed int
* - second parameter is converted to signed int
*/
int fixup_sint_sint(void** param, int param_no)
{
if (param_no != 1 && param_no != 2 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_sint(param);
}
#if 0
/*! \brief
* fixup for functions that get two parameters
* - first parameter is converted to signed int
* - second parameter is converted to unsigned int
*/
int fixup_sint_uint(void** param, int param_no)
{
if (param_no != 1 && param_no != 2 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
if (param_no == 1)
return fixup_sint(param);
return fixup_uint(param);
}
/*! \brief
* fixup for functions that get two parameters
* - first parameter is converted to unsigned int
* - second parameter is converted to signed int
*/
int fixup_uint_sint(void** param, int param_no)
{
if (param_no != 1 && param_no != 2 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
if (param_no == 1)
return fixup_uint(param);
return fixup_sint(param);
}
#endif
/*! \brief
* - helper function: Convert char* parameter to regular expression structure
* - the input parameter must be pkg-allocated and will be freed by function
* (it is how it comes from the config parser)
*/
static int fixup_regexp(void** param, int rflags)
{
regex_t* re;
if ((re=pkg_malloc(sizeof(regex_t)))==0) {
LM_ERR("no more pkg memory\n");
return E_OUT_OF_MEM;
}
if (regcomp(re, *param, (REG_EXTENDED|REG_ICASE|REG_NEWLINE)&(~rflags))) {
pkg_free(re);
LM_ERR("bad re %s\n", (char*)*param);
return E_BAD_RE;
}
/* free string */
pkg_free(*param);
/* replace it with the compiled re */
*param=re;
return 0;
}
static int fixup_regexp_dynamic(void** param,int rflags)
{
gparam_p gp;
int ret;
regex_t* re;
ret = fixup_sgp(param);
if (ret < 0)
return ret;
gp = (gparam_p)*param;
if (gp->type == GPARAM_TYPE_STR) {
/* we can compile the regex right now */
if ((re=pkg_malloc(sizeof(regex_t)))==0) {
LM_ERR("no more pkg memory\n");
return E_OUT_OF_MEM;
}
if (regcomp(re, gp->v.sval.s, (REG_EXTENDED|REG_ICASE|REG_NEWLINE)&(~rflags))) {
pkg_free(re);
LM_ERR("bad re %s\n", (char*)*param);
return E_BAD_RE;
}
/* replace it with the compiled re */
gp->type=GPARAM_TYPE_REGEX;
gp->v.re=re;
return 0;
}
/* regex will be compiled at runtime */
return 0;
}
/*! \brief
* - helper function: free the regular expression parameter
*/
int fixup_free_regexp(void** param)
{
if(*param)
{
regfree((regex_t*)(*param));
pkg_free(*param);
*param = 0;
}
return 0;
}
/*! \brief
* fixup for functions that get one parameter
* - first parameter is converted to regular expression structure
*/
int fixup_regexp_null(void** param, int param_no)
{
if(param_no != 1)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_regexp(param, 0);
}
/*! \brief
* fixup for functions that get one parameter
* - first parameter is converted to regular expression structure - accepts non-plaintext input
*/
int fixup_regexp_dynamic_null(void** param, int param_no)
{
if(param_no != 1)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_regexp_dynamic(param, 0);
}
static char *re_buff=NULL;
static int re_buff_len = 0;
regex_t* fixup_get_regex(struct sip_msg* msg, gparam_p gp,int *do_free)
{
pv_value_t value;
str val;
regex_t* ret_re;
if(gp->type==GPARAM_TYPE_REGEX) {
/* pre-allocated at startup - just return it */
if (do_free)
*do_free=0;
return gp->v.re;
}
if(gp->type==GPARAM_TYPE_PVS) {
if(pv_get_spec_value(msg, gp->v.pvs, &value)!=0
|| value.flags&PV_VAL_NULL || !(value.flags&PV_VAL_STR)){
LM_ERR("no valid PV value found (error in scripts)\n");
return NULL;
}
val = value.rs;
goto build_re;
}
if(gp->type==GPARAM_TYPE_PVE){
if(pv_printf_s( msg, gp->v.pve, &val)!=0){
LM_ERR("cannot print the PV-formated string\n");
return NULL;
}
goto build_re;
}
return NULL;
build_re:
if (val.len + 1 > re_buff_len) {
re_buff = pkg_realloc(re_buff,val.len + 1);
if (re_buff == NULL) {
LM_ERR("No more pkg \n");
return NULL;
}
re_buff_len = val.len + 1;
}
memcpy(re_buff,val.s,val.len);
re_buff[val.len] = 0;
if ((ret_re=pkg_malloc(sizeof(regex_t)))==0) {
LM_ERR("no more pkg memory\n");
return NULL;
}
if (regcomp(ret_re, re_buff, (REG_EXTENDED|REG_ICASE|REG_NEWLINE))) {
pkg_free(ret_re);
LM_ERR("bad re %s\n", re_buff);
return NULL;
}
if (do_free)
*do_free=1;
return ret_re;
}
/*! \brief
* fixup for functions that get one parameter
* - first parameter is converted to regular expression structure
* where "match-any-character" operators also match a newline
*/
int fixup_regexpNL_null(void** param, int param_no)
{
if(param_no != 1)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_regexp(param, REG_NEWLINE);
}
/**
* fixup free for functions that get one parameter
* - first parameter was converted to regular expression
*/
int fixup_free_regexp_null(void** param, int param_no)
{
if(param_no != 1)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_free_regexp(param);
}
/*! \brief
* fixup for functions that get two parameters
* - first parameter is converted to regular expression structure
* - second parameter is not converted
*/
int fixup_regexp_none(void** param, int param_no)
{
if (param_no != 1 && param_no != 2 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
if (param_no == 1)
return fixup_regexp(param, 0);
return 0;
}
/*! \brief
* fixup for functions that get two parameters
* - first parameter is converted to regular expression structure
* where "match-any-character" operators also match a newline
* - second parameter is not converted
*/
int fixup_regexpNL_none(void** param, int param_no)
{
if (param_no != 1 && param_no != 2 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
if (param_no == 1)
return fixup_regexp(param, REG_NEWLINE);
return 0;
}
/**
* fixup free for functions that get two parameters
* - first parameter was converted to regular expression
* - second parameter was notconverted
*/
int fixup_free_regexp_none(void** param, int param_no)
{
if (param_no != 1 && param_no != 2 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
if (param_no == 1)
return fixup_free_regexp(param);
return 0;
}
/*! \brief
* - helper function: Convert char* parameter to PV spec structure
*/
int fixup_pvar(void **param)
{
pv_spec_t *sp;
str s;
sp = (pv_spec_t*)pkg_malloc(sizeof(pv_spec_t));
if (sp == 0) {
LM_ERR("no pkg memory left\n");
return E_UNSPEC;
}
s.s = (char*)*param; s.len = strlen(s.s);
if (pv_parse_spec(&s, sp) == 0) {
LM_ERR("parsing of pseudo variable %s failed!\n", (char*)*param);
pkg_free(sp);
return E_UNSPEC;
}
if (sp->type == PVT_NULL) {
LM_ERR("bad pseudo variable\n");
pkg_free(sp);
return E_UNSPEC;
}
*param = (void*)sp;
return 0;
}
/*! \brief
* - helper function: free the PV parameter
*/
int fixup_free_pvar(void** param)
{
if (*param) {
pv_spec_free((pv_spec_t*)*param);
}
return 0;
}
/*! \brief
* fixup for functions that get one parameter
* - first parameter is converted to PV spec
*/
int fixup_pvar_null(void** param, int param_no)
{
if(param_no != 1)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_pvar(param);
}
/*! \brief
* fixup free for functions that get one parameter
* - first parameter was converted to PV spec
*/
int fixup_free_pvar_null(void** param, int param_no)
{
if(param_no != 1)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_free_pvar(param);
}
/*! \brief
* fixup for functions that get two parameters
* - both parameters are converted to PV spec
*/
int fixup_pvar_pvar(void** param, int param_no)
{
if (param_no == 1)
{
return fixup_pvar(param);
}
if (param_no != 2)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_pvar(param);
}
/*! \brief
* fixup free for functions that get two parameters
* - both parameters were converted to PV spec
*/
int fixup_free_pvar_pvar(void** param, int param_no)
{
if(param_no == 1)
{
return fixup_free_pvar(param);
}
if (param_no != 2)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_free_pvar(param);
}
/*! \brief
* fixup for functions that get two parameters
* - first parameter is converted to PV spec
* - second parameter is converted to str*
*/
int fixup_pvar_str(void** param, int param_no)
{
if (param_no == 1)
{
return fixup_pvar(param);
}
if (param_no != 2)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_str(param);
}
/*! \brief
* fixup free for functions that get two parameters
* - first parameter was converted to PV spec
* - second parameter was converted to str*
*/
int fixup_free_pvar_str(void** param, int param_no)
{
if(param_no == 1)
{
return fixup_free_pvar(param);
}
if (param_no != 2)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_free_str(param);
}
/*! \brief
* fixup for functions that get three parameters
* - first parameter is converted to PV spec
* - second parameter is converted to str*
* - third parameter is converted to str*
*/
int fixup_pvar_str_str(void** param, int param_no)
{
if (param_no == 1)
{
return fixup_pvar(param);
}
if (param_no != 2 && param_no != 3)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_str(param);
}
/*! \brief
* fixup free for functions that get three parameters
* - first parameter was converted to PV spec
* - second parameter was converted to str*
* - third parameter was converted to str*
*/
int fixup_free_pvar_str_str(void** param, int param_no)
{
if(param_no == 1)
{
return fixup_free_pvar(param);
}
if (param_no != 2 && param_no != 3)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_free_str(param);
}
/*! \brief
* - helper function
* Convert char* parameter to gparam_t (int or PV)
*/
int fixup_igp(void** param)
{
str s;
gparam_p gp = NULL;
gp = (gparam_p)pkg_malloc(sizeof(gparam_t));
if(gp == NULL)
{
LM_ERR("no more memory\n");
return E_UNSPEC;
}
memset(gp, 0, sizeof(gparam_t));
s.s = (char*)*param; s.len = strlen(s.s);
if(s.s[0]==PV_MARKER)
{
gp->type = GPARAM_TYPE_PVS;
gp->v.pvs = (pv_spec_t*)pkg_malloc(sizeof(pv_spec_t));
if (gp->v.pvs == NULL)
{
LM_ERR("no pkg memory left for pv_spec_t\n");
pkg_free(gp);
return E_UNSPEC;
}
if(pv_parse_spec(&s, gp->v.pvs)==NULL)
{
LM_ERR("Unsupported User Field identifier\n");
pkg_free(gp->v.pvs);
pkg_free(gp);
return E_UNSPEC;
}
} else {
gp->type = GPARAM_TYPE_INT;
if(str2sint(&s, &gp->v.ival) != 0)
{
LM_ERR("Bad number <%s>\n", (char*)(*param));
return E_UNSPEC;
}
}
*param = (void*)gp;
return 0;
}
int fixup_sgp(void** param)
{
str s;
gparam_p gp = NULL;
gp = (gparam_p)pkg_malloc(sizeof(gparam_t));
if(gp == NULL)
{
LM_ERR("no more memory\n");
return E_UNSPEC;
}
memset(gp, 0, sizeof(gparam_t));
s.s = (char*)*param; s.len = strlen(s.s);
if(s.s[0]==PV_MARKER)
{
gp->type = GPARAM_TYPE_PVS;
gp->v.pvs = (pv_spec_t*)pkg_malloc(sizeof(pv_spec_t));
if (gp->v.pvs == NULL)
{
LM_ERR("no pkg memory left for pv_spec_t\n");
pkg_free(gp);
return E_UNSPEC;
}
if(pv_parse_spec(&s, gp->v.pvs)==NULL)
{
LM_ERR("Unsupported User Field identifier\n");
pkg_free(gp->v.pvs);
pkg_free(gp);
return E_UNSPEC;
}
} else {
gp->type = GPARAM_TYPE_STR;
gp->v.sval = s;
}
*param = (void*)gp;
return 0;
}
/*! \brief
* fixup for functions that get one parameter
* - first parameter is converted to gparam_t (int or PV)
*/
int fixup_igp_null(void** param, int param_no)
{
if (param_no != 1)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_igp(param);
}
int fixup_sgp_null(void** param, int param_no)
{
if (param_no != 1)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_sgp(param);
}
/*! \brief
* fixup for functions that get two parameters
* - first parameter is converted to gparam_t (int or PV)
* - second parameter is converted to gparam_t (int or PV)
*/
int fixup_igp_igp(void** param, int param_no)
{
if (param_no != 1 && param_no != 2 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_igp(param);
}
/*! \brief
* fixup for functions that get three parameters
* - first parameter is converted to gparam_t (int or PV)
* - second parameter is converted to gparam_t (int or PV)
* - third parameter is converted to gparam_t (int or PV)
*/
int fixup_igp_igp_igp(void** param, int param_no)
{
if (param_no < 1 || param_no > 3 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_igp(param);
}
int fixup_sgp_sgp(void** param, int param_no)
{
if (param_no != 1 && param_no != 2 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_sgp(param);
}
/*! \brief
* fixup for functions that get three parameters
* - first parameter is converted to gparam_t (int or PV)
* - second parameter is converted to PV spec
* - third parameter is converted to PV spec
*/
int fixup_igp_pvar_pvar(void** param, int param_no)
{
if(param_no == 1) {
return fixup_igp(param);
}
if (param_no != 2 && param_no != 3) {
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_pvar(param);
}
/*! \brief
* fixup free for functions that get three parameters
* - first parameter was converted to gparam_t (int or PV)
* - second parameter was converted to PV spec
* - third parameter was converted to PV spec
*/
int fixup_free_igp_pvar_pvar(void** param, int param_no)
{
if(param_no == 1) {
return 0;
}
if (param_no != 2 && param_no != 3) {
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_free_pvar(param);
}
/*! \brief
* - helper function
* Return integer value from a gparam_t
*/
int fixup_get_ivalue(struct sip_msg* msg, gparam_p gp, int *val)
{
pv_value_t value;
if(gp->type==GPARAM_TYPE_INT)
{
*val = gp->v.ival;
return 0;
}
if(pv_get_spec_value(msg, gp->v.pvs, &value)!=0
|| value.flags&PV_VAL_NULL || !(value.flags&PV_VAL_INT))
{
LM_ERR("no valid PV value found (error in scripts)\n");
return -1;
}
*val = value.ri;
return 0;
}
/*! \brief
* - helper function
* Convert char* parameter to gparam_t (str or pv_elem_t)
*/
int fixup_spve(void** param)
{
str s;
gparam_p gp = NULL;
gp = (gparam_p)pkg_malloc(sizeof(gparam_t));
if(gp == NULL)
{
LM_ERR("no more memory\n");
return E_UNSPEC;
}
memset(gp, 0, sizeof(gparam_t));
s.s = (char*)(*param); s.len = strlen(s.s);
if(pv_parse_format(&s, &gp->v.pve)<0)
{
LM_ERR("wrong format[%s]\n", s.s);
return E_UNSPEC;
}
if(gp->v.pve->spec.getf==NULL)
{
gp->type = GPARAM_TYPE_STR;
pv_elem_free_all(gp->v.pve);
gp->v.sval = s;
} else {
gp->type = GPARAM_TYPE_PVE;
}
*param = (void*)gp;
return 0;
}
/*! \brief
* fixup for functions that get one parameter
* - first parameter is converted to gparam_t (str or pv_elem_t)
*/
int fixup_spve_null(void** param, int param_no)
{
if (param_no != 1)
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_spve(param);
}
/*! \brief
* fixup for functions that get two parameters
* - first parameter is converted to gparam_t (str or pv_elem_t)
* - second parameter is converted to gparam_t (str or pv_elem_t)
*/
int fixup_spve_spve(void** param, int param_no)
{
if (param_no != 1 && param_no != 2 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
return fixup_spve(param);
}
/*! \brief
* fixup for functions that get two parameters
* - first parameter is converted to gparam_t (str or pv_elem_t)
* - second parameter is converted to uint
*/
int fixup_spve_uint(void** param, int param_no)
{
if (param_no != 1 && param_no != 2 )
{
LM_ERR("invalid parameter number %d\n", param_no);
return E_UNSPEC;
}
if (param_no == 1)
return fixup_spve(param);
return fixup_uint(param);
}
/*! \brief
* - helper function
* Return string value from a gparam_t
*/
int fixup_get_svalue(struct sip_msg* msg, gparam_p gp, str *val)
{
pv_value_t value;
if(gp->type==GPARAM_TYPE_STR)
{
*val = gp->v.sval;
return 0;
}
if(gp->type==GPARAM_TYPE_PVS)
{
if(pv_get_spec_value(msg, gp->v.pvs, &value)!=0
|| value.flags&PV_VAL_NULL || !(value.flags&PV_VAL_STR))
{
LM_ERR("no valid PV value found (error in scripts)\n");
return -1;
}
*val = value.rs;
return 0;
}
if(gp->type==GPARAM_TYPE_PVE)
{
if(pv_printf_s( msg, gp->v.pve, val)!=0)
{
LM_ERR("cannot print the PV-formated string\n");
return -1;
}
return 0;
}
return -1;
}
/*! \brief
* - helper function
* Return string and/or int value from a gparam_t
*/
int fixup_get_isvalue(struct sip_msg* msg, gparam_p gp,
int *i_val, str *s_val, unsigned int *flags)
{
pv_value_t value;
*flags = 0;
switch(gp->type)
{
case GPARAM_TYPE_INT:
*i_val = gp->v.ival;
*flags |= GPARAM_INT_VALUE_FLAG;
break;
case GPARAM_TYPE_STR:
*s_val = gp->v.sval;
*flags |= GPARAM_STR_VALUE_FLAG;
break;
case GPARAM_TYPE_PVE:
if(pv_printf_s( msg, gp->v.pve, s_val)!=0)
{
LM_ERR("cannot print the PV-formated string\n");
return -1;
}
*flags |= GPARAM_STR_VALUE_FLAG;
break;
case GPARAM_TYPE_PVS:
if(pv_get_spec_value(msg, gp->v.pvs, &value)!=0
|| value.flags&PV_VAL_NULL)
{
LM_ERR("no valid PV value found (error in scripts)\n");
return -1;
}
if(value.flags&PV_VAL_INT)
{
*i_val = value.ri;
*flags |= GPARAM_INT_VALUE_FLAG;
}
if(value.flags&PV_VAL_STR)
{
*s_val = value.rs;
*flags |= GPARAM_STR_VALUE_FLAG;
}
break;
default:
LM_ERR("unexpected gp->type=[%d]\n", gp->type);
return -1;
}
/* Let's convert to int, if possible */
if (!(*flags & GPARAM_INT_VALUE_FLAG) && (*flags & GPARAM_STR_VALUE_FLAG)
&& str2sint(s_val, i_val) == 0)
*flags |= GPARAM_INT_VALUE_FLAG;
if (!*flags) return -1;
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/maplerain/opensips.git
git@gitee.com:maplerain/opensips.git
maplerain
opensips
opensips
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891