mirror of
https://github.com/systemd/systemd.git
synced 2024-11-01 17:51:22 +03:00
resolved: unify DnsServer handling code between Link and Manager
This copies concepts we introduced for the DnsSearchDomain stuff, and reworks the operations on lists of dns servers to be reusable and generic for use both with the Link and the Manager object.
This commit is contained in:
parent
a51c10485a
commit
4b95f1798f
@ -40,7 +40,7 @@ int manager_add_dns_server_by_string(Manager *m, DnsServerType type, const char
|
||||
return r;
|
||||
|
||||
/* Filter out duplicates */
|
||||
s = manager_find_dns_server(m, type, family, &address);
|
||||
s = dns_server_find(manager_get_first_dns_server(m, type), family, &address);
|
||||
if (s) {
|
||||
/*
|
||||
* Drop the marker. This is used to find the servers
|
||||
@ -141,7 +141,7 @@ int config_parse_dns_servers(
|
||||
|
||||
if (isempty(rvalue))
|
||||
/* Empty assignment means clear the list */
|
||||
manager_flush_dns_servers(m, ltype);
|
||||
dns_server_unlink_all(manager_get_first_dns_server(m, ltype));
|
||||
else {
|
||||
/* Otherwise, add to the list */
|
||||
r = manager_parse_dns_server_string_and_warn(m, ltype, rvalue);
|
||||
|
@ -240,6 +240,50 @@ const struct hash_ops dns_server_hash_ops = {
|
||||
.compare = dns_server_compare_func
|
||||
};
|
||||
|
||||
void dns_server_unlink_all(DnsServer *first) {
|
||||
DnsServer *next;
|
||||
|
||||
if (!first)
|
||||
return;
|
||||
|
||||
next = first->servers_next;
|
||||
dns_server_unlink(first);
|
||||
|
||||
dns_server_unlink_all(next);
|
||||
}
|
||||
|
||||
void dns_server_unlink_marked(DnsServer *first) {
|
||||
DnsServer *next;
|
||||
|
||||
if (!first)
|
||||
return;
|
||||
|
||||
next = first->servers_next;
|
||||
|
||||
if (first->marked)
|
||||
dns_server_unlink(first);
|
||||
|
||||
dns_server_unlink_marked(next);
|
||||
}
|
||||
|
||||
void dns_server_mark_all(DnsServer *first) {
|
||||
if (!first)
|
||||
return;
|
||||
|
||||
first->marked = true;
|
||||
dns_server_mark_all(first->servers_next);
|
||||
}
|
||||
|
||||
DnsServer *dns_server_find(DnsServer *first, int family, const union in_addr_union *in_addr) {
|
||||
DnsServer *s;
|
||||
|
||||
LIST_FOREACH(servers, s, first)
|
||||
if (s->family == family && in_addr_equal(family, &s->address, in_addr) > 0)
|
||||
return s;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DnsServer *manager_get_first_dns_server(Manager *m, DnsServerType t) {
|
||||
assert(m);
|
||||
|
||||
@ -256,60 +300,6 @@ DnsServer *manager_get_first_dns_server(Manager *m, DnsServerType t) {
|
||||
}
|
||||
}
|
||||
|
||||
void manager_flush_dns_servers(Manager *m, DnsServerType type) {
|
||||
assert(m);
|
||||
|
||||
for (;;) {
|
||||
DnsServer *first;
|
||||
|
||||
first = manager_get_first_dns_server(m, type);
|
||||
if (!first)
|
||||
break;
|
||||
|
||||
dns_server_unlink(first);
|
||||
}
|
||||
}
|
||||
|
||||
void manager_flush_marked_dns_servers(Manager *m, DnsServerType type) {
|
||||
DnsServer *first, *s, *next;
|
||||
|
||||
assert(m);
|
||||
|
||||
first = manager_get_first_dns_server(m, type);
|
||||
|
||||
LIST_FOREACH_SAFE(servers, s, next, first) {
|
||||
if (!s->marked)
|
||||
continue;
|
||||
|
||||
dns_server_unlink(s);
|
||||
}
|
||||
}
|
||||
|
||||
void manager_mark_dns_servers(Manager *m, DnsServerType type) {
|
||||
DnsServer *first, *s;
|
||||
|
||||
assert(m);
|
||||
|
||||
first = manager_get_first_dns_server(m, type);
|
||||
LIST_FOREACH(servers, s, first)
|
||||
s->marked = true;
|
||||
}
|
||||
|
||||
DnsServer* manager_find_dns_server(Manager *m, DnsServerType type, int family, const union in_addr_union *in_addr) {
|
||||
DnsServer *first, *s;
|
||||
|
||||
assert(m);
|
||||
assert(in_addr);
|
||||
|
||||
first = manager_get_first_dns_server(m, type);
|
||||
|
||||
LIST_FOREACH(servers, s, first)
|
||||
if (s->family == family && in_addr_equal(family, &s->address, in_addr) > 0)
|
||||
return s;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DnsServer *manager_set_dns_server(Manager *m, DnsServer *s) {
|
||||
assert(m);
|
||||
|
||||
|
@ -72,14 +72,15 @@ void dns_server_move_back_and_unmark(DnsServer *s);
|
||||
void dns_server_packet_received(DnsServer *s, usec_t rtt);
|
||||
void dns_server_packet_lost(DnsServer *s, usec_t usec);
|
||||
|
||||
DnsServer *dns_server_find(DnsServer *first, int family, const union in_addr_union *in_addr);
|
||||
|
||||
void dns_server_unlink_all(DnsServer *first);
|
||||
void dns_server_unlink_marked(DnsServer *first);
|
||||
void dns_server_mark_all(DnsServer *first);
|
||||
|
||||
DnsServer *manager_get_first_dns_server(Manager *m, DnsServerType t);
|
||||
|
||||
void manager_flush_dns_servers(Manager *m, DnsServerType t);
|
||||
void manager_flush_marked_dns_servers(Manager *m, DnsServerType type);
|
||||
void manager_mark_dns_servers(Manager *m, DnsServerType type);
|
||||
|
||||
DnsServer *manager_set_dns_server(Manager *m, DnsServer *s);
|
||||
DnsServer *manager_find_dns_server(Manager *m, DnsServerType t, int family, const union in_addr_union *in_addr);
|
||||
DnsServer *manager_get_dns_server(Manager *m);
|
||||
void manager_next_dns_server(Manager *m);
|
||||
|
||||
|
@ -65,7 +65,7 @@ Link *link_free(Link *l) {
|
||||
if (!l)
|
||||
return NULL;
|
||||
|
||||
link_flush_dns_servers(l);
|
||||
dns_server_unlink_marked(l->dns_servers);
|
||||
dns_search_domain_unlink_all(l->search_domains);
|
||||
|
||||
while (l->addresses)
|
||||
@ -162,7 +162,7 @@ static int link_update_dns_servers(Link *l) {
|
||||
if (r < 0)
|
||||
goto clear;
|
||||
|
||||
link_mark_dns_servers(l);
|
||||
dns_server_mark_all(l->dns_servers);
|
||||
|
||||
STRV_FOREACH(nameserver, nameservers) {
|
||||
union in_addr_union a;
|
||||
@ -173,7 +173,7 @@ static int link_update_dns_servers(Link *l) {
|
||||
if (r < 0)
|
||||
goto clear;
|
||||
|
||||
s = link_find_dns_server(l, family, &a);
|
||||
s = dns_server_find(l->dns_servers, family, &a);
|
||||
if (s)
|
||||
dns_server_move_back_and_unmark(s);
|
||||
else {
|
||||
@ -183,11 +183,11 @@ static int link_update_dns_servers(Link *l) {
|
||||
}
|
||||
}
|
||||
|
||||
link_flush_marked_dns_servers(l);
|
||||
dns_server_unlink_marked(l->dns_servers);
|
||||
return 0;
|
||||
|
||||
clear:
|
||||
link_flush_dns_servers(l);
|
||||
dns_server_unlink_all(l->dns_servers);
|
||||
return r;
|
||||
}
|
||||
|
||||
@ -314,47 +314,6 @@ LinkAddress *link_find_address(Link *l, int family, const union in_addr_union *i
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void link_flush_dns_servers(Link *l) {
|
||||
assert(l);
|
||||
|
||||
while (l->dns_servers)
|
||||
dns_server_unlink(l->dns_servers);
|
||||
}
|
||||
|
||||
void link_flush_marked_dns_servers(Link *l) {
|
||||
DnsServer *s, *next;
|
||||
|
||||
assert(l);
|
||||
|
||||
LIST_FOREACH_SAFE(servers, s, next, l->dns_servers) {
|
||||
if (!s->marked)
|
||||
continue;
|
||||
|
||||
dns_server_unlink(s);
|
||||
}
|
||||
}
|
||||
|
||||
void link_mark_dns_servers(Link *l) {
|
||||
DnsServer *s;
|
||||
|
||||
assert(l);
|
||||
|
||||
LIST_FOREACH(servers, s, l->dns_servers)
|
||||
s->marked = true;
|
||||
}
|
||||
|
||||
DnsServer* link_find_dns_server(Link *l, int family, const union in_addr_union *in_addr) {
|
||||
DnsServer *s;
|
||||
|
||||
assert(l);
|
||||
assert(in_addr);
|
||||
|
||||
LIST_FOREACH(servers, s, l->dns_servers)
|
||||
if (s->family == family && in_addr_equal(family, &s->address, in_addr))
|
||||
return s;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DnsServer* link_set_dns_server(Link *l, DnsServer *s) {
|
||||
assert(l);
|
||||
|
||||
|
@ -79,17 +79,10 @@ bool link_relevant(Link *l, int family);
|
||||
LinkAddress* link_find_address(Link *l, int family, const union in_addr_union *in_addr);
|
||||
void link_add_rrs(Link *l, bool force_remove);
|
||||
|
||||
void link_flush_dns_servers(Link *l);
|
||||
void link_flush_marked_dns_servers(Link *l);
|
||||
void link_mark_dns_servers(Link *l);
|
||||
|
||||
DnsServer* link_set_dns_server(Link *l, DnsServer *s);
|
||||
DnsServer* link_find_dns_server(Link *l, int family, const union in_addr_union *in_addr);
|
||||
DnsServer* link_get_dns_server(Link *l);
|
||||
void link_next_dns_server(Link *l);
|
||||
|
||||
void link_flush_search_domains(Link *l);
|
||||
|
||||
int link_address_new(Link *l, LinkAddress **ret, int family, const union in_addr_union *in_addr);
|
||||
LinkAddress *link_address_free(LinkAddress *a);
|
||||
int link_address_update_rtnl(LinkAddress *a, sd_netlink_message *m);
|
||||
|
@ -533,9 +533,8 @@ Manager *manager_free(Manager *m) {
|
||||
if (!m)
|
||||
return NULL;
|
||||
|
||||
manager_flush_dns_servers(m, DNS_SERVER_SYSTEM);
|
||||
manager_flush_dns_servers(m, DNS_SERVER_FALLBACK);
|
||||
|
||||
dns_server_unlink_all(m->dns_servers);
|
||||
dns_server_unlink_all(m->fallback_dns_servers);
|
||||
dns_search_domain_unlink_all(m->search_domains);
|
||||
|
||||
while ((l = hashmap_first(m->links)))
|
||||
|
@ -85,7 +85,7 @@ int manager_read_resolv_conf(Manager *m) {
|
||||
goto clear;
|
||||
}
|
||||
|
||||
manager_mark_dns_servers(m, DNS_SERVER_SYSTEM);
|
||||
dns_server_mark_all(m->dns_servers);
|
||||
dns_search_domain_mark_all(m->search_domains);
|
||||
|
||||
FOREACH_LINE(line, f, r = -errno; goto clear) {
|
||||
@ -118,7 +118,7 @@ int manager_read_resolv_conf(Manager *m) {
|
||||
/* Flush out all servers and search domains that are still
|
||||
* marked. Those are then ones that didn't appear in the new
|
||||
* /etc/resolv.conf */
|
||||
manager_flush_marked_dns_servers(m, DNS_SERVER_SYSTEM);
|
||||
dns_server_unlink_marked(m->dns_servers);
|
||||
dns_search_domain_unlink_marked(m->search_domains);
|
||||
|
||||
/* Whenever /etc/resolv.conf changes, start using the first
|
||||
@ -134,7 +134,7 @@ int manager_read_resolv_conf(Manager *m) {
|
||||
return 0;
|
||||
|
||||
clear:
|
||||
manager_flush_dns_servers(m, DNS_SERVER_SYSTEM);
|
||||
dns_server_unlink_all(m->dns_servers);
|
||||
dns_search_domain_unlink_all(m->search_domains);
|
||||
return r;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user