Lesser General Public License for more details.
*/
+#include <arpa/inet.h>
#include <endian.h>
#include <errno.h>
#include <netinet/in.h>
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);
+}
Lesser General Public License for more details.
*/
+#include <arpa/inet.h>
+#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
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;
+}
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;
#ifndef LIBLOC_DATABASE_H
#define LIBLOC_DATABASE_H
+#include <netinet/in.h>
#include <stdio.h>
#include <stdint.h>
#include <loc/libloc.h>
+#include <loc/network.h>
#include <loc/as.h>
struct loc_database;
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
#ifndef LIBLOC_H
#define LIBLOC_H
+#include <netinet/in.h>
#include <stdarg.h>
#ifdef __cplusplus
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" */
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);
#ifdef LIBLOC_PRIVATE
+#include <netinet/in.h>
#include <stdbool.h>
#include <syslog.h>
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
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);
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
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;
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))
}
LOC_EXPORT struct loc_network* loc_network_unref(struct loc_network* network) {
+ if (!network)
+ return NULL;
+
if (--network->refcount > 0)
return 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;
}
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;
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;
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;
}
#include <string.h>
#include <loc/libloc.h>
+#include <loc/database.h>
#include <loc/network.h>
#include <loc/writer.h>
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;