#include "configuration.h"
#include <sys/socket.h>
+#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <jansson.h>
}
static int
-handle_json(json_t *root)
+load_vrps(json_t *root)
{
- json_t *listen;
+ struct stat attr;
json_t *vrps;
- json_t *interval;
- char const *address;
- char const *port;
char const *vrps_location;
- int queue;
int vrps_check_interval;
- int refresh_interval;
- int retry_interval;
- int expire_interval;
int error;
- if (!json_is_object(root)) {
- warnx("The root of the JSON file is not a JSON object.");
- return -EINVAL;
- }
-
- listen = json_object_get(root, OPTNAME_LISTEN);
- if (listen != NULL) {
- if (!json_is_object(listen)) {
- warnx("The '%s' element is not a JSON object.",
- OPTNAME_LISTEN);
- return -EINVAL;
- }
-
- error = json_get_string(listen, OPTNAME_LISTEN_ADDRESS,
- DEFAULT_ADDR, &address);
- if (error)
- return error;
-
- error = json_get_string(listen, OPTNAME_LISTEN_PORT,
- DEFAULT_PORT, &port);
- if (error)
- return error;
-
- error = load_range(listen, OPTNAME_LISTEN_QUEUE,
- DEFAULT_QUEUE, &queue,
- MIN_LISTEN_QUEUE, MAX_LISTEN_QUEUE);
- if (error)
- return error;
- config.queue = queue;
-
- } else {
- address = DEFAULT_ADDR;
- port = DEFAULT_PORT;
- config.queue = DEFAULT_QUEUE;
- }
-
vrps = json_object_get(root, OPTNAME_VRPS);
if (vrps != NULL) {
if (!json_is_object(vrps)) {
if (config.vrps_location == NULL) {
warn("'%s' couldn't be allocated.",
OPTNAME_VRPS_LOCATION);
- return errno;
+ return -errno;
}
/*
config.vrps_check_interval = DEFAULT_VRPS_CHECK_INTERVAL;
}
- /*
- * Exclusively for RTR v1, so this are optional values to configure
- * since RTR v1 isn't fully supported yet
- */
+ /* Validate required data */
+ error = stat(config.vrps_location, &attr) < 0;
+ if (error) {
+ warn("VRPs location '%s' isn't a valid path",
+ config.vrps_location);
+ return -errno;
+ }
+ if (S_ISDIR(attr.st_mode) != 0) {
+ warnx("VRPs location '%s' isn't a file", config.vrps_location);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+/*
+ * Exclusively for RTR v1, so this are optional values to configure
+ * since RTR v1 isn't fully supported yet
+ */
+static int
+load_intervals(json_t *root)
+{
+ json_t *interval;
+ int refresh_interval;
+ int retry_interval;
+ int expire_interval;
+ int error;
interval = json_object_get(root, OPTNAME_RTR_INTERVAL);
if (interval != NULL) {
if (!json_is_object(interval)) {
config.expire_interval = DEFAULT_EXPIRE_INTERVAL;
}
+ return 0;
+}
+
+static int
+handle_json(json_t *root)
+{
+ json_t *listen;
+ char const *address;
+ char const *port;
+ int queue;
+ int error;
+
+ if (!json_is_object(root)) {
+ warnx("The root of the JSON file is not a JSON object.");
+ return -EINVAL;
+ }
+
+ listen = json_object_get(root, OPTNAME_LISTEN);
+ if (listen != NULL) {
+ if (!json_is_object(listen)) {
+ warnx("The '%s' element is not a JSON object.",
+ OPTNAME_LISTEN);
+ return -EINVAL;
+ }
+
+ error = json_get_string(listen, OPTNAME_LISTEN_ADDRESS,
+ DEFAULT_ADDR, &address);
+ if (error)
+ return error;
+
+ error = json_get_string(listen, OPTNAME_LISTEN_PORT,
+ DEFAULT_PORT, &port);
+ if (error)
+ return error;
+
+ error = load_range(listen, OPTNAME_LISTEN_QUEUE,
+ DEFAULT_QUEUE, &queue,
+ MIN_LISTEN_QUEUE, MAX_LISTEN_QUEUE);
+ if (error)
+ return error;
+ config.queue = queue;
+
+ } else {
+ address = DEFAULT_ADDR;
+ port = DEFAULT_PORT;
+ config.queue = DEFAULT_QUEUE;
+ }
+
+ error = load_vrps(root);
+ if (error)
+ return error;
+
+ error = load_intervals(root);
+ if (error)
+ return error;
+
return init_addrinfo(address, port);
}