]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/network/networkd-address-pool.c
hibernate-resume: add resumeflags= kernel option
[thirdparty/systemd.git] / src / network / networkd-address-pool.c
index 889fe1e30d3a724c104624557758be0d4eac645b..db6c1456dc7e5c20b70783ffae1a29b26b8a8e58 100644 (file)
@@ -1,31 +1,14 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
-  This file is part of systemd.
-
-  Copyright 2014 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
+/* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include "alloc-util.h"
 #include "networkd-address-pool.h"
-#include "networkd.h"
+#include "networkd-manager.h"
 #include "set.h"
 #include "string-util.h"
 
-int address_pool_new(
+#define RANDOM_PREFIX_TRIAL_MAX  1024
+
+static int address_pool_new(
                 Manager *m,
                 AddressPool **ret,
                 int family,
@@ -38,14 +21,16 @@ int address_pool_new(
         assert(ret);
         assert(u);
 
-        p = new0(AddressPool, 1);
+        p = new(AddressPool, 1);
         if (!p)
                 return -ENOMEM;
 
-        p->manager = m;
-        p->family = family;
-        p->prefixlen = prefixlen;
-        p->in_addr = *u;
+        *p = (AddressPool) {
+                .manager = m,
+                .family = family,
+                .prefixlen = prefixlen,
+                .in_addr = *u,
+        };
 
         LIST_PREPEND(address_pools, m->address_pools, p);
 
@@ -121,7 +106,7 @@ static bool address_pool_prefix_is_taken(
         }
 
         /* And don't clash with configured but un-assigned addresses either */
-        LIST_FOREACH(networks, n, p->manager->networks) {
+        ORDERED_HASHMAP_FOREACH(n, p->manager->networks, i) {
                 Address *a;
 
                 LIST_FOREACH(addresses, a, n->static_addresses) {
@@ -138,31 +123,34 @@ static bool address_pool_prefix_is_taken(
 
 int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found) {
         union in_addr_union u;
+        unsigned i;
+        int r;
 
         assert(p);
         assert(prefixlen > 0);
         assert(found);
 
-        if (p->prefixlen > prefixlen)
+        if (p->prefixlen >= prefixlen)
                 return 0;
 
         u = p->in_addr;
-        for (;;) {
+
+        for (i = 0; i < RANDOM_PREFIX_TRIAL_MAX; i++) {
+                r = in_addr_random_prefix(p->family, &u, p->prefixlen, prefixlen);
+                if (r <= 0)
+                        return r;
+
                 if (!address_pool_prefix_is_taken(p, &u, prefixlen)) {
-                        _cleanup_free_ char *s = NULL;
+                        if (DEBUG_LOGGING) {
+                                _cleanup_free_ char *s = NULL;
 
-                        in_addr_to_string(p->family, &u, &s);
-                        log_debug("Found range %s/%u", strna(s), prefixlen);
+                                (void) in_addr_to_string(p->family, &u, &s);
+                                log_debug("Found range %s/%u", strna(s), prefixlen);
+                        }
 
                         *found = u;
                         return 1;
                 }
-
-                if (!in_addr_prefix_next(p->family, &u, prefixlen))
-                        return 0;
-
-                if (!in_addr_prefix_intersect(p->family, &p->in_addr, p->prefixlen, &u, prefixlen))
-                        return 0;
         }
 
         return 0;