]> git.ipfire.org Git - location/libloc.git/commitdiff
location-downloader: Load the right database version
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 16 May 2020 13:57:13 +0000 (13:57 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 16 May 2020 13:57:13 +0000 (13:57 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/loc/format.h
src/loc/resolv.h
src/python/location-downloader.in
src/python/locationmodule.c
src/resolv.c

index 149c999f7b1e6037a283e736a68b18bd9c6c5835..2910798c7fcf0cc7b0dc1cd4e9522342cdd26463 100644 (file)
@@ -26,12 +26,11 @@ enum loc_database_version {
        LOC_DATABASE_VERSION_1     = 1,
 };
 
-#ifdef LIBLOC_PRIVATE
-
 #define LOC_DATABASE_VERSION_LATEST LOC_DATABASE_VERSION_1
 
-#define STR(x) #x
-#define LOC_DATABASE_DOMAIN(version) "_v" STR(version) "._db.location.ipfire.org"
+#ifdef LIBLOC_PRIVATE
+
+#define LOC_DATABASE_DOMAIN "_v%u._db.location.ipfire.org"
 
 #define LOC_DATABASE_PAGE_SIZE  4096
 
index 3b5e9906aea1cd738db6f4fb3739b7ef16531244..3b70c606f217b26ded7a0c03e3995a79fee7bd9d 100644 (file)
@@ -21,6 +21,6 @@
 
 #include <loc/libloc.h>
 
-int loc_discover_latest_version(struct loc_ctx* ctx, const char* domain, time_t* t);
+int loc_discover_latest_version(struct loc_ctx* ctx, unsigned int version, time_t* t);
 
 #endif
index c8a0ef4400e3c6e1682911ec99fe543408a0509b..7d06030e0178407d81cf30de460659acea6ee19f 100644 (file)
@@ -45,7 +45,8 @@ log = logging.getLogger("location.downloader")
 log.propagate = 1
 
 class Downloader(object):
-       def __init__(self, mirrors):
+       def __init__(self, version, mirrors):
+               self.version = version
                self.mirrors = list(mirrors)
 
                # Randomize mirrors
@@ -207,7 +208,10 @@ class Downloader(object):
 
 class CLI(object):
        def __init__(self):
-               self.downloader = Downloader(mirrors=MIRRORS)
+               # Which version are we downloading?
+               self.version = location.DATABASE_VERSION_LATEST
+
+               self.downloader = Downloader(version=self.version, mirrors=MIRRORS)
 
        def parse_cli(self):
                parser = argparse.ArgumentParser(
@@ -274,21 +278,18 @@ class CLI(object):
                sys.exit(0)
 
        def handle_update(self, ns):
-               # Fetch the version we need from DNS
-               t = location.discover_latest_version()
+               # Fetch the timestamp we need from DNS
+               t = location.discover_latest_version(self.version)
 
                # Parse timestamp into datetime format
-               try:
-                       timestamp = datetime.datetime.fromtimestamp(t)
-               except:
-                       raise
+               timestamp = datetime.datetime.fromtimestamp(t) if t else None
 
                # Open database
                try:
                        db = location.Database(ns.database)
 
                        # Check if we are already on the latest version
-                       if db.created_at >= timestamp.timestamp():
+                       if timestamp and db.created_at >= timestamp.timestamp():
                                log.info("Already on the latest version")
                                return
 
@@ -297,7 +298,7 @@ class CLI(object):
 
                # Try downloading a new database
                try:
-                       t = self.downloader.download(DATABASE_FILENAME,
+                       t = self.downloader.download("%s/%s" % (self.version, DATABASE_FILENAME),
                                public_key=ns.public_key, timestamp=timestamp)
 
                # If no file could be downloaded, log a message
index dd83e53b46d603b916c13beb7ea9bc7231b89106..a04cab7f0c7f5c415f02bfb1257cc34985b7b73d 100644 (file)
@@ -17,6 +17,7 @@
 #include <Python.h>
 #include <syslog.h>
 
+#include <loc/format.h>
 #include <loc/resolv.h>
 
 #include "locationmodule.h"
@@ -49,18 +50,16 @@ static PyObject* set_log_level(PyObject* m, PyObject* args) {
 }
 
 static PyObject* discover_latest_version(PyObject* m, PyObject* args) {
-       const char* domain = NULL;
+       unsigned int version = 0;
 
-       if (!PyArg_ParseTuple(args, "|s", &domain))
+       if (!PyArg_ParseTuple(args, "i", &version))
                return NULL;
 
        time_t t = 0;
 
-       int r = loc_discover_latest_version(loc_ctx, domain, &t);
-       if (r) {
-               PyErr_SetFromErrno(PyExc_OSError);
-               return NULL;
-       }
+       int r = loc_discover_latest_version(loc_ctx, version, &t);
+       if (r)
+               Py_RETURN_NONE;
 
        return PyLong_FromUnsignedLong(t);
 }
@@ -170,5 +169,9 @@ PyMODINIT_FUNC PyInit__location(void) {
        if (PyModule_AddIntConstant(m, "NETWORK_FLAG_ANYCAST", LOC_NETWORK_FLAG_ANYCAST))
                return NULL;
 
+       // Add latest database version
+       if (PyModule_AddIntConstant(m, "DATABASE_VERSION_LATEST", LOC_DATABASE_VERSION_LATEST))
+               return NULL;
+
        return m;
 }
index a213c7fa4726725d809c89fcd947c67fc0e13063..261ae5ce2932cd650f4008f89d77150f41263a15 100644 (file)
@@ -43,7 +43,8 @@ static int parse_timestamp(const unsigned char* txt, time_t* t) {
     return 0;
 }
 
-LOC_EXPORT int loc_discover_latest_version(struct loc_ctx* ctx, const char* domain, time_t* t) {
+LOC_EXPORT int loc_discover_latest_version(struct loc_ctx* ctx,
+        unsigned int version, time_t* t) {
     // Initialise the resolver
     int r = res_init();
     if (r) {
@@ -51,9 +52,9 @@ LOC_EXPORT int loc_discover_latest_version(struct loc_ctx* ctx, const char* doma
         return r;
     }
 
-    // Fall back to default domain
-    if (!domain)
-        domain = LOC_DATABASE_DOMAIN(LOC_DATABASE_VERSION_LATEST);
+    // Make domain
+    char domain[64];
+    snprintf(domain, 63, LOC_DATABASE_DOMAIN, version);
 
     unsigned char answer[PACKETSZ];
     int len;