Go to the documentation of this file.
48 #define NODE_NAME_MAX 1024
74 if (
strlcpy(node_name,
name,
sizeof(node_name)) >=
sizeof(node_name)) {
82 if ((
next = strchr(key,
'.')) != NULL)
87 SCLogWarning(
"Failed to allocate memory for configuration.");
94 SCLogWarning(
"Failed to allocate memory for configuration.");
103 }
while (
next != NULL);
113 static SCConfNode *SCConfGetNodeOrCreate(
const char *
name,
int final)
129 FatalError(
"ERROR: Failed to allocate memory for root configuration node, "
132 SCLogDebug(
"configuration module initialized");
169 if (node->
name != NULL)
171 if (node->
val != NULL)
191 if (
strlcpy(node_name,
name,
sizeof(node_name)) >=
sizeof(node_name)) {
198 if ((
next = strchr(key,
'.')) != NULL)
202 }
while (
next != NULL && node != NULL);
245 if (node == NULL || node->
final) {
248 if (node->
val != NULL)
274 val = strchr(
name,
'=');
280 while (isspace((
int)
name[strlen(
name) - 1])) {
284 while (isspace((
int)*val)) {
327 if (node->
val != NULL)
357 SCLogDebug(
"failed to lookup configuration parameter '%s'",
name);
384 if (r == 1 && *vptr == NULL) {
395 SCLogDebug(
"failed to lookup configuration parameter '%s'",
name);
399 if (node->
val == NULL)
425 if (ret == 0 && dflt) {
443 const char *strval = NULL;
450 if (strval == NULL) {
458 tmpint = strtoimax(strval, &endptr, 0);
459 if (strval[0] ==
'\0' || *endptr !=
'\0') {
465 if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
478 const char *strval = NULL;
485 tmpint = strtoimax(strval, &endptr, 0);
486 if (strval[0] ==
'\0' || *endptr !=
'\0') {
488 "for %s with base %s: '%s'",
492 if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
494 " base %s out of range: '%s'",
508 if (ret == 0 && dflt) {
526 const char *strval = NULL;
544 const char *strval = NULL;
560 if (ret == 0 && dflt) {
580 const char *trues[] = {
"1",
"yes",
"true",
"on"};
583 for (u = 0; u <
sizeof(trues) /
sizeof(trues[0]); u++) {
584 if (strcasecmp(val, trues[u]) == 0) {
605 const char *falses[] = {
"0",
"no",
"false",
"off"};
608 for (u = 0; u <
sizeof(falses) /
sizeof(falses[0]); u++) {
609 if (strcasecmp(val, falses[u]) == 0) {
629 const char *strval = NULL;
637 tmpdo = strtod(strval, &endptr);
638 if (strval[0] ==
'\0' || *endptr !=
'\0')
659 const char *strval = NULL;
667 tmpfl = strtof(strval, &endptr);
668 if (strval[0] ==
'\0' || *endptr !=
'\0')
693 const char *strval = NULL;
698 if (strval == NULL || strval[0] ==
'\0')
701 if (SCParseTimeDuration(strval, val) != 0)
767 SCLogDebug(
"configuration module de-initialized");
770 static char *ConfPrintNameArray(
char **name_arr,
int level)
772 static char name[128*128];
776 for (i = 0; i <= level; i++) {
792 static char *
name[128];
793 static int level = -1;
801 if (prefix == NULL) {
802 printf(
"%s = %s\n", ConfPrintNameArray(
name, level),
806 printf(
"%s.%s = %s\n", prefix,
807 ConfPrintNameArray(
name, level), child->
val);
854 if (node == NULL ||
name == NULL) {
859 if (child->
name != NULL && strcmp(child->
name,
name) == 0)
900 if (!strncmp(child->
val, key, strlen(child->
val))) {
903 if ((!strcmp(subchild->
name, key)) && (!strcmp(subchild->
val, value))) {
971 for (item =
TAILQ_FIRST(&node->head); item != NULL; item = it) {
977 if (item->
name != NULL)
979 if (item->
val != NULL)
986 if (node->
val != NULL) {
1001 return node->
is_seq == 0 ? 0 : 1;
1016 if (ifaces_list_node == NULL) {
1017 SCLogError(
"unable to find %s config", ifaces_node_name);
1022 if (if_node == NULL)
1023 SCLogNotice(
"unable to find interface %s in DPDK config", iface);
1039 const char *default_iface =
"default";
1043 if (*if_root == NULL && *if_default == NULL) {
1044 SCLogError(
"unable to find configuration for the interface \"%s\" or the default "
1045 "configuration (\"%s\")",
1046 iface, default_iface);
1051 if (*if_root == NULL) {
1052 *if_root = *if_default;
1063 static int ConfTestGetNonExistant(
void)
1065 char name[] =
"non-existant-value";
1075 static int ConfTestSetAndGet(
void)
1077 char name[] =
"some-name";
1078 char value[] =
"some-value";
1079 const char *value0 = NULL;
1084 FAIL_IF(strcmp(value, value0) != 0);
1096 static int ConfTestOverrideValue1(
void)
1098 char name[] =
"some-name";
1099 char value0[] =
"some-value";
1100 char value1[] =
"new-value";
1101 const char *val = NULL;
1107 FAIL_IF(strcmp(val, value1) != 0);
1118 static int ConfTestOverrideValue2(
void)
1120 char name[] =
"some-name";
1121 char value0[] =
"some-value";
1122 char value1[] =
"new-value";
1123 const char *val = NULL;
1129 FAIL_IF(strcmp(val, value0) != 0);
1140 static int ConfTestGetInt(
void)
1142 char name[] =
"some-int.x";
1166 static int ConfTestGetBool(
void)
1168 char name[] =
"some-bool";
1169 const char *trues[] = {
1175 const char *falses[] = {
1185 for (u = 0; u <
sizeof(trues) /
sizeof(trues[0]); u++) {
1191 for (u = 0; u <
sizeof(falses) /
sizeof(falses[0]); u++) {
1200 static int ConfNodeLookupChildTest(
void)
1202 const char *test_vals[] = {
"one",
"two",
"three" };
1208 for (u = 0; u <
sizeof(test_vals)/
sizeof(test_vals[0]); u++) {
1235 if (parent != NULL) {
1242 static int ConfNodeLookupChildValueTest(
void)
1244 const char *test_vals[] = {
"one",
"two",
"three" };
1251 for (u = 0; u <
sizeof(test_vals)/
sizeof(test_vals[0]); u++) {
1260 FAIL_IF(strcmp(value,
"one") != 0);
1264 FAIL_IF(strcmp(value,
"two") != 0);
1268 FAIL_IF(strcmp(value,
"three") != 0);
1278 static int ConfGetChildValueWithDefaultTest(
void)
1280 const char *val =
"";
1283 SCConfSet(
"af-packet.0.interface",
"eth0");
1284 SCConfSet(
"af-packet.1.interface",
"default");
1285 SCConfSet(
"af-packet.1.cluster-type",
"cluster_cpu");
1290 FAIL_IF(strcmp(val,
"cluster_cpu"));
1292 SCConfSet(
"af-packet.0.cluster-type",
"cluster_flow");
1295 FAIL_IF(strcmp(val,
"cluster_flow"));
1302 static int ConfGetChildValueIntWithDefaultTest(
void)
1307 SCConfSet(
"af-packet.0.interface",
"eth0");
1308 SCConfSet(
"af-packet.1.interface",
"default");
1326 static int ConfGetChildValueBoolWithDefaultTest(
void)
1331 SCConfSet(
"af-packet.0.interface",
"eth0");
1332 SCConfSet(
"af-packet.1.interface",
"default");
1333 SCConfSet(
"af-packet.1.use-mmap",
"yes");
1340 SCConfSet(
"af-packet.0.use-mmap",
"no");
1353 static int ConfNodeRemoveTest(
void)
1373 static int ConfSetTest(
void)
1401 static int ConfGetNodeOrCreateTest(
void)
1410 node = SCConfGetNodeOrCreate(
"node0", 0);
1415 node = SCConfGetNodeOrCreate(
"node0", 0);
1421 node = SCConfGetNodeOrCreate(
"parent.child.grandchild", 0);
1426 node = SCConfGetNodeOrCreate(
"parent.child.grandchild", 0);
1429 FAIL_IF(strcmp(node->
val,
"parent.child.grandchild") != 0);
1432 SCConfNode *child1 = SCConfGetNodeOrCreate(
"parent.kids.child1", 0);
1433 SCConfNode *child2 = SCConfGetNodeOrCreate(
"parent.kids.child2", 0);
1434 FAIL_IF(child1 == NULL || child2 == NULL);
1444 static int ConfNodePruneTest(
void)
1475 static int ConfNodeIsSequenceTest(
void)
1489 static int ConfSetFromStringTest(
void)
1522 static int ConfNodeHasChildrenTest(
void)
1547 UtRegisterTest(
"ConfTestGetNonExistant", ConfTestGetNonExistant);
1550 UtRegisterTest(
"ConfTestOverrideValue1", ConfTestOverrideValue1);
1551 UtRegisterTest(
"ConfTestOverrideValue2", ConfTestOverrideValue2);
1554 UtRegisterTest(
"ConfNodeLookupChildTest", ConfNodeLookupChildTest);
1556 ConfNodeLookupChildValueTest);
1559 ConfGetChildValueWithDefaultTest);
1561 ConfGetChildValueIntWithDefaultTest);
1563 ConfGetChildValueBoolWithDefaultTest);
1564 UtRegisterTest(
"ConfGetNodeOrCreateTest", ConfGetNodeOrCreateTest);
1566 UtRegisterTest(
"ConfNodeIsSequenceTest", ConfNodeIsSequenceTest);
1568 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.