]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: move functions related to address pool
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 2 Oct 2020 05:15:57 +0000 (14:15 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 6 Oct 2020 17:50:50 +0000 (02:50 +0900)
src/network/networkd-address-pool.c
src/network/networkd-address-pool.h
src/network/networkd-address.c
src/network/networkd-manager.c
src/network/networkd-manager.h

index 24a6bff2a1e3fe9593fbff3492e31ac0592da206..12ce45036ba5ef2cacd26b9090d43d03d0e7a7c4 100644 (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;
+}
index 7db1c4f26c17f6fee24a82b0c583ca25de107ef7..0c5cd5cedd35a42476a43bfeb3b3f1204d0a0b0c 100644 (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);
index 170adfbbf696e21f24c5446b8a659d6fcc040177..0efb3eafb2088863e009987cf166a2cd54b0fd43 100644 (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)
index 00f172bc90383c933cacbe28c339772fce909ae8..137424929fcf8a1000dfd8ff20e14aaa1ad7da51 100644 (file)
 /* 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;
index f95367660ab3ec0bfc1a5a53026c32075a06ce81..c4e44421e254789c86a03f9acdaad4a6e928fb2e 100644 (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);