struct loc_database_enumerator {
struct loc_ctx* ctx;
struct loc_database* db;
+ enum loc_database_enumerator_mode mode;
int refcount;
// Search string
char* string;
char country_code[3];
+ uint32_t asn;
+ enum loc_network_flags flags;
// Index of the AS we are looking at
unsigned int as_index;
clock_t end = clock();
- INFO(db->ctx, "Opened database in %.8fs\n",
- (double)(end - start) / CLOCKS_PER_SEC);
+ INFO(db->ctx, "Opened database in %.4fms\n",
+ (double)(end - start) / CLOCKS_PER_SEC * 1000);
return 0;
}
clock_t end = clock();
// Log how fast this has been
- DEBUG(db->ctx, "Found AS%u in %.8fs\n", as_number,
- (double)(end - start) / CLOCKS_PER_SEC);
+ DEBUG(db->ctx, "Found AS%u in %.4fms\n", as_number,
+ (double)(end - start) / CLOCKS_PER_SEC * 1000);
return 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);
+ DEBUG(db->ctx, "Executed network search in %.4fms\n",
+ (double)(end - start) / CLOCKS_PER_SEC * 1000);
return r;
}
// Enumerator
-LOC_EXPORT int loc_database_enumerator_new(struct loc_database_enumerator** enumerator, struct loc_database* db) {
+LOC_EXPORT int loc_database_enumerator_new(struct loc_database_enumerator** enumerator,
+ struct loc_database* db, enum loc_database_enumerator_mode mode) {
struct loc_database_enumerator* e = calloc(1, sizeof(*e));
if (!e)
return -ENOMEM;
// Reference context
e->ctx = loc_ref(db->ctx);
e->db = loc_database_ref(db);
+ e->mode = mode;
e->refcount = 1;
// Initialise graph search
return 0;
}
-LOC_EXPORT struct loc_as* loc_database_enumerator_next_as(struct loc_database_enumerator* enumerator) {
+LOC_EXPORT int loc_database_enumerator_set_asn(
+ struct loc_database_enumerator* enumerator, unsigned int asn) {
+ enumerator->asn = asn;
+
+ return 0;
+}
+
+LOC_EXPORT int loc_database_enumerator_set_flag(
+ struct loc_database_enumerator* enumerator, enum loc_network_flags flag) {
+ enumerator->flags |= flag;
+
+ return 0;
+}
+
+LOC_EXPORT int loc_database_enumerator_next_as(
+ struct loc_database_enumerator* enumerator, struct loc_as** as) {
+ *as = NULL;
+
+ // Do not do anything if not in AS mode
+ if (enumerator->mode != LOC_DB_ENUMERATE_ASES)
+ return 0;
+
struct loc_database* db = enumerator->db;
- struct loc_as* as;
while (enumerator->as_index < db->as_count) {
// Fetch the next AS
- int r = loc_database_fetch_as(db, &as, enumerator->as_index++);
+ int r = loc_database_fetch_as(db, as, enumerator->as_index++);
if (r)
- return NULL;
+ return r;
- r = loc_as_match_string(as, enumerator->string);
+ r = loc_as_match_string(*as, enumerator->string);
if (r == 1) {
DEBUG(enumerator->ctx, "AS%d (%s) matches %s\n",
- loc_as_get_number(as), loc_as_get_name(as), enumerator->string);
+ loc_as_get_number(*as), loc_as_get_name(*as), enumerator->string);
- return as;
+ return 0;
}
// No match
- loc_as_unref(as);
+ loc_as_unref(*as);
+ *as = NULL;
}
// Reset the index
enumerator->as_index = 0;
// We have searched through all of them
- return NULL;
+ return 0;
}
static int loc_database_enumerator_stack_push_node(
return 0;
}
-static int loc_database_enumerator_network_depth_first_search(
- struct loc_database_enumerator* e, struct loc_network** network) {
+LOC_EXPORT int loc_database_enumerator_next_network(
+ struct loc_database_enumerator* enumerator, struct loc_network** network) {
// Reset network
*network = NULL;
+
+ // Do not do anything if not in network mode
+ if (enumerator->mode != LOC_DB_ENUMERATE_NETWORKS)
+ return 0;
+
int r;
- DEBUG(e->ctx, "Called with a stack of %u nodes\n", e->network_stack_depth);
+ DEBUG(enumerator->ctx, "Called with a stack of %u nodes\n",
+ enumerator->network_stack_depth);
// Perform DFS
- while (e->network_stack_depth > 0) {
- DEBUG(e->ctx, "Stack depth: %u\n", e->network_stack_depth);
+ while (enumerator->network_stack_depth > 0) {
+ DEBUG(enumerator->ctx, "Stack depth: %u\n", enumerator->network_stack_depth);
// Get object from top of the stack
- struct loc_node_stack* node = &e->network_stack[e->network_stack_depth];
+ struct loc_node_stack* node = &enumerator->network_stack[enumerator->network_stack_depth];
// Remove the node from the stack if we have already visited it
- if (e->networks_visited[node->offset]) {
- e->network_stack_depth--;
+ if (enumerator->networks_visited[node->offset]) {
+ enumerator->network_stack_depth--;
continue;
}
- in6_addr_set_bit(&e->network_address,
+ // Mark the bits on the path correctly
+ in6_addr_set_bit(&enumerator->network_address,
(node->depth > 0) ? node->depth - 1 : 0, node->i);
- //for (unsigned int i = stack->depth + 1; i < 128; i++)
- // in6_addr_set_bit(&e->network_address, i, 0);
-
- DEBUG(e->ctx, "Looking at node %jd\n", node->offset);
- e->networks_visited[node->offset]++;
+ DEBUG(enumerator->ctx, "Looking at node %jd\n", node->offset);
+ enumerator->networks_visited[node->offset]++;
// Pop node from top of the stack
struct loc_database_network_node_v0* n =
- e->db->network_nodes_v0 + node->offset;
+ enumerator->db->network_nodes_v0 + node->offset;
// Add edges to stack
- r = loc_database_enumerator_stack_push_node(e,
+ r = loc_database_enumerator_stack_push_node(enumerator,
be32toh(n->one), 1, node->depth + 1);
if (r)
return r;
- r = loc_database_enumerator_stack_push_node(e,
+ r = loc_database_enumerator_stack_push_node(enumerator,
be32toh(n->zero), 0, node->depth + 1);
if (r)
if (__loc_database_node_is_leaf(n)) {
off_t network_index = be32toh(n->network);
- DEBUG(e->ctx, "Node has a network at %jd\n", network_index);
+ DEBUG(enumerator->ctx, "Node has a network at %jd\n", network_index);
// Fetch the network object
- r = loc_database_fetch_network(e->db, network,
- &e->network_address, node->depth, network_index);
+ r = loc_database_fetch_network(enumerator->db, network,
+ &enumerator->network_address, node->depth, network_index);
// Break on any errors
if (r)
// Check if we are interested in this network
// Skip if the country code does not match
- if (e->country_code && !loc_network_match_country_code(*network, e->country_code)) {
+ if (enumerator->country_code &&
+ !loc_network_match_country_code(*network, enumerator->country_code)) {
+ loc_network_unref(*network);
+ *network = NULL;
+
+ continue;
+ }
+
+ // Skip if the ASN does not match
+ if (enumerator->asn &&
+ !loc_network_match_asn(*network, enumerator->asn)) {
loc_network_unref(*network);
+ *network = NULL;
+
continue;
}
+ // Skip if flags do not match
+ if (enumerator->flags &&
+ !loc_network_match_flag(*network, enumerator->flags)) {
+ loc_network_unref(*network);
+ *network = NULL;
+ }
+
return 0;
}
}
// Reached the end of the search
- // TODO cleanup
- return 0;
-}
+ // Mark all nodes as non-visited
+ for (unsigned int i = 0; i < enumerator->db->network_nodes_count; i++)
+ enumerator->networks_visited[i] = 0;
-LOC_EXPORT struct loc_network* loc_database_enumerator_next_network(
- struct loc_database_enumerator* enumerator) {
- struct loc_network* network = NULL;
-
- int r = loc_database_enumerator_network_depth_first_search(enumerator, &network);
- if (r) {
- return NULL;
- }
-
- return network;
+ return 0;
}