-/*
- Add given value to the list of configs cfg
-*/
-config_t *
-add_config_val(config_t **cfg, int argtype, char *val)
-{
- config_t *p;
- char *q;
-cp
- p = (config_t*)xmalloc(sizeof(*p));
- p->data.val = 0;
-
- switch(argtype)
- {
- case TYPE_INT:
- p->data.val = strtol(val, &q, 0);
- if(q && *q)
- p->data.val = 0;
- break;
- case TYPE_NAME:
- p->data.ptr = xmalloc(strlen(val) + 1);
- strcpy(p->data.ptr, val);
- break;
- case TYPE_IP:
- p->data.ip = strtoip(val);
- break;
- case TYPE_BOOL:
- if(!strcasecmp("yes", val))
- p->data.val = stupid_true;
- else if(!strcasecmp("no", val))
- p->data.val = stupid_false;
- else
- p->data.val = 0;
- }
-
- p->argtype = argtype;
-
- if(p->data.val)
- {
- p->next = *cfg;
- *cfg = p;
-cp
- return p;
- }
- else
- {
- free(p);
-cp
- return NULL;
- }
+ if(result)
+ return result;
+ else
+ return strcmp(a->file, b->file);
+}
+
+void init_configuration(splay_tree_t ** config_tree) {
+ cp();
+
+ *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config);
+}
+
+void exit_configuration(splay_tree_t ** config_tree) {
+ cp();
+
+ splay_delete_tree(*config_tree);
+ *config_tree = NULL;
+}
+
+config_t *new_config(void) {
+ cp();
+
+ return xmalloc_and_zero(sizeof(config_t));
+}
+
+void free_config(config_t *cfg) {
+ cp();
+
+ if(cfg->variable)
+ free(cfg->variable);
+
+ if(cfg->value)
+ free(cfg->value);
+
+ if(cfg->file)
+ free(cfg->file);
+
+ free(cfg);
+}
+
+void config_add(splay_tree_t *config_tree, config_t *cfg) {
+ cp();
+
+ splay_insert(config_tree, cfg);
+}
+
+config_t *lookup_config(splay_tree_t *config_tree, char *variable) {
+ config_t cfg, *found;
+
+ cp();
+
+ cfg.variable = variable;
+ cfg.file = "";
+ cfg.line = 0;
+
+ found = splay_search_closest_greater(config_tree, &cfg);
+
+ if(!found)
+ return NULL;
+
+ if(strcasecmp(found->variable, variable))
+ return NULL;
+
+ return found;
+}
+
+config_t *lookup_config_next(splay_tree_t *config_tree, const config_t *cfg) {
+ splay_node_t *node;
+ config_t *found;
+
+ cp();
+
+ node = splay_search_node(config_tree, cfg);
+
+ if(node) {
+ if(node->next) {
+ found = node->next->data;
+
+ if(!strcasecmp(found->variable, cfg->variable))
+ return found;
+ }
+ }
+
+ return NULL;
+}
+
+bool get_config_bool(const config_t *cfg, bool *result) {
+ cp();
+
+ if(!cfg)
+ return false;
+
+ if(!strcasecmp(cfg->value, "yes")) {
+ *result = true;
+ return true;
+ } else if(!strcasecmp(cfg->value, "no")) {
+ *result = false;
+ return true;
+ }
+
+ logger(LOG_ERR, _("\"yes\" or \"no\" expected for configuration variable %s in %s line %d"),
+ cfg->variable, cfg->file, cfg->line);
+
+ return false;
+}
+
+bool get_config_int(const config_t *cfg, int *result) {
+ cp();
+
+ if(!cfg)
+ return false;
+
+ if(sscanf(cfg->value, "%d", result) == 1)
+ return true;
+
+ logger(LOG_ERR, _("Integer expected for configuration variable %s in %s line %d"),
+ cfg->variable, cfg->file, cfg->line);
+
+ return false;
+}
+
+bool get_config_string(const config_t *cfg, char **result) {
+ cp();
+
+ if(!cfg)
+ return false;
+
+ *result = xstrdup(cfg->value);
+
+ return true;
+}
+
+bool get_config_address(const config_t *cfg, struct addrinfo **result) {
+ struct addrinfo *ai;
+
+ cp();
+
+ if(!cfg)
+ return false;
+
+ ai = str2addrinfo(cfg->value, NULL, 0);
+
+ if(ai) {
+ *result = ai;
+ return true;
+ }
+
+ logger(LOG_ERR, _("Hostname or IP address expected for configuration variable %s in %s line %d"),
+ cfg->variable, cfg->file, cfg->line);
+
+ return false;
+}
+
+bool get_config_subnet(const config_t *cfg, subnet_t ** result) {
+ subnet_t subnet = {0};
+
+ cp();
+
+ if(!cfg)
+ return false;
+
+ if(!str2net(&subnet, cfg->value)) {
+ logger(LOG_ERR, _("Subnet expected for configuration variable %s in %s line %d"),
+ cfg->variable, cfg->file, cfg->line);
+ return false;
+ }
+
+ /* Teach newbies what subnets are... */
+
+ if(((subnet.type == SUBNET_IPV4)
+ && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof subnet.net.ipv4.address))
+ || ((subnet.type == SUBNET_IPV6)
+ && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof subnet.net.ipv6.address))) {
+ logger(LOG_ERR, _ ("Network address and prefix length do not match for configuration variable %s in %s line %d"),
+ cfg->variable, cfg->file, cfg->line);
+ return false;
+ }
+
+ *(*result = new_subnet()) = subnet;
+
+ return true;