network: move functions related to address pool

This commit is contained in:
Yu Watanabe 2020-10-02 14:15:57 +09:00
parent 093e35334d
commit ed76f58521
5 changed files with 54 additions and 55 deletions

View file

@ -39,7 +39,7 @@ static int address_pool_new(
return 0;
}
int address_pool_new_from_string(
static int address_pool_new_from_string(
Manager *m,
AddressPool **ret,
int family,
@ -71,6 +71,33 @@ void address_pool_free(AddressPool *p) {
free(p);
}
int address_pool_setup_default(Manager *m) {
AddressPool *p;
int r;
assert(m);
/* Add in the well-known private address ranges. */
r = address_pool_new_from_string(m, &p, AF_INET6, "fd00::", 8);
if (r < 0)
return r;
r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8);
if (r < 0)
return r;
r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12);
if (r < 0)
return r;
r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16);
if (r < 0)
return r;
return 0;
}
static bool address_pool_prefix_is_taken(
AddressPool *p,
const union in_addr_union *u,
@ -120,7 +147,7 @@ static bool address_pool_prefix_is_taken(
return false;
}
int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found) {
static int address_pool_acquire_one(AddressPool *p, int family, unsigned prefixlen, union in_addr_union *found) {
union in_addr_union u;
unsigned i;
int r;
@ -129,6 +156,9 @@ int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union
assert(prefixlen > 0);
assert(found);
if (p->family != family)
return 0;
if (p->prefixlen >= prefixlen)
return 0;
@ -154,3 +184,21 @@ int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union
return 0;
}
int address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
AddressPool *p;
int r;
assert(m);
assert(IN_SET(family, AF_INET, AF_INET6));
assert(prefixlen > 0);
assert(found);
LIST_FOREACH(address_pools, p, m->address_pools) {
r = address_pool_acquire_one(p, family, prefixlen, found);
if (r != 0)
return r;
}
return 0;
}

View file

@ -19,7 +19,7 @@ struct AddressPool {
LIST_FIELDS(AddressPool, address_pools);
};
int address_pool_new_from_string(Manager *m, AddressPool **ret, int family, const char *p, unsigned prefixlen);
void address_pool_free(AddressPool *p);
int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found);
int address_pool_setup_default(Manager *m);
int address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found);

View file

@ -681,7 +681,7 @@ static int address_acquire(Link *link, Address *original, Address **ret) {
/* The address is configured to be 0.0.0.0 or [::] by the user?
* Then let's acquire something more useful from the pool. */
r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
r = address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
if (r < 0)
return r;
if (r == 0)

View file

@ -48,33 +48,6 @@
/* use 128 MB for receive socket kernel queue. */
#define RCVBUF_SIZE (128*1024*1024)
static int setup_default_address_pool(Manager *m) {
AddressPool *p;
int r;
assert(m);
/* Add in the well-known private address ranges. */
r = address_pool_new_from_string(m, &p, AF_INET6, "fd00::", 8);
if (r < 0)
return r;
r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8);
if (r < 0)
return r;
r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12);
if (r < 0)
return r;
r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16);
if (r < 0)
return r;
return 0;
}
static int manager_reset_all(Manager *m) {
Link *link;
int r;
@ -868,7 +841,7 @@ int manager_new(Manager **ret) {
if (r < 0)
return r;
r = setup_default_address_pool(m);
r = address_pool_setup_default(m);
if (r < 0)
return r;
@ -1136,26 +1109,6 @@ int manager_enumerate(Manager *m) {
return 0;
}
int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
AddressPool *p;
int r;
assert(m);
assert(prefixlen > 0);
assert(found);
LIST_FOREACH(address_pools, p, m->address_pools) {
if (p->family != family)
continue;
r = address_pool_acquire(p, prefixlen, found);
if (r != 0)
return r;
}
return 0;
}
Link* manager_find_uplink(Manager *m, Link *exclude) {
_cleanup_free_ struct local_address *gateways = NULL;
int n, i;

View file

@ -86,8 +86,6 @@ int manager_enumerate(Manager *m);
void manager_dirty(Manager *m);
int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found);
Link* manager_find_uplink(Manager *m, Link *exclude);
int manager_set_hostname(Manager *m, const char *hostname);