#include "resolved-conf.h"
#include "string-util.h"
-int manager_parse_dns_server(Manager *m, DnsServerType type, const char *string) {
- DnsServer *first;
- int r;
+int manager_add_dns_server_by_string(Manager *m, DnsServerType type, const char *word) {
+ union in_addr_union address;
+ DnsServer *first, *s;
+ int family, r;
assert(m);
- assert(string);
+ assert(word);
+
+ r = in_addr_from_string_auto(word, &family, &address);
+ if (r < 0)
+ return r;
first = type == DNS_SERVER_FALLBACK ? m->fallback_dns_servers : m->dns_servers;
+ /* Filter out duplicates */
+ LIST_FOREACH(servers, s, first)
+ if (s->family == family && in_addr_equal(family, &s->address, &address))
+ break;
+
+ if (s) {
+ /*
+ * Drop the marker. This is used to find the servers
+ * that ceased to exist, see
+ * manager_mark_dns_servers() and
+ * manager_flush_marked_dns_servers().
+ */
+ s->marked = false;
+ return 0;
+ }
+
+ return dns_server_new(m, NULL, type, NULL, family, &address);
+}
+
+int manager_parse_dns_server_string_and_warn(Manager *m, DnsServerType type, const char *string) {
+ int r;
+
+ assert(m);
+ assert(string);
+
for(;;) {
_cleanup_free_ char *word = NULL;
- union in_addr_union addr;
- bool found = false;
- DnsServer *s;
- int family;
r = extract_first_word(&string, &word, NULL, 0);
if (r < 0)
- return log_error_errno(r, "Failed to parse resolved dns server syntax \"%s\": %m", string);
+ return r;
if (r == 0)
break;
- r = in_addr_from_string_auto(word, &family, &addr);
+ r = manager_add_dns_server_by_string(m, type, word);
if (r < 0) {
- log_warning("Ignoring invalid DNS address '%s'", word);
+ log_warning_errno(r, "Failed to add DNS server address '%s', ignoring.", word);
continue;
}
-
- /* Filter out duplicates */
- LIST_FOREACH(servers, s, first)
- if (s->family == family && in_addr_equal(family, &s->address, &addr)) {
- found = true;
- break;
- }
-
- if (found)
- continue;
-
- r = dns_server_new(m, NULL, type, NULL, family, &addr);
- if (r < 0)
- return r;
}
return 0;
}
-int config_parse_dnsv(
+int config_parse_dns_servers(
const char *unit,
const char *filename,
unsigned line,
manager_flush_dns_servers(m, ltype);
else {
/* Otherwise, add to the list */
- r = manager_parse_dns_server(m, ltype, rvalue);
+ r = manager_parse_dns_server_string_and_warn(m, ltype, rvalue);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse DNS server string '%s'. Ignoring.", rvalue);
return 0;
int manager_parse_dns_server(Manager *m, DnsServerType type, const char *string);
int manager_parse_config_file(Manager *m);
+int manager_parse_dns_server_string_and_warn(Manager *m, DnsServerType type, const char *string);
+int manager_add_dns_server_by_string(Manager *m, DnsServerType type, const char *word);
+
const struct ConfigPerfItem* resolved_gperf_lookup(const char *key, unsigned length);
-int config_parse_dnsv(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_dns_servers(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_support(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
.hash = dns_server_hash_func,
.compare = dns_server_compare_func
};
+
+void manager_flush_dns_servers(Manager *m, DnsServerType type) {
+ DnsServer **first, *s;
+
+ assert(m);
+
+ first = type == DNS_SERVER_FALLBACK ? &m->fallback_dns_servers : &m->dns_servers;
+
+ while (*first) {
+ s = *first;
+
+ LIST_REMOVE(servers, *first, s);
+ dns_server_unref(s);
+ }
+}
+
+void manager_flush_marked_dns_servers(Manager *m, DnsServerType type) {
+ DnsServer **first, *s, *next;
+
+ assert(m);
+
+ first = type == DNS_SERVER_FALLBACK ? &m->fallback_dns_servers : &m->dns_servers;
+
+ LIST_FOREACH_SAFE(servers, s, next, *first) {
+ if (!s->marked)
+ continue;
+
+ LIST_REMOVE(servers, *first, s);
+ dns_server_unref(s);
+ }
+}
+
+void manager_mark_dns_servers(Manager *m, DnsServerType type) {
+ DnsServer *first, *s;
+
+ assert(m);
+
+ first = type == DNS_SERVER_FALLBACK ? m->fallback_dns_servers : m->dns_servers;
+
+ LIST_FOREACH(servers, s, first)
+ s->marked = true;
+}
void dns_server_packet_received(DnsServer *s, usec_t rtt);
void dns_server_packet_lost(DnsServer *s, usec_t usec);
+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);
+
DEFINE_TRIVIAL_CLEANUP_FUNC(DnsServer*, dns_server_unref);
extern const struct hash_ops dns_server_hash_ops;
%struct-type
%includes
%%
-Resolve.DNS, config_parse_dnsv, DNS_SERVER_SYSTEM, 0
-Resolve.FallbackDNS, config_parse_dnsv, DNS_SERVER_FALLBACK, 0
-Resolve.LLMNR, config_parse_support, 0, offsetof(Manager, llmnr_support)
+Resolve.DNS, config_parse_dns_servers, DNS_SERVER_SYSTEM, 0
+Resolve.FallbackDNS, config_parse_dns_servers, DNS_SERVER_FALLBACK, 0
+Resolve.LLMNR, config_parse_support, 0, offsetof(Manager, llmnr_support)
m->llmnr_support = SUPPORT_YES;
m->read_resolv_conf = true;
- r = manager_parse_dns_server(m, DNS_SERVER_FALLBACK, DNS_SERVERS);
+ r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_FALLBACK, DNS_SERVERS);
if (r < 0)
return r;
_cleanup_fclose_ FILE *f = NULL;
struct stat st, own;
char line[LINE_MAX];
- DnsServer *s, *nx;
+ DnsServer *s;
usec_t t;
int r;
goto clear;
}
- LIST_FOREACH(servers, s, m->dns_servers)
- s->marked = true;
+ manager_mark_dns_servers(m, DNS_SERVER_SYSTEM);
FOREACH_LINE(line, f, r = -errno; goto clear) {
- union in_addr_union address;
- int family;
- char *l;
+ _cleanup_strv_free_ char **d = NULL;
const char *a;
+ char *l;
truncate_nl(line);
continue;
a = first_word(l, "nameserver");
- if (!a)
- continue;
+ if (a) {
+ r = manager_add_dns_server_by_string(m, DNS_SERVER_SYSTEM, a);
+ if (r < 0)
+ log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a);
- r = in_addr_from_string_auto(a, &family, &address);
- if (r < 0) {
- log_warning("Failed to parse name server %s.", a);
continue;
}
-
- LIST_FOREACH(servers, s, m->dns_servers)
- if (s->family == family && in_addr_equal(family, &s->address, &address) > 0)
- break;
-
- if (s)
- s->marked = false;
- else {
- r = dns_server_new(m, NULL, DNS_SERVER_SYSTEM, NULL, family, &address);
- if (r < 0)
- goto clear;
- }
}
- LIST_FOREACH_SAFE(servers, s, nx, m->dns_servers)
- if (s->marked) {
- LIST_REMOVE(servers, m->dns_servers, s);
- dns_server_unref(s);
- }
+ manager_flush_marked_dns_servers(m, DNS_SERVER_SYSTEM);
/* Whenever /etc/resolv.conf changes, start using the first
* DNS server of it. This is useful to deal with broken
if (*count == MAXNS)
fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f);
+ (*count) ++;
fprintf(f, "nameserver %s\n", t);
- (*count) ++;
}
static void write_resolv_conf_search(
- const char *domain, FILE *f,
+ const char *domain,
+ FILE *f,
unsigned *count,
unsigned *length) {
return;
}
- fprintf(f, " %s", domain);
-
(*length) += strlen(domain);
(*count) ++;
+
+ fputc(' ', f);
+ fputs(domain, f);
}
static int write_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
if (ordered_set_isempty(dns))
fputs("# No DNS servers known.\n", f);
else {
- DnsServer *s;
unsigned count = 0;
+ DnsServer *s;
ORDERED_SET_FOREACH(s, dns, i)
write_resolv_conf_server(s, f, &count);
if (!domains)
return -ENOMEM;
- /* First add the system-wide servers */
+ /* First add the system-wide servers and domains */
LIST_FOREACH(servers, s, m->dns_servers) {
r = ordered_set_put(dns, s);
if (r == -EEXIST)
dns_zone_verify_all(&s->zone);
}
-void manager_flush_dns_servers(Manager *m, DnsServerType t) {
- DnsServer *s;
-
- assert(m);
-
- if (t == DNS_SERVER_SYSTEM)
- while (m->dns_servers) {
- s = m->dns_servers;
-
- LIST_REMOVE(servers, m->dns_servers, s);
- dns_server_unref(s);
- }
-
- if (t == DNS_SERVER_FALLBACK)
- while (m->fallback_dns_servers) {
- s = m->fallback_dns_servers;
-
- LIST_REMOVE(servers, m->fallback_dns_servers, s);
- dns_server_unref(s);
- }
-}
-
int manager_is_own_hostname(Manager *m, const char *name) {
int r;
void manager_verify_all(Manager *m);
-void manager_flush_dns_servers(Manager *m, DnsServerType t);
-
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
#define EXTRA_CMSG_SPACE 1024