#include "socket-util.h"
#include "af-list.h"
#include "utf8.h"
+#include "fileio-label.h"
#include "resolved-dns-domain.h"
#include "resolved-conf.h"
return 0;
eaddrinuse:
- log_warning("There appears to be another LLMNR respondering running. Turning off LLMNR support.");
+ log_warning("There appears to be another LLMNR responder running. Turning off LLMNR support.");
m->llmnr_support = SUPPORT_NO;
manager_llmnr_stop(m);
return r;
}
-static void write_resolve_conf_server(DnsServer *s, FILE *f, unsigned *count) {
+static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
_cleanup_free_ char *t = NULL;
int r;
(*count) ++;
}
+static void write_resolv_conf_search(const char *domain, FILE *f,
+ unsigned *count, unsigned *length) {
+ assert(domain);
+ assert(f);
+ assert(length);
+
+ if (*count >= MAXDNSRCH ||
+ *length + strlen(domain) > 256) {
+ if (*count == MAXDNSRCH)
+ fputs(" # Too many search domains configured, remaining ones ignored.", f);
+ if (*length <= 256)
+ fputs(" # Total length of all search domains is too long, remaining ones ignored.", f);
+
+ return;
+ }
+
+ fprintf(f, " %s", domain);
+
+ (*length) += strlen(domain);
+ (*count) ++;
+}
+
+static int write_resolv_conf_contents(FILE *f, Set *dns, Set *domains) {
+ Iterator i;
+
+ fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
+ "# Third party programs must not access this file directly, but\n"
+ "# only through the symlink at /etc/resolv.conf. To manage\n"
+ "# resolv.conf(5) in a different way, replace the symlink by a\n"
+ "# static file or a different symlink.\n\n", f);
+
+ if (set_isempty(dns))
+ fputs("# No DNS servers known.\n", f);
+ else {
+ DnsServer *s;
+ unsigned count = 0;
+
+ SET_FOREACH(s, dns, i)
+ write_resolv_conf_server(s, f, &count);
+ }
+
+ if (!set_isempty(domains)) {
+ unsigned length = 0, count = 0;
+ char *domain;
+
+ fputs("search", f);
+ SET_FOREACH(domain, domains, i)
+ write_resolv_conf_search(domain, f, &count, &length);
+ fputs("\n", f);
+ }
+
+ return fflush_and_check(f);
+}
+
+
int manager_write_resolv_conf(Manager *m) {
static const char path[] = "/run/systemd/resolve/resolv.conf";
_cleanup_free_ char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
- _cleanup_set_free_ Set *dns = NULL;
- unsigned count = 0;
+ _cleanup_set_free_ Set *dns = NULL, *domains = NULL;
DnsServer *s;
Iterator i;
Link *l;
manager_read_resolv_conf(m);
/* Add the full list to a set, to filter out duplicates */
- dns = set_new(dns_server_hash_func, dns_server_compare_func);
+ dns = set_new(&dns_server_hash_ops);
if (!dns)
return -ENOMEM;
+ domains = set_new(&dns_name_hash_ops);
+ if (!domains)
+ return -ENOMEM;
+
/* First add the system-wide servers */
LIST_FOREACH(servers, s, m->dns_servers) {
r = set_put(dns, s);
return r;
}
- /* Then, add the per-link servers */
- HASHMAP_FOREACH(l, m->links, i)
+ /* Then, add the per-link servers and domains */
+ HASHMAP_FOREACH(l, m->links, i) {
+ char **domain;
+
LIST_FOREACH(servers, s, l->dns_servers) {
r = set_put(dns, s);
if (r == -EEXIST)
return r;
}
+ if (!l->unicast_scope)
+ continue;
+
+ STRV_FOREACH(domain, l->unicast_scope->domains) {
+ r = set_put(domains, *domain);
+ if (r == -EEXIST)
+ continue;
+ if (r < 0)
+ return r;
+ }
+ }
+
/* If we found nothing, add the fallback servers */
if (set_isempty(dns)) {
LIST_FOREACH(servers, s, m->fallback_dns_servers) {
}
}
- r = fopen_temporary(path, &f, &temp_path);
+ r = fopen_temporary_label(path, path, &f, &temp_path);
if (r < 0)
return r;
fchmod(fileno(f), 0644);
- fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
- "# Third party programs must not access this file directly, but\n"
- "# only through the symlink at /etc/resolv.conf. To manage\n"
- "# resolv.conf(5) in a different way, replace the symlink by a\n"
- "# static file or a different symlink.\n\n", f);
-
- if (set_isempty(dns))
- fputs("# No DNS servers known.\n", f);
- else {
- SET_FOREACH(s, dns, i)
- write_resolve_conf_server(s, f, &count);
- }
-
- r = fflush_and_check(f);
+ r = write_resolv_conf_contents(f, dns, domains);
if (r < 0)
goto fail;
_cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
union {
struct cmsghdr header; /* For alignment */
- uint8_t buffer[CMSG_SPACE(MAX(sizeof(struct in_pktinfo), sizeof(struct in6_pktinfo)))
+ uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo))
+ CMSG_SPACE(int) /* ttl/hoplimit */
+ EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */];
} control;
return r;
}
+/* lo having ifindex 1 is hardcoded in the kernel */
+#define LOOPBACK_IFINDEX 1
+
int manager_ifindex_is_loopback(Manager *m, int ifindex) {
Link *l;
assert(m);
return -EINVAL;
l = hashmap_get(m->links, INT_TO_PTR(ifindex));
- if (l->flags & IFF_LOOPBACK)
+ if (!l)
+ /* in case we don't yet track the link, rely on the hardcoded value */
+ return ifindex == LOOPBACK_IFINDEX;
+ else if (l->flags & IFF_LOOPBACK)
return 1;
return 0;