]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
database: Implement lookup
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Jan 2018 15:56:28 +0000 (15:56 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Jan 2018 15:56:28 +0000 (15:56 +0000)
This allows to pass an IP address to the database and
to return the result

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/database.c
src/libloc.c
src/libloc.sym
src/loc/database.h
src/loc/libloc.h
src/loc/network.h
src/loc/private.h
src/network.c
src/stringpool.c
src/test-network.c

index dd3e77c20a946426d49e9f2bb2dc098c7a0724f6..9cf5f32ebf726022189e71c637e472fbc26cc821 100644 (file)
@@ -14,6 +14,7 @@
        Lesser General Public License for more details.
 */
 
        Lesser General Public License for more details.
 */
 
+#include <arpa/inet.h>
 #include <endian.h>
 #include <errno.h>
 #include <netinet/in.h>
 #include <endian.h>
 #include <errno.h>
 #include <netinet/in.h>
@@ -397,3 +398,162 @@ static int loc_database_fetch_network(struct loc_database* db, struct loc_networ
 
        return r;
 }
 
        return r;
 }
+
+static int __loc_database_lookup_leaf_node(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);
+
+       // Fetch the network
+       int r = loc_database_fetch_network(db, network,
+               network_address, be32toh(node->one));
+       if (r)
+               return r;
+
+       // Check if the given IP address is inside the network
+       r = loc_network_match_address(*network, address);
+       if (r) {
+               DEBUG(db->ctx, "Searched address is not part of the network\n");
+
+               loc_network_unref(*network);
+               *network = NULL;
+               return 1;
+       }
+
+       // A network was found and the IP address matches
+       return 0;
+}
+
+// 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) {
+
+       // 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;
+
+       off_t node_index;
+
+       // Try to go down the ones path first
+       if (node->one) {
+               node_index = be32toh(node->one);
+               in6_addr_set_bit(network_address, level, 1);
+
+               // Check boundaries
+               if (node_index > 0 && (size_t)node_index <= db->network_nodes_count) {
+                       r = __loc_database_lookup_max(db, address, network, network_address,
+                               db->network_nodes_v0 + node_index, level + 1);
+
+                       // Abort when match was found or error
+                       if (r <= 0)
+                               return r;
+               }
+       }
+
+       // ... and if that fails, we try to go down one step on a zero
+       // branch and then try the ones again...
+       if (node->zero) {
+               node_index = be32toh(node->zero);
+               in6_addr_set_bit(network_address, level, 0);
+
+               // Check boundaries
+               if (node_index > 0 && (size_t)node_index <= db->network_nodes_count) {
+                       r = __loc_database_lookup_max(db, address, network, network_address,
+                               db->network_nodes_v0 + node_index, level + 1);
+
+                       // Abort when match was found or error
+                       if (r <= 0)
+                               return r;
+               }
+       }
+
+       // End of path
+       return 1;
+}
+
+// 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) {
+       // 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;
+
+       off_t node_index;
+
+       // Follow the path
+       int bit = in6_addr_get_bit(address, level);
+       in6_addr_set_bit(network_address, level, bit);
+
+       if (bit == 0)
+               node_index = be32toh(node->zero);
+       else
+               node_index = be32toh(node->one);
+
+       // If we point back to root, the path ends here
+       if (node_index == 0) {
+               DEBUG(db->ctx, "Tree ends here\n");
+               return 1;
+       }
+
+       // Check boundaries
+       if ((size_t)node_index >= db->network_nodes_count)
+               return -EINVAL;
+
+       // Move on to the next node
+       r = __loc_database_lookup(db, address, network, network_address,
+               db->network_nodes_v0 + node_index, level + 1);
+
+       // End here if a result was found
+       if (r == 0)
+               return r;
+
+       // Raise any errors
+       else if (r < 0)
+               return r;
+
+       DEBUG(db->ctx, "Could not find an exact match at %u\n", level);
+
+       // If nothing was found, we have to search for an inexact match
+       return __loc_database_lookup_max(db, address, network, network_address, node, level);
+}
+
+LOC_EXPORT int loc_database_lookup(struct loc_database* db,
+               struct in6_addr* address, struct loc_network** network) {
+       struct in6_addr network_address;
+       memset(&network_address, 0, sizeof(network_address));
+
+       *network = NULL;
+
+       // Save start time
+       clock_t start = clock();
+
+       int r = __loc_database_lookup(db, address, network, &network_address,
+               db->network_nodes_v0, 0);
+
+       clock_t end = clock();
+
+       // Log how fast this has been
+       DEBUG(db->ctx, "Executed network search in %.8fs\n",
+               (double)(end - start) / CLOCKS_PER_SEC);
+
+       return r;
+}
+
+LOC_EXPORT int loc_database_lookup_from_string(struct loc_database* db,
+               const char* string, struct loc_network** network) {
+       struct in6_addr address;
+
+       int r = loc_parse_address(db->ctx, string, &address);
+       if (r)
+               return r;
+
+       return loc_database_lookup(db, &address, network);
+}
index 8b8872c404da93571bab75ea25e95b5428a65dd0..630a4d5c8c7d414ba46a708d766d31e72db58a5b 100644 (file)
@@ -14,6 +14,8 @@
        Lesser General Public License for more details.
 */
 
        Lesser General Public License for more details.
 */
 
