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);
377 const char *name,
const char **vptr)
381 if (ret == 0 && dflt) {
399 const char *strval = NULL;
403 if (
ConfGet(name, &strval) == 0)
406 if (strval == NULL) {
414 tmpint = strtoimax(strval, &endptr, 0);
415 if (strval[0] ==
'\0' || *endptr !=
'\0') {
421 if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
434 const char *strval = NULL;
441 tmpint = strtoimax(strval, &endptr, 0);
442 if (strval[0] ==
'\0' || *endptr !=
'\0') {
444 "for %s with base %s: '%s'",
445 name, base->
name, strval);
448 if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
450 " base %s out of range: '%s'",
451 name, base->
name, strval);
461 const char *name, intmax_t *val)
465 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')
699 SCLogDebug(
"configuration module de-initialized");
702 static char *ConfPrintNameArray(
char **name_arr,
int level)
704 static char name[128*128];
708 for (i = 0; i <= level; i++) {
709 strlcat(name, name_arr[i],
sizeof(name));
711 strlcat(name,
".",
sizeof(name));
724 static char *name[128];
725 static int level = -1;
730 if (
unlikely(name[level] == NULL)) {
733 if (prefix == NULL) {
734 printf(
"%s = %s\n", ConfPrintNameArray(name, level),
738 printf(
"%s.%s = %s\n", prefix,
739 ConfPrintNameArray(name, level), child->
val);
789 if (node == NULL || name == NULL) {
794 if (child->
name != NULL && strcmp(child->
name, name) == 0)
836 if (!strncmp(child->
val, key, strlen(child->
val))) {
839 if ((!strcmp(subchild->
name, key)) && (!strcmp(subchild->
val, value))) {
874 const char *defaultpath = NULL;
879 if (
ConfGet(
"include-path", &defaultpath) == 1) {
881 size_t path_len =
sizeof(char) * (strlen(defaultpath) +
886 strlcpy(path, defaultpath, path_len);
887 if (path[strlen(path) - 1] !=
'/')
917 for (item =
TAILQ_FIRST(&node->head); item != NULL; item = it) {
923 if (item->
name != NULL)
925 if (item->
val != NULL)
932 if (node->
val != NULL) {
947 return node->
is_seq == 0 ? 0 : 1;
962 if (ifaces_list_node == NULL) {
963 SCLogError(
"unable to find %s config", ifaces_node_name);
969 SCLogNotice(
"unable to find interface %s in DPDK config", iface);
983 const char *ifaces_node_name,
const char *iface,
ConfNode **if_root,
ConfNode **if_default)
985 const char *default_iface =
"default";
989 if (*if_root == NULL && *if_default == NULL) {
990 SCLogError(
"unable to find configuration for the interface \"%s\" or the default "
991 "configuration (\"%s\")",
992 iface, default_iface);
997 if (*if_root == NULL) {
998 *if_root = *if_default;
1009 static int ConfTestGetNonExistant(
void)
1011 char name[] =
"non-existant-value";
1021 static int ConfTestSetAndGet(
void)
1023 char name[] =
"some-name";
1024 char value[] =
"some-value";
1025 const char *value0 = NULL;
1030 FAIL_IF(strcmp(value, value0) != 0);
1042 static int ConfTestOverrideValue1(
void)
1044 char name[] =
"some-name";
1045 char value0[] =
"some-value";
1046 char value1[] =
"new-value";
1047 const char *val = NULL;
1053 FAIL_IF(strcmp(val, value1) != 0);
1064 static int ConfTestOverrideValue2(
void)
1066 char name[] =
"some-name";
1067 char value0[] =
"some-value";
1068 char value1[] =
"new-value";
1069 const char *val = NULL;
1075 FAIL_IF(strcmp(val, value0) != 0);
1086 static int ConfTestGetInt(
void)
1088 char name[] =
"some-int.x";
1112 static int ConfTestGetBool(
void)
1114 char name[] =
"some-bool";
1115 const char *trues[] = {
1121 const char *falses[] = {
1131 for (u = 0; u <
sizeof(trues) /
sizeof(trues[0]); u++) {
1137 for (u = 0; u <
sizeof(falses) /
sizeof(falses[0]); u++) {
1146 static int ConfNodeLookupChildTest(
void)
1148 const char *test_vals[] = {
"one",
"two",
"three" };
1154 for (u = 0; u <
sizeof(test_vals)/
sizeof(test_vals[0]); u++) {
1181 if (parent != NULL) {
1188 static int ConfNodeLookupChildValueTest(
void)
1190 const char *test_vals[] = {
"one",
"two",
"three" };
1197 for (u = 0; u <
sizeof(test_vals)/
sizeof(test_vals[0]); u++) {
1206 FAIL_IF(strcmp(value,
"one") != 0);
1210 FAIL_IF(strcmp(value,
"two") != 0);
1214 FAIL_IF(strcmp(value,
"three") != 0);
1224 static int ConfGetChildValueWithDefaultTest(
void)
1226 const char *val =
"";
1229 ConfSet(
"af-packet.0.interface",
"eth0");
1230 ConfSet(
"af-packet.1.interface",
"default");
1231 ConfSet(
"af-packet.1.cluster-type",
"cluster_cpu");
1236 FAIL_IF(strcmp(val,
"cluster_cpu"));
1238 ConfSet(
"af-packet.0.cluster-type",
"cluster_flow");
1241 FAIL_IF(strcmp(val,
"cluster_flow"));
1248 static int ConfGetChildValueIntWithDefaultTest(
void)
1253 ConfSet(
"af-packet.0.interface",
"eth0");
1254 ConfSet(
"af-packet.1.interface",
"default");
1255 ConfSet(
"af-packet.1.threads",
"2");
1262 ConfSet(
"af-packet.0.threads",
"1");
1272 static int ConfGetChildValueBoolWithDefaultTest(
void)
1277 ConfSet(
"af-packet.0.interface",
"eth0");
1278 ConfSet(
"af-packet.1.interface",
"default");
1279 ConfSet(
"af-packet.1.use-mmap",
"yes");
1286 ConfSet(
"af-packet.0.use-mmap",
"no");
1299 static int ConfNodeRemoveTest(
void)
1319 static int ConfSetTest(
void)
1347 static int ConfGetNodeOrCreateTest(
void)
1356 node = ConfGetNodeOrCreate(
"node0", 0);
1361 node = ConfGetNodeOrCreate(
"node0", 0);
1367 node = ConfGetNodeOrCreate(
"parent.child.grandchild", 0);
1372 node = ConfGetNodeOrCreate(
"parent.child.grandchild", 0);
1375 FAIL_IF(strcmp(node->
val,
"parent.child.grandchild") != 0);
1378 ConfNode *child1 = ConfGetNodeOrCreate(
"parent.kids.child1", 0);
1379 ConfNode *child2 = ConfGetNodeOrCreate(
"parent.kids.child2", 0);
1380 FAIL_IF(child1 == NULL || child2 == NULL);
1390 static int ConfNodePruneTest(
void)
1421 static int ConfNodeIsSequenceTest(
void)
1435 static int ConfSetFromStringTest(
void)
1468 static int ConfNodeHasChildrenTest(
void)
1474 ConfSet(
"no-children",
"value");
1481 ConfSet(
"parent.child",
"value");
1493 UtRegisterTest(
"ConfTestGetNonExistant", ConfTestGetNonExistant);
1496 UtRegisterTest(
"ConfTestOverrideValue1", ConfTestOverrideValue1);
1497 UtRegisterTest(
"ConfTestOverrideValue2", ConfTestOverrideValue2);
1500 UtRegisterTest(
"ConfNodeLookupChildTest", ConfNodeLookupChildTest);
1502 ConfNodeLookupChildValueTest);
1505 ConfGetChildValueWithDefaultTest);
1507 ConfGetChildValueIntWithDefaultTest);
1509 ConfGetChildValueBoolWithDefaultTest);
1510 UtRegisterTest(
"ConfGetNodeOrCreateTest", ConfGetNodeOrCreateTest);
1512 UtRegisterTest(
"ConfNodeIsSequenceTest", ConfNodeIsSequenceTest);
1514 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)
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)
Prune a configuration node.
#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.
int PathIsRelative(const char *path)
Check if a path is relative.
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.
char * ConfLoadCompleteIncludePath(const char *file)
Create the path for an include entry.
#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.