network: use address_get() in address_exists()

And rename address_exists() to link_has_ipv6_address().
This commit is contained in:
Yu Watanabe 2020-12-04 17:29:16 +09:00
parent 1d30fc5cb6
commit c5a0aeb33a
3 changed files with 16 additions and 22 deletions

View File

@ -447,29 +447,23 @@ int address_get(Link *link, const Address *in, Address **ret) {
return -ENOENT; return -ENOENT;
} }
static bool address_exists_internal(Set *addresses, int family, const union in_addr_union *in_addr) { int link_has_ipv6_address(Link *link, const struct in6_addr *address) {
Address *address; _cleanup_(address_freep) Address *a = NULL;
int r;
SET_FOREACH(address, addresses) {
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(link);
assert(IN_SET(family, AF_INET, AF_INET6)); assert(address);
assert(in_addr);
if (address_exists_internal(link->addresses, family, in_addr)) r = address_new(&a);
return true; if (r < 0)
if (address_exists_internal(link->addresses_foreign, family, in_addr)) return r;
return true;
return false; /* address_compare_func() only compares the local address for IPv6 case. So, it is enough to
* set only family and the address. */
a->family = AF_INET6;
a->in_addr.in6 = *address;
return address_get(link, a, NULL) >= 0;
} }
static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {

View File

@ -49,7 +49,6 @@ typedef struct Address {
int address_new(Address **ret); int address_new(Address **ret);
Address *address_free(Address *address); Address *address_free(Address *address);
int address_get(Link *link, const Address *in, Address **ret); int address_get(Link *link, const Address *in, Address **ret);
bool address_exists(Link *link, int family, const union in_addr_union *in_addr);
int address_configure(const Address *address, Link *link, link_netlink_message_handler_t callback, bool update, Address **ret); int address_configure(const Address *address, Link *link, link_netlink_message_handler_t callback, bool update, Address **ret);
int address_remove(const Address *address, Link *link, link_netlink_message_handler_t callback); int address_remove(const Address *address, Link *link, link_netlink_message_handler_t callback);
bool address_equal(const Address *a1, const Address *a2); bool address_equal(const Address *a1, const Address *a2);
@ -63,6 +62,7 @@ int link_set_addresses(Link *link);
int link_drop_addresses(Link *link); int link_drop_addresses(Link *link);
int link_drop_foreign_addresses(Link *link); int link_drop_foreign_addresses(Link *link);
bool link_address_is_dynamic(const Link *link, const Address *address); bool link_address_is_dynamic(const Link *link, const Address *address);
int link_has_ipv6_address(Link *link, const struct in6_addr *address);
void ipv4_dad_unref(Link *link); void ipv4_dad_unref(Link *link);
int ipv4_dad_stop(Link *link); int ipv4_dad_stop(Link *link);

View File

@ -483,7 +483,7 @@ static int ndisc_router_process_default(Link *link, sd_ndisc_router *rt) {
if (r < 0) if (r < 0)
return log_link_error_errno(link, r, "Failed to get gateway address from RA: %m"); return log_link_error_errno(link, r, "Failed to get gateway address from RA: %m");
if (address_exists(link, AF_INET6, &gateway)) { if (link_has_ipv6_address(link, &gateway.in6) > 0) {
if (DEBUG_LOGGING) { if (DEBUG_LOGGING) {
_cleanup_free_ char *buffer = NULL; _cleanup_free_ char *buffer = NULL;