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 LIST_REMOVE(scopes, s->manager->dns_scopes, s);
64 strv_free(s->domains);
70 DnsServer *dns_scope_get_server(DnsScope *s) {
74 return link_get_dns_server(s->link);
76 return manager_get_dns_server(s->manager);
79 void dns_scope_next_dns_server(DnsScope *s) {
83 link_next_dns_server(s->link);
85 manager_next_dns_server(s->manager);
88 int dns_scope_send(DnsScope *s, DnsPacket *p) {
96 srv = dns_scope_get_server(s);
101 if (p->size > s->link->mtu)
104 ifindex = s->link->ifindex;
108 mtu = manager_find_mtu(s->manager);
115 if (p->size > DNS_PACKET_UNICAST_SIZE_MAX)
118 if (srv->family == AF_INET)
119 r = manager_dns_ipv4_send(s->manager, srv, ifindex, p);
120 else if (srv->family == AF_INET6)
121 r = manager_dns_ipv6_send(s->manager, srv, ifindex, p);
123 return -EAFNOSUPPORT;
131 int dns_scope_tcp_socket(DnsScope *s) {
132 _cleanup_close_ int fd = -1;
133 union sockaddr_union sa = {};
135 int one, ifindex, ret;
141 srv = dns_scope_get_server(s);
146 ifindex = s->link->ifindex;
148 sa.sa.sa_family = srv->family;
149 if (srv->family == AF_INET) {
150 sa.in.sin_port = htobe16(53);
151 sa.in.sin_addr = srv->address.in;
152 salen = sizeof(sa.in);
153 } else if (srv->family == AF_INET6) {
154 sa.in6.sin6_port = htobe16(53);
155 sa.in6.sin6_addr = srv->address.in6;
156 sa.in6.sin6_scope_id = ifindex;
157 salen = sizeof(sa.in6);
159 return -EAFNOSUPPORT;
161 fd = socket(srv->family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
166 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
168 r = connect(fd, &sa.sa, salen);
169 if (r < 0 && errno != EINPROGRESS)
177 DnsScopeMatch dns_scope_test(DnsScope *s, const char *domain) {
183 STRV_FOREACH(i, s->domains)
184 if (dns_name_endswith(domain, *i))
185 return DNS_SCOPE_YES;
187 if (dns_name_root(domain))
190 if (is_localhost(domain))
193 if (s->type == DNS_SCOPE_MDNS) {
194 if (dns_name_endswith(domain, "254.169.in-addr.arpa") ||
195 dns_name_endswith(domain, "0.8.e.f.ip6.arpa"))
196 return DNS_SCOPE_YES;
197 else if (dns_name_endswith(domain, "local") &&
198 !dns_name_single_label(domain))
199 return DNS_SCOPE_MAYBE;
204 if (s->type == DNS_SCOPE_DNS) {
205 if (dns_name_endswith(domain, "254.169.in-addr.arpa") ||
206 dns_name_endswith(domain, "0.8.e.f.ip6.arpa") ||
207 dns_name_single_label(domain))
210 return DNS_SCOPE_MAYBE;
213 assert_not_reached("Unknown scope type");