+#include <arpa/inet.h>
+#include <netinet/in.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
@@ -156,3 +158,34 @@ LOC_EXPORT int loc_load(struct loc_ctx* ctx, const char* path) {
 
        return 0;
 }
 
        return 0;
 }
+
+LOC_EXPORT int loc_parse_address(struct loc_ctx* ctx, const char* string, struct in6_addr* address) {
+       DEBUG(ctx, "Paring IP address %s\n", string);
+
+       // Try parsing this as an IPv6 address
+       int r = inet_pton(AF_INET6, string, address);
+
+       // If inet_pton returns one it has been successful
+       if (r == 1) {
+               DEBUG(ctx, "%s is an IPv6 address\n", string);
+               return 0;
+       }
+
+       // Try parsing this as an IPv4 address
+       struct in_addr ipv4_address;
+       r = inet_pton(AF_INET, string, &ipv4_address);
+       if (r == 1) {
+               DEBUG(ctx, "%s is an IPv4 address\n", string);
+
+               // Convert to IPv6-mapped address
+               address->s6_addr32[0] = htonl(0x0000);
+               address->s6_addr32[1] = htonl(0x0000);
+               address->s6_addr32[2] = htonl(0xffff);
+               address->s6_addr32[3] = ipv4_address.s_addr;
+
+               return 0;
+       }
+
+       DEBUG(ctx, "%s is not an valid IP address\n", string);
+       return 1;
+}
index bdccb707b4d38afc060027b77714d9f3a307f50a..628fca48e6fa87f5e8bd3e83f225a3f113fd52e5 100644 (file)
@@ -16,6 +16,8 @@ global:
        loc_database_get_as;
        loc_database_get_description;
        loc_database_get_vendor;
        loc_database_get_as;
        loc_database_get_description;
        loc_database_get_vendor;
+       loc_database_lookup;
+       loc_database_lookup_from_string;
        loc_database_new;
        loc_database_ref;
        loc_database_unref;
        loc_database_new;
        loc_database_ref;
        loc_database_unref;
index 28c29e9912aef47578568212bfb6e54aa3f2312c..78963c078dc74ceefd8ede0a443b6974aac26966 100644 (file)
 #ifndef LIBLOC_DATABASE_H
 #define LIBLOC_DATABASE_H
 
 #ifndef LIBLOC_DATABASE_H
 #define LIBLOC_DATABASE_H
 
+#include <netinet/in.h>
 #include <stdio.h>
 #include <stdint.h>
 
 #include <loc/libloc.h>
 #include <stdio.h>
 #include <stdint.h>
 
 #include <loc/libloc.h>
+#include <loc/network.h>
 #include <loc/as.h>
 
 struct loc_database;
 #include <loc/as.h>
 
 struct loc_database;
@@ -37,4 +39,9 @@ size_t loc_database_count_as(struct loc_database* db);
 
 int loc_database_write(struct loc_database* db, FILE* f);
 
 
 int loc_database_write(struct loc_database* db, FILE* f);
 
+int loc_database_lookup(struct loc_database* db,
+               struct in6_addr* address, struct loc_network** network);
+int loc_database_lookup_from_string(struct loc_database* db,
+               const char* string, struct loc_network** network);
+
 #endif
 #endif
index 5cf31c5b14d984db78fc29dc8eddb840f1b66c36..0eca01566326a0dcacf7fc7219ddecb45816cada 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef LIBLOC_H
 #define LIBLOC_H
 
 #ifndef LIBLOC_H
 #define LIBLOC_H
 
+#include <netinet/in.h>
 #include <stdarg.h>
 
 #ifdef __cplusplus
 #include <stdarg.h>
 
 #ifdef __cplusplus
@@ -36,6 +37,7 @@ int loc_get_log_priority(struct loc_ctx* ctx);
 void loc_set_log_priority(struct loc_ctx* ctx, int priority);
 
 int loc_load(struct loc_ctx* ctx, const char* path);
 void loc_set_log_priority(struct loc_ctx* ctx, int priority);
 
 int loc_load(struct loc_ctx* ctx, const char* path);
+int loc_parse_address(struct loc_ctx* ctx, const char* string, struct in6_addr* address);
 
 #ifdef __cplusplus
 } /* extern "C" */
 
 #ifdef __cplusplus
 } /* extern "C" */
index be5b3775ea177aa22b0dda380351d34cc04c8b2f..f804f79aeb6e084554cd657b3eadc7fb07f8356e 100644 (file)
@@ -30,6 +30,7 @@ int loc_network_new_from_string(struct loc_ctx* ctx, struct loc_network** networ
 struct loc_network* loc_network_ref(struct loc_network* network);
 struct loc_network* loc_network_unref(struct loc_network* network);
 char* loc_network_str(struct loc_network* network);
 struct loc_network* loc_network_ref(struct loc_network* network);
 struct loc_network* loc_network_unref(struct loc_network* network);
 char* loc_network_str(struct loc_network* network);
+int loc_network_match_address(struct loc_network* network, const struct in6_addr* address);
 
 const char* loc_network_get_country_code(struct loc_network* network);
 int loc_network_set_country_code(struct loc_network* network, const char* country_code);
 
 const char* loc_network_get_country_code(struct loc_network* network);
 int loc_network_set_country_code(struct loc_network* network, const char* country_code);
index 19e0398e5cd16f9e26f46455b011a7704b98cd44..e7ed0f448cdbac71b93f1dd690cc84bcb15b3582 100644 (file)
@@ -19,6 +19,7 @@
 
 #ifdef LIBLOC_PRIVATE
 
 
 #ifdef LIBLOC_PRIVATE
 
+#include <netinet/in.h>
 #include <stdbool.h>
 #include <syslog.h>
 
 #include <stdbool.h>
 #include <syslog.h>
 
@@ -56,5 +57,17 @@ void loc_log(struct loc_ctx *ctx,
        int priority, const char *file, int line, const char *fn,
        const char *format, ...) __attribute__((format(printf, 6, 7)));
 
        int priority, const char *file, int line, const char *fn,
        const char *format, ...) __attribute__((format(printf, 6, 7)));
 
+static inline int in6_addr_cmp(const struct in6_addr* a1, const struct in6_addr* a2) {
+       return memcmp(&a1->s6_addr, &a1->s6_addr, sizeof(a1->s6_addr));
+}
+
+static inline int in6_addr_get_bit(const struct in6_addr* address, unsigned int i) {
+       return ((address->s6_addr[i / 8] >> (i % 8)) & 1);
+}
+
+static inline void in6_addr_set_bit(struct in6_addr* address, unsigned int i, unsigned int val) {
+       address->s6_addr[i / 8] ^= (-val ^ address->s6_addr[i / 8]) & (1 << (i % 8));
+}
+
 #endif
 #endif
 #endif
 #endif
index bcdc025af30dd70b18fc6617082d4af415212507..73a299fe29a604d9cc7d672bbb53d37da1e0226a 100644 (file)
@@ -69,7 +69,7 @@ static struct in6_addr prefix_to_bitmask(unsigned int prefix) {
        return bitmask;
 }
 
        return bitmask;
 }
 
-static struct in6_addr make_start_address(struct in6_addr* address, unsigned int prefix) {
+static struct in6_addr make_start_address(const struct in6_addr* address, unsigned int prefix) {
        struct in6_addr a;
        struct in6_addr bitmask = prefix_to_bitmask(prefix);
 
        struct in6_addr a;
        struct in6_addr bitmask = prefix_to_bitmask(prefix);
 
@@ -80,6 +80,17 @@ static struct in6_addr make_start_address(struct in6_addr* address, unsigned int
        return a;
 }
 
        return a;
 }
 
+static struct in6_addr make_last_address(const struct in6_addr* address, unsigned int prefix) {
+       struct in6_addr a;
+       struct in6_addr bitmask = prefix_to_bitmask(prefix);
+
+       // Perform bitwise OR
+       for (unsigned int i = 0; i < 4; i++)
+               a.s6_addr32[i] = address->s6_addr[i] | ~bitmask.s6_addr32[i];
+
+       return a;
+}
+
 LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network,
                struct in6_addr* address, unsigned int prefix) {
        // Address cannot be unspecified
 LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network,
                struct in6_addr* address, unsigned int prefix) {
        // Address cannot be unspecified
@@ -135,37 +146,6 @@ static int loc_network_address_family(struct loc_network* network) {
        return AF_INET6;
 }
 
        return AF_INET6;
 }
 
-static int parse_address(struct loc_ctx* ctx, const char* string, struct in6_addr* address) {
-       DEBUG(ctx, "Paring IP address %s\n", string);
-
-       // Try parsing this as an IPv6 address
-       int r = inet_pton(AF_INET6, string, address);
-
-       // If inet_pton returns one it has been successful
-       if (r == 1) {
-               DEBUG(ctx, "%s is an IPv6 address\n", string);
-               return 0;
-       }
-
-       // Try parsing this as an IPv4 address
-       struct in_addr ipv4_address;
-       r = inet_pton(AF_INET, string, &ipv4_address);
-       if (r == 1) {
-               DEBUG(ctx, "%s is an IPv4 address\n", string);
-
-               // Convert to IPv6-mapped address
-               address->s6_addr32[0] = htonl(0x0000);
-               address->s6_addr32[1] = htonl(0x0000);
-               address->s6_addr32[2] = htonl(0xffff);
-               address->s6_addr32[3] = ipv4_address.s_addr;
-
-               return 0;
-       }
-
-       DEBUG(ctx, "%s is not an valid IP address\n", string);
-       return 1;
-}
-
 LOC_EXPORT int loc_network_new_from_string(struct loc_ctx* ctx, struct loc_network** network,
                const char* address_string) {
        struct in6_addr start_address;
 LOC_EXPORT int loc_network_new_from_string(struct loc_ctx* ctx, struct loc_network** network,
                const char* address_string) {
        struct in6_addr start_address;
@@ -187,7 +167,7 @@ LOC_EXPORT int loc_network_new_from_string(struct loc_ctx* ctx, struct loc_netwo
 
                if (prefix) {
                        // Parse the address
 
                if (prefix) {
                        // Parse the address
-                       r = parse_address(ctx, address_string, &start_address);
+                       r = loc_parse_address(ctx, address_string, &start_address);
 
                        // Map the prefix to IPv6 if needed
                        if (IN6_IS_ADDR_V4MAPPED(&start_address))
 
                        // Map the prefix to IPv6 if needed
                        if (IN6_IS_ADDR_V4MAPPED(&start_address))
@@ -219,6 +199,9 @@ static void loc_network_free(struct loc_network* network) {
 }
 
 LOC_EXPORT struct loc_network* loc_network_unref(struct loc_network* network) {
 }
 
 LOC_EXPORT struct loc_network* loc_network_unref(struct loc_network* network) {
+       if (!network)
+               return NULL;
+
        if (--network->refcount > 0)
                return network;
 
        if (--network->refcount > 0)
                return network;
 
@@ -284,6 +267,22 @@ LOC_EXPORT char* loc_network_str(struct loc_network* network) {
        return string;
 }
 
        return string;
 }
 
+LOC_EXPORT int loc_network_match_address(struct loc_network* network, const struct in6_addr* address) {
+       // Address must be larger then the start address
+       if (in6_addr_cmp(&network->start_address, address) > 0)
+               return 1;
+
+       // Determine the last address in this network
+       struct in6_addr last_address = make_last_address(&network->start_address, network->prefix);
+
+       // Address must be smaller than the last address
+       if (in6_addr_cmp(&last_address, address) > 0)
+               return 1;
+
+       // The address is inside this network
+       return 0;
+}
+
 LOC_EXPORT const char* loc_network_get_country_code(struct loc_network* network) {
        return network->country_code;
 }
 LOC_EXPORT const char* loc_network_get_country_code(struct loc_network* network) {
        return network->country_code;
 }
@@ -393,7 +392,7 @@ LOC_EXPORT struct loc_network_tree_node* loc_network_tree_get_root(struct loc_ne
 static struct loc_network_tree_node* loc_network_tree_get_node(struct loc_network_tree_node* node, int path) {
        struct loc_network_tree_node** n;
 
 static struct loc_network_tree_node* loc_network_tree_get_node(struct loc_network_tree_node* node, int path) {
        struct loc_network_tree_node** n;
 
-       if (path)
+       if (path == 0)
                n = &node->zero;
        else
                n = &node->one;
                n = &node->zero;
        else
                n = &node->one;
@@ -411,9 +410,9 @@ static struct loc_network_tree_node* loc_network_tree_get_node(struct loc_networ
 static struct loc_network_tree_node* loc_network_tree_get_path(struct loc_network_tree* tree, const struct in6_addr* address) {
        struct loc_network_tree_node* node = tree->root;
 
 static struct loc_network_tree_node* loc_network_tree_get_path(struct loc_network_tree* tree, const struct in6_addr* address) {
        struct loc_network_tree_node* node = tree->root;
 
-       for (unsigned int i = 127; i > 0; i--) {
+       for (unsigned int i = 0; i < 128; i++) {
                // Check if the ith bit is one or zero
                // Check if the ith bit is one or zero
-               node = loc_network_tree_get_node(node, ((address->s6_addr32[i / 32] & (1 << (i % 32))) == 0));
+               node = loc_network_tree_get_node(node, in6_addr_get_bit(address, i));
        }
 
        return node;
        }
 
        return node;
index 119740c5064577916154ba8ef817c7ee016dfdf9..6468c5e8f07889aec976601382f706a7c4d38673 100644 (file)
@@ -90,9 +90,11 @@ LOC_EXPORT int loc_stringpool_open(struct loc_ctx* ctx, struct loc_stringpool**
                return r;
 
        // Map data into memory
                return r;
 
        // Map data into memory
-       r = loc_stringpool_mmap(*pool, f, length, offset);
-       if (r)
-               return r;
+       if (length > 0) {
+               r = loc_stringpool_mmap(*pool, f, length, offset);
+               if (r)
+                       return r;
+       }
 
        return 0;
 }
 
        return 0;
 }
index 1007bd1df3f19fcdf7897fc0ca71d92a0ddc22ff..b7b51201231b31630b431903f06ffc06cdbeebad 100644 (file)
@@ -21,6 +21,7 @@
 #include <string.h>
 
 #include <loc/libloc.h>
 #include <string.h>
 
 #include <loc/libloc.h>
+#include <loc/database.h>
 #include <loc/network.h>
 #include <loc/writer.h>
 
 #include <loc/network.h>
 #include <loc/writer.h>
 
@@ -139,6 +140,37 @@ int main(int argc, char** argv) {
        loc_network_unref(network3);
        loc_network_unref(network4);
        loc_network_tree_unref(tree);
        loc_network_unref(network3);
        loc_network_unref(network4);
        loc_network_tree_unref(tree);
+
+       // And open it again from disk
+       f = fopen("test.db", "r");
+       if (!f) {
+               fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       struct loc_database* db;
+       err = loc_database_new(ctx, &db, f);
+       if (err) {
+               fprintf(stderr, "Could not open database: %s\n", strerror(-err));
+               exit(EXIT_FAILURE);
+       }
+
+       // Lookup an exact match
+       err = loc_database_lookup_from_string(db, "2001:db8::", &network1);
+       if (err) {
+               fprintf(stderr, "Could not look up the given IP address\n");
+               exit(EXIT_FAILURE);
+       }
+       loc_network_unref(network1);
+
+       // Lookup a non-exact match
+       err = loc_database_lookup_from_string(db, "2001:db8:fffe:1::", &network1);
+       if (err) {
+               fprintf(stderr, "Could not look up the given IP address\n");
+               exit(EXIT_FAILURE);
+       }
+       loc_network_unref(network1);
+
        loc_unref(ctx);
 
        return EXIT_SUCCESS;
        loc_unref(ctx);
 
        return EXIT_SUCCESS;