From 0b58db658b5c3f586ac3a837427f1f7fec2abb2e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 24 Nov 2015 20:50:37 +0100 Subject: resolved: make sure order of dns servers is stable Previously, we'd keep adding new dns servers we discover to the end of our linked list of servers. When we encountered a pre-existing server, we'd just leave it where it was. In essence that meant that old servers ended up at the front, and new servers at the end, but not in an order that would reflect the configuration. With this change we ensure that every pre-existing server we want to add again we move to the back of the linked list, so that the order is stable and in sync with the requested configuration. --- src/resolve/resolved-conf.c | 2 +- src/resolve/resolved-dns-server.c | 63 +++++++++++++++++++++++++++++++++++---- src/resolve/resolved-dns-server.h | 7 +++-- src/resolve/resolved-link.c | 2 +- 4 files changed, 63 insertions(+), 11 deletions(-) (limited to 'src/resolve') diff --git a/src/resolve/resolved-conf.c b/src/resolve/resolved-conf.c index 7e3b613816..c11a8badd9 100644 --- a/src/resolve/resolved-conf.c +++ b/src/resolve/resolved-conf.c @@ -48,7 +48,7 @@ int manager_add_dns_server_by_string(Manager *m, DnsServerType type, const char * manager_mark_dns_servers() and * manager_flush_marked_dns_servers(). */ - s->marked = false; + dns_server_move_back_and_unmark(s); return 0; } diff --git a/src/resolve/resolved-dns-server.c b/src/resolve/resolved-dns-server.c index 371594c710..81301ab800 100644 --- a/src/resolve/resolved-dns-server.c +++ b/src/resolve/resolved-dns-server.c @@ -48,25 +48,34 @@ int dns_server_new( return -ENOMEM; s->n_ref = 1; + s->manager = m; s->type = type; s->family = family; s->address = *in_addr; s->resend_timeout = DNS_TIMEOUT_MIN_USEC; - if (type == DNS_SERVER_LINK) { + switch (type) { + + case DNS_SERVER_LINK: + s->link = l; LIST_FIND_TAIL(servers, l->dns_servers, tail); LIST_INSERT_AFTER(servers, l->dns_servers, tail, s); - s->link = l; - } else if (type == DNS_SERVER_SYSTEM) { + break; + + case DNS_SERVER_SYSTEM: LIST_FIND_TAIL(servers, m->dns_servers, tail); LIST_INSERT_AFTER(servers, m->dns_servers, tail, s); - } else if (type == DNS_SERVER_FALLBACK) { + break; + + case DNS_SERVER_FALLBACK: LIST_FIND_TAIL(servers, m->fallback_dns_servers, tail); LIST_INSERT_AFTER(servers, m->fallback_dns_servers, tail, s); - } else + break; + + default: assert_not_reached("Unknown server type"); + } - s->manager = m; s->linked = true; /* A new DNS server that isn't fallback is added and the one @@ -145,6 +154,48 @@ void dns_server_unlink(DnsServer *s) { dns_server_unref(s); } +void dns_server_move_back_and_unmark(DnsServer *s) { + DnsServer *tail; + + assert(s); + + if (!s->marked) + return; + + s->marked = false; + + if (!s->linked || !s->servers_next) + return; + + /* Move us to the end of the list, so that the order is + * strictly kept, if we are not at the end anyway. */ + + switch (s->type) { + + case DNS_SERVER_LINK: + assert(s->link); + LIST_FIND_TAIL(servers, s, tail); + LIST_REMOVE(servers, s->link->dns_servers, s); + LIST_INSERT_AFTER(servers, s->link->dns_servers, tail, s); + break; + + case DNS_SERVER_SYSTEM: + LIST_FIND_TAIL(servers, s, tail); + LIST_REMOVE(servers, s->manager->dns_servers, s); + LIST_INSERT_AFTER(servers, s->manager->dns_servers, tail, s); + break; + + case DNS_SERVER_FALLBACK: + LIST_FIND_TAIL(servers, s, tail); + LIST_REMOVE(servers, s->manager->fallback_dns_servers, s); + LIST_INSERT_AFTER(servers, s->manager->fallback_dns_servers, tail, s); + break; + + default: + assert_not_reached("Unknown server type"); + } +} + void dns_server_packet_received(DnsServer *s, usec_t rtt) { assert(s); diff --git a/src/resolve/resolved-dns-server.h b/src/resolve/resolved-dns-server.h index 9fed8f74a2..0077456cbc 100644 --- a/src/resolve/resolved-dns-server.h +++ b/src/resolve/resolved-dns-server.h @@ -31,6 +31,7 @@ typedef enum DnsServerType { DNS_SERVER_LINK, } DnsServerType; +#include "resolved-manager.h" #include "resolved-link.h" struct DnsServer { @@ -39,7 +40,6 @@ struct DnsServer { unsigned n_ref; DnsServerType type; - Link *link; int family; @@ -57,9 +57,9 @@ struct DnsServer { int dns_server_new( Manager *m, - DnsServer **s, + DnsServer **ret, DnsServerType type, - Link *l, + Link *link, int family, const union in_addr_union *address); @@ -67,6 +67,7 @@ DnsServer* dns_server_ref(DnsServer *s); DnsServer* dns_server_unref(DnsServer *s); void dns_server_unlink(DnsServer *s); +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); diff --git a/src/resolve/resolved-link.c b/src/resolve/resolved-link.c index ef3db773fd..4931fc9d3b 100644 --- a/src/resolve/resolved-link.c +++ b/src/resolve/resolved-link.c @@ -174,7 +174,7 @@ static int link_update_dns_servers(Link *l) { s = link_find_dns_server(l, family, &a); if (s) - s->marked = false; + dns_server_move_back_and_unmark(s); else { r = dns_server_new(l->manager, NULL, DNS_SERVER_LINK, l, family, &a); if (r < 0) -- cgit v1.2.3-54-g00ecf