From: Michael Tremer Date: Sat, 16 May 2020 13:57:13 +0000 (+0000) Subject: location-downloader: Load the right database version X-Git-Tag: 0.9.1~28 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3ee36b5ee3a8b09ab7e461ec915e34c1c87ff531;p=people%2Fms%2Flibloc.git location-downloader: Load the right database version Signed-off-by: Michael Tremer --- diff --git a/src/loc/format.h b/src/loc/format.h index 149c999..2910798 100644 --- a/src/loc/format.h +++ b/src/loc/format.h @@ -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 diff --git a/src/loc/resolv.h b/src/loc/resolv.h index 3b5e990..3b70c60 100644 --- a/src/loc/resolv.h +++ b/src/loc/resolv.h @@ -21,6 +21,6 @@ #include -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 diff --git a/src/python/location-downloader.in b/src/python/location-downloader.in index c8a0ef4..7d06030 100644 --- a/src/python/location-downloader.in +++ b/src/python/location-downloader.in @@ -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 diff --git a/src/python/locationmodule.c b/src/python/locationmodule.c index dd83e53..a04cab7 100644 --- a/src/python/locationmodule.c +++ b/src/python/locationmodule.c @@ -17,6 +17,7 @@ #include #include +#include #include #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; } diff --git a/src/resolv.c b/src/resolv.c index a213c7f..261ae5c 100644 --- a/src/resolv.c +++ b/src/resolv.c @@ -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;