]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/network.c
network: Allow creating any valid networks
[people/ms/libloc.git] / src / network.c
index a6b679c84f4e8909ff737d84fb353cf7dbe19479..0266ebf3edc0fd63e159b0faa9a785e87a2654f6 100644 (file)
 #  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;
@@ -51,10 +51,6 @@ static int valid_prefix(struct in6_addr* address, unsigned int prefix) {
        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;
@@ -62,92 +58,20 @@ static int valid_prefix(struct in6_addr* address, unsigned int prefix) {
        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;
@@ -156,17 +80,14 @@ LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network
        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;
@@ -371,7 +292,7 @@ LOC_EXPORT char* loc_network_format_last_address(struct loc_network* network) {
        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;
@@ -404,11 +325,19 @@ LOC_EXPORT int loc_network_set_country_code(struct loc_network* network, const c
        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]);
 }
@@ -423,10 +352,6 @@ LOC_EXPORT int loc_network_set_asn(struct loc_network* network, uint32_t asn) {
        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;
 }
@@ -437,10 +362,6 @@ LOC_EXPORT int loc_network_set_flag(struct loc_network* network, uint32_t 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);
@@ -459,17 +380,17 @@ LOC_EXPORT int loc_network_cmp(struct loc_network* self, struct loc_network* oth
 
 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;
@@ -745,7 +666,7 @@ LOC_EXPORT struct loc_network_list* loc_network_exclude_list(
        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);
 
@@ -758,7 +679,7 @@ LOC_EXPORT int loc_network_to_database_v1(struct loc_network* network, struct lo
        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";