Go to the documentation of this file.
47 #define NODE_NAME_MAX 1024
73 if (
strlcpy(node_name,
name,
sizeof(node_name)) >=
sizeof(node_name)) {
81 if ((
next = strchr(key,
'.')) != NULL)
86 SCLogWarning(
"Failed to allocate memory for configuration.");
93 SCLogWarning(
"Failed to allocate memory for configuration.");
102 }
while (
next != NULL);
112 static SCConfNode *SCConfGetNodeOrCreate(
const char *
name,
int final)
128 FatalError(
"ERROR: Failed to allocate memory for root configuration node, "
131 SCLogDebug(
"configuration module initialized");
166 if (node->
name != NULL)
168 if (node->
val != NULL)
188 if (
strlcpy(node_name,
name,
sizeof(node_name)) >=
sizeof(node_name)) {
195 if ((
next = strchr(key,
'.')) != NULL)
199 }
while (
next != NULL && node != NULL);
242 if (node == NULL || node->
final) {
245 if (node->
val != NULL)
271 val = strchr(
name,
'=');
277 while (isspace((
int)
name[strlen(
name) - 1])) {
281 while (isspace((
int)*val)) {
324 if (node->
val != NULL)
354 SCLogDebug(
"failed to lookup configuration parameter '%s'",
name);
368 SCLogDebug(
"failed to lookup configuration parameter '%s'",
name);
372 if (node->
val == NULL)
398 if (ret == 0 && dflt) {
416 const char *strval = NULL;
423 if (strval == NULL) {
431 tmpint = strtoimax(strval, &endptr, 0);
432 if (strval[0] ==
'\0' || *endptr !=
'\0') {
438 if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
451 const char *strval = NULL;
458 tmpint = strtoimax(strval, &endptr, 0);
459 if (strval[0] ==
'\0' || *endptr !=
'\0') {
461 "for %s with base %s: '%s'",
465 if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
467 " base %s out of range: '%s'",
481 if (ret == 0 && dflt) {
499 const char *strval = NULL;
517 const char *strval = NULL;
533 if (ret == 0 && dflt) {
553 const char *trues[] = {
"1",
"yes",
"true",
"on"};
556 for (u = 0; u <
sizeof(trues) /
sizeof(trues[0]); u++) {
557 if (strcasecmp(val, trues[u]) == 0) {
578 const char *falses[] = {
"0",
"no",
"false",
"off"};
581 for (u = 0; u <
sizeof(falses) /
sizeof(falses[0]); u++) {
582 if (strcasecmp(val, falses[u]) == 0) {
602 const char *strval = NULL;
610 tmpdo = strtod(strval, &endptr);
611 if (strval[0] ==
'\0' || *endptr !=
'\0')
632 const char *strval = NULL;
640 tmpfl = strtof(strval, &endptr);
641 if (strval[0] ==
'\0' || *endptr !=
'\0')
710 SCLogDebug(
"configuration module de-initialized");
713 static char *ConfPrintNameArray(
char **name_arr,
int level)
715 static char name[128*128];
719 for (i = 0; i <= level; i++) {
735 static char *
name[128];
736 static int level = -1;
744 if (prefix == NULL) {
745 printf(
"%s = %s\n", ConfPrintNameArray(
name, level),
749 printf(
"%s.%s = %s\n", prefix,
750 ConfPrintNameArray(
name, level), child->
val);
800 if (node == NULL ||
name == NULL) {
805 if (child->
name != NULL && strcmp(child->
name,
name) == 0)
846 if (!strncmp(child->
val, key, strlen(child->
val))) {
849 if ((!strcmp(subchild->
name, key)) && (!strcmp(subchild->
val, value))) {
897 for (item =
TAILQ_FIRST(&node->head); item != NULL; item = it) {
903 if (item->
name != NULL)
905 if (item->
val != NULL)
912 if (node->
val != NULL) {
927 return node->
is_seq == 0 ? 0 : 1;
942 if (ifaces_list_node == NULL) {
943 SCLogError(
"unable to find %s config", ifaces_node_name);
949 SCLogNotice(
"unable to find interface %s in DPDK config", iface);
965 const char *default_iface =
"default";
969 if (*if_root == NULL && *if_default == NULL) {
970 SCLogError(
"unable to find configuration for the interface \"%s\" or the default "
971 "configuration (\"%s\")",
972 iface, default_iface);
977 if (*if_root == NULL) {
978 *if_root = *if_default;
989 static int ConfTestGetNonExistant(
void)
991 char name[] =
"non-existant-value";
1001 static int ConfTestSetAndGet(
void)
1003 char name[] =
"some-name";
1004 char value[] =
"some-value";
1005 const char *value0 = NULL;
1010 FAIL_IF(strcmp(value, value0) != 0);
1022 static int ConfTestOverrideValue1(
void)
1024 char name[] =
"some-name";
1025 char value0[] =
"some-value";
1026 char value1[] =
"new-value";
1027 const char *val = NULL;
1033 FAIL_IF(strcmp(val, value1) != 0);
1044 static int ConfTestOverrideValue2(
void)
1046 char name[] =
"some-name";
1047 char value0[] =
"some-value";
1048 char value1[] =
"new-value";
1049 const char *val = NULL;
1055 FAIL_IF(strcmp(val, value0) != 0);
1066 static int ConfTestGetInt(
void)
1068 char name[] =
"some-int.x";
1092 static int ConfTestGetBool(
void)
1094 char name[] =
"some-bool";
1095 const char *trues[] = {
1101 const char *falses[] = {
1111 for (u = 0; u <
sizeof(trues) /
sizeof(trues[0]); u++) {
1117 for (u = 0; u <
sizeof(falses) /
sizeof(falses[0]); u++) {
1126 static int ConfNodeLookupChildTest(
void)
1128 const char *test_vals[] = {
"one",
"two",
"three" };
1134 for (u = 0; u <
sizeof(test_vals)/
sizeof(test_vals[0]); u++) {
1161 if (parent != NULL) {
1168 static int ConfNodeLookupChildValueTest(
void)
1170 const char *test_vals[] = {
"one",
"two",
"three" };
1177 for (u = 0; u <
sizeof(test_vals)/
sizeof(test_vals[0]); u++) {
1186 FAIL_IF(strcmp(value,
"one") != 0);
1190 FAIL_IF(strcmp(value,
"two") != 0);
1194 FAIL_IF(strcmp(value,
"three") != 0);
1204 static int ConfGetChildValueWithDefaultTest(
void)
1206 const char *val =
"";
1209 SCConfSet(
"af-packet.0.interface",
"eth0");
1210 SCConfSet(
"af-packet.1.interface",
"default");
1211 SCConfSet(
"af-packet.1.cluster-type",
"cluster_cpu");
1216 FAIL_IF(strcmp(val,
"cluster_cpu"));
1218 SCConfSet(
"af-packet.0.cluster-type",
"cluster_flow");
1221 FAIL_IF(strcmp(val,
"cluster_flow"));
1228 static int ConfGetChildValueIntWithDefaultTest(
void)
1233 SCConfSet(
"af-packet.0.interface",
"eth0");
1234 SCConfSet(
"af-packet.1.interface",
"default");
1252 static int ConfGetChildValueBoolWithDefaultTest(
void)
1257 SCConfSet(
"af-packet.0.interface",
"eth0");
1258 SCConfSet(
"af-packet.1.interface",
"default");
1259 SCConfSet(
"af-packet.1.use-mmap",
"yes");
1266 SCConfSet(
"af-packet.0.use-mmap",
"no");
1279 static int ConfNodeRemoveTest(
void)
1299 static int ConfSetTest(
void)
1327 static int ConfGetNodeOrCreateTest(
void)
1336 node = SCConfGetNodeOrCreate(
"node0", 0);
1341 node = SCConfGetNodeOrCreate(
"node0", 0);
1347 node = SCConfGetNodeOrCreate(
"parent.child.grandchild", 0);
1352 node = SCConfGetNodeOrCreate(
"parent.child.grandchild", 0);
1355 FAIL_IF(strcmp(node->
val,
"parent.child.grandchild") != 0);
1358 SCConfNode *child1 = SCConfGetNodeOrCreate(
"parent.kids.child1", 0);
1359 SCConfNode *child2 = SCConfGetNodeOrCreate(
"parent.kids.child2", 0);
1360 FAIL_IF(child1 == NULL || child2 == NULL);
1370 static int ConfNodePruneTest(
void)
1401 static int ConfNodeIsSequenceTest(
void)
1415 static int ConfSetFromStringTest(
void)
1448 static int ConfNodeHasChildrenTest(
void)
1473 UtRegisterTest(
"ConfTestGetNonExistant", ConfTestGetNonExistant);
1476 UtRegisterTest(
"ConfTestOverrideValue1", ConfTestOverrideValue1);
1477 UtRegisterTest(
"ConfTestOverrideValue2", ConfTestOverrideValue2);
1480 UtRegisterTest(
"ConfNodeLookupChildTest", ConfNodeLookupChildTest);
1482 ConfNodeLookupChildValueTest);
1485 ConfGetChildValueWithDefaultTest);
1487 ConfGetChildValueIntWithDefaultTest);
1489 ConfGetChildValueBoolWithDefaultTest);
1490 UtRegisterTest(
"ConfGetNodeOrCreateTest", ConfGetNodeOrCreateTest);
1492 UtRegisterTest(
"ConfNodeIsSequenceTest", ConfNodeIsSequenceTest);
1494 UtRegisterTest(
"ConfNodeHasChildrenTest", ConfNodeHasChildrenTest);
int SCConfValIsTrue(const char *val)
Check if a value is true.
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void SCConfNodeRemove(SCConfNode *node)
Remove (and SCFree) the provided configuration node.
const char * SCConfGetValueNode(const SCConfNode *node)
void SCConfNodeDump(const SCConfNode *node, const char *prefix)
Dump a configuration node and all its children.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
SCConfNode * SCConfGetRootNode(void)
Get the root configuration node.
struct HtpBodyChunk_ * next
int SCConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
#define TAILQ_EMPTY(head)
int SCConfNodeChildValueIsTrue(const SCConfNode *node, const char *key)
Test if a configuration node has a true value.
#define TAILQ_FOREACH(var, head, field)
int SCConfGetChildValueBool(const SCConfNode *base, const char *name, int *val)
struct SCConfNode_ * parent
void SCConfRegisterTests(void)
int SCConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
int SCConfValIsFalse(const char *val)
Check if a value is false.
#define TAILQ_INSERT_TAIL(head, elm, field)
int SCConfGetDouble(const char *name, double *val)
Retrieve a configuration value as a double.
const char * SCConfNodeLookupChildValue(const SCConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
bool SCConfNodeHasChildren(const SCConfNode *node)
Check if a node has any children.
SCConfNode * SCConfGetNextNode(const SCConfNode *node)
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
int SCConfNodeIsSequence(const SCConfNode *node)
Check if a node is a sequence or node.
size_t strlcpy(char *dst, const char *src, size_t siz)
void SCConfInit(void)
Initialize the configuration system.
void SCConfDump(void)
Dump configuration to stdout.
SCConfNode * SCConfNodeGetNodeOrCreate(SCConfNode *parent, const char *name, int final)
Helper function to get a node, creating it if it does not exist.
#define TAILQ_REMOVE(head, elm, field)
#define TAILQ_FIRST(head)
int SCConfGetChildValueIntWithDefault(const SCConfNode *base, const SCConfNode *dflt, const char *name, intmax_t *val)
#define PASS
Pass the test.
size_t strlcat(char *, const char *src, size_t siz)
int SCConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
SCConfNode * SCConfGetChildWithDefault(const SCConfNode *base, const SCConfNode *dflt, const char *name)
#define SCLogWarning(...)
Macro used to log WARNING messages.
SCConfNode * SCConfSetIfaceNode(const char *ifaces_node_name, const char *iface)
Finds an interface from the list of interfaces.
SCConfNode * SCConfGetFirstNode(const SCConfNode *parent)
int SCConfGetChildValueInt(const SCConfNode *base, const char *name, intmax_t *val)
void SCConfCreateContextBackup(void)
Creates a backup of the conf_hash hash_table used by the conf API.
void SCConfNodeFree(SCConfNode *node)
Free a SCConfNode and all of its children.
int SCConfSetFromString(const char *input, int final)
Set a configuration parameter from a string.
int SCConfSetFinal(const char *name, const char *val)
Set a final configuration value.
SCConfNode * SCConfNodeLookupChild(const SCConfNode *node, const char *name)
Lookup a child configuration node by name.
SCConfNode * SCConfNodeLookupKeyValue(const SCConfNode *base, const char *key, const char *value)
Lookup for a key value under a specific node.
int SCConfGetChildValue(const SCConfNode *base, const char *name, const char **vptr)
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
#define TAILQ_NEXT(elm, field)
void SCConfDeInit(void)
De-initializes the configuration system.
SCConfNode * SCConfGetNode(const char *name)
Get a SCConfNode by name.
#define SCLogError(...)
Macro used to log ERROR messages.
int SCConfRemove(const char *name)
Remove a configuration parameter from the configuration db.
void SCConfRestoreContextBackup(void)
Restores the backup of the hash_table present in backup_conf_hash back to conf_hash.
int SCConfSet(const char *name, const char *val)
Set a configuration value.
int SCConfSetRootAndDefaultNodes(const char *ifaces_node_name, const char *iface, SCConfNode **if_root, SCConfNode **if_default)
Finds and sets root and default node of the interface.
SCConfNode * SCConfNodeNew(void)
Allocate a new configuration node.
SCConfNode * ConfFindDeviceConfig(SCConfNode *node, const char *iface)
Find the configuration node for a specific device.
int SCConfGetChildValueWithDefault(const SCConfNode *base, const SCConfNode *dflt, const char *name, const char **vptr)
int SCConfGetChildValueBoolWithDefault(const SCConfNode *base, const SCConfNode *dflt, const char *name, int *val)
#define SCLogNotice(...)
Macro used to log NOTICE messages.
int SCConfGetFloat(const char *name, float *val)
Retrieve a configuration value as a float.
void SCConfNodePrune(SCConfNode *node)
Create the path for an include entry.