#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>
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,
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,
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,
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,
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) {
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) {
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;
}
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) {
// adjust our search pointers
loc_country_unref(*country);
- if (result < 0) {
+ if (result > 0) {
lo = i + 1;
} else
hi = i - 1;
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) {
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];
}
// 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;
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
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,
// 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;