From: Michael Tremer Date: Mon, 8 Jan 2018 15:56:28 +0000 (+0000) Subject: database: Implement lookup X-Git-Tag: 0.9.0~118 X-Git-Url: http://git.ipfire.org/?p=people%2Fms%2Flibloc.git;a=commitdiff_plain;h=2a30e4de98bd3362868f2673fa679559c42a6fbe database: Implement lookup This allows to pass an IP address to the database and to return the result Signed-off-by: Michael Tremer --- diff --git a/src/database.c b/src/database.c index dd3e77c..9cf5f32 100644 --- a/src/database.c +++ b/src/database.c @@ -14,6 +14,7 @@ Lesser General Public License for more details. */ +#include #include #include #include @@ -397,3 +398,162 @@ 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, + 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); +} diff --git a/src/libloc.c b/src/libloc.c index 8b8872c..630a4d5 100644 --- a/src/libloc.c +++ b/src/libloc.c @@ -14,6 +14,8 @@ Lesser General Public License for more details. */ +#include +#include #include #include #include @@ -156,3 +158,34 @@ LOC_EXPORT int loc_load(struct loc_ctx* ctx, const char* path) { 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; +} diff --git a/src/libloc.sym b/src/libloc.sym index bdccb70..628fca4 100644 --- a/src/libloc.sym +++ b/src/libloc.sym @@ -16,6 +16,8 @@ global: 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; diff --git a/src/loc/database.h b/src/loc/database.h index 28c29e9..78963c0 100644 --- a/src/loc/database.h +++ b/src/loc/database.h @@ -17,10 +17,12 @@ #ifndef LIBLOC_DATABASE_H #define LIBLOC_DATABASE_H +#include #include #include #include +#include #include 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_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 diff --git a/src/loc/libloc.h b/src/loc/libloc.h index 5cf31c5..0eca015 100644 --- a/src/loc/libloc.h +++ b/src/loc/libloc.h @@ -17,6 +17,7 @@ #ifndef LIBLOC_H #define LIBLOC_H +#include #include #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); +int loc_parse_address(struct loc_ctx* ctx, const char* string, struct in6_addr* address); #ifdef __cplusplus } /* extern "C" */ diff --git a/src/loc/network.h b/src/loc/network.h index be5b377..f804f79 100644 --- a/src/loc/network.h +++ b/src/loc/network.h @@ -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); +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); diff --git a/src/loc/private.h b/src/loc/private.h index 19e0398..e7ed0f4 100644 --- a/src/loc/private.h +++ b/src/loc/private.h @@ -19,6 +19,7 @@ #ifdef LIBLOC_PRIVATE +#include #include #include @@ -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))); +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 diff --git a/src/network.c b/src/network.c index bcdc025..73a299f 100644 --- a/src/network.c +++ b/src/network.c @@ -69,7 +69,7 @@ static struct in6_addr prefix_to_bitmask(unsigned int prefix) { 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); @@ -80,6 +80,17 @@ static struct in6_addr make_start_address(struct in6_addr* address, unsigned int 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 @@ -135,37 +146,6 @@ static int loc_network_address_family(struct loc_network* network) { 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; @@ -187,7 +167,7 @@ LOC_EXPORT int loc_network_new_from_string(struct loc_ctx* ctx, struct loc_netwo 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)) @@ -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) { + if (!network) + return NULL; + if (--network->refcount > 0) return network; @@ -284,6 +267,22 @@ LOC_EXPORT char* loc_network_str(struct loc_network* network) { 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; } @@ -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; - if (path) + if (path == 0) 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; - 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 - 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; diff --git a/src/stringpool.c b/src/stringpool.c index 119740c..6468c5e 100644 --- a/src/stringpool.c +++ b/src/stringpool.c @@ -90,9 +90,11 @@ LOC_EXPORT int loc_stringpool_open(struct loc_ctx* ctx, struct loc_stringpool** 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; } diff --git a/src/test-network.c b/src/test-network.c index 1007bd1..b7b5120 100644 --- a/src/test-network.c +++ b/src/test-network.c @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -139,6 +140,37 @@ int main(int argc, char** argv) { 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;