conf: Introduce new function to input configuration.

The input modules are needing a per interface configuration. This
implies some new operations to be able to parse easily te configuration.

The syntax of the configuration file is for example:
af-packet:
  - interface: eth0
    threads: 2
  - interface: eth1
    threads: 3
We need a way to express get a configuration variable for interface[eth0].
This is by using ConfNodeLookupKeyValue() to get the matching node. And
after that value can be fetch by using ConfGetChildValue*() functions.
remotes/origin/master-1.1.x
Eric Leblond 14 years ago committed by Victor Julien
parent e80b30c082
commit dc667af1a1

@ -247,6 +247,20 @@ ConfGet(char *name, char **vptr)
}
}
int ConfGetChildValue(ConfNode *base, char *name, char **vptr)
{
ConfNode *node = ConfNodeLookupChild(base, name);
if (node == NULL) {
SCLogDebug("failed to lookup configuration parameter '%s'", name);
return 0;
}
else {
*vptr = node->val;
return 1;
}
}
/**
* \brief Retrieve a configuration value as an integer.
*
@ -278,6 +292,28 @@ ConfGetInt(char *name, intmax_t *val)
return 1;
}
int ConfGetChildValueInt(ConfNode *base, char *name, intmax_t *val)
{
char *strval;
intmax_t tmpint;
char *endptr;
if (ConfGetChildValue(base, name, &strval) == 0)
return 0;
errno = 0;
tmpint = strtoimax(strval, &endptr, 0);
if (strval[0] == '\0' || *endptr != '\0')
return 0;
if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN))
return 0;
*val = tmpint;
return 1;
}
/**
* \brief Retrieve a configuration value as an boolen.
*
@ -302,6 +338,23 @@ ConfGetBool(char *name, int *val)
return 1;
}
int ConfGetChildValueBool(ConfNode *base, char *name, int *val)
{
char *strval;
*val = 0;
if (ConfGetChildValue(base, name, &strval) == 0)
return 0;
*val = ConfValIsTrue(strval);
*val = ConfValIsTrue(strval);
return 1;
}
/**
* \brief Check if a value is true.
*
@ -560,6 +613,32 @@ ConfNodeLookupChildValue(ConfNode *node, const char *name)
return NULL;
}
/**
* \brief Lookup for a key value under a specific node
*
* \return the ConfNode matching or NULL
*/
ConfNode *ConfNodeLookupKeyValue(ConfNode *base, const char *key, const char *value)
{
ConfNode *child;
TAILQ_FOREACH(child, &base->head, next) {
SCLogWarning(SC_ERR_MEM_ALLOC,"conf:found one child %s:%s", child->name, child->val);
if (!strncmp(child->val, key, sizeof(child->val))) {
ConfNode *subchild;
TAILQ_FOREACH(subchild, &child->head, next) {
if ((!strcmp(subchild->name, key)) && (!strcmp(subchild->val, value))) {
SCLogWarning(SC_ERR_MEM_ALLOC,"was looking for %s:%s", subchild->name, subchild->val);
return child;
}
}
}
}
return NULL;
}
/**
* \brief Test if a configuration node has a true value.
*

@ -70,4 +70,10 @@ void ConfRegisterTests();
int ConfNodeChildValueIsTrue(ConfNode *node, const char *key);
int ConfValIsTrue(const char *val);
ConfNode *ConfNodeLookupKeyValue(ConfNode *base, const char *key, const char *value);
int ConfGetChildValue(ConfNode *base, char *name, char **vptr);
int ConfGetChildValueInt(ConfNode *base, char *name, intmax_t *val);
int ConfGetChildValueBool(ConfNode *base, char *name, int *val);
#endif /* ! __CONF_H__ */

Loading…
Cancel
Save