]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
python: Make lookup function available
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Jan 2018 16:42:46 +0000 (16:42 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Jan 2018 16:42:46 +0000 (16:42 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libloc.sym
src/python/database.c
src/python/network.c
src/python/network.h

index 628fca48e6fa87f5e8bd3e83f225a3f113fd52e5..9a28e05dfea83f0742189b2119aaec5563537740 100644 (file)
@@ -27,6 +27,7 @@ global:
        loc_network_get_country_code;
        loc_network_new;
        loc_network_new_from_string;
+       loc_network_ref;
        loc_network_set_asn;
        loc_network_set_country_code;
        loc_network_str;
index 69399b3db888d4ff419c8d8d2f84e1b148119ab8..931b54d4040e3650cec27b265b8ff61ac065d24e 100644 (file)
@@ -22,6 +22,7 @@
 #include "locationmodule.h"
 #include "as.h"
 #include "database.h"
+#include "network.h"
 
 static PyObject* Database_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
        DatabaseObject* self = (DatabaseObject*)type->tp_alloc(type, 0);
@@ -100,6 +101,32 @@ static PyObject* Database_get_as(DatabaseObject* self, PyObject* args) {
        Py_RETURN_NONE;
 }
 
+static PyObject* Database_lookup(DatabaseObject* self, PyObject* args) {
+       struct loc_network* network = NULL;
+       const char* address = NULL;
+
+       if (!PyArg_ParseTuple(args, "s", &address))
+               return NULL;
+
+       // Try to retrieve a matching network
+       int r = loc_database_lookup_from_string(self->db, address, &network);
+
+       // We got a network
+       if (r == 0) {
+               PyObject* obj = new_network(&NetworkType, network);
+               loc_network_unref(network);
+
+               return obj;
+
+       // Nothing found
+       } else if (r == 1) {
+               Py_RETURN_NONE;
+       }
+
+       // Unexpected error
+       return NULL;
+}
+
 static struct PyMethodDef Database_methods[] = {
        {
                "get_as",
@@ -107,6 +134,12 @@ static struct PyMethodDef Database_methods[] = {
                METH_VARARGS,
                NULL,
        },
+       {
+               "lookup",
+               (PyCFunction)Database_lookup,
+               METH_VARARGS,
+               NULL,
+       },
        { NULL },
 };
 
index ae7b2985e95044a02e98348c239ddd195764b7a1..f36b3393ff177747b77c762337cab04dea2f1b9d 100644 (file)
 #include "locationmodule.h"
 #include "network.h"
 
+PyObject* new_network(PyTypeObject* type, struct loc_network* network) {
+       NetworkObject* self = (NetworkObject*)type->tp_alloc(type, 0);
+       if (self) {
+               self->network = loc_network_ref(network);
+       }
+
+       return (PyObject*)self;
+}
+
 static PyObject* Network_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
        NetworkObject* self = (NetworkObject*)type->tp_alloc(type, 0);
 
index 4a4b26373194c0bad48e22e335ad9ef5445d7d72..43665ba0f0be45bf75ad4c109d0768a487ca826c 100644 (file)
@@ -28,4 +28,6 @@ typedef struct {
 
 extern PyTypeObject NetworkType;
 
+PyObject* new_network(PyTypeObject* type, struct loc_network* network);
+
 #endif /* PYTHON_LOCATION_NETWORK_H */