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;
107 if (srv->family == AF_INET)
108 r = manager_dns_ipv4_send(s->manager, srv, ifindex, p);
109 else if (srv->family == AF_INET6)
110 r = manager_dns_ipv6_send(s->manager, srv, ifindex, p);
112 return -EAFNOSUPPORT;
120 int dns_scope_tcp_socket(DnsScope *s) {
121 _cleanup_close_ int fd = -1;
122 union sockaddr_union sa = {};
124 int one, ifindex, ret;
130 srv = dns_scope_get_server(s);
135 ifindex = s->link->ifindex;
137 sa.sa.sa_family = srv->family;
138 if (srv->family == AF_INET) {
139 sa.in.sin_port = htobe16(53);
140 sa.in.sin_addr = srv->address.in;
141 salen = sizeof(sa.in);
142 } else if (srv->family == AF_INET6) {
143 sa.in6.sin6_port = htobe16(53);
144 sa.in6.sin6_addr = srv->address.in6;
145 sa.in6.sin6_scope_id = ifindex;
146 salen = sizeof(sa.in6);
148 return -EAFNOSUPPORT;
150 fd = socket(srv->family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
155 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
157 r = connect(fd, &sa.sa, salen);
158 if (r < 0 && errno != EINPROGRESS)
166 DnsScopeMatch dns_scope_test(DnsScope *s, const char *domain) {
172 STRV_FOREACH(i, s->domains)
173 if (dns_name_endswith(domain, *i))
174 return DNS_SCOPE_YES;
176 if (dns_name_root(domain))
179 if (s->type == DNS_SCOPE_MDNS) {
180 if (dns_name_endswith(domain, "254.169.in-addr.arpa") ||
181 dns_name_endswith(domain, "0.8.e.f.ip6.arpa"))
182 return DNS_SCOPE_YES;
183 else if (dns_name_endswith(domain, "local") ||
184 !dns_name_single_label(domain))
185 return DNS_SCOPE_MAYBE;
190 if (s->type == DNS_SCOPE_DNS) {
191 if (dns_name_endswith(domain, "254.169.in-addr.arpa") ||
192 dns_name_endswith(domain, "0.8.e.f.ip6.arpa") ||
193 dns_name_single_label(domain))
196 return DNS_SCOPE_MAYBE;
199 assert_not_reached("Unknown scope type");