]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
location-query: Add command to dump the whole database
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 13 May 2020 16:38:09 +0000 (16:38 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 13 May 2020 16:38:09 +0000 (16:38 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/database.c
src/python/location-query.in

index 2f0a3b0fb1b3c5cba75e68018d405cb96760f4de..581ed5be1c4db469344168eaafe8a998aaffad3c 100644 (file)
@@ -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,
index 4534279e72d6e7da34d565a2c025c824bb5386ec..fa3a6cb7a297e02a9c8e57a051dd53dff893e3d0 100644 (file)
@@ -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