1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-20 18:04:03 +03:00

network: introduce address_exists() helper function

This commit is contained in:
Yu Watanabe 2020-07-18 05:42:59 +09:00
parent d15818f227
commit 5eec0a0810
3 changed files with 34 additions and 28 deletions

View 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;

View 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);

View 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;
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;
}
}
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;
(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);