diff options
author | Lennart Poettering <lennart@poettering.net> | 2015-11-24 17:03:12 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2015-11-25 21:58:37 +0100 |
commit | f2f1dbe50fea13abadc9c1e845a29031b90b40f3 (patch) | |
tree | f1ffeb91f88f74a9e320ea19b057e88f35babca0 /src/resolve/resolved-dns-server.c | |
parent | 84129d46cd6e95e142973da93aede4c7433c9600 (diff) |
resolved: move dns server picking code from resolved-manager.c to resolved-dns-server.c
Diffstat (limited to 'src/resolve/resolved-dns-server.c')
-rw-r--r-- | src/resolve/resolved-dns-server.c | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/resolve/resolved-dns-server.c b/src/resolve/resolved-dns-server.c index 8b88a3d690..207ec4c603 100644 --- a/src/resolve/resolved-dns-server.c +++ b/src/resolve/resolved-dns-server.c @@ -21,7 +21,9 @@ #include "alloc-util.h" #include "resolved-dns-server.h" +#include "resolved-resolv-conf.h" #include "siphash24.h" +#include "string-util.h" /* After how much time to repeat classic DNS requests */ #define DNS_TIMEOUT_MIN_USEC (500 * USEC_PER_MSEC) @@ -205,3 +207,95 @@ void manager_mark_dns_servers(Manager *m, DnsServerType type) { LIST_FOREACH(servers, s, first) s->marked = true; } + +DnsServer* manager_find_dns_server(Manager *m, int family, const union in_addr_union *in_addr) { + DnsServer *s; + + assert(m); + assert(in_addr); + + LIST_FOREACH(servers, s, m->dns_servers) + if (s->family == family && in_addr_equal(family, &s->address, in_addr) > 0) + return s; + + LIST_FOREACH(servers, s, m->fallback_dns_servers) + 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); + + if (m->current_dns_server == s) + return s; + + if (s) { + _cleanup_free_ char *ip = NULL; + + in_addr_to_string(s->family, &s->address, &ip); + log_info("Switching to system DNS server %s.", strna(ip)); + } + + m->current_dns_server = s; + + if (m->unicast_scope) + dns_cache_flush(&m->unicast_scope->cache); + + return s; +} + +DnsServer *manager_get_dns_server(Manager *m) { + Link *l; + assert(m); + + /* Try to read updates resolv.conf */ + manager_read_resolv_conf(m); + + /* If no DNS server was chose so far, pick the first one */ + if (!m->current_dns_server) + manager_set_dns_server(m, m->dns_servers); + + if (!m->current_dns_server) { + bool found = false; + Iterator i; + + /* No DNS servers configured, let's see if there are + * any on any links. If not, we use the fallback + * servers */ + + HASHMAP_FOREACH(l, m->links, i) + if (l->dns_servers) { + found = true; + break; + } + + if (!found) + manager_set_dns_server(m, m->fallback_dns_servers); + } + + return m->current_dns_server; +} + +void manager_next_dns_server(Manager *m) { + assert(m); + + /* If there's currently no DNS server set, then the next + * manager_get_dns_server() will find one */ + if (!m->current_dns_server) + return; + + /* Change to the next one */ + if (m->current_dns_server->servers_next) { + manager_set_dns_server(m, m->current_dns_server->servers_next); + return; + } + + /* If there was no next one, then start from the beginning of + * the list */ + if (m->current_dns_server->type == DNS_SERVER_FALLBACK) + manager_set_dns_server(m, m->fallback_dns_servers); + else + manager_set_dns_server(m, m->dns_servers); +} |