]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/database.c
database: Fix checking pointer
[people/ms/libloc.git] / src / database.c
index 825d7982199cd5f8de14bcecc5df5d3a33aa0577..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>
@@ -132,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,
@@ -155,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,
@@ -178,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,
@@ -201,7 +205,7 @@ static int loc_database_read_countries_section_v0(struct loc_database* db,
        size_t countries_length = be32toh(header->countries_length);
 
        DEBUG(db->ctx, "Reading countries section from %jd (%zu bytes)\n",
-               countries_offset, countries_length);
+               (intmax_t)countries_offset, countries_length);
 
        if (countries_length > 0) {
                db->countries_v0 = mmap(NULL, countries_length, PROT_READ,
@@ -397,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) {
@@ -466,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) {
@@ -497,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;
        }
 
@@ -610,7 +614,7 @@ static int loc_database_fetch_country(struct loc_database* db,
        if ((size_t)pos >= db->countries_count)
                return -EINVAL;
 
-       DEBUG(db->ctx, "Fetching country at position %jd\n", pos);
+       DEBUG(db->ctx, "Fetching country at position %jd\n", (intmax_t)pos);
 
        int r;
        switch (db->version) {
@@ -664,7 +668,7 @@ LOC_EXPORT int loc_database_get_country(struct loc_database* db,
                // adjust our search pointers
                loc_country_unref(*country);
 
-               if (result < 0) {
+               if (result > 0) {
                        lo = i + 1;
                } else
                        hi = i - 1;
@@ -751,10 +755,6 @@ LOC_EXPORT int loc_database_enumerator_set_country_code(struct loc_database_enum
                return 0;
        }
 
-       // Country codes must be two characters
-       if (strlen(country_code) != 2)
-               return -EINVAL;
-
        // Treat A1, A2, A3 as special country codes,
        // but perform search for flags instead
        if (strcmp(country_code, "A1") == 0) {
@@ -768,6 +768,10 @@ LOC_EXPORT int loc_database_enumerator_set_country_code(struct loc_database_enum
                        LOC_NETWORK_FLAG_ANYCAST);
        }
 
+       // Country codes must be two characters
+       if (!loc_country_code_is_valid(country_code))
+               return -EINVAL;
+
        for (unsigned int i = 0; i < 3; i++) {
                enumerator->country_code[i] = country_code[i];
        }
@@ -840,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;
@@ -880,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
@@ -904,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,
@@ -917,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;