]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
network: Allow creating any valid networks
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 6 Mar 2022 15:08:47 +0000 (15:08 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 6 Mar 2022 15:09:36 +0000 (15:09 +0000)
Formerly, we did not allow creating special networks like ::1/128, ::/0,
127.0.0.0/8, and so on.

In order to represent all bogons, we will have to allow this.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/network.c
src/test-network.c

index d7daa80b2bdfdb923e1c5b884d57b422aae3dab1..0266ebf3edc0fd63e159b0faa9a785e87a2654f6 100644 (file)
@@ -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;
@@ -64,39 +60,18 @@ static int valid_prefix(struct in6_addr* address, unsigned int prefix) {
 
 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;
index 92088ee2a29ed15a7397bd9298f45cdf69100fdb..1d4550cc910b46003e960cd17f2ecffeb0f1a6cb 100644 (file)
@@ -259,13 +259,6 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
-       // Try adding localhost
-       err = loc_writer_add_network(writer, &network, "::1/128");
-       if (err != -EINVAL) {
-               fprintf(stderr, "It was possible to add localhost (::1/128): %d\n", err);
-               exit(EXIT_FAILURE);
-       }
-
        FILE* f = tmpfile();
        if (!f) {
                fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));