]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: introduce address_exists() helper function
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 17 Jul 2020 20:42:59 +0000 (05:42 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 17 Jul 2020 20:51:41 +0000 (05:51 +0900)
src/network/networkd-address.c
src/network/networkd-address.h
src/network/networkd-ndisc.c

index 6f1ee3b534970e416cdb60b558f89926ced3dcb0..2c36854588d09960a7bb1a3fcdaf051f8a831c2e 100644 (file)
@@ -431,6 +431,32 @@ int address_get(Link *link,
         return -ENOENT;
 }
 
+static bool address_exists_internal(Set *addresses, int family, const union in_addr_union *in_addr) {
+        Address *address;
+        Iterator i;
+
+        SET_FOREACH(address, addresses, i) {
+                if (address->family != family)
+                        continue;
+                if (in_addr_equal(address->family, &address->in_addr, in_addr))
+                        return true;
+        }
+
+        return false;
+}
+
+bool address_exists(Link *link, int family, const union in_addr_union *in_addr) {
+        assert(link);
+        assert(IN_SET(family, AF_INET, AF_INET6));
+        assert(in_addr);
+
+        if (address_exists_internal(link->addresses, family, in_addr))
+                return true;
+        if (address_exists_internal(link->addresses_foreign, family, in_addr))
+                return true;
+        return false;
+}
+
 static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
         int r;
 
index bd0485e0abee59f54e197a33d050398b13116f8c..d55059ee252edfbddf65a0fad5bb56bef51fd5de 100644 (file)
@@ -57,6 +57,7 @@ void address_free(Address *address);
 int address_add_foreign(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret);
 int address_add(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret);
 int address_get(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret);
+bool address_exists(Link *link, int family, const union in_addr_union *in_addr);
 int address_update(Address *address, unsigned char flags, unsigned char scope, const struct ifa_cacheinfo *cinfo);
 int address_drop(Address *address);
 int address_configure(Address *address, Link *link, link_netlink_message_handler_t callback, bool update);
index 52c315ac4e549f30539236de4c936c19ae5d637c..848c4ba7adc1ccc202f12b113ba16a87cf1ef592 100644 (file)
@@ -148,8 +148,6 @@ static int ndisc_router_process_default(Link *link, sd_ndisc_router *rt) {
         unsigned preference;
         uint32_t mtu;
         usec_t time_now;
-        Address *address;
-        Iterator i;
         int r;
 
         assert(link);
@@ -166,34 +164,15 @@ 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");
 
-        SET_FOREACH(address, link->addresses, i) {
-                if (address->family != AF_INET6)
-                        continue;
-                if (in_addr_equal(AF_INET6, &gateway, &address->in_addr)) {
-                        if (DEBUG_LOGGING) {
-                                _cleanup_free_ char *buffer = NULL;
-
-                                (void) in_addr_to_string(AF_INET6, &address->in_addr, &buffer);
-                                log_link_debug(link, "No NDisc route added, gateway %s matches local address",
-                                               strnull(buffer));
-                        }
-                        return 0;
-                }
-        }
-
-        SET_FOREACH(address, link->addresses_foreign, i) {
-                if (address->family != AF_INET6)
-                        continue;
-                if (in_addr_equal(AF_INET6, &gateway, &address->in_addr)) {
-                        if (DEBUG_LOGGING) {
-                                _cleanup_free_ char *buffer = NULL;
+        if (address_exists(link, AF_INET6, &gateway)) {
+                if (DEBUG_LOGGING) {
+                        _cleanup_free_ char *buffer = NULL;
 
-                                (void) in_addr_to_string(AF_INET6, &address->in_addr, &buffer);
-                                log_link_debug(link, "No NDisc route added, gateway %s matches local address",
-                                               strnull(buffer));
-                        }
-                        return 0;
+                        (void) in_addr_to_string(AF_INET6, &gateway, &buffer);
+                        log_link_debug(link, "No NDisc route added, gateway %s matches local address",
+                                       strnull(buffer));
                 }
+                return 0;
         }
 
         r = sd_ndisc_router_get_preference(rt, &preference);