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
#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
log.propagate = 1
class Downloader(object):
- def __init__(self, mirrors):
+ def __init__(self, version, mirrors):
+ self.version = version
self.mirrors = list(mirrors)
# Randomize mirrors
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(
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
# 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
#include <Python.h>
#include <syslog.h>
+#include <loc/format.h>
#include <loc/resolv.h>
#include "locationmodule.h"
}
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);
}
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;
}
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) {
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;