]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/database.c
database: Make level variable unsigned (as used in log messages)
[people/ms/libloc.git] / src / database.c
index 9cf5f32ebf726022189e71c637e472fbc26cc821..563546a130d93b45dc14c1878d14ecdf4057b6ef 100644 (file)
@@ -44,6 +44,7 @@ struct loc_database {
        time_t created_at;
        off_t vendor;
        off_t description;
+       off_t license;
 
        // ASes in the database
        struct loc_database_as_v0* as_v0;
@@ -173,6 +174,7 @@ static int loc_database_read_header_v0(struct loc_database* db, FILE* f) {
        db->created_at  = be64toh(header.created_at);
        db->vendor      = be32toh(header.vendor);
        db->description = be32toh(header.description);
+       db->license     = be32toh(header.license);
 
        // Open pool
        off_t pool_offset  = be32toh(header.pool_offset);
@@ -266,15 +268,31 @@ LOC_EXPORT struct loc_database* loc_database_ref(struct loc_database* db) {
 }
 
 static void loc_database_free(struct loc_database* db) {
+       int r;
+
        DEBUG(db->ctx, "Releasing database %p\n", db);
 
        // Removing all ASes
        if (db->as_v0) {
-               int r = munmap(db->as_v0, db->as_count * sizeof(*db->as_v0));
+               r = munmap(db->as_v0, db->as_count * sizeof(*db->as_v0));
                if (r)
                        ERROR(db->ctx, "Could not unmap AS section: %s\n", strerror(errno));
        }
 
+       // Remove mapped network sections
+       if (db->networks_v0) {
+               r = munmap(db->networks_v0, db->networks_count * sizeof(*db->networks_v0));
+               if (r)
+                       ERROR(db->ctx, "Could not unmap networks section: %s\n", strerror(errno));
+       }
+
+       // Remove mapped network nodes section
+       if (db->network_nodes_v0) {
+               r = munmap(db->network_nodes_v0, db->network_nodes_count * sizeof(*db->network_nodes_v0));
+               if (r)
+                       ERROR(db->ctx, "Could not unmap network nodes section: %s\n", strerror(errno));
+       }
+
        loc_stringpool_unref(db->pool);
 
        loc_unref(db->ctx);
@@ -301,6 +319,10 @@ LOC_EXPORT const char* loc_database_get_description(struct loc_database* db) {
        return loc_stringpool_get(db->pool, db->description);
 }
 
+LOC_EXPORT const char* loc_database_get_license(struct loc_database* db) {
+       return loc_stringpool_get(db->pool, db->license);
+}
+
 LOC_EXPORT size_t loc_database_count_as(struct loc_database* db) {
        return db->as_count;
 }
@@ -399,14 +421,14 @@ static int loc_database_fetch_network(struct loc_database* db, struct loc_networ
        return r;
 }
 
-static int __loc_database_lookup_leaf_node(struct loc_database* db, const struct in6_addr* address,
+static int __loc_database_node_is_leaf(const struct loc_database_network_node_v0* node) {
+       return (node->zero == htobe32(0xffffffff));
+}
+
+static int __loc_database_lookup_handle_leaf(struct loc_database* db, const struct in6_addr* address,
                struct loc_network** network, struct in6_addr* network_address,
                const struct loc_database_network_node_v0* node) {
-       // Check if this node is a leaf node
-       if (node->zero != htobe32(0xffffffff))
-               return 1;
-
-       DEBUG(db->ctx, "Node is a leaf: %jd\n", node - db->network_nodes_v0);
+       DEBUG(db->ctx, "Handling leaf node at %jd\n", node - db->network_nodes_v0);
 
        // Fetch the network
        int r = loc_database_fetch_network(db, network,
@@ -431,13 +453,12 @@ static int __loc_database_lookup_leaf_node(struct loc_database* db, const struct
 // Returns the highest result available
 static int __loc_database_lookup_max(struct loc_database* db, const struct in6_addr* address,
                struct loc_network** network, struct in6_addr* network_address,
-               const struct loc_database_network_node_v0* node, int level) {
-
+               const struct loc_database_network_node_v0* node, unsigned int level) {
        // If the node is a leaf node, we end here
-       int r = __loc_database_lookup_leaf_node(db, address, network, network_address, node);
-       if (r <= 0)
-               return r;
+       if (__loc_database_node_is_leaf(node))
+               return __loc_database_lookup_handle_leaf(db, address, network, network_address, node);
 
+       int r;
        off_t node_index;
 
        // Try to go down the ones path first
@@ -480,12 +501,12 @@ static int __loc_database_lookup_max(struct loc_database* db, const struct in6_a
 // Searches for an exact match along the path
 static int __loc_database_lookup(struct loc_database* db, const struct in6_addr* address,
                struct loc_network** network, struct in6_addr* network_address,
-               const struct loc_database_network_node_v0* node, int level) {
+               const struct loc_database_network_node_v0* node, unsigned int level) {
        // If the node is a leaf node, we end here
-       int r = __loc_database_lookup_leaf_node(db, address, network, network_address, node);
-       if (r <= 0)
-               return r;
+       if (__loc_database_node_is_leaf(node))
+               return __loc_database_lookup_handle_leaf(db, address, network, network_address, node);
 
+       int r;
        off_t node_index;
 
        // Follow the path