From: Michael Tremer Date: Wed, 13 May 2020 16:38:09 +0000 (+0000) Subject: location-query: Add command to dump the whole database X-Git-Tag: 0.9.1~64 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a68a46f52900c6adf5a092dd55d28d4a462fd3b8;p=location%2Flibloc.git location-query: Add command to dump the whole database Signed-off-by: Michael Tremer --- diff --git a/src/python/database.c b/src/python/database.c index 2f0a3b0..581ed5b 100644 --- a/src/python/database.c +++ b/src/python/database.c @@ -207,6 +207,25 @@ static PyObject* new_database_enumerator(PyTypeObject* type, struct loc_database return (PyObject*)self; } +static PyObject* Database_iterate_all(DatabaseObject* self, enum loc_database_enumerator_mode what) { + struct loc_database_enumerator* enumerator; + + int r = loc_database_enumerator_new(&enumerator, self->db, what); + if (r) { + PyErr_SetFromErrno(PyExc_SystemError); + return NULL; + } + + PyObject* obj = new_database_enumerator(&DatabaseEnumeratorType, enumerator); + loc_database_enumerator_unref(enumerator); + + return obj; +} + +static PyObject* Database_ases(DatabaseObject* self) { + return Database_iterate_all(self, LOC_DB_ENUMERATE_ASES); +} + static PyObject* Database_search_as(DatabaseObject* self, PyObject* args) { const char* string = NULL; @@ -230,6 +249,10 @@ static PyObject* Database_search_as(DatabaseObject* self, PyObject* args) { return obj; } +static PyObject* Database_networks(DatabaseObject* self) { + return Database_iterate_all(self, LOC_DB_ENUMERATE_NETWORKS); +} + static PyObject* Database_search_networks(DatabaseObject* self, PyObject* args, PyObject* kwargs) { char* kwlist[] = { "country_code", "asn", "flags", "family", NULL }; const char* country_code = NULL; @@ -334,6 +357,13 @@ static struct PyMethodDef Database_methods[] = { }; static struct PyGetSetDef Database_getsetters[] = { + { + "ases", + (getter)Database_ases, + NULL, + NULL, + NULL, + }, { "created_at", (getter)Database_get_created_at, @@ -355,6 +385,13 @@ static struct PyGetSetDef Database_getsetters[] = { NULL, NULL, }, + { + "networks", + (getter)Database_networks, + NULL, + NULL, + NULL, + }, { "vendor", (getter)Database_get_vendor, diff --git a/src/python/location-query.in b/src/python/location-query.in index 4534279..fa3a6cb 100644 --- a/src/python/location-query.in +++ b/src/python/location-query.in @@ -22,6 +22,7 @@ import ipaddress import os import socket import sys +import time # Load our location module import location @@ -142,6 +143,13 @@ class CLI(object): lookup.add_argument("address", nargs="+") lookup.set_defaults(func=self.handle_lookup) + # Dump the whole database + dump = subparsers.add_parser("dump", + help=_("Dump the entire database"), + ) + dump.add_argument("output", nargs="?", type=argparse.FileType("w")) + dump.set_defaults(func=self.handle_dump) + # Get AS get_as = subparsers.add_parser("get-as", help=_("Get information about one or multiple Autonomous Systems"), @@ -271,6 +279,48 @@ class CLI(object): return ret + def handle_dump(self, db, ns): + # Use output file or write to stdout + f = ns.output or sys.stdout + + # Write metadata + f.write("#\n# Location Database Export\n#\n") + + f.write("# Generated: %s\n" % time.strftime( + "%a, %d %b %Y %H:%M:%S GMT", time.gmtime(db.created_at), + )) + + if db.vendor: + f.write("# Vendor: %s\n" % db.vendor) + + if db.license: + f.write("# License: %s\n" % db.license) + + f.write("#\n") + + if db.description: + for line in db.description.splitlines(): + f.write("# %s\n" % line) + + f.write("#\n") + + # Iterate over all ASes + for a in db.ases: + f.write("\n") + f.write("aut-num: AS%s\n" % a.number) + f.write("name: %s\n" % a.name) + + # Iterate over all networks + for n in db.networks: + f.write("\n") + f.write("net: %s\n" % n) + + if n.country_code: + f.write("country: %s\n" % n.country_code) + + if n.asn: + f.write("autnum: %s\n" % n.asn) + def handle_get_as(self, db, ns): """ Gets information about Autonomous Systems