resolved-dns-server.c revision 0b58db658b5c3f586ac3a837427f1f7fec2abb2e
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "resolved-dns-server.h"
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering#include "resolved-resolv-conf.h"
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "siphash24.h"
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering#include "string-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen/* After how much time to repeat classic DNS requests */
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen#define DNS_TIMEOUT_MIN_USEC (500 * USEC_PER_MSEC)
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen#define DNS_TIMEOUT_MAX_USEC (5 * USEC_PER_SEC)
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_server_new(
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Manager *m,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DnsServer **ret,
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering DnsServerType type,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Link *l,
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering int family,
3c0cf502796be355431d4a64d738e75f543aa51dLennart Poettering const union in_addr_union *in_addr) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DnsServer *s, *tail;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(m);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering assert((type == DNS_SERVER_LINK) == !!l);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(in_addr);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s = new0(DnsServer, 1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!s)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -ENOMEM;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen s->n_ref = 1;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering s->manager = m;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering s->type = type;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s->family = family;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering s->address = *in_addr;
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen s->resend_timeout = DNS_TIMEOUT_MIN_USEC;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering switch (type) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering case DNS_SERVER_LINK:
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering s->link = l;
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen LIST_FIND_TAIL(servers, l->dns_servers, tail);
6073b6f26ab9fc6bf335faa7073ec443eef093fdTom Gundersen LIST_INSERT_AFTER(servers, l->dns_servers, tail, s);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering break;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering case DNS_SERVER_SYSTEM:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_FIND_TAIL(servers, m->dns_servers, tail);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_INSERT_AFTER(servers, m->dns_servers, tail, s);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering break;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering case DNS_SERVER_FALLBACK:
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering LIST_FIND_TAIL(servers, m->fallback_dns_servers, tail);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering LIST_INSERT_AFTER(servers, m->fallback_dns_servers, tail, s);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering break;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering default:
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering assert_not_reached("Unknown server type");
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering s->linked = true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering /* A new DNS server that isn't fallback is added and the one
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering * we used so far was a fallback one? Then let's try to pick
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering * the new one */
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering if (type != DNS_SERVER_FALLBACK &&
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering m->current_dns_server &&
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering m->current_dns_server->type == DNS_SERVER_FALLBACK)
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering manager_set_dns_server(m, NULL);
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (ret)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *ret = s;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom GundersenDnsServer* dns_server_ref(DnsServer *s) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!s)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen assert(s->n_ref > 0);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen s->n_ref ++;
cab5b05903096e1c9cf5575ccc73f89d15c8db69Lennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen return s;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen}
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart PoetteringDnsServer* dns_server_unref(DnsServer *s) {
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen if (!s)
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen return NULL;
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(s->n_ref > 0);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering s->n_ref --;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (s->n_ref > 0)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(s);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringvoid dns_server_unlink(DnsServer *s) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(s);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(s->manager);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering /* This removes the specified server from the linked list of
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering * servers, but any server might still stay around if it has
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering * refs, for example from an ongoing transaction. */
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (!s->linked)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering switch (s->type) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering case DNS_SERVER_LINK:
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(s->link);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering LIST_REMOVE(servers, s->link->dns_servers, s);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering break;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering case DNS_SERVER_SYSTEM:
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering LIST_REMOVE(servers, s->manager->dns_servers, s);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering break;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering case DNS_SERVER_FALLBACK:
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering LIST_REMOVE(servers, s->manager->fallback_dns_servers, s);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering break;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering s->linked = false;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (s->link && s->link->current_dns_server == s)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering link_set_dns_server(s->link, NULL);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (s->manager->current_dns_server == s)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering manager_set_dns_server(s->manager, NULL);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering dns_server_unref(s);
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen}
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poetteringvoid dns_server_move_back_and_unmark(DnsServer *s) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering DnsServer *tail;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert(s);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (!s->marked)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering s->marked = false;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering if (!s->linked || !s->servers_next)
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering return;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering /* Move us to the end of the list, so that the order is
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering * strictly kept, if we are not at the end anyway. */
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering switch (s->type) {
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering case DNS_SERVER_LINK:
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert(s->link);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering LIST_FIND_TAIL(servers, s, tail);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering LIST_REMOVE(servers, s->link->dns_servers, s);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering LIST_INSERT_AFTER(servers, s->link->dns_servers, tail, s);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering break;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering case DNS_SERVER_SYSTEM:
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering LIST_FIND_TAIL(servers, s, tail);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering LIST_REMOVE(servers, s->manager->dns_servers, s);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering LIST_INSERT_AFTER(servers, s->manager->dns_servers, tail, s);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering break;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering case DNS_SERVER_FALLBACK:
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering LIST_FIND_TAIL(servers, s, tail);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering LIST_REMOVE(servers, s->manager->fallback_dns_servers, s);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering LIST_INSERT_AFTER(servers, s->manager->fallback_dns_servers, tail, s);
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering break;
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering default:
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering assert_not_reached("Unknown server type");
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering }
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering}
0b58db658b5c3f586ac3a837427f1f7fec2abb2eLennart Poettering
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersenvoid dns_server_packet_received(DnsServer *s, usec_t rtt) {
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen assert(s);
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering if (rtt <= s->max_rtt)
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering return;
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering s->max_rtt = rtt;
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering s->resend_timeout = MIN(MAX(DNS_TIMEOUT_MIN_USEC, s->max_rtt * 2), DNS_TIMEOUT_MAX_USEC);
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen}
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersenvoid dns_server_packet_lost(DnsServer *s, usec_t usec) {
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen assert(s);
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering if (s->resend_timeout > usec)
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering return;
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering
84129d46cd6e95e142973da93aede4c7433c9600Lennart Poettering s->resend_timeout = MIN(s->resend_timeout * 2, DNS_TIMEOUT_MAX_USEC);
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen}
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersenstatic void dns_server_hash_func(const void *p, struct siphash *state) {
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering const DnsServer *s = p;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen assert(s);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen siphash24_compress(&s->family, sizeof(s->family), state);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen siphash24_compress(&s->address, FAMILY_ADDRESS_SIZE(s->family), state);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering}
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidtstatic int dns_server_compare_func(const void *a, const void *b) {
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering const DnsServer *x = a, *y = b;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering if (x->family < y->family)
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return -1;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering if (x->family > y->family)
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return 1;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family));
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering}
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidtconst struct hash_ops dns_server_hash_ops = {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt .hash = dns_server_hash_func,
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt .compare = dns_server_compare_func
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt};
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart PoetteringDnsServer *manager_get_first_dns_server(Manager *m, DnsServerType t) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering assert(m);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering switch (t) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering case DNS_SERVER_SYSTEM:
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return m->dns_servers;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering case DNS_SERVER_FALLBACK:
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return m->fallback_dns_servers;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering default:
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering return NULL;
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering }
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering}
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poetteringvoid manager_flush_dns_servers(Manager *m, DnsServerType type) {
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering assert(m);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering for (;;) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering DnsServer *first;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering first = manager_get_first_dns_server(m, type);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (!first)
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering break;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering dns_server_unlink(first);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering }
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering}
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poetteringvoid manager_flush_marked_dns_servers(Manager *m, DnsServerType type) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering DnsServer *first, *s, *next;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering assert(m);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering first = manager_get_first_dns_server(m, type);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering LIST_FOREACH_SAFE(servers, s, next, first) {
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering if (!s->marked)
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering continue;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering dns_server_unlink(s);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering }
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering}
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poetteringvoid manager_mark_dns_servers(Manager *m, DnsServerType type) {
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering DnsServer *first, *s;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering assert(m);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering first = manager_get_first_dns_server(m, type);
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering LIST_FOREACH(servers, s, first)
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering s->marked = true;
636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2Lennart Poettering}
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart PoetteringDnsServer* manager_find_dns_server(Manager *m, DnsServerType type, int family, const union in_addr_union *in_addr) {
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering DnsServer *first, *s;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering assert(m);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering assert(in_addr);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering first = manager_get_first_dns_server(m, type);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering LIST_FOREACH(servers, s, first)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (s->family == family && in_addr_equal(family, &s->address, in_addr) > 0)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return s;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return NULL;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering}
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart PoetteringDnsServer *manager_set_dns_server(Manager *m, DnsServer *s) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering assert(m);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (m->current_dns_server == s)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return s;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (s) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering _cleanup_free_ char *ip = NULL;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering in_addr_to_string(s->family, &s->address, &ip);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering log_info("Switching to system DNS server %s.", strna(ip));
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering dns_server_unref(m->current_dns_server);
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering m->current_dns_server = dns_server_ref(s);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (m->unicast_scope)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering dns_cache_flush(&m->unicast_scope->cache);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return s;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering}
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart PoetteringDnsServer *manager_get_dns_server(Manager *m) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering Link *l;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering assert(m);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering /* Try to read updates resolv.conf */
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering manager_read_resolv_conf(m);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering /* If no DNS server was chose so far, pick the first one */
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (!m->current_dns_server)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering manager_set_dns_server(m, m->dns_servers);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (!m->current_dns_server) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering bool found = false;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering Iterator i;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering /* No DNS servers configured, let's see if there are
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering * any on any links. If not, we use the fallback
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering * servers */
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering HASHMAP_FOREACH(l, m->links, i)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (l->dns_servers) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering found = true;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering break;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (!found)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering manager_set_dns_server(m, m->fallback_dns_servers);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return m->current_dns_server;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering}
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poetteringvoid manager_next_dns_server(Manager *m) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering assert(m);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering /* If there's currently no DNS server set, then the next
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering * manager_get_dns_server() will find one */
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (!m->current_dns_server)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering /* Change to the next one, but make sure to follow the linked
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering * list only if the server is still linked. */
0eac462399c8e87bcce252cf058eba9f2678f2bdLennart Poettering if (m->current_dns_server->linked && m->current_dns_server->servers_next) {
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering manager_set_dns_server(m, m->current_dns_server->servers_next);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering return;
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering }
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering /* If there was no next one, then start from the beginning of
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering * the list */
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering if (m->current_dns_server->type == DNS_SERVER_FALLBACK)
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering manager_set_dns_server(m, m->fallback_dns_servers);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering else
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering manager_set_dns_server(m, m->dns_servers);
f2f1dbe50fea13abadc9c1e845a29031b90b40f3Lennart Poettering}