String to integer conversion can now check the result against allowed min/max values. Use it in sip module.

git-svn-id: http://voip.null.ro/svn/yate@4562 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
marian 2011-08-23 15:14:28 +00:00
parent 82a1e0dd88
commit 4fc28f755c
6 changed files with 36 additions and 28 deletions

View File

@ -77,10 +77,11 @@ const char* Configuration::getValue(const String& sect, const String& key, const
return s ? s->c_str() : defvalue;
}
int Configuration::getIntValue(const String& sect, const String& key, int defvalue) const
int Configuration::getIntValue(const String& sect, const String& key, int defvalue,
int minvalue, int maxvalue, bool clump) const
{
const NamedString *s = getKey(sect,key);
return s ? s->toInteger(defvalue) : defvalue;
return s ? s->toInteger(defvalue,0,minvalue,maxvalue,clump) : defvalue;
}
int Configuration::getIntValue(const String& sect, const String& key, const TokenDict* tokens, int defvalue) const

View File

@ -281,10 +281,11 @@ const char* NamedList::getValue(const String& name, const char* defvalue) const
return s ? s->c_str() : defvalue;
}
int NamedList::getIntValue(const String& name, int defvalue) const
int NamedList::getIntValue(const String& name, int defvalue, int minvalue, int maxvalue,
bool clump) const
{
const NamedString *s = getParam(name);
return s ? s->toInteger(defvalue) : defvalue;
return s ? s->toInteger(defvalue,0,minvalue,maxvalue,clump) : defvalue;
}
int NamedList::getIntValue(const String& name, const TokenDict* tokens, int defvalue) const

View File

@ -417,7 +417,8 @@ String String::substr(int offs, int len) const
return String(c_str()+offs,len);
}
int String::toInteger(int defvalue, int base) const
int String::toInteger(int defvalue, int base, int minvalue, int maxvalue,
bool clump) const
{
if (!m_string)
return defvalue;
@ -425,7 +426,11 @@ int String::toInteger(int defvalue, int base) const
int val = strtoi(m_string,&eptr,base);
if (!eptr || *eptr)
return defvalue;
return val;
if (val >= minvalue && val <= maxvalue)
return val;
if (clump)
return (val < minvalue) ? minvalue : maxvalue;
return defvalue;
}
int String::toInteger(const TokenDict* tokens, int defvalue, int base) const

View File

@ -1125,16 +1125,6 @@ static unsigned int getMaxpkt(int val, int defVal)
return 524;
}
// Retrieve an integer value from a list
// Check bounds. Return default value if out of bounds
static inline int checkIntValue(NamedList& params, const String& param, int def, int min, int max)
{
int tmp = params.getIntValue(param,def);
if (tmp >= min && tmp <= max)
return tmp;
return def;
}
// Skip tabs, spaces, CR and LF from buffer start
// Return true if the buffer changed
static bool skipSpaces(String& buf, bool crlf = true)
@ -3052,8 +3042,7 @@ void YateSIPTCPListener::init(const NamedList& params, bool first)
Debug(&plugin,DebugConf,"Listener(%s,'%s') ssl context is empty [%p]",
protoName(),c_str(),this);
}
int backlog = params.getIntValue("backlog",5);
m_backlog = (backlog >= 0 ? backlog : 0);
m_backlog = params.getIntValue("backlog",5,0);
m_bind = setAddr(addr,port) || first;
m_transParamsChanged = m_transParamsChanged || first;
if (rtpLip != m_transParams["rtp_localip"]) {
@ -3437,8 +3426,8 @@ void YateSIPEngine::initialize(NamedList* params)
NamedList dummy("");
if (!params)
params = &dummy;
m_reqTransCount = checkIntValue(*params,"sip_req_trans_count",4,2,10);
m_rspTransCount = checkIntValue(*params,"sip_rsp_trans_count",5,2,10);
m_reqTransCount = params->getIntValue("sip_req_trans_count",4,2,10,false);
m_rspTransCount = params->getIntValue("sip_rsp_trans_count",5,2,10,false);
DDebug(this,DebugAll,"Initialized sip_req_trans_count=%d sip_rsp_trans_count=%d",
m_reqTransCount,m_rspTransCount);
}
@ -7370,11 +7359,7 @@ void SIPDriver::initialize()
m_parser.initialize(s_cfg.getSection("codecs"),s_cfg.getSection("hacks"),s_cfg.getSection("general"));
if (!m_endpoint) {
Thread::Priority prio = Thread::priority(s_cfg.getValue("general","thread"));
unsigned int partyMutexCount = s_cfg.getIntValue("general","party_mutexcount",47);
if (partyMutexCount < 13)
partyMutexCount = 13;
else if (partyMutexCount > 101)
partyMutexCount = 101;
unsigned int partyMutexCount = s_cfg.getIntValue("general","party_mutexcount",47,13,101);
m_endpoint = new YateSIPEndPoint(prio,partyMutexCount);
if (!(m_endpoint->Init())) {
delete m_endpoint;

View File

@ -29,6 +29,7 @@
#error C++ is required
#endif
#include <limits.h>
#include <sys/types.h>
#include <stddef.h>
#include <unistd.h>
@ -1573,9 +1574,14 @@ public:
* Convert the string to an integer value.
* @param defvalue Default to return if the string is not a number
* @param base Numeration base, 0 to autodetect
* @param minvalue Minimum value allowed
* @param maxvalue Maximum value allowed
* @param clump Control the out of bound values: true to adjust to the nearest
* bound, false to return the default value
* @return The integer interpretation or defvalue.
*/
int toInteger(int defvalue = 0, int base = 0) const;
int toInteger(int defvalue = 0, int base = 0, int minvalue = INT_MIN,
int maxvalue = INT_MAX, bool clump = true) const;
/**
* Convert the string to an integer value looking up first a token table.
@ -3599,9 +3605,14 @@ public:
* Retrieve the numeric value of a parameter.
* @param name Name of parameter to locate
* @param defvalue Default value to return if not found
* @param minvalue Minimum value allowed for the parameter
* @param maxvalue Maximum value allowed for the parameter
* @param clump Control the out of bound values: true to adjust to the nearest
* bound, false to return the default value
* @return The number contained in the named parameter or the default
*/
int getIntValue(const String& name, int defvalue = 0) const;
int getIntValue(const String& name, int defvalue = 0, int minvalue = INT_MIN,
int maxvalue = INT_MAX, bool clump = true) const;
/**
* Retrieve the numeric value of a parameter trying first a table lookup.

View File

@ -105,9 +105,14 @@ public:
* @param sect Name of the section
* @param key Name of the key in section
* @param defvalue Default value to return if not found
* @param minvalue Minimum value allowed for the parameter
* @param maxvalue Maximum value allowed for the parameter
* @param clump Control the out of bound values: true to adjust to the nearest
* bound, false to return the default value
* @return The number contained in the key or the default
*/
int getIntValue(const String& sect, const String& key, int defvalue = 0) const;
int getIntValue(const String& sect, const String& key, int defvalue = 0,
int minvalue = INT_MIN, int maxvalue = INT_MAX, bool clump = true) const;
/**
* Retrieve the numeric value of a key in a section trying first a table lookup.