2014-06-05 09:34:32 +00:00
|
|
|
/**
|
|
|
|
* Configuration parser
|
|
|
|
*
|
2014-06-05 09:35:41 +00:00
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2014-06-05 09:34:32 +00:00
|
|
|
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
*/
|
|
|
|
|
2014-06-05 09:34:50 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2014-06-05 09:34:32 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-06-05 09:34:50 +00:00
|
|
|
#include "cfg.h"
|
2014-06-05 09:35:23 +00:00
|
|
|
#include "node.h"
|
|
|
|
#include "path.h"
|
2014-06-05 09:34:32 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2014-06-10 16:57:40 +00:00
|
|
|
int config_parse(const char *filename, config_t *cfg, struct settings *g)
|
2014-06-05 09:34:32 +00:00
|
|
|
{
|
2014-06-10 16:57:40 +00:00
|
|
|
if (!config_read_file(cfg, filename)) {
|
2014-06-05 09:34:50 +00:00
|
|
|
error("Failed to parse configuration: %s in %s:%d",
|
|
|
|
config_error_text(cfg),
|
|
|
|
config_error_file(cfg),
|
|
|
|
config_error_line(cfg)
|
|
|
|
);
|
2014-06-05 09:34:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 18:47:22 +00:00
|
|
|
/* Get and check config sections */
|
2014-06-10 16:57:40 +00:00
|
|
|
config_setting_t *cfg_root = config_root_setting(cfg);
|
|
|
|
if (!cfg_root || !config_setting_is_group(cfg_root))
|
2014-06-10 18:47:22 +00:00
|
|
|
error("Missing global section in config file: %s", filename);
|
2014-06-05 09:34:32 +00:00
|
|
|
|
2014-06-10 16:57:40 +00:00
|
|
|
config_setting_t *cfg_nodes = config_setting_get_member(cfg_root, "nodes");
|
2014-06-05 09:34:50 +00:00
|
|
|
if (!cfg_nodes || !config_setting_is_group(cfg_nodes))
|
2014-06-10 18:47:22 +00:00
|
|
|
error("Missing node section in config file: %s", filename);
|
2014-06-05 09:34:50 +00:00
|
|
|
|
2014-06-10 16:57:40 +00:00
|
|
|
config_setting_t *cfg_paths = config_setting_get_member(cfg_root, "paths");
|
2014-06-05 09:34:50 +00:00
|
|
|
if (!cfg_paths || !config_setting_is_list(cfg_paths))
|
2014-06-10 18:47:22 +00:00
|
|
|
error("Missing path section in config file: %s", filename);
|
2014-06-05 09:34:50 +00:00
|
|
|
|
2014-06-10 16:57:40 +00:00
|
|
|
/* Read global settings */
|
|
|
|
config_parse_global(cfg_root, g);
|
|
|
|
|
2014-06-05 09:34:50 +00:00
|
|
|
int node_count = config_setting_length(cfg_nodes);
|
|
|
|
int path_count = config_setting_length(cfg_paths);
|
|
|
|
|
|
|
|
g->nodes = malloc(node_count * sizeof(struct node));
|
|
|
|
if (!g->nodes)
|
|
|
|
error("Failed to allocate memory for nodes");
|
|
|
|
|
|
|
|
g->paths = malloc(path_count * sizeof(struct path));
|
|
|
|
if (!g->paths)
|
|
|
|
error("Failed to allocate memory for paths");
|
|
|
|
|
|
|
|
|
|
|
|
/* Parse nodes */
|
|
|
|
for (int i=0; i < node_count; i++) {
|
|
|
|
config_parse_node(config_setting_get_elem(cfg_nodes, i), g);
|
2014-06-05 09:34:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 09:34:50 +00:00
|
|
|
/* Parse paths */
|
|
|
|
for (int i=0; i < path_count; i++) {
|
|
|
|
config_parse_path(config_setting_get_elem(cfg_paths, i), g);
|
2014-06-05 09:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return CONFIG_TRUE;
|
|
|
|
}
|
|
|
|
|
2014-06-10 16:57:40 +00:00
|
|
|
int config_parse_global(config_setting_t *c, struct settings *g)
|
2014-06-05 09:34:32 +00:00
|
|
|
{
|
2014-06-05 09:34:50 +00:00
|
|
|
if (!config_setting_lookup_string(c, "name", &g->name))
|
|
|
|
cerror(c, "Missing node name");
|
|
|
|
|
|
|
|
config_setting_lookup_int(c, "affinity", &g->affinity);
|
2014-06-05 09:35:23 +00:00
|
|
|
config_setting_lookup_int(c, "priority", &g->priority);
|
2014-06-05 09:34:50 +00:00
|
|
|
config_setting_lookup_int(c, "protocol", &g->protocol);
|
2014-06-05 09:34:32 +00:00
|
|
|
|
2014-06-05 09:35:41 +00:00
|
|
|
g->cfg = c;
|
|
|
|
|
2014-06-05 09:34:32 +00:00
|
|
|
return CONFIG_TRUE;
|
|
|
|
}
|
|
|
|
|
2014-06-10 16:57:40 +00:00
|
|
|
int config_parse_path(config_setting_t *c, struct settings *g)
|
2014-06-05 09:34:32 +00:00
|
|
|
{
|
2014-06-05 09:34:50 +00:00
|
|
|
struct node *in, *out;
|
|
|
|
const char *in_str = NULL;
|
|
|
|
const char *out_str = NULL;
|
|
|
|
int enabled = 1;
|
|
|
|
int reverse = 0;
|
2014-06-05 09:34:32 +00:00
|
|
|
|
2014-06-05 09:34:50 +00:00
|
|
|
/* Optional settings */
|
|
|
|
config_setting_lookup_bool(c, "enabled", &enabled);
|
|
|
|
config_setting_lookup_bool(c, "reverse", &reverse);
|
2014-06-05 09:34:32 +00:00
|
|
|
|
2014-06-05 09:34:50 +00:00
|
|
|
/* Required settings */
|
|
|
|
if (!config_setting_lookup_string(c, "in", &in_str))
|
|
|
|
cerror(c, "Missing input node for path");
|
2014-06-05 09:34:32 +00:00
|
|
|
|
2014-06-05 09:34:50 +00:00
|
|
|
if (!config_setting_lookup_string(c, "out", &out_str))
|
|
|
|
cerror(c, "Missing output node for path");
|
|
|
|
|
|
|
|
info("Loading path from '%s' to '%s'", in_str, out_str);
|
|
|
|
|
|
|
|
in = node_lookup_name(in_str, g->nodes, g->node_count);
|
|
|
|
if (!in)
|
|
|
|
cerror(c, "Invalid input node '%s'");
|
|
|
|
|
|
|
|
out = node_lookup_name(out_str, g->nodes, g->node_count);
|
|
|
|
if (!out)
|
|
|
|
cerror(c, "Invalid output node '%s'", out_str);
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
if (path_create(&g->paths[g->path_count], in, out))
|
|
|
|
cerror(c, "Failed to parse path");
|
|
|
|
|
2014-06-05 09:35:41 +00:00
|
|
|
g->cfg = c;
|
2014-06-05 09:34:50 +00:00
|
|
|
g->path_count++;
|
|
|
|
|
|
|
|
if (reverse) {
|
|
|
|
if (path_create(&g->paths[g->path_count], out, in))
|
|
|
|
cerror(c, "Failed to parse path");
|
|
|
|
|
|
|
|
g->path_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
warn(" Path is not enabled");
|
|
|
|
}
|
|
|
|
|
2014-06-10 16:57:40 +00:00
|
|
|
int config_parse_node(config_setting_t *c, struct settings *g)
|
2014-06-05 09:34:50 +00:00
|
|
|
{
|
|
|
|
const char *name = NULL;
|
|
|
|
const char *type_str = NULL;
|
|
|
|
const char *remote_str = NULL;
|
|
|
|
const char *local_str = NULL;
|
|
|
|
|
2014-06-05 09:35:41 +00:00
|
|
|
struct sockaddr_in local, remote;
|
2014-06-05 09:34:50 +00:00
|
|
|
enum node_type type;
|
|
|
|
|
|
|
|
/* Required settings */
|
|
|
|
name = config_setting_name(c);
|
|
|
|
if (!name)
|
|
|
|
cerror(c, "Missing node name");
|
|
|
|
|
|
|
|
if (!config_setting_lookup_string(c, "type", &type_str))
|
|
|
|
cerror(c, "Missing node type");
|
|
|
|
|
|
|
|
if (!config_setting_lookup_string(c, "remote", &remote_str))
|
|
|
|
cerror(c, "Missing node remote address");
|
|
|
|
|
|
|
|
if (!config_setting_lookup_string(c, "local", &local_str))
|
|
|
|
cerror(c, "Missing node local address");
|
|
|
|
|
|
|
|
type = node_lookup_type(type_str);
|
|
|
|
if (type == NODE_INVALID)
|
|
|
|
cerror(c, "Invalid node type '%s'", type);
|
|
|
|
|
|
|
|
|
|
|
|
info("Loading %s node '%s'", type_str, name);
|
|
|
|
|
2014-06-05 09:35:33 +00:00
|
|
|
if (resolve_addr(local_str, &local, 0))
|
2014-06-05 09:34:50 +00:00
|
|
|
cerror(c, "Failed to resolve local address '%s' of node '%s'", local_str, name);
|
|
|
|
|
2014-06-05 09:35:33 +00:00
|
|
|
if (resolve_addr(remote_str, &remote, 0))
|
2014-06-05 09:34:50 +00:00
|
|
|
cerror(c, "Failed to resolve remote address '%s' of node '%s'", remote_str, name);
|
|
|
|
|
|
|
|
if (node_create(&g->nodes[g->node_count], name, type, local, remote))
|
|
|
|
cerror(c, "Failed to parse node");
|
|
|
|
|
2014-06-05 09:35:41 +00:00
|
|
|
g->cfg = c;
|
2014-06-05 09:34:50 +00:00
|
|
|
g->node_count++;
|
2014-06-05 09:34:32 +00:00
|
|
|
}
|