]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
database: Implement reading a network from the database
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Jan 2018 11:55:58 +0000 (11:55 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Jan 2018 11:55:58 +0000 (11:55 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/database.c
src/loc/network.h
src/network.c

index f789c690b0a16791e22e17ccf644c791589665a7..dd3e77c20a946426d49e9f2bb2dc098c7a0724f6 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <endian.h>
 #include <errno.h>
+#include <netinet/in.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -30,6 +31,7 @@
 #include <loc/as.h>
 #include <loc/database.h>
 #include <loc/format.h>
+#include <loc/network.h>
 #include <loc/private.h>
 #include <loc/stringpool.h>
 
@@ -369,3 +371,29 @@ LOC_EXPORT int loc_database_get_as(struct loc_database* db, struct loc_as** as,
 
        return 1;
 }
+
+// Returns the network at position pos
+static int loc_database_fetch_network(struct loc_database* db, struct loc_network** network, struct in6_addr* address, off_t pos) {
+       if ((size_t)pos >= db->networks_count)
+               return -EINVAL;
+
+       DEBUG(db->ctx, "Fetching network at position %jd\n", pos);
+
+       int r;
+       switch (db->version) {
+               case 0:
+                       r = loc_network_new_from_database_v0(db->ctx, network, address, db->networks_v0 + pos);
+                       break;
+
+               default:
+                       return -1;
+       }
+
+       if (r == 0) {
+               char* string = loc_network_str(*network);
+               DEBUG(db->ctx, "Got network %s\n", string);
+               free(string);
+       }
+
+       return r;
+}
index 05e2650ca1a19c7b4c6e22e3f9f0c61636b375ba..be5b3775ea177aa22b0dda380351d34cc04c8b2f 100644 (file)
@@ -24,7 +24,7 @@
 
 struct loc_network;
 int loc_network_new(struct loc_ctx* ctx, struct loc_network** network,
-               struct in6_addr start_address, unsigned int prefix);
+               struct in6_addr* start_address, unsigned int prefix);
 int loc_network_new_from_string(struct loc_ctx* ctx, struct loc_network** network,
                const char* address_string);
 struct loc_network* loc_network_ref(struct loc_network* network);
@@ -38,6 +38,8 @@ uint32_t loc_network_get_asn(struct loc_network* network);
 int loc_network_set_asn(struct loc_network* network, uint32_t asn);
 
 int loc_network_to_database_v0(struct loc_network* network, struct loc_database_network_v0* dbobj);
+int loc_network_new_from_database_v0(struct loc_ctx* ctx, struct loc_network** network,
+               struct in6_addr* address, const struct loc_database_network_v0* dbobj);
 
 struct loc_network_tree;
 int loc_network_tree_new(struct loc_ctx* ctx, struct loc_network_tree** tree);
index 9bba2224c60b196edcdda2b64ffca6fd63cb5453..bcdc025af30dd70b18fc6617082d4af415212507 100644 (file)
@@ -81,7 +81,7 @@ static struct in6_addr make_start_address(struct in6_addr* address, unsigned int
 }
 
 LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network,
-               struct in6_addr address, unsigned int prefix) {
+               struct in6_addr* address, unsigned int prefix) {
        // Address cannot be unspecified
        if (IN6_IS_ADDR_UNSPECIFIED(&address)) {
                DEBUG(ctx, "Start address is unspecified\n");
@@ -107,7 +107,7 @@ LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network
        }
 
        // Validate the prefix
-       if (valid_prefix(&address, prefix) != 0) {
+       if (valid_prefix(address, prefix) != 0) {
                DEBUG(ctx, "Invalid prefix: %u\n", prefix);
                return -EINVAL;
        }
@@ -120,7 +120,7 @@ LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network
        n->refcount = 1;
 
        // Store the first address in the network
-       n->start_address = make_start_address(&address, prefix);
+       n->start_address = make_start_address(address, prefix);
        n->prefix = prefix;
 
        DEBUG(n->ctx, "Network allocated at %p\n", n);
@@ -199,7 +199,7 @@ LOC_EXPORT int loc_network_new_from_string(struct loc_ctx* ctx, struct loc_netwo
        free(buffer);
 
        if (r == 0) {
-               r = loc_network_new(ctx, network, start_address, prefix);
+               r = loc_network_new(ctx, network, &start_address, prefix);
        }
 
        return r;
@@ -324,6 +324,31 @@ LOC_EXPORT int loc_network_to_database_v0(struct loc_network* network, struct lo
        return 0;
 }
 
+LOC_EXPORT int loc_network_new_from_database_v0(struct loc_ctx* ctx, struct loc_network** network,
+               struct in6_addr* address, const struct loc_database_network_v0* dbobj) {
+       int r = loc_network_new(ctx, network, address, dbobj->prefix);
+       if (r)
+               return r;
+
+       // Import country code
+       char country_code[3];
+       for (unsigned int i = 0; i < 2; i++) {
+               country_code[i] = dbobj->country_code[i];
+       }
+       country_code[2] = '\0';
+
+       r = loc_network_set_country_code(*network, country_code);
+       if (r)
+               return r;
+
+       // Import ASN
+       r = loc_network_set_asn(*network, be32toh(dbobj->asn));
+       if (r)
+               return r;
+
+       return 0;
+}
+
 struct loc_network_tree {
        struct loc_ctx* ctx;
        int refcount;