]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Create daemon and conf to look for VRPs file updates
authorpcarana <pc.moreno2099@gmail.com>
Tue, 5 Mar 2019 00:33:24 +0000 (18:33 -0600)
committerpcarana <pc.moreno2099@gmail.com>
Tue, 5 Mar 2019 00:33:24 +0000 (18:33 -0600)
src/Makefile.am
src/configuration.c
src/configuration.h
src/csv.c
src/csv.h
src/main.c
src/updates_daemon.c [new file with mode: 0644]
src/updates_daemon.h [new file with mode: 0644]
src/vrps.c
src/vrps.h

index 25e66db30bf61cdc5d4e698fbee7ab20ccbbfabc..21449a288ac7b3df44bca45937672bf362b7d40e 100644 (file)
@@ -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
index b1ff5d197721bd0ad7f1ae0c74ae8eba64e87a49..2070ce5c76533f69d2d73775158095894b0f2771 100644 (file)
@@ -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"
 
 #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)
 {
index 4726312757335aeb86cbb5476f14ba540b538ed0..3474f68f7db96b7d6449e3400dd5ca1812f5bf43 100644 (file)
@@ -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);
index 85bb4f4281a0cd5f029164543bb417c827e96964..ec852ca76a0139fd9475a23ab5f04b1446bf5692 100644 (file)
--- a/src/csv.c
+++ b/src/csv.c
@@ -6,6 +6,8 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
+#include <sys/stat.h>
 
 #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);
+}
index 3b96f80261b1cefc055cf3df17d196c0153b382a..9b4b2d1c298ad972ae04257bad2eaa4035e99a3a 100644 (file)
--- 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_ */
index 44dd4a0017e36a868bbb6d9fca99d01bf2af8914..58c209fb0ad0d7535ac72614fcac5f3f88fff7be 100644 (file)
@@ -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 (file)
index 0000000..19657ef
--- /dev/null
@@ -0,0 +1,32 @@
+#include "updates_daemon.h"
+
+#include <err.h>
+#include <errno.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <unistd.h>
+
+#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 (file)
index 0000000..47124df
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef SRC_UPDATES_DAEMON_H_
+#define SRC_UPDATES_DAEMON_H_
+
+int updates_daemon_init(void);
+
+#endif /* SRC_UPDATES_DAEMON_H_ */
index ac45724010a5319a8977ef4e30e3bf473f03a5a8..53a253b1b49dec405bee942965bae0f0f9ca91be 100644 (file)
@@ -1,6 +1,5 @@
 #include "vrps.h"
 
-#include <time.h>
 #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;
+}
index 1a07a353cba5aa223fdba3e2edd6ded933e903f6..8b4081f5abb709dd419f1e4ff76fa92c5bf89b0e 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef SRC_VRPS_H_
 #define SRC_VRPS_H_
 
+#include <time.h>
 #include <netinet/ip.h>
 
 #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_ */