Go to the documentation of this file.
46 #define NODE_NAME_MAX 1024
64 static ConfNode *ConfGetNodeOrCreate(
const char *name,
int final)
72 if (
strlcpy(node_name, name,
sizeof(node_name)) >=
sizeof(node_name)) {
74 "Configuration name too long: %s", name);
81 if ((
next = strchr(key,
'.')) != NULL)
87 "Failed to allocate memory for configuration.");
95 "Failed to allocate memory for configuration.");
104 }
while (
next != NULL);
122 "ERROR: Failed to allocate memory for root configuration node, "
125 SCLogDebug(
"configuration module initialized");
160 if (node->
name != NULL)
162 if (node->
val != NULL)
182 if (
strlcpy(node_name, name,
sizeof(node_name)) >=
sizeof(node_name)) {
184 "Configuration name too long: %s", name);
190 if ((
next = strchr(key,
'.')) != NULL)
194 }
while (
next != NULL && node != NULL);
219 int ConfSet(
const char *name,
const char *val)
221 ConfNode *node = ConfGetNodeOrCreate(name, 0);
222 if (node == NULL || node->
final) {
225 if (node->
val != NULL)
247 char *name =
SCStrdup(input), *val = NULL;
251 val = strchr(name,
'=');
257 while (isspace((
int)name[strlen(name) - 1])) {
258 name[strlen(name) - 1] =
'\0';
261 while (isspace((
int)*val)) {
300 ConfNode *node = ConfGetNodeOrCreate(name, 1);
304 if (node->
val != NULL)
330 int ConfGet(
const char *name,
const char **vptr)
334 SCLogDebug(
"failed to lookup configuration parameter '%s'", name);
371 SCLogDebug(
"failed to lookup configuration parameter '%s'", name);
376 if (node->
val == NULL) {
377 SCLogDebug(
"value for configuration parameter '%s' is NULL", name);
392 SCLogDebug(
"failed to lookup configuration parameter '%s'", name);
416 const char *name,
const char **vptr)
420 if (ret == 0 && dflt) {
438 const char *strval = NULL;
442 if (
ConfGet(name, &strval) == 0)
445 if (strval == NULL) {
447 "for %s: NULL", name);
452 tmpint = strtoimax(strval, &endptr, 0);
453 if (strval[0] ==
'\0' || *endptr !=
'\0') {
455 "for %s: '%s'", name, strval);
458 if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
460 "of range: '%s'", name, strval);
470 const char *strval = NULL;
477 tmpint = strtoimax(strval, &endptr, 0);
478 if (strval[0] ==
'\0' || *endptr !=
'\0') {
480 "for %s with base %s: '%s'", name, base->
name, strval);
483 if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
485 " base %s out of range: '%s'", name, base->
name, strval);
495 const char *name, intmax_t *val)
499 if (ret == 0 && dflt) {
518 const char *strval = NULL;
531 const char *strval = NULL;
543 const char *name,
int *val)
547 if (ret == 0 && dflt) {
567 const char *trues[] = {
"1",
"yes",
"true",
"on"};
570 for (u = 0; u <
sizeof(trues) /
sizeof(trues[0]); u++) {
571 if (strcasecmp(val, trues[u]) == 0) {
592 const char *falses[] = {
"0",
"no",
"false",
"off"};
595 for (u = 0; u <
sizeof(falses) /
sizeof(falses[0]); u++) {
596 if (strcasecmp(val, falses[u]) == 0) {
616 const char *strval = NULL;
620 if (
ConfGet(name, &strval) == 0)
624 tmpdo = strtod(strval, &endptr);
625 if (strval[0] ==
'\0' || *endptr !=
'\0')
646 const char *strval = NULL;
650 if (
ConfGet(name, &strval) == 0)
654 tmpfl = strtof(strval, &endptr);
655 if (strval[0] ==
'\0' || *endptr !=
'\0')
728 SCLogDebug(
"configuration module de-initialized");
731 static char *ConfPrintNameArray(
char **name_arr,
int level)
733 static char name[128*128];
737 for (i = 0; i <= level; i++) {
738 strlcat(name, name_arr[i],
sizeof(name));
740 strlcat(name,
".",
sizeof(name));
753 static char *name[128];
754 static int level = -1;
759 if (
unlikely(name[level] == NULL)) {
762 if (prefix == NULL) {
763 printf(
"%s = %s\n", ConfPrintNameArray(name, level),
767 printf(
"%s.%s = %s\n", prefix,
768 ConfPrintNameArray(name, level), child->
val);
818 if (node == NULL || name == NULL) {
823 if (child->
name != NULL && strcmp(child->
name, name) == 0)
865 if (!strncmp(child->
val, key, strlen(child->
val))) {
868 if ((!strcmp(subchild->
name, key)) && (!strcmp(subchild->
val, value))) {
903 const char *defaultpath = NULL;
908 if (
ConfGet(
"include-path", &defaultpath) == 1) {
910 size_t path_len =
sizeof(char) * (strlen(defaultpath) +
915 strlcpy(path, defaultpath, path_len);
916 if (path[strlen(path) - 1] !=
'/')
946 for (item =
TAILQ_FIRST(&node->head); item != NULL; item = it) {
952 if (item->
name != NULL)
954 if (item->
val != NULL)
961 if (node->
val != NULL) {
976 return node->
is_seq == 0 ? 0 : 1;
984 static int ConfTestGetNonExistant(
void)
986 char name[] =
"non-existant-value";
996 static int ConfTestSetAndGet(
void)
998 char name[] =
"some-name";
999 char value[] =
"some-value";
1000 const char *value0 = NULL;
1005 FAIL_IF(strcmp(value, value0) != 0);
1017 static int ConfTestOverrideValue1(
void)
1019 char name[] =
"some-name";
1020 char value0[] =
"some-value";
1021 char value1[] =
"new-value";
1022 const char *val = NULL;
1028 FAIL_IF(strcmp(val, value1) != 0);
1039 static int ConfTestOverrideValue2(
void)
1041 char name[] =
"some-name";
1042 char value0[] =
"some-value";
1043 char value1[] =
"new-value";
1044 const char *val = NULL;
1050 FAIL_IF(strcmp(val, value0) != 0);
1061 static int ConfTestGetInt(
void)
1063 char name[] =
"some-int.x";
1087 static int ConfTestGetBool(
void)
1089 char name[] =
"some-bool";
1090 const char *trues[] = {
1096 const char *falses[] = {
1106 for (u = 0; u <
sizeof(trues) /
sizeof(trues[0]); u++) {
1112 for (u = 0; u <
sizeof(falses) /
sizeof(falses[0]); u++) {
1121 static int ConfNodeLookupChildTest(
void)
1123 const char *test_vals[] = {
"one",
"two",
"three" };
1129 for (u = 0; u <
sizeof(test_vals)/
sizeof(test_vals[0]); u++) {
1156 if (parent != NULL) {
1163 static int ConfNodeLookupChildValueTest(
void)
1165 const char *test_vals[] = {
"one",
"two",
"three" };
1172 for (u = 0; u <
sizeof(test_vals)/
sizeof(test_vals[0]); u++) {
1181 FAIL_IF(strcmp(value,
"one") != 0);
1185 FAIL_IF(strcmp(value,
"two") != 0);
1189 FAIL_IF(strcmp(value,
"three") != 0);
1199 static int ConfGetChildValueWithDefaultTest(
void)
1201 const char *val =
"";
1204 ConfSet(
"af-packet.0.interface",
"eth0");
1205 ConfSet(
"af-packet.1.interface",
"default");
1206 ConfSet(
"af-packet.1.cluster-type",
"cluster_cpu");
1211 FAIL_IF(strcmp(val,
"cluster_cpu"));
1213 ConfSet(
"af-packet.0.cluster-type",
"cluster_flow");
1216 FAIL_IF(strcmp(val,
"cluster_flow"));
1223 static int ConfGetChildValueIntWithDefaultTest(
void)
1228 ConfSet(
"af-packet.0.interface",
"eth0");
1229 ConfSet(
"af-packet.1.interface",
"default");
1230 ConfSet(
"af-packet.1.threads",
"2");
1237 ConfSet(
"af-packet.0.threads",
"1");
1247 static int ConfGetChildValueBoolWithDefaultTest(
void)
1252 ConfSet(
"af-packet.0.interface",
"eth0");
1253 ConfSet(
"af-packet.1.interface",
"default");
1254 ConfSet(
"af-packet.1.use-mmap",
"yes");
1261 ConfSet(
"af-packet.0.use-mmap",
"no");
1274 static int ConfNodeRemoveTest(
void)
1294 static int ConfSetTest(
void)
1322 static int ConfGetNodeOrCreateTest(
void)
1331 node = ConfGetNodeOrCreate(
"node0", 0);
1336 node = ConfGetNodeOrCreate(
"node0", 0);
1342 node = ConfGetNodeOrCreate(
"parent.child.grandchild", 0);
1347 node = ConfGetNodeOrCreate(
"parent.child.grandchild", 0);
1350 FAIL_IF(strcmp(node->
val,
"parent.child.grandchild") != 0);
1353 ConfNode *child1 = ConfGetNodeOrCreate(
"parent.kids.child1", 0);
1354 ConfNode *child2 = ConfGetNodeOrCreate(
"parent.kids.child2", 0);
1355 FAIL_IF(child1 == NULL || child2 == NULL);
1365 static int ConfNodePruneTest(
void)
1396 static int ConfNodeIsSequenceTest(
void)
1410 static int ConfSetFromStringTest(
void)
1443 static int ConfNodeHasChildrenTest(
void)
1449 ConfSet(
"no-children",
"value");
1456 ConfSet(
"parent.child",
"value");
1468 UtRegisterTest(
"ConfTestGetNonExistant", ConfTestGetNonExistant);
1471 UtRegisterTest(
"ConfTestOverrideValue1", ConfTestOverrideValue1);
1472 UtRegisterTest(
"ConfTestOverrideValue2", ConfTestOverrideValue2);
1475 UtRegisterTest(
"ConfNodeLookupChildTest", ConfNodeLookupChildTest);
1477 ConfNodeLookupChildValueTest);
1480 ConfGetChildValueWithDefaultTest);
1482 ConfGetChildValueIntWithDefaultTest);
1484 ConfGetChildValueBoolWithDefaultTest);
1485 UtRegisterTest(
"ConfGetNodeOrCreateTest", ConfGetNodeOrCreateTest);
1487 UtRegisterTest(
"ConfNodeIsSequenceTest", ConfNodeIsSequenceTest);
1489 UtRegisterTest(
"ConfNodeHasChildrenTest", ConfNodeHasChildrenTest);
int ConfGetChildValueInt(const ConfNode *base, const char *name, intmax_t *val)
int ConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
int ConfNodeChildValueIsTrue(const ConfNode *node, const char *key)
Test if a configuration node has a true value.
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as an boolen.
int ConfGetDouble(const char *name, double *val)
Retrieve a configuration value as a double.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
int ConfGetChildValueBoolWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name, int *val)
void ConfNodeDump(const ConfNode *node, const char *prefix)
Dump a configuration node and all its children.
struct HtpBodyChunk_ * next
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
ConfNode * ConfNodeNew(void)
Allocate a new configuration node.
#define TAILQ_EMPTY(head)
#define TAILQ_FOREACH(var, head, field)
void ConfDump(void)
Dump configuration to stdout.
ConfNode * ConfNodeLookupKeyValue(const ConfNode *base, const char *key, const char *value)
Lookup for a key value under a specific node.
int ConfSetFinal(const char *name, const char *val)
Set a final configuration value.
#define TAILQ_INSERT_TAIL(head, elm, field)
int ConfGetChildValueIntWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name, intmax_t *val)
ConfNode * ConfGetChildWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name)
@ SC_ERR_CONF_NAME_TOO_LONG
int ConfValIsTrue(const char *val)
Check if a value is true.
#define FAIL_IF_NOT(expr)
Fail a test if expression to true.
int ConfRemove(const char *name)
Remove a configuration parameter from the configuration db.
size_t strlcpy(char *dst, const char *src, size_t siz)
void ConfNodeFree(ConfNode *node)
Free a ConfNode and all of its children.
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
ConfNode * ConfGetRootNode(void)
Get the root configuration node.
void ConfNodeRemove(ConfNode *node)
Remove (and SCFree) the provided configuration node.
#define TAILQ_REMOVE(head, elm, field)
#define TAILQ_FIRST(head)
void ConfNodePrune(ConfNode *node)
Prune a configuration node.
#define PASS
Pass the test.
size_t strlcat(char *, const char *src, size_t siz)
struct ConfNode_ * parent
int ConfGetChildValueWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name, const char **vptr)
@ SC_ERR_INVALID_ARGUMENT
void ConfCreateContextBackup(void)
Creates a backup of the conf_hash hash_table used by the conf API.
bool ConfNodeHasChildren(const ConfNode *node)
Check if a node has any children.
@ SC_ERR_INVALID_YAML_CONF_ENTRY
int ConfGetChildValue(const ConfNode *base, const char *name, const char **vptr)
ConfNode * ConfNodeLookupChild(const ConfNode *node, const char *name)
Lookup a child configuration node by name.
int ConfGetFloat(const char *name, float *val)
Retrieve a configuration value as a float.
#define FAIL_IF(expr)
Fail a test if expression evaluates to false.
void ConfRegisterTests(void)
#define TAILQ_NEXT(elm, field)
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
int ConfNodeIsSequence(const ConfNode *node)
Check if a node is a sequence or node.
void ConfRestoreContextBackup(void)
Restores the backup of the hash_table present in backup_conf_hash back to conf_hash.
#define FatalError(x,...)
int ConfGetChildValueBool(const ConfNode *base, const char *name, int *val)
int ConfSetFromString(const char *input, int final)
Set a configuration parameter from a string.
void ConfInit(void)
Initialize the configuration system.
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
int PathIsRelative(const char *path)
Check if a path is relative.
int ConfValIsFalse(const char *val)
Check if a value is false.
int ConfGetValue(const char *name, const char **vptr)
Retrieve the value of a configuration node.
void ConfDeInit(void)
De-initializes the configuration system.
int ConfSet(const char *name, const char *val)
Set a configuration value.
char * ConfLoadCompleteIncludePath(const char *file)
Create the path for an include entry.
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.