]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: address: introduce link_get_ipv4/ipv6_address()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 14 May 2021 08:33:32 +0000 (17:33 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 18 May 2021 11:20:24 +0000 (20:20 +0900)
src/network/networkd-address.c
src/network/networkd-address.h
src/network/networkd-ndisc.c

index c350b6e93312bc3f722f8374bf05b571e5ad8908..462907439a9455ed4c73d17a835ef5e09d1cb1c1 100644 (file)
@@ -489,7 +489,7 @@ int address_get(Link *link, const Address *in, Address **ret) {
         return -ENOENT;
 }
 
-int link_has_ipv6_address(Link *link, const struct in6_addr *address) {
+int link_get_ipv6_address(Link *link, const struct in6_addr *address, Address **ret) {
         _cleanup_(address_freep) Address *a = NULL;
         int r;
 
@@ -505,10 +505,10 @@ int link_has_ipv6_address(Link *link, const struct in6_addr *address) {
         a->family = AF_INET6;
         a->in_addr.in6 = *address;
 
-        return address_get(link, a, NULL) >= 0;
+        return address_get(link, a, ret);
 }
 
-static int link_get_ipv4_address(Set *addresses, const struct in_addr *address, Address **ret) {
+static int addresses_get_ipv4_address(Set *addresses, const struct in_addr *address, Address **ret) {
         Address *a;
 
         assert(address);
@@ -529,7 +529,35 @@ static int link_get_ipv4_address(Set *addresses, const struct in_addr *address,
         return -ENOENT;
 }
 
+int link_get_ipv4_address(Link *link, const struct in_addr *address, unsigned char prefixlen, Address **ret) {
+        int r;
+
+        assert(link);
+        assert(address);
+
+        if (prefixlen != 0) {
+                _cleanup_(address_freep) Address *a = NULL;
+
+                /* If prefixlen is set, then we can use address_get(). */
+
+                r = address_new(&a);
+                if (r < 0)
+                        return r;
+
+                a->family = AF_INET;
+                a->in_addr.in = *address;
+                a->prefixlen = prefixlen;
+
+                return address_get(link, a, ret);
+        }
+
+        if (addresses_get_ipv4_address(link->addresses, address, ret) >= 0)
+                return 0;
+        return addresses_get_ipv4_address(link->addresses_foreign, address, ret);
+}
+
 int manager_has_address(Manager *manager, int family, const union in_addr_union *address, bool check_ready) {
+        Address *a;
         Link *link;
         int r;
 
@@ -537,18 +565,12 @@ int manager_has_address(Manager *manager, int family, const union in_addr_union
         assert(IN_SET(family, AF_INET, AF_INET6));
         assert(address);
 
-        if (family == AF_INET)
-                HASHMAP_FOREACH(link, manager->links) {
-                        Address *a;
-
-                        if (link_get_ipv4_address(link->addresses, &address->in, &a) >= 0)
-                                return !check_ready || address_is_ready(a);
-                        if (link_get_ipv4_address(link->addresses_foreign, &address->in, &a) >= 0)
+        if (family == AF_INET) {
+                HASHMAP_FOREACH(link, manager->links)
+                        if (link_get_ipv4_address(link, &address->in, 0, &a) >= 0)
                                 return !check_ready || address_is_ready(a);
-                }
-        else {
+        } else {
                 _cleanup_(address_freep) Address *tmp = NULL;
-                Address *a;
 
                 r = address_new(&tmp);
                 if (r < 0)
index 34dea4aa6455292017cc6ab01c5b3a49aa888531..f63848e2e7a14b3637a2226835564c61785a8b43 100644 (file)
@@ -66,7 +66,8 @@ DEFINE_NETWORK_SECTION_FUNCTIONS(Address, address_free);
 int link_drop_addresses(Link *link);
 int link_drop_foreign_addresses(Link *link);
 bool link_address_is_dynamic(const Link *link, const Address *address);
-int link_has_ipv6_address(Link *link, const struct in6_addr *address);
+int link_get_ipv6_address(Link *link, const struct in6_addr *address, Address **ret);
+int link_get_ipv4_address(Link *link, const struct in_addr *address, unsigned char prefixlen, Address **ret);
 int manager_has_address(Manager *manager, int family, const union in_addr_union *address, bool check_ready);
 
 void ipv4_dad_unref(Link *link);
index cfcf4cc36aa8bc0d563b1b4b1f1ec5582f36958c..56aa5ec8a77c18b0bc8374cb12fd27fcb81e92b6 100644 (file)
@@ -541,7 +541,7 @@ static int ndisc_router_process_default(Link *link, sd_ndisc_router *rt) {
         if (r < 0)
                 return log_link_error_errno(link, r, "Failed to get gateway address from RA: %m");
 
-        if (link_has_ipv6_address(link, &gateway) > 0) {
+        if (link_get_ipv6_address(link, &gateway, NULL) >= 0) {
                 if (DEBUG_LOGGING) {
                         _cleanup_free_ char *buffer = NULL;
 
@@ -918,7 +918,7 @@ static int ndisc_router_process_route(Link *link, sd_ndisc_router *rt) {
         if (r < 0)
                 return log_link_error_errno(link, r, "Failed to get gateway address from RA: %m");
 
-        if (link_has_ipv6_address(link, &gateway) > 0) {
+        if (link_get_ipv6_address(link, &gateway, NULL) >= 0) {
                 if (DEBUG_LOGGING) {
                         _cleanup_free_ char *buf = NULL;