From: Yu Watanabe Date: Fri, 2 Oct 2020 05:15:57 +0000 (+0900) Subject: network: move functions related to address pool X-Git-Tag: v247-rc1~117^2~41 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ed76f585217bf544274d350512254d0c2e70d408;p=thirdparty%2Fsystemd.git network: move functions related to address pool --- diff --git a/src/network/networkd-address-pool.c b/src/network/networkd-address-pool.c index 24a6bff2a1e..12ce45036ba 100644 --- a/src/network/networkd-address-pool.c +++ b/src/network/networkd-address-pool.c @@ -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; +} diff --git a/src/network/networkd-address-pool.h b/src/network/networkd-address-pool.h index 7db1c4f26c1..0c5cd5cedd3 100644 --- a/src/network/networkd-address-pool.h +++ b/src/network/networkd-address-pool.h @@ -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); diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c index 170adfbbf69..0efb3eafb20 100644 --- a/src/network/networkd-address.c +++ b/src/network/networkd-address.c @@ -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) diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index 00f172bc903..137424929fc 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -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; diff --git a/src/network/networkd-manager.h b/src/network/networkd-manager.h index f95367660ab..c4e44421e25 100644 --- a/src/network/networkd-manager.h +++ b/src/network/networkd-manager.h @@ -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);