]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/database.c
database: Fix checking pointer
[people/ms/libloc.git] / src / database.c
index ea2ae867f1225c5c0757bbba34f2b3b48cf19a7d..5d808fe5ca9e93484982304a24001aab21994e87 100644 (file)
@@ -16,7 +16,6 @@
 
 #include <arpa/inet.h>
 #include <ctype.h>
-#include <endian.h>
 #include <errno.h>
 #include <netinet/in.h>
 #include <stddef.h>
 #include <time.h>
 #include <unistd.h>
 
+#ifdef HAVE_ENDIAN_H
+#  include <endian.h>
+#endif
+
 #include <loc/libloc.h>
 #include <loc/as.h>
+#include <loc/compat.h>
+#include <loc/country.h>
 #include <loc/database.h>
 #include <loc/format.h>
 #include <loc/network.h>
@@ -59,6 +64,10 @@ struct loc_database {
        struct loc_database_network_v0* networks_v0;
        size_t networks_count;
 
+       // Countries
+       struct loc_database_country_v0* countries_v0;
+       size_t countries_count;
+
        struct loc_stringpool* pool;
 };
 
@@ -127,7 +136,7 @@ static int loc_database_read_as_section_v0(struct loc_database* db,
        off_t as_offset  = be32toh(header->as_offset);
        size_t as_length = be32toh(header->as_length);
 
-       DEBUG(db->ctx, "Reading AS section from %jd (%zu bytes)\n", as_offset, as_length);
+       DEBUG(db->ctx, "Reading AS section from %jd (%zu bytes)\n", (intmax_t)as_offset, as_length);
 
        if (as_length > 0) {
                db->as_v0 = mmap(NULL, as_length, PROT_READ,
@@ -150,7 +159,7 @@ static int loc_database_read_network_nodes_section_v0(struct loc_database* db,
        size_t network_nodes_length = be32toh(header->network_tree_length);
 
        DEBUG(db->ctx, "Reading network nodes section from %jd (%zu bytes)\n",
-               network_nodes_offset, network_nodes_length);
+               (intmax_t)network_nodes_offset, network_nodes_length);
 
        if (network_nodes_length > 0) {
                db->network_nodes_v0 = mmap(NULL, network_nodes_length, PROT_READ,
@@ -173,7 +182,7 @@ static int loc_database_read_networks_section_v0(struct loc_database* db,
        size_t networks_length = be32toh(header->network_data_length);
 
        DEBUG(db->ctx, "Reading networks section from %jd (%zu bytes)\n",
-               networks_offset, networks_length);
+               (intmax_t)networks_offset, networks_length);
 
        if (networks_length > 0) {
                db->networks_v0 = mmap(NULL, networks_length, PROT_READ,
@@ -190,6 +199,30 @@ static int loc_database_read_networks_section_v0(struct loc_database* db,
        return 0;
 }
 
+static int loc_database_read_countries_section_v0(struct loc_database* db,
+               FILE* f, const struct loc_database_header_v0* header) {
+       off_t countries_offset  = be32toh(header->countries_offset);
+       size_t countries_length = be32toh(header->countries_length);
+
+       DEBUG(db->ctx, "Reading countries section from %jd (%zu bytes)\n",
+               (intmax_t)countries_offset, countries_length);
+
+       if (countries_length > 0) {
+               db->countries_v0 = mmap(NULL, countries_length, PROT_READ,
+                       MAP_SHARED, fileno(f), countries_offset);
+
+               if (db->countries_v0 == MAP_FAILED)
+                       return -errno;
+       }
+
+       db->countries_count = countries_length / sizeof(*db->countries_v0);
+
+       INFO(db->ctx, "Read %zu countries from the database\n",
+               db->countries_count);
+
+       return 0;
+}
+
 static int loc_database_read_header_v0(struct loc_database* db, FILE* f) {
        struct loc_database_header_v0 header;
 
@@ -231,6 +264,11 @@ static int loc_database_read_header_v0(struct loc_database* db, FILE* f) {
        if (r)
                return r;
 
+       // countries
+       r = loc_database_read_countries_section_v0(db, f, &header);
+       if (r)
+               return r;
+
        return 0;
 }
 
@@ -363,7 +401,7 @@ static int loc_database_fetch_as(struct loc_database* db, struct loc_as** as, of
        if ((size_t)pos >= db->as_count)
                return -EINVAL;
 
-       DEBUG(db->ctx, "Fetching AS at position %jd\n", pos);
+       DEBUG(db->ctx, "Fetching AS at position %jd\n", (intmax_t)pos);
 
        int r;
        switch (db->version) {
@@ -432,7 +470,7 @@ static int loc_database_fetch_network(struct loc_database* db, struct loc_networ
        if ((size_t)pos >= db->networks_count)
                return -EINVAL;
 
-       DEBUG(db->ctx, "Fetching network at position %jd\n", pos);
+       DEBUG(db->ctx, "Fetching network at position %jd\n", (intmax_t)pos);
 
        int r;
        switch (db->version) {
@@ -463,13 +501,13 @@ static int __loc_database_lookup_handle_leaf(struct loc_database* db, const stru
                const struct loc_database_network_node_v0* node) {
        off_t network_index = be32toh(node->network);
 
-       DEBUG(db->ctx, "Handling leaf node at %jd (%jd)\n", node - db->network_nodes_v0, network_index);
+       DEBUG(db->ctx, "Handling leaf node at %jd (%jd)\n", (intmax_t)(node - db->network_nodes_v0), (intmax_t)network_index);
 
        // Fetch the network
        int r = loc_database_fetch_network(db, network,
                network_address, prefix, network_index);
        if (r) {
-               ERROR(db->ctx, "Could not fetch network %jd from database\n", network_index);
+               ERROR(db->ctx, "Could not fetch network %jd from database\n", (intmax_t)network_index);
                return r;
        }
 
@@ -570,6 +608,78 @@ LOC_EXPORT int loc_database_lookup_from_string(struct loc_database* db,
        return loc_database_lookup(db, &address, network);
 }
 
+// Returns the country at position pos
+static int loc_database_fetch_country(struct loc_database* db,
+               struct loc_country** country, off_t pos) {
+       if ((size_t)pos >= db->countries_count)
+               return -EINVAL;
+
+       DEBUG(db->ctx, "Fetching country at position %jd\n", (intmax_t)pos);
+
+       int r;
+       switch (db->version) {
+               case 0:
+                       r = loc_country_new_from_database_v0(db->ctx, db->pool, country, db->countries_v0 + pos);
+                       break;
+
+               default:
+                       return -1;
+       }
+
+       if (r == 0) {
+               DEBUG(db->ctx, "Got country %s\n", loc_country_get_code(*country));
+       }
+
+       return r;
+}
+
+// Performs a binary search to find the country in the list
+LOC_EXPORT int loc_database_get_country(struct loc_database* db,
+               struct loc_country** country, const char* code) {
+       off_t lo = 0;
+       off_t hi = db->countries_count - 1;
+
+       // Save start time
+       clock_t start = clock();
+
+       while (lo <= hi) {
+               off_t i = (lo + hi) / 2;
+
+               // Fetch country in the middle between lo and hi
+               int r = loc_database_fetch_country(db, country, i);
+               if (r)
+                       return r;
+
+               // Check if this is a match
+               const char* cc = loc_country_get_code(*country);
+               int result = strcmp(code, cc);
+
+               if (result == 0) {
+                       clock_t end = clock();
+
+                       // Log how fast this has been
+                       DEBUG(db->ctx, "Found country %s in %.4fms\n", cc,
+                               (double)(end - start) / CLOCKS_PER_SEC * 1000);
+
+                       return 0;
+               }
+
+               // If it wasn't, we release the country and
+               // adjust our search pointers
+               loc_country_unref(*country);
+
+               if (result > 0) {
+                       lo = i + 1;
+               } else
+                       hi = i - 1;
+       }
+
+       // Nothing found
+       *country = NULL;
+
+       return 1;
+}
+
 // Enumerator
 
 LOC_EXPORT int loc_database_enumerator_new(struct loc_database_enumerator** enumerator,
@@ -645,8 +755,21 @@ LOC_EXPORT int loc_database_enumerator_set_country_code(struct loc_database_enum
                return 0;
        }
 
+       // Treat A1, A2, A3 as special country codes,
+       // but perform search for flags instead
+       if (strcmp(country_code, "A1") == 0) {
+               return loc_database_enumerator_set_flag(enumerator,
+                       LOC_NETWORK_FLAG_ANONYMOUS_PROXY);
+       } else if (strcmp(country_code, "A2") == 0) {
+               return loc_database_enumerator_set_flag(enumerator,
+                       LOC_NETWORK_FLAG_SATELLITE_PROVIDER);
+       } else if (strcmp(country_code, "A3") == 0) {
+               return loc_database_enumerator_set_flag(enumerator,
+                       LOC_NETWORK_FLAG_ANYCAST);
+       }
+
        // Country codes must be two characters
-       if (strlen(country_code) != 2)
+       if (!loc_country_code_is_valid(country_code))
                return -EINVAL;
 
        for (unsigned int i = 0; i < 3; i++) {
@@ -721,7 +844,7 @@ static int loc_database_enumerator_stack_push_node(
        // Increase stack size
        int s = ++e->network_stack_depth;
 
-       DEBUG(e->ctx, "Added node %jd to stack (%d)\n", offset, depth);
+       DEBUG(e->ctx, "Added node %jd to stack (%d)\n", (intmax_t)offset, depth);
 
        e->network_stack[s].offset = offset;
        e->network_stack[s].i = i;
@@ -761,7 +884,7 @@ LOC_EXPORT int loc_database_enumerator_next_network(
                in6_addr_set_bit(&enumerator->network_address,
                        (node->depth > 0) ? node->depth - 1 : 0, node->i);
 
-               DEBUG(enumerator->ctx, "Looking at node %jd\n", node->offset);
+               DEBUG(enumerator->ctx, "Looking at node %jd\n", (intmax_t)node->offset);
                enumerator->networks_visited[node->offset]++;
 
                // Pop node from top of the stack
@@ -785,7 +908,7 @@ LOC_EXPORT int loc_database_enumerator_next_network(
                if (__loc_database_node_is_leaf(n)) {
                        off_t network_index = be32toh(n->network);
 
-                       DEBUG(enumerator->ctx, "Node has a network at %jd\n", network_index);
+                       DEBUG(enumerator->ctx, "Node has a network at %jd\n", (intmax_t)network_index);
 
                        // Fetch the network object
                        r = loc_database_fetch_network(enumerator->db, network,
@@ -798,7 +921,7 @@ LOC_EXPORT int loc_database_enumerator_next_network(
                        // Check if we are interested in this network
 
                        // Skip if the country code does not match
-                       if (enumerator->country_code &&
+                       if (*enumerator->country_code &&
                                        !loc_network_match_country_code(*network, enumerator->country_code)) {
                                loc_network_unref(*network);
                                *network = NULL;