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");
168 if (node->
name != NULL)
170 if (node->
val != NULL)
190 if (
strlcpy(node_name,
name,
sizeof(node_name)) >=
sizeof(node_name)) {
197 if ((
next = strchr(key,
'.')) != NULL)
201 }
while (
next != NULL && node != NULL);
244 if (node == NULL || node->
final) {
247 if (node->
val != NULL)
273 val = strchr(
name,
'=');
279 while (isspace((
int)
name[strlen(
name) - 1])) {
283 while (isspace((
int)*val)) {
326 if (node->
val != NULL)
356 SCLogDebug(
"failed to lookup configuration parameter '%s'",
name);
383 if (r == 1 && *vptr == NULL) {
394 SCLogDebug(
"failed to lookup configuration parameter '%s'",
name);
398 if (node->
val == NULL)
424 if (ret == 0 && dflt) {
442 const char *strval = NULL;
449 if (strval == NULL) {
457 tmpint = strtoimax(strval, &endptr, 0);
458 if (strval[0] ==
'\0' || *endptr !=
'\0') {
464 if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
477 const char *strval = NULL;
484 tmpint = strtoimax(strval, &endptr, 0);
485 if (strval[0] ==
'\0' || *endptr !=
'\0') {
487 "for %s with base %s: '%s'",
491 if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
493 " base %s out of range: '%s'",
507 if (ret == 0 && dflt) {
525 const char *strval = NULL;
543 const char *strval = NULL;
559 if (ret == 0 && dflt) {
579 const char *trues[] = {
"1",
"yes",
"true",
"on"};
582 for (u = 0; u <
sizeof(trues) /
sizeof(trues[0]); u++) {
583 if (strcasecmp(val, trues[u]) == 0) {
604 const char *falses[] = {
"0",
"no",
"false",
"off"};
607 for (u = 0; u <
sizeof(falses) /
sizeof(falses[0]); u++) {
608 if (strcasecmp(val, falses[u]) == 0) {
628 const char *strval = NULL;
636 tmpdo = strtod(strval, &endptr);
637 if (strval[0] ==
'\0' || *endptr !=
'\0')
658 const char *strval = NULL;
666 tmpfl = strtof(strval, &endptr);
667 if (strval[0] ==
'\0' || *endptr !=
'\0')
692 const char *strval = NULL;
697 if (strval == NULL || strval[0] ==
'\0')
700 if (SCParseTimeDuration(strval, val) != 0)
766 SCLogDebug(
"configuration module de-initialized");
769 static char *ConfPrintNameArray(
char **name_arr,
int level)
771 static char name[128*128];
775 for (i = 0; i <= level; i++) {
791 static char *
name[128];
792 static int level = -1;
800 if (prefix == NULL) {
801 printf(
"%s = %s\n", ConfPrintNameArray(
name, level),
805 printf(
"%s.%s = %s\n", prefix,
806 ConfPrintNameArray(
name, level), child->
val);
853 if (node == NULL ||
name == NULL) {
858 if (child->
name != NULL && strcmp(child->
name,
name) == 0)
899 if (!strncmp(child->
val, key, strlen(child->
val))) {
902 if ((!strcmp(subchild->
name, key)) && (!strcmp(subchild->
val, value))) {
970 for (item =
TAILQ_FIRST(&node->head); item != NULL; item = it) {
976 if (item->
name != NULL)
978 if (item->
val != NULL)
985 if (node->
val != NULL) {
1000 return node->
is_seq == 0 ? 0 : 1;
1015 if (ifaces_list_node == NULL) {
1016 SCLogError(
"unable to find %s config", ifaces_node_name);
1021 if (if_node == NULL)
1022 SCLogNotice(
"unable to find interface %s in DPDK config", iface);
1038 const char *default_iface =
"default";
1042 if (*if_root == NULL && *if_default == NULL) {
1043 SCLogError(
"unable to find configuration for the interface \"%s\" or the default "
1044 "configuration (\"%s\")",
1045 iface, default_iface);
1050 if (*if_root == NULL) {
1051 *if_root = *if_default;
1062 static int ConfTestGetNonExistant(
void)
1064 char name[] =
"non-existant-value";
1074 static int ConfTestSetAndGet(
void)
1076 char name[] =
"some-name";
1077 char value[] =
"some-value";
1078 const char *value0 = NULL;
1083 FAIL_IF(strcmp(value, value0) != 0);
1095 static int ConfTestOverrideValue1(
void)
1097 char name[] =
"some-name";
1098 char value0[] =
"some-value";
1099 char value1[] =
"new-value";
1100 const char *val = NULL;
1106 FAIL_IF(strcmp(val, value1) != 0);
1117 static int ConfTestOverrideValue2(
void)
1119 char name[] =
"some-name";
1120 char value0[] =
"some-value";
1121 char value1[] =
"new-value";
1122 const char *val = NULL;
1128 FAIL_IF(strcmp(val, value0) != 0);
1139 static int ConfTestGetInt(
void)
1141 char name[] =
"some-int.x";
1165 static int ConfTestGetBool(
void)
1167 char name[] =
"some-bool";
1168 const char *trues[] = {
1174 const char *falses[] = {
1184 for (u = 0; u <
sizeof(trues) /
sizeof(trues[0]); u++) {
1190 for (u = 0; u <
sizeof(falses) /
sizeof(falses[0]); u++) {
1199 static int ConfNodeLookupChildTest(
void)
1201 const char *test_vals[] = {
"one",
"two",
"three" };
1207 for (u = 0; u <
sizeof(test_vals)/
sizeof(test_vals[0]); u++) {
1234 if (parent != NULL) {
1241 static int ConfNodeLookupChildValueTest(
void)
1243 const char *test_vals[] = {
"one",
"two",
"three" };
1250 for (u = 0; u <
sizeof(test_vals)/
sizeof(test_vals[0]); u++) {
1259 FAIL_IF(strcmp(value,
"one") != 0);
1263 FAIL_IF(strcmp(value,
"two") != 0);
1267 FAIL_IF(strcmp(value,
"three") != 0);
1277 static int ConfGetChildValueWithDefaultTest(
void)
1279 const char *val =
"";
1282 SCConfSet(
"af-packet.0.interface",
"eth0");
1283 SCConfSet(
"af-packet.1.interface",
"default");
1284 SCConfSet(
"af-packet.1.cluster-type",
"cluster_cpu");
1289 FAIL_IF(strcmp(val,
"cluster_cpu"));
1291 SCConfSet(
"af-packet.0.cluster-type",
"cluster_flow");
1294 FAIL_IF(strcmp(val,
"cluster_flow"));
1301 static int ConfGetChildValueIntWithDefaultTest(
void)
1306 SCConfSet(
"af-packet.0.interface",
"eth0");
1307 SCConfSet(
"af-packet.1.interface",
"default");
1325 static int ConfGetChildValueBoolWithDefaultTest(
void)
1330 SCConfSet(
"af-packet.0.interface",
"eth0");
1331 SCConfSet(
"af-packet.1.interface",
"default");
1332 SCConfSet(
"af-packet.1.use-mmap",
"yes");
1339 SCConfSet(
"af-packet.0.use-mmap",
"no");
1352 static int ConfNodeRemoveTest(
void)
1372 static int ConfSetTest(
void)
1400 static int ConfGetNodeOrCreateTest(
void)
1409 node = SCConfGetNodeOrCreate(
"node0", 0);
1414 node = SCConfGetNodeOrCreate(
"node0", 0);
1420 node = SCConfGetNodeOrCreate(
"parent.child.grandchild", 0);
1425 node = SCConfGetNodeOrCreate(
"parent.child.grandchild", 0);
1428 FAIL_IF(strcmp(node->
val,
"parent.child.grandchild") != 0);
1431 SCConfNode *child1 = SCConfGetNodeOrCreate(
"parent.kids.child1", 0);
1432 SCConfNode *child2 = SCConfGetNodeOrCreate(
"parent.kids.child2", 0);
1433 FAIL_IF(child1 == NULL || child2 == NULL);
1443 static int ConfNodePruneTest(
void)
1474 static int ConfNodeIsSequenceTest(
void)
1488 static int ConfSetFromStringTest(
void)
1521 static int ConfNodeHasChildrenTest(
void)
1546 UtRegisterTest(
"ConfTestGetNonExistant", ConfTestGetNonExistant);
1549 UtRegisterTest(
"ConfTestOverrideValue1", ConfTestOverrideValue1);
1550 UtRegisterTest(
"ConfTestOverrideValue2", ConfTestOverrideValue2);
1553 UtRegisterTest(
"ConfNodeLookupChildTest", ConfNodeLookupChildTest);
1555 ConfNodeLookupChildValueTest);
1558 ConfGetChildValueWithDefaultTest);
1560 ConfGetChildValueIntWithDefaultTest);
1562 ConfGetChildValueBoolWithDefaultTest);
1563 UtRegisterTest(
"ConfGetNodeOrCreateTest", ConfGetNodeOrCreateTest);
1565 UtRegisterTest(
"ConfNodeIsSequenceTest", ConfNodeIsSequenceTest);
1567 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.
int SCConfGetTime(const char *name, uint64_t *val)
Retrieve a configuration value as a time duration in seconds.
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.
int SCConfGetNonNull(const char *name, const char **vptr)
Retrieve the non-null value of a configuration node.
SCConfNode * SCConfGetChildWithDefault(const SCConfNode *base, const SCConfNode *dflt, const char *name)
int SCConfNodeChildValueIsFalse(const SCConfNode *node, const char *key)
Test if a configuration node has a false value.
#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.
#define DEBUG_VALIDATE_BUG_ON(exp)
void SCConfNodePrune(SCConfNode *node)
Create the path for an include entry.