1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <netinet/tcp.h>
25 #include "socket-util.h"
26 #include "resolved-dns-domain.h"
27 #include "resolved-dns-scope.h"
29 #define SEND_TIMEOUT_USEC (2*USEC_PER_SEC)
31 int dns_scope_new(Manager *m, DnsScope **ret, DnsScopeType t) {
37 s = new0(DnsScope, 1);
44 LIST_PREPEND(scopes, m->dns_scopes, s);
50 DnsScope* dns_scope_free(DnsScope *s) {
54 while (s->transactions) {
57 q = s->transactions->query;
58 dns_query_transaction_free(s->transactions);
63 dns_cache_flush(&s->cache);
65 LIST_REMOVE(scopes, s->manager->dns_scopes, s);
66 strv_free(s->domains);
72 DnsServer *dns_scope_get_server(DnsScope *s) {
76 return link_get_dns_server(s->link);
78 return manager_get_dns_server(s->manager);
81 void dns_scope_next_dns_server(DnsScope *s) {
85 link_next_dns_server(s->link);
87 manager_next_dns_server(s->manager);
90 int dns_scope_send(DnsScope *s, DnsPacket *p) {
98 srv = dns_scope_get_server(s);
103 if (p->size > s->link->mtu)
106 ifindex = s->link->ifindex;
110 mtu = manager_find_mtu(s->manager);
117 if (p->size > DNS_PACKET_UNICAST_SIZE_MAX)
120 if (srv->family == AF_INET)
121 r = manager_dns_ipv4_send(s->manager, srv, ifindex, p);
122 else if (srv->family == AF_INET6)
123 r = manager_dns_ipv6_send(s->manager, srv, ifindex, p);
125 return -EAFNOSUPPORT;
133 int dns_scope_tcp_socket(DnsScope *s) {
134 _cleanup_close_ int fd = -1;
135 union sockaddr_union sa = {};
137 int one, ifindex, ret;
143 srv = dns_scope_get_server(s);
148 ifindex = s->link->ifindex;
150 sa.sa.sa_family = srv->family;
151 if (srv->family == AF_INET) {
152 sa.in.sin_port = htobe16(53);
153 sa.in.sin_addr = srv->address.in;
154 salen = sizeof(sa.in);
155 } else if (srv->family == AF_INET6) {
156 sa.in6.sin6_port = htobe16(53);
157 sa.in6.sin6_addr = srv->address.in6;
158 sa.in6.sin6_scope_id = ifindex;
159 salen = sizeof(sa.in6);
161 return -EAFNOSUPPORT;
163 fd = socket(srv->family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
168 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
170 r = connect(fd, &sa.sa, salen);
171 if (r < 0 && errno != EINPROGRESS)
179 DnsScopeMatch dns_scope_test(DnsScope *s, const char *domain) {
185 STRV_FOREACH(i, s->domains)
186 if (dns_name_endswith(domain, *i))
187 return DNS_SCOPE_YES;
189 if (dns_name_root(domain))
192 if (is_localhost(domain))
195 if (s->type == DNS_SCOPE_MDNS) {
196 if (dns_name_endswith(domain, "254.169.in-addr.arpa") ||
197 dns_name_endswith(domain, "0.8.e.f.ip6.arpa"))
198 return DNS_SCOPE_YES;
199 else if (dns_name_endswith(domain, "local") &&
200 !dns_name_single_label(domain))
201 return DNS_SCOPE_MAYBE;
206 if (s->type == DNS_SCOPE_DNS) {
207 if (dns_name_endswith(domain, "254.169.in-addr.arpa") ||
208 dns_name_endswith(domain, "0.8.e.f.ip6.arpa") ||
209 dns_name_single_label(domain))
212 return DNS_SCOPE_MAYBE;
215 assert_not_reached("Unknown scope type");