# include <endian.h>
#endif
-#include <loc/libloc.h>
-#include <loc/compat.h>
-#include <loc/country.h>
-#include <loc/network.h>
-#include <loc/network-list.h>
-#include <loc/private.h>
+#include <libloc/libloc.h>
+#include <libloc/compat.h>
+#include <libloc/country.h>
+#include <libloc/network.h>
+#include <libloc/network-list.h>
+#include <libloc/private.h>
struct loc_network {
struct loc_ctx* ctx;
if (prefix > 128)
return 1;
- // And the prefix cannot be zero
- if (prefix == 0)
- return 1;
-
// For IPv4-mapped addresses the prefix has to be 96 or lager
if (IN6_IS_ADDR_V4MAPPED(address) && prefix <= 96)
return 1;
return 0;
}
-static struct in6_addr prefix_to_bitmask(unsigned int prefix) {
- struct in6_addr bitmask;
-
- for (unsigned int i = 0; i < 16; i++)
- bitmask.s6_addr[i] = 0;
-
- for (int i = prefix, j = 0; i > 0; i -= 8, j++) {
- if (i >= 8)
- bitmask.s6_addr[j] = 0xff;
- else
- bitmask.s6_addr[j] = 0xff << (8 - i);
- }
-
- return bitmask;
-}
-
-static struct in6_addr make_first_address(const struct in6_addr* address, const struct in6_addr* bitmask) {
- struct in6_addr a;
-
- // Perform bitwise AND
- for (unsigned int i = 0; i < 4; i++)
- a.s6_addr32[i] = address->s6_addr32[i] & bitmask->s6_addr32[i];
-
- return a;
-}
-
-static struct in6_addr make_last_address(const struct in6_addr* address, const struct in6_addr* bitmask) {
- struct in6_addr a;
-
- // Perform bitwise OR
- for (unsigned int i = 0; i < 4; i++)
- a.s6_addr32[i] = address->s6_addr32[i] | ~bitmask->s6_addr32[i];
-
- return a;
-}
-
-static struct in6_addr address_increment(const struct in6_addr* address) {
- struct in6_addr a = *address;
-
- for (int octet = 15; octet >= 0; octet--) {
- if (a.s6_addr[octet] < 255) {
- a.s6_addr[octet]++;
- break;
- } else {
- a.s6_addr[octet] = 0;
- }
- }
-
- 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
- if (IN6_IS_ADDR_UNSPECIFIED(address)) {
- DEBUG(ctx, "Start address is unspecified\n");
- return -EINVAL;
- }
-
- // Address cannot be loopback
- if (IN6_IS_ADDR_LOOPBACK(address)) {
- DEBUG(ctx, "Start address is loopback address\n");
- return -EINVAL;
- }
-
- // Address cannot be link-local
- if (IN6_IS_ADDR_LINKLOCAL(address)) {
- DEBUG(ctx, "Start address cannot be link-local\n");
- return -EINVAL;
- }
-
- // Address cannot be site-local
- if (IN6_IS_ADDR_SITELOCAL(address)) {
- DEBUG(ctx, "Start address cannot be site-local\n");
- return -EINVAL;
- }
-
// Validate the prefix
if (valid_prefix(address, prefix) != 0) {
- DEBUG(ctx, "Invalid prefix: %u\n", prefix);
- return -EINVAL;
+ ERROR(ctx, "Invalid prefix: %u\n", prefix);
+ errno = EINVAL;
+ return 1;
}
struct loc_network* n = calloc(1, sizeof(*n));
- if (!n)
- return -ENOMEM;
+ if (!n) {
+ errno = ENOMEM;
+ return 1;
+ }
n->ctx = loc_ref(ctx);
n->refcount = 1;
n->prefix = prefix;
// Convert the prefix into a bitmask
- struct in6_addr bitmask = prefix_to_bitmask(n->prefix);
+ struct in6_addr bitmask = loc_prefix_to_bitmask(n->prefix);
// Store the first and last address in the network
- n->first_address = make_first_address(address, &bitmask);
- n->last_address = make_last_address(&n->first_address, &bitmask);
+ n->first_address = loc_address_and(address, &bitmask);
+ n->last_address = loc_address_or(&n->first_address, &bitmask);
// Set family
- if (IN6_IS_ADDR_V4MAPPED(&n->first_address))
- n->family = AF_INET;
- else
- n->family = AF_INET6;
+ n->family = loc_address_family(&n->first_address);
DEBUG(n->ctx, "Network allocated at %p\n", n);
*network = n;
return loc_network_format_address(network, &network->last_address);
}
-LOC_EXPORT int loc_network_match_address(struct loc_network* network, const struct in6_addr* address) {
+LOC_EXPORT int loc_network_matches_address(struct loc_network* network, const struct in6_addr* address) {
// Address must be larger than the start address
if (in6_addr_cmp(&network->first_address, address) > 0)
return 0;
return 0;
}
-LOC_EXPORT int loc_network_match_country_code(struct loc_network* network, const char* country_code) {
+LOC_EXPORT int loc_network_matches_country_code(struct loc_network* network, const char* country_code) {
+ // Search for any special flags
+ const int flag = loc_country_special_code_to_flag(country_code);
+
+ // If we found a flag, we will return whether it is set or not
+ if (flag)
+ return loc_network_has_flag(network, flag);
+
// Check country code
if (!loc_country_code_is_valid(country_code))
return -EINVAL;
+ // Check for an exact match
return (network->country_code[0] == country_code[0])
&& (network->country_code[1] == country_code[1]);
}
return 0;
}
-LOC_EXPORT int loc_network_match_asn(struct loc_network* network, uint32_t asn) {
- return network->asn == asn;
-}
-
LOC_EXPORT int loc_network_has_flag(struct loc_network* network, uint32_t flag) {
return network->flags & flag;
}
return 0;
}
-LOC_EXPORT int loc_network_match_flag(struct loc_network* network, uint32_t flag) {
- return loc_network_has_flag(network, flag);
-}
-
LOC_EXPORT int loc_network_cmp(struct loc_network* self, struct loc_network* other) {
// Compare address
int r = in6_addr_cmp(&self->first_address, &other->first_address);
LOC_EXPORT int loc_network_overlaps(struct loc_network* self, struct loc_network* other) {
// Either of the start addresses must be in the other subnet
- if (loc_network_match_address(self, &other->first_address))
+ if (loc_network_matches_address(self, &other->first_address))
return 1;
- if (loc_network_match_address(other, &self->first_address))
+ if (loc_network_matches_address(other, &self->first_address))
return 1;
// Or either of the end addresses is in the other subnet
- if (loc_network_match_address(self, &other->last_address))
+ if (loc_network_matches_address(self, &other->last_address))
return 1;
- if (loc_network_match_address(other, &self->last_address))
+ if (loc_network_matches_address(other, &self->last_address))
return 1;
return 0;
loc_network_set_asn(*subnet2, asn);
}
+ // Copy flags
+ loc_network_set_flag(*subnet1, network->flags);
+ loc_network_set_flag(*subnet2, network->flags);
+
return 0;
}
return NULL;
}
+ off_t smallest_subnet = 0;
+
while (!loc_network_list_empty(to_check)) {
- struct loc_network* subnet_to_check = loc_network_list_pop(to_check);
+ struct loc_network* subnet_to_check = loc_network_list_pop_first(to_check);
// Check whether the subnet to check is part of the input list
if (loc_network_list_contains(list, subnet_to_check)) {
// Marks whether this subnet passed all checks
int passed = 1;
- for (unsigned int i = 0; i < loc_network_list_size(list); i++) {
+ for (unsigned int i = smallest_subnet; i < loc_network_list_size(list); i++) {
subnet = loc_network_list_get(list, i);
// Drop this subnet if is a subnet of another subnet
- if (loc_network_is_subnet(subnet_to_check, subnet)) {
+ if (loc_network_is_subnet(subnet, subnet_to_check)) {
passed = 0;
loc_network_unref(subnet);
break;
}
// Break it down if it overlaps
- if (loc_network_overlaps(subnet_to_check, subnet)) {
+ if (loc_network_overlaps(subnet, subnet_to_check)) {
passed = 0;
- struct loc_network_list* excluded = loc_network_exclude(subnet_to_check, subnet);
- if (excluded) {
- loc_network_list_merge(to_check, excluded);
- loc_network_list_unref(excluded);
- }
+ __loc_network_exclude_to_list(subnet_to_check, subnet, to_check);
loc_network_unref(subnet);
break;
}
+ // If the subnet is strictly greater, we do not need to continue the search
+ r = loc_network_cmp(subnet, subnet_to_check);
+ if (r > 0) {
+ loc_network_unref(subnet);
+ break;
+
+ // If it is strictly smaller, we can continue the search from here next
+ // time because all networks that are to be checked can only be larger
+ // than this one.
+ } else if (r < 0) {
+ smallest_subnet = i;
+ }
+
loc_network_unref(subnet);
}
loc_network_list_unref(to_check);
- // Sort the result
- loc_network_list_sort(subnets);
-
return subnets;
}
-LOC_EXPORT int loc_network_to_database_v1(struct loc_network* network, struct loc_database_network_v1* dbobj) {
+int loc_network_to_database_v1(struct loc_network* network, struct loc_database_network_v1* dbobj) {
// Add country code
loc_country_code_copy(dbobj->country_code, network->country_code);
return 0;
}
-LOC_EXPORT int loc_network_new_from_database_v1(struct loc_ctx* ctx, struct loc_network** network,
+int loc_network_new_from_database_v1(struct loc_ctx* ctx, struct loc_network** network,
struct in6_addr* address, unsigned int prefix, const struct loc_database_network_v1* dbobj) {
char country_code[3] = "\0\0";