-/*-*- 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,
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);
}
/* 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) {
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;