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)) {
74 SCLogError(
"Configuration name too long: %s", 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 ConfNode *ConfGetNodeOrCreate(
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)) {
189 SCLogError(
"Configuration name too long: %s", name);
195 if ((
next = strchr(key,
'.')) != NULL)
199 }
while (
next != NULL && node != NULL);
224 int ConfSet(
const char *name,
const char *val)
226 ConfNode *node = ConfGetNodeOrCreate(name, 0);
227 if (node == NULL || node->
final) {
230 if (node->
val != NULL)
252 char *name =
SCStrdup(input), *val = NULL;
256 val = strchr(name,
'=');
262 while (isspace((
int)name[strlen(name) - 1])) {
263 name[strlen(name) - 1] =
'\0';
266 while (isspace((
int)*val)) {
305 ConfNode *node = ConfGetNodeOrCreate(name, 1);
309 if (node->
val != NULL)
335 int ConfGet(
const char *name,
const char **vptr)
339 SCLogDebug(
"failed to lookup configuration parameter '%s'", name);
353 SCLogDebug(
"failed to lookup configuration parameter '%s'", name);
357 if (node->
val == NULL)
379 const char *name,
const char **vptr)
383 if (ret == 0 && dflt) {
401 const char *strval = NULL;
405 if (
ConfGet(name, &strval) == 0)
408 if (strval == NULL) {
416 tmpint = strtoimax(strval, &endptr, 0);
417 if (strval[0] ==
'\0' || *endptr !=
'\0') {
423 if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
436 const char *strval = NULL;
443 tmpint = strtoimax(strval, &endptr, 0);
444 if (strval[0] ==
'\0' || *endptr !=
'\0') {
446 "for %s with base %s: '%s'",
447 name, base->
name, strval);
450 if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
452 " base %s out of range: '%s'",
453 name, base->
name, strval);
462 const char *name, intmax_t *val)
466 if (ret == 0 && dflt) {
484 const char *strval = NULL;
487 if (
ConfGet(name, &strval) != 1)
502 const char *strval = NULL;
514 const char *name,
int *val)
518 if (ret == 0 && dflt) {
538 const char *trues[] = {
"1",
"yes",
"true",
"on"};
541 for (u = 0; u <
sizeof(trues) /
sizeof(trues[0]); u++) {
542 if (strcasecmp(val, trues[u]) == 0) {
563 const char *falses[] = {
"0",
"no",
"false",
"off"};
566 for (u = 0; u <
sizeof(falses) /
sizeof(falses[0]); u++) {
567 if (strcasecmp(val, falses[u]) == 0) {
587 const char *strval = NULL;
591 if (
ConfGet(name, &strval) == 0)
595 tmpdo = strtod(strval, &endptr);
596 if (strval[0] ==
'\0' || *endptr !=
'\0')
617 const char *strval = NULL;
621 if (
ConfGet(name, &strval) == 0)
625 tmpfl = strtof(strval, &endptr);
626 if (strval[0] ==
'\0' || *endptr !=
'\0')
695 SCLogDebug(
"configuration module de-initialized");
698 static char *ConfPrintNameArray(
char **name_arr,
int level)
700 static char name[128*128];
704 for (i = 0; i <= level; i++) {
705 strlcat(name, name_arr[i],
sizeof(name));
707 strlcat(name,
".",
sizeof(name));
720 static char *name[128];
721 static int level = -1;
726 if (
unlikely(name[level] == NULL)) {
729 if (prefix == NULL) {
730 printf(
"%s = %s\n", ConfPrintNameArray(name, level),
734 printf(
"%s.%s = %s\n", prefix,
735 ConfPrintNameArray(name, level), child->
val);
785 if (node == NULL || name == NULL) {
790 if (child->
name != NULL && strcmp(child->
name, name) == 0)
832 if (!strncmp(child->
val, key, strlen(child->
val))) {
835 if ((!strcmp(subchild->
name, key)) && (!strcmp(subchild->
val, value))) {
883 for (item =
TAILQ_FIRST(&node->head); item != NULL; item = it) {
889 if (item->
name != NULL)
891 if (item->
val != NULL)
898 if (node->
val != NULL) {
913 return node->
is_seq == 0 ? 0 : 1;
928 if (ifaces_list_node == NULL) {
929 SCLogError(
"unable to find %s config", ifaces_node_name);
935 SCLogNotice(
"unable to find interface %s in DPDK config", iface);
949 const char *ifaces_node_name,
const char *iface,
ConfNode **if_root,
ConfNode **if_default)
951 const char *default_iface =
"default";
955 if (*if_root == NULL && *if_default == NULL) {
956 SCLogError(
"unable to find configuration for the interface \"%s\" or the default "
957 "configuration (\"%s\")",
958 iface, default_iface);
963 if (*if_root == NULL) {
964 *if_root = *if_default;
975 static int ConfTestGetNonExistant(
void)
977 char name[] =
"non-existant-value";
987 static int ConfTestSetAndGet(
void)
989 char name[] =
"some-name";
990 char value[] =
"some-value";
991 const char *value0 = NULL;
996 FAIL_IF(strcmp(value, value0) != 0);
1008 static int ConfTestOverrideValue1(
void)
1010 char name[] =
"some-name";
1011 char value0[] =
"some-value";
1012 char value1[] =
"new-value";
1013 const char *val = NULL;
1019 FAIL_IF(strcmp(val, value1) != 0);
1030 static int ConfTestOverrideValue2(
void)
1032 char name[] =
"some-name";
1033 char value0[] =
"some-value";
1034 char value1[] =
"new-value";
1035 const char *val = NULL;
1041 FAIL_IF(strcmp(val, value0) != 0);
1052 static int ConfTestGetInt(
void)
1054 char name[] =
"some-int.x";
1078 static int ConfTestGetBool(
void)
1080 char name[] =
"some-bool";
1081 const char *trues[] = {
1087 const char *falses[] = {
1097 for (u = 0; u <
sizeof(trues) /
sizeof(trues[0]); u++) {
1103 for (u = 0; u <
sizeof(falses) /
sizeof(falses[0]); u++) {
1112 static int ConfNodeLookupChildTest(
void)
1114 const char *test_vals[] = {
"one",
"two",
"three" };
1120 for (u = 0; u <
sizeof(test_vals)/
sizeof(test_vals[0]); u++) {
1147 if (parent != NULL) {
1154 static int ConfNodeLookupChildValueTest(
void)
1156 const char *test_vals[] = {
"one",
"two",
"three" };
1163 for (u = 0; u <
sizeof(test_vals)/
sizeof(test_vals[0]); u++) {
1172 FAIL_IF(strcmp(value,
"one") != 0);
1176 FAIL_IF(strcmp(value,
"two") != 0);
1180 FAIL_IF(strcmp(value,
"three") != 0);
1190 static int ConfGetChildValueWithDefaultTest(
void)
1192 const char *val =
"";
1195 ConfSet(
"af-packet.0.interface",
"eth0");
1196 ConfSet(
"af-packet.1.interface",
"default");
1197 ConfSet(
"af-packet.1.cluster-type",
"cluster_cpu");
1202 FAIL_IF(strcmp(val,
"cluster_cpu"));
1204 ConfSet(
"af-packet.0.cluster-type",
"cluster_flow");
1207 FAIL_IF(strcmp(val,
"cluster_flow"));
1214 static int ConfGetChildValueIntWithDefaultTest(
void)
1219 ConfSet(
"af-packet.0.interface",
"eth0");
1220 ConfSet(
"af-packet.1.interface",
"default");
1221 ConfSet(
"af-packet.1.threads",
"2");
1228 ConfSet(
"af-packet.0.threads",
"1");
1238 static int ConfGetChildValueBoolWithDefaultTest(
void)
1243 ConfSet(
"af-packet.0.interface",
"eth0");
1244 ConfSet(
"af-packet.1.interface",
"default");
1245 ConfSet(
"af-packet.1.use-mmap",
"yes");
1252 ConfSet(
"af-packet.0.use-mmap",
"no");
1265 static int ConfNodeRemoveTest(
void)
1285 static int ConfSetTest(
void)
1313 static int ConfGetNodeOrCreateTest(
void)
1322 node = ConfGetNodeOrCreate(
"node0", 0);
1327 node = ConfGetNodeOrCreate(
"node0", 0);
1333 node = ConfGetNodeOrCreate(
"parent.child.grandchild", 0);
1338 node = ConfGetNodeOrCreate(
"parent.child.grandchild", 0);
1341 FAIL_IF(strcmp(node->
val,
"parent.child.grandchild") != 0);
1344 ConfNode *child1 = ConfGetNodeOrCreate(
"parent.kids.child1", 0);
1345 ConfNode *child2 = ConfGetNodeOrCreate(
"parent.kids.child2", 0);
1346 FAIL_IF(child1 == NULL || child2 == NULL);
1356 static int ConfNodePruneTest(
void)
1387 static int ConfNodeIsSequenceTest(
void)
1401 static int ConfSetFromStringTest(
void)
1434 static int ConfNodeHasChildrenTest(
void)
1440 ConfSet(
"no-children",
"value");
1447 ConfSet(
"parent.child",
"value");
1459 UtRegisterTest(
"ConfTestGetNonExistant", ConfTestGetNonExistant);
1462 UtRegisterTest(
"ConfTestOverrideValue1", ConfTestOverrideValue1);
1463 UtRegisterTest(
"ConfTestOverrideValue2", ConfTestOverrideValue2);
1466 UtRegisterTest(
"ConfNodeLookupChildTest", ConfNodeLookupChildTest);
1468 ConfNodeLookupChildValueTest);
1471 ConfGetChildValueWithDefaultTest);
1473 ConfGetChildValueIntWithDefaultTest);
1475 ConfGetChildValueBoolWithDefaultTest);
1476 UtRegisterTest(
"ConfGetNodeOrCreateTest", ConfGetNodeOrCreateTest);
1478 UtRegisterTest(
"ConfNodeIsSequenceTest", ConfNodeIsSequenceTest);
1480 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 a boolean.
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)
int ConfValIsTrue(const char *val)
Check if a value is true.
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
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)
Create the path for an include entry.
#define PASS
Pass the test.
size_t strlcat(char *, const char *src, size_t siz)
struct ConfNode_ * parent
ConfNode * ConfFindDeviceConfig(ConfNode *node, const char *iface)
Find the configuration node for a specific device.
int ConfGetChildValueWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name, const char **vptr)
#define SCLogWarning(...)
Macro used to log WARNING messages.
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.
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 true.
void ConfRegisterTests(void)
#define TAILQ_NEXT(elm, field)
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.
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 SCLogError(...)
Macro used to log ERROR messages.
ConfNode * ConfNodeGetNodeOrCreate(ConfNode *parent, const char *name, int final)
Helper function to get a node, creating it if it does not exist.
int ConfValIsFalse(const char *val)
Check if a value is false.
int ConfSetRootAndDefaultNodes(const char *ifaces_node_name, const char *iface, ConfNode **if_root, ConfNode **if_default)
Finds and sets root and default node of the interface.
void ConfDeInit(void)
De-initializes the configuration system.
int ConfSet(const char *name, const char *val)
Set a configuration value.
ConfNode * ConfSetIfaceNode(const char *ifaces_node_name, const char *iface)
Finds an interface from the list of interfaces.
#define SCLogNotice(...)
Macro used to log NOTICE messages.
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.