networkd-address-pool.c revision cf1d700da3fe04cd4c27980a292301694be3b05f
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
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/>.
***/
#include "networkd.h"
#include "networkd-address-pool.h"
#include "set.h"
int address_pool_new(
Manager *m,
AddressPool **ret,
int family,
const union in_addr_union *u,
unsigned prefixlen) {
AddressPool *p;
assert(m);
assert(u);
if (!p)
return -ENOMEM;
p->manager = m;
p->in_addr = *u;
*ret = p;
return 0;
}
Manager *m,
AddressPool **ret,
int family,
const char *p,
unsigned prefixlen) {
union in_addr_union u;
int r;
assert(m);
assert(p);
r = in_addr_from_string(family, p, &u);
if (r < 0)
return r;
}
void address_pool_free(AddressPool *p) {
if (!p)
return;
if (p->manager)
free(p);
}
static bool address_pool_prefix_is_taken(
AddressPool *p,
const union in_addr_union *u,
unsigned prefixlen) {
Iterator i;
Link *l;
Network *n;
assert(p);
assert(u);
Address *a;
Iterator j;
/* Don't clash with assigned addresses */
SET_FOREACH(a, l->addresses, j) {
continue;
return true;
}
/* Don't clash with addresses already pulled from the pool, but not assigned yet */
continue;
return true;
}
}
/* And don't clash with configured but un-assigned addresses either */
Address *a;
continue;
return true;
}
}
return false;
}
union in_addr_union u;
assert(p);
return 0;
u = p->in_addr;
for (;;) {
if (!address_pool_prefix_is_taken(p, &u, prefixlen)) {
_cleanup_free_ char *s = NULL;
in_addr_to_string(p->family, &u, &s);
*found = u;
return 1;
}
return 0;
return 0;
}
return 0;
}