From: pcarana Date: Tue, 5 Mar 2019 00:33:24 +0000 (-0600) Subject: Create daemon and conf to look for VRPs file updates X-Git-Tag: v0.0.2~52^2~45 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=90b53ccb63e761855ef728458e1bf8462582b4cc;p=thirdparty%2FFORT-validator.git Create daemon and conf to look for VRPs file updates --- diff --git a/src/Makefile.am b/src/Makefile.am index 25e66db3..21449a28 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,6 +10,7 @@ rtr_server_SOURCES += common.c common.h rtr_server_SOURCES += configuration.c configuration.h rtr_server_SOURCES += csv.c csv.h rtr_server_SOURCES += line_file.c line_file.h +rtr_server_SOURCES += updates_daemon.c updates_daemon.h rtr_server_SOURCES += vrps.c vrps.h rtr_server_SOURCES += rtr/pdu_handler.c rtr/pdu_handler.h diff --git a/src/configuration.c b/src/configuration.c index b1ff5d19..2070ce5c 100644 --- a/src/configuration.c +++ b/src/configuration.c @@ -13,7 +13,9 @@ #define OPTNAME_LISTEN "listen" #define OPTNAME_LISTEN_ADDRESS "address" #define OPTNAME_LISTEN_PORT "port" -#define OPTNAME_VRPS_LOCATION "vrpsLocation" +#define OPTNAME_VRPS "vrps" +#define OPTNAME_VRPS_LOCATION "location" +#define OPTNAME_VRPS_CHECK_INTERVAL "checkInterval" #define OPTNAME_RTR_INTERVAL "rtrInterval" #define OPTNAME_RTR_INTERVAL_REFRESH "refresh" #define OPTNAME_RTR_INTERVAL_RETRY "retry" @@ -21,12 +23,15 @@ #define DEFAULT_ADDR NULL #define DEFAULT_PORT "323" -#define DEFAULT_VRPS NULL +#define DEFAULT_VRPS_LOCATION NULL +#define DEFAULT_VRPS_CHECK_INTERVAL 60 #define DEFAULT_REFRESH_INTERVAL 3600 #define DEFAULT_RETRY_INTERVAL 600 #define DEFAULT_EXPIRE_INTERVAL 7200 /* Protocol timing parameters ranges */ +#define MIN_VRPS_CHECK_INTERVAL 1 +#define MAX_VRPS_CHECK_INTERVAL 7200 #define MIN_REFRESH_INTERVAL 1 #define MAX_REFRESH_INTERVAL 86400 #define MIN_RETRY_INTERVAL 1 @@ -41,6 +46,8 @@ struct rtr_config { char *port; /** VRPs (Validated ROA Payload) location */ char *vrps_location; + /** Interval used to look for updates at VRPs location */ + int vrps_check_interval; /** Intervals use at RTR v1 End of data PDU **/ int refresh_interval; int retry_interval; @@ -116,10 +123,12 @@ static int handle_json(json_t *root) { json_t *listen; + json_t *vrps; json_t *interval; char const *address; char const *port; - char const *vrps; + char const *vrps_location; + int vrps_check_interval; int refresh_interval; int retry_interval; int expire_interval; @@ -153,11 +162,30 @@ handle_json(json_t *root) port = DEFAULT_PORT; } - error = json_get_string(root, OPTNAME_VRPS_LOCATION, - DEFAULT_VRPS, &vrps); - if (error) - return error; - config.vrps_location = str_clone(vrps); + vrps = json_object_get(root, OPTNAME_VRPS); + if (vrps != NULL) { + if (!json_is_object(vrps)) { + warnx("The '%s' element is not a JSON object.", + OPTNAME_VRPS); + return -EINVAL; + } + + error = json_get_string(vrps, OPTNAME_VRPS_LOCATION, + DEFAULT_VRPS_LOCATION, &vrps_location); + if (error) + return error; + config.vrps_location = str_clone(vrps_location); + + error = load_interval(vrps, OPTNAME_VRPS_CHECK_INTERVAL, + DEFAULT_VRPS_CHECK_INTERVAL, &vrps_check_interval, + MIN_VRPS_CHECK_INTERVAL, MAX_VRPS_CHECK_INTERVAL); + if (error) + return error; + config.vrps_check_interval = vrps_check_interval; + } else { + config.vrps_location = DEFAULT_VRPS_LOCATION; + config.vrps_check_interval = DEFAULT_VRPS_CHECK_INTERVAL; + } interval = json_object_get(root, OPTNAME_RTR_INTERVAL); if (interval != NULL) { @@ -280,6 +308,12 @@ config_get_vrps_location(void) return config.vrps_location; } +int +config_get_vrps_check_interval(void) +{ + return config.vrps_check_interval; +} + int config_get_refresh_interval(void) { diff --git a/src/configuration.h b/src/configuration.h index 47263127..3474f68f 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -9,6 +9,7 @@ void config_cleanup(void); struct addrinfo const *config_get_server_addrinfo(void); char const *config_get_server_port(void); char const *config_get_vrps_location(void); +int config_get_vrps_check_interval(void); int config_get_refresh_interval(void); int config_get_retry_interval(void); int config_get_expire_interval(void); diff --git a/src/csv.c b/src/csv.c index 85bb4f42..ec852ca7 100644 --- a/src/csv.c +++ b/src/csv.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include "configuration.h" #include "address.h" @@ -232,10 +234,13 @@ end: return error; } -int -csv_parse_vrps_file() +static int +load_vrps_file(bool check_update) { + struct line_file *lfile; + struct stat attr; + time_t last_update; char const *location; int error; @@ -250,12 +255,38 @@ csv_parse_vrps_file() if (error) goto end1; /* Error msg already printed. */ + // Look for the last update date + error = stat(location, &attr); + if (error) { + warn("Couldn't get last modified date of %s, skip update", + location); + goto end2; + } + + last_update = attr.st_mtim.tv_nsec; + if (check_update && last_update <= get_vrps_last_modified_date()) + goto end2; + error = load_vrps(lfile); if (error) goto end2; + set_vrps_last_modified_date(last_update); + // TODO Double check of date end2: lfile_close(lfile); end1: return error; } + +int +csv_parse_vrps_file() +{ + return load_vrps_file(false); +} + +int +csv_check_vrps_file() +{ + return load_vrps_file(true); +} diff --git a/src/csv.h b/src/csv.h index 3b96f802..9b4b2d1c 100644 --- a/src/csv.h +++ b/src/csv.h @@ -2,5 +2,6 @@ #define SRC_CSV_H_ int csv_parse_vrps_file(); +int csv_check_vrps_file(); #endif /* SRC_CSV_H_ */ diff --git a/src/main.c b/src/main.c index 44dd4a00..58c209fb 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,7 @@ #include "rtr/rtr.h" #include "configuration.h" #include "csv.h" +#include "updates_daemon.h" #include "vrps.h" /* @@ -52,6 +53,10 @@ main(int argc, char *argv[]) if (err) goto end2; + err = updates_daemon_init(); + if (err) + goto end2; + err = rtr_listen(); end2: diff --git a/src/updates_daemon.c b/src/updates_daemon.c new file mode 100644 index 00000000..19657ef6 --- /dev/null +++ b/src/updates_daemon.c @@ -0,0 +1,32 @@ +#include "updates_daemon.h" + +#include +#include +#include +#include +#include + +#include "csv.h" +#include "configuration.h" + +static void * +check_vrps_updates(void *param_void) { + do { + csv_check_vrps_file(); + sleep(config_get_vrps_check_interval()); + } while (true); + + return NULL; +} + +int +updates_daemon_init(void) { + pthread_t thread; + errno = pthread_create(&thread, NULL, check_vrps_updates, NULL); + if (errno) { + warn("Could not spawn the update daemon thread"); + return errno; + } + pthread_detach(thread); + return 0; +} diff --git a/src/updates_daemon.h b/src/updates_daemon.h new file mode 100644 index 00000000..47124df2 --- /dev/null +++ b/src/updates_daemon.h @@ -0,0 +1,6 @@ +#ifndef SRC_UPDATES_DAEMON_H_ +#define SRC_UPDATES_DAEMON_H_ + +int updates_daemon_init(void); + +#endif /* SRC_UPDATES_DAEMON_H_ */ diff --git a/src/vrps.c b/src/vrps.c index ac457240..53a253b1 100644 --- a/src/vrps.c +++ b/src/vrps.c @@ -1,6 +1,5 @@ #include "vrps.h" -#include #include "array_list.h" #define FLAG_WITHDRAWAL 0 @@ -19,6 +18,7 @@ struct deltasdb db; u_int32_t current_serial; u_int16_t v0_session_id; u_int16_t v1_session_id; +time_t last_modified_date; int deltas_db_init(void) @@ -232,6 +232,12 @@ get_vrps_delta(u_int32_t *start_serial, u_int32_t *end_serial, return get_delta_diff(delta0, delta1, result); } +void +set_vrps_last_modified_date(time_t new_date) +{ + last_modified_date = new_date; +} + u_int32_t last_serial_number(void) { @@ -245,3 +251,9 @@ current_session_id(u_int8_t rtr_version) return v1_session_id; return v0_session_id; } + +time_t +get_vrps_last_modified_date(void) +{ + return last_modified_date; +} diff --git a/src/vrps.h b/src/vrps.h index 1a07a353..8b4081f5 100644 --- a/src/vrps.h +++ b/src/vrps.h @@ -1,6 +1,7 @@ #ifndef SRC_VRPS_H_ #define SRC_VRPS_H_ +#include #include #define NO_DATA_AVAILABLE -2 @@ -37,8 +38,10 @@ unsigned int get_vrps_delta(u_int32_t *, u_int32_t *, struct vrp **); void vrp_destroy(struct vrp *); void delta_destroy(struct delta *); void deltas_db_destroy(void); +void set_vrps_last_modified_date(time_t); u_int32_t last_serial_number(void); u_int16_t current_session_id(u_int8_t); +time_t get_vrps_last_modified_date(void); #endif /* SRC_VRPS_H_ */