1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-01 00:51:24 +03:00

resolved: support for TCP DNS queries

This commit is contained in:
Lennart Poettering 2014-07-16 20:15:47 +02:00
parent c73ce96b56
commit ad86766293
8 changed files with 473 additions and 237 deletions

View File

@ -25,56 +25,84 @@
#include "resolved.h" #include "resolved.h"
#include "resolved-dns-domain.h" #include "resolved-dns-domain.h"
static void bus_method_resolve_hostname_complete(DnsQuery *q) { static int reply_query_state(DnsQuery *q) {
_cleanup_free_ char *ip = NULL;
const char *name;
int r; int r;
assert(q); if (q->request_hostname)
name = q->request_hostname;
else {
r = in_addr_to_string(q->request_family, &q->request_address, &ip);
if (r < 0)
return r;
switch(q->state) { name = ip;
}
case DNS_QUERY_SKIPPED: switch (q->state) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "Not appropriate name servers or networks found");
break; case DNS_QUERY_NO_SERVERS:
return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "Not appropriate name servers or networks found");
case DNS_QUERY_TIMEOUT: case DNS_QUERY_TIMEOUT:
r = sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out"); return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
break;
case DNS_QUERY_ATTEMPTS_MAX: case DNS_QUERY_ATTEMPTS_MAX:
r = sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed"); return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
break;
case DNS_QUERY_RESOURCES:
return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_RESOURCES, "Not enough resources");
case DNS_QUERY_INVALID_REPLY:
return sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
case DNS_QUERY_FAILURE: { case DNS_QUERY_FAILURE: {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
if (q->rcode == DNS_RCODE_NXDOMAIN) assert(q->received);
sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "Hostname %s does not exist", q->request_hostname);
if (DNS_PACKET_RCODE(q->received) == DNS_RCODE_NXDOMAIN)
sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", name);
else { else {
const char *rc, *n; const char *rc, *n;
char p[DECIMAL_STR_MAX(q->rcode)]; char p[3]; /* the rcode is 4 bits long */
rc = dns_rcode_to_string(q->rcode); rc = dns_rcode_to_string(DNS_PACKET_RCODE(q->received));
if (!rc) { if (!rc) {
sprintf(p, "%i", q->rcode); sprintf(p, "%i", DNS_PACKET_RCODE(q->received));
rc = p; rc = p;
} }
n = strappenda(_BUS_ERROR_DNS, rc); n = strappenda(_BUS_ERROR_DNS, rc);
sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", name, rc);
sd_bus_error_setf(&error, n, "Could not resolve hostname %s, server or network returned error %s", q->request_hostname, rc);
} }
r = sd_bus_reply_method_error(q->request, &error); return sd_bus_reply_method_error(q->request, &error);
break;
} }
case DNS_QUERY_SUCCESS: { case DNS_QUERY_NULL:
case DNS_QUERY_SENT:
case DNS_QUERY_SUCCESS:
assert_not_reached("Impossible state");
}
}
static void bus_method_resolve_hostname_complete(DnsQuery *q) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL; _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
unsigned i, n, added = 0; unsigned i, n, added = 0;
int r;
assert(q->packet); assert(q);
r = dns_packet_skip_question(q->packet); if (q->state != DNS_QUERY_SUCCESS) {
r = reply_query_state(q);
goto finish;
}
assert(q->received);
r = dns_packet_skip_question(q->received);
if (r < 0) if (r < 0)
goto parse_fail; goto parse_fail;
@ -86,13 +114,13 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
if (r < 0) if (r < 0)
goto finish; goto finish;
n = DNS_PACKET_ANCOUNT(q->packet) + n = DNS_PACKET_ANCOUNT(q->received) +
DNS_PACKET_NSCOUNT(q->packet) + DNS_PACKET_NSCOUNT(q->received) +
DNS_PACKET_ARCOUNT(q->packet); DNS_PACKET_ARCOUNT(q->received);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
r = dns_packet_read_rr(q->packet, &rr, NULL); r = dns_packet_read_rr(q->received, &rr, NULL);
if (r < 0) if (r < 0)
goto parse_fail; goto parse_fail;
@ -126,7 +154,7 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
if (r < 0) if (r < 0)
goto finish; goto finish;
r = sd_bus_message_append(reply, "i", q->packet->ifindex); r = sd_bus_message_append(reply, "i", q->received->ifindex);
if (r < 0) if (r < 0)
goto finish; goto finish;
@ -138,8 +166,8 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
} }
if (added <= 0) { if (added <= 0) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Hostname %s does not have RR of this type", q->request_hostname); r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have RR of this type", q->request_hostname);
break; goto finish;
} }
r = sd_bus_message_close_container(reply); r = sd_bus_message_close_container(reply);
@ -147,18 +175,10 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
goto finish; goto finish;
r = sd_bus_send(q->manager->bus, reply, NULL); r = sd_bus_send(q->manager->bus, reply, NULL);
break; goto finish;
}
parse_fail: parse_fail:
case DNS_QUERY_INVALID_REPLY:
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply"); r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
break;
case DNS_QUERY_NULL:
case DNS_QUERY_SENT:
assert_not_reached("Unexpected query state");
}
finish: finish:
if (r < 0) if (r < 0)
@ -223,63 +243,25 @@ static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, voi
} }
static void bus_method_resolve_address_complete(DnsQuery *q) { static void bus_method_resolve_address_complete(DnsQuery *q) {
_cleanup_free_ char *ip = NULL; _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
unsigned i, n, added = 0;
_cleanup_free_ char *reverse = NULL;
int r; int r;
assert(q); assert(q);
in_addr_to_string(q->request_family, &q->request_address, &ip); if (q->state != DNS_QUERY_SUCCESS) {
r = reply_query_state(q);
switch(q->state) { goto finish;
case DNS_QUERY_SKIPPED:
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "Not appropriate name servers or networks found");
break;
case DNS_QUERY_TIMEOUT:
r = sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
break;
case DNS_QUERY_ATTEMPTS_MAX:
r = sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
break;
case DNS_QUERY_FAILURE: {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
if (q->rcode == DNS_RCODE_NXDOMAIN)
sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "No hostname known for address %s ", ip);
else {
const char *rc, *n;
char p[DECIMAL_STR_MAX(q->rcode)];
rc = dns_rcode_to_string(q->rcode);
if (!rc) {
sprintf(p, "%i", q->rcode);
rc = p;
} }
n = strappenda(_BUS_ERROR_DNS, rc); assert(q->received);
sd_bus_error_setf(&error, n, "Could not resolve address %s, server or network returned error %s", ip, rc);
}
r = sd_bus_reply_method_error(q->request, &error);
break;
}
case DNS_QUERY_SUCCESS: {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
unsigned i, n, added = 0;
_cleanup_free_ char *reverse = NULL;
assert(q->packet);
r = dns_name_reverse(q->request_family, &q->request_address, &reverse); r = dns_name_reverse(q->request_family, &q->request_address, &reverse);
if (r < 0) if (r < 0)
goto finish; goto finish;
r = dns_packet_skip_question(q->packet); r = dns_packet_skip_question(q->received);
if (r < 0) if (r < 0)
goto parse_fail; goto parse_fail;
@ -291,13 +273,14 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
if (r < 0) if (r < 0)
goto finish; goto finish;
n = DNS_PACKET_ANCOUNT(q->packet) + n = DNS_PACKET_ANCOUNT(q->received) +
DNS_PACKET_NSCOUNT(q->packet) + DNS_PACKET_NSCOUNT(q->received) +
DNS_PACKET_ARCOUNT(q->packet); DNS_PACKET_ARCOUNT(q->received);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
r = dns_packet_read_rr(q->packet, &rr, NULL); r = dns_packet_read_rr(q->received, &rr, NULL);
if (r < 0) if (r < 0)
goto parse_fail; goto parse_fail;
@ -316,8 +299,12 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
} }
if (added <= 0) { if (added <= 0) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Address %s does not have RR of this type", ip); _cleanup_free_ char *ip = NULL;
break;
in_addr_to_string(q->request_family, &q->request_address, &ip);
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Address '%s' does not have RR of this type", ip);
goto finish;
} }
r = sd_bus_message_close_container(reply); r = sd_bus_message_close_container(reply);
@ -325,18 +312,10 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
goto finish; goto finish;
r = sd_bus_send(q->manager->bus, reply, NULL); r = sd_bus_send(q->manager->bus, reply, NULL);
break; goto finish;
}
parse_fail: parse_fail:
case DNS_QUERY_INVALID_REPLY:
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply"); r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
break;
case DNS_QUERY_NULL:
case DNS_QUERY_SENT:
assert_not_reached("Unexpected query state");
}
finish: finish:
if (r < 0) if (r < 0)

View File

@ -71,6 +71,7 @@ static inline uint8_t* DNS_PACKET_DATA(DnsPacket *p) {
#define DNS_PACKET_QR(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 15) & 1) #define DNS_PACKET_QR(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 15) & 1)
#define DNS_PACKET_OPCODE(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 11) & 15) #define DNS_PACKET_OPCODE(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 11) & 15)
#define DNS_PACKET_RCODE(p) (be16toh(DNS_PACKET_HEADER(p)->flags) & 15) #define DNS_PACKET_RCODE(p) (be16toh(DNS_PACKET_HEADER(p)->flags) & 15)
#define DNS_PACKET_TC(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 9) & 1)
#define DNS_PACKET_QDCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->qdcount) #define DNS_PACKET_QDCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->qdcount)
#define DNS_PACKET_ANCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->ancount) #define DNS_PACKET_ANCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->ancount)
#define DNS_PACKET_NSCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->nscount) #define DNS_PACKET_NSCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->nscount)

View File

@ -31,7 +31,12 @@ DnsQueryTransaction* dns_query_transaction_free(DnsQueryTransaction *t) {
return NULL; return NULL;
sd_event_source_unref(t->timeout_event_source); sd_event_source_unref(t->timeout_event_source);
dns_packet_unref(t->packet);
dns_packet_unref(t->sent);
dns_packet_unref(t->received);
sd_event_source_unref(t->tcp_event_source);
safe_close(t->tcp_fd);
if (t->query) { if (t->query) {
LIST_REMOVE(transactions_by_query, t->query->transactions, t); LIST_REMOVE(transactions_by_query, t->query->transactions, t);
@ -62,6 +67,8 @@ static int dns_query_transaction_new(DnsQuery *q, DnsQueryTransaction **ret, Dns
if (!t) if (!t)
return -ENOMEM; return -ENOMEM;
t->tcp_fd = -1;
do do
random_bytes(&t->id, sizeof(t->id)); random_bytes(&t->id, sizeof(t->id));
while (t->id == 0 || while (t->id == 0 ||
@ -87,6 +94,14 @@ static int dns_query_transaction_new(DnsQuery *q, DnsQueryTransaction **ret, Dns
return 0; return 0;
} }
static void dns_query_transaction_stop(DnsQueryTransaction *t) {
assert(t);
t->timeout_event_source = sd_event_source_unref(t->timeout_event_source);
t->tcp_event_source = sd_event_source_unref(t->tcp_event_source);
t->tcp_fd = safe_close(t->tcp_fd);
}
static void dns_query_transaction_set_state(DnsQueryTransaction *t, DnsQueryState state) { static void dns_query_transaction_set_state(DnsQueryTransaction *t, DnsQueryState state) {
assert(t); assert(t);
@ -95,24 +110,179 @@ static void dns_query_transaction_set_state(DnsQueryTransaction *t, DnsQueryStat
t->state = state; t->state = state;
if (state != DNS_QUERY_SENT) if (state != DNS_QUERY_SENT) {
t->timeout_event_source = sd_event_source_unref(t->timeout_event_source); dns_query_transaction_stop(t);
dns_query_finish(t->query); dns_query_finish(t->query);
}
} }
int dns_query_transaction_reply(DnsQueryTransaction *t, DnsPacket *p) { static int on_tcp_ready(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
DnsQueryTransaction *t = userdata;
int r;
assert(t);
if (revents & EPOLLOUT) {
struct iovec iov[2];
be16_t sz;
ssize_t ss;
sz = htobe16(t->sent->size);
iov[0].iov_base = &sz;
iov[0].iov_len = sizeof(sz);
iov[1].iov_base = DNS_PACKET_DATA(t->sent);
iov[1].iov_len = t->sent->size;
IOVEC_INCREMENT(iov, 2, t->tcp_written);
ss = writev(fd, iov, 2);
if (ss < 0) {
if (errno != EINTR && errno != EAGAIN) {
dns_query_transaction_set_state(t, DNS_QUERY_RESOURCES);
return -errno;
}
} else
t->tcp_written += ss;
/* Are we done? If so, disable the event source for EPOLLOUT */
if (t->tcp_written >= sizeof(sz) + t->sent->size) {
r = sd_event_source_set_io_events(s, EPOLLIN);
if (r < 0) {
dns_query_transaction_set_state(t, DNS_QUERY_RESOURCES);
return r;
}
}
}
if (revents & (EPOLLIN|EPOLLHUP|EPOLLRDHUP)) {
if (t->tcp_read < sizeof(t->tcp_read_size)) {
ssize_t ss;
ss = read(fd, (uint8_t*) &t->tcp_read_size + t->tcp_read, sizeof(t->tcp_read_size) - t->tcp_read);
if (ss < 0) {
if (errno != EINTR && errno != EAGAIN) {
dns_query_transaction_set_state(t, DNS_QUERY_RESOURCES);
return -errno;
}
} else if (ss == 0) {
dns_query_transaction_set_state(t, DNS_QUERY_RESOURCES);
return -EIO;
} else
t->tcp_read += ss;
}
if (t->tcp_read >= sizeof(t->tcp_read_size)) {
if (be16toh(t->tcp_read_size) < DNS_PACKET_HEADER_SIZE) {
dns_query_transaction_set_state(t, DNS_QUERY_INVALID_REPLY);
return -EBADMSG;
}
if (t->tcp_read < sizeof(t->tcp_read_size) + be16toh(t->tcp_read_size)) {
ssize_t ss;
if (!t->received) {
r = dns_packet_new(&t->received, be16toh(t->tcp_read_size));
if (r < 0) {
dns_query_transaction_set_state(t, DNS_QUERY_RESOURCES);
return r;
}
}
ss = read(fd,
(uint8_t*) DNS_PACKET_DATA(t->received) + t->tcp_read - sizeof(t->tcp_read_size),
sizeof(t->tcp_read_size) + be16toh(t->tcp_read_size) - t->tcp_read);
if (ss < 0) {
if (errno != EINTR && errno != EAGAIN) {
dns_query_transaction_set_state(t, DNS_QUERY_RESOURCES);
return -errno;
}
} else if (ss == 0) {
dns_query_transaction_set_state(t, DNS_QUERY_RESOURCES);
return -EIO;
} else
t->tcp_read += ss;
}
if (t->tcp_read >= sizeof(t->tcp_read_size) + be16toh(t->tcp_read_size)) {
t->received->size = be16toh(t->tcp_read_size);
dns_query_transaction_reply(t, t->received);
return 0;
}
}
}
return 0;
}
static int dns_query_transaction_start_tcp(DnsQueryTransaction *t) {
int r;
assert(t);
if (t->tcp_fd >= 0)
return 0;
t->tcp_written = 0;
t->tcp_read = 0;
t->received = dns_packet_unref(t->received);
t->tcp_fd = dns_scope_tcp_socket(t->scope);
if (t->tcp_fd < 0)
return t->tcp_fd;
r = sd_event_add_io(t->query->manager->event, &t->tcp_event_source, t->tcp_fd, EPOLLIN|EPOLLOUT, on_tcp_ready, t);
if (r < 0) {
t->tcp_fd = safe_close(t->tcp_fd);
return r;
}
return 0;
}
void dns_query_transaction_reply(DnsQueryTransaction *t, DnsPacket *p) {
int r;
assert(t); assert(t);
assert(p); assert(p);
t->packet = dns_packet_ref(p); if (t->state != DNS_QUERY_SENT)
return;
if (t->received != p) {
dns_packet_unref(t->received);
t->received = dns_packet_ref(p);
}
if (t->tcp_fd >= 0) {
if (DNS_PACKET_TC(p)) {
/* Truncated via TCP? Somebody must be fucking with us */
dns_query_transaction_set_state(t, DNS_QUERY_INVALID_REPLY);
return;
}
if (DNS_PACKET_ID(p) != t->id) {
/* Not the reply to our query? Somebody must be fucking with us */
dns_query_transaction_set_state(t, DNS_QUERY_INVALID_REPLY);
return;
}
}
if (DNS_PACKET_TC(p)) {
/* Response was truncated, let's try again with good old TCP */
r = dns_query_transaction_start_tcp(t);
if (r < 0) {
dns_query_transaction_set_state(t, DNS_QUERY_RESOURCES);
return;
}
}
if (DNS_PACKET_RCODE(p) == DNS_RCODE_SUCCESS) if (DNS_PACKET_RCODE(p) == DNS_RCODE_SUCCESS)
dns_query_transaction_set_state(t, DNS_QUERY_SUCCESS); dns_query_transaction_set_state(t, DNS_QUERY_SUCCESS);
else else
dns_query_transaction_set_state(t, DNS_QUERY_FAILURE); dns_query_transaction_set_state(t, DNS_QUERY_FAILURE);
return 0;
} }
static int on_transaction_timeout(sd_event_source *s, usec_t usec, void *userdata) { static int on_transaction_timeout(sd_event_source *s, usec_t usec, void *userdata) {
@ -127,26 +297,20 @@ static int on_transaction_timeout(sd_event_source *s, usec_t usec, void *userdat
r = dns_query_transaction_start(t); r = dns_query_transaction_start(t);
if (r < 0) if (r < 0)
dns_query_transaction_set_state(t, DNS_QUERY_FAILURE); dns_query_transaction_set_state(t, DNS_QUERY_RESOURCES);
return 0; return 0;
} }
int dns_query_transaction_start(DnsQueryTransaction *t) { static int dns_query_make_packet(DnsQueryTransaction *t) {
_cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
unsigned n; unsigned n;
int r; int r;
assert(t); assert(t);
t->timeout_event_source = sd_event_source_unref(t->timeout_event_source); if (t->sent)
if (t->n_attempts >= ATTEMPTS_MAX) {
dns_query_transaction_set_state(t, DNS_QUERY_ATTEMPTS_MAX);
return 0; return 0;
}
t->n_attempts++;
r = dns_packet_new_query(&p, 0); r = dns_packet_new_query(&p, 0);
if (r < 0) if (r < 0)
@ -161,7 +325,38 @@ int dns_query_transaction_start(DnsQueryTransaction *t) {
DNS_PACKET_HEADER(p)->qdcount = htobe16(t->query->n_keys); DNS_PACKET_HEADER(p)->qdcount = htobe16(t->query->n_keys);
DNS_PACKET_HEADER(p)->id = t->id; DNS_PACKET_HEADER(p)->id = t->id;
r = dns_scope_send(t->scope, p); t->sent = p;
p = NULL;
return 0;
}
int dns_query_transaction_start(DnsQueryTransaction *t) {
int r;
assert(t);
dns_query_transaction_stop(t);
if (t->n_attempts >= ATTEMPTS_MAX) {
dns_query_transaction_set_state(t, DNS_QUERY_ATTEMPTS_MAX);
return 0;
}
t->n_attempts++;
r = dns_query_make_packet(t);
if (r < 0)
return r;
/* Try via UDP, and if that fails due to large size try via TCP */
r = dns_scope_send(t->scope, t->sent);
if (r == -EMSGSIZE)
r = dns_query_transaction_start_tcp(t);
if (r == -ESRCH) {
dns_query_transaction_set_state(t, DNS_QUERY_NO_SERVERS);
return 0;
}
if (r < 0) { if (r < 0) {
/* Couldn't send? Try immediately again, with a new server */ /* Couldn't send? Try immediately again, with a new server */
dns_scope_next_dns_server(t->scope); dns_scope_next_dns_server(t->scope);
@ -169,18 +364,12 @@ int dns_query_transaction_start(DnsQueryTransaction *t) {
return dns_query_transaction_start(t); return dns_query_transaction_start(t);
} }
if (r > 0) { r = sd_event_add_time(t->query->manager->event, &t->timeout_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + TRANSACTION_TIMEOUT_USEC, 0, on_transaction_timeout, t);
int q; if (r < 0)
return r;
q = sd_event_add_time(t->query->manager->event, &t->timeout_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + TRANSACTION_TIMEOUT_USEC, 0, on_transaction_timeout, t);
if (q < 0)
return q;
dns_query_transaction_set_state(t, DNS_QUERY_SENT); dns_query_transaction_set_state(t, DNS_QUERY_SENT);
} else return 1;
dns_query_transaction_set_state(t, DNS_QUERY_SKIPPED);
return r;
} }
DnsQuery *dns_query_free(DnsQuery *q) { DnsQuery *dns_query_free(DnsQuery *q) {
@ -190,7 +379,7 @@ DnsQuery *dns_query_free(DnsQuery *q) {
return NULL; return NULL;
sd_bus_message_unref(q->request); sd_bus_message_unref(q->request);
dns_packet_unref(q->packet); dns_packet_unref(q->received);
sd_event_source_unref(q->timeout_event_source); sd_event_source_unref(q->timeout_event_source);
while (q->transactions) while (q->transactions)
@ -367,8 +556,8 @@ fail:
void dns_query_finish(DnsQuery *q) { void dns_query_finish(DnsQuery *q) {
DnsQueryTransaction *t; DnsQueryTransaction *t;
DnsQueryState state = DNS_QUERY_SKIPPED; DnsQueryState state = DNS_QUERY_NO_SERVERS;
uint16_t rcode = 0; DnsPacket *received = NULL;
assert(q); assert(q);
@ -381,25 +570,28 @@ void dns_query_finish(DnsQuery *q) {
if (t->state == DNS_QUERY_SENT || t->state == DNS_QUERY_NULL) if (t->state == DNS_QUERY_SENT || t->state == DNS_QUERY_NULL)
return; return;
/* One of the transactions is sucecssful, let's use it */ /* One of the transactions is successful, let's use it */
if (t->state == DNS_QUERY_SUCCESS) { if (t->state == DNS_QUERY_SUCCESS) {
q->packet = dns_packet_ref(t->packet); q->received = dns_packet_ref(t->received);
dns_query_set_state(q, DNS_QUERY_SUCCESS); dns_query_set_state(q, DNS_QUERY_SUCCESS);
return; return;
} }
/* One of the transactions has failed, let's see
* whether we find anything better, but if not, return
* its response packet */
if (t->state == DNS_QUERY_FAILURE) { if (t->state == DNS_QUERY_FAILURE) {
received = t->received;
state = DNS_QUERY_FAILURE; state = DNS_QUERY_FAILURE;
continue;
}
if (rcode == 0 && t->packet) if (state == DNS_QUERY_NO_SERVERS && t->state != DNS_QUERY_NO_SERVERS)
rcode = DNS_PACKET_RCODE(t->packet);
} else if (state == DNS_QUERY_SKIPPED && t->state != DNS_QUERY_SKIPPED)
state = t->state; state = t->state;
} }
if (state == DNS_QUERY_FAILURE) if (state == DNS_QUERY_FAILURE)
q->rcode = rcode; q->received = dns_packet_ref(received);
dns_query_set_state(q, state); dns_query_set_state(q, state);
} }

View File

@ -39,10 +39,11 @@ typedef enum DnsQueryState {
DNS_QUERY_SENT, DNS_QUERY_SENT,
DNS_QUERY_FAILURE, DNS_QUERY_FAILURE,
DNS_QUERY_SUCCESS, DNS_QUERY_SUCCESS,
DNS_QUERY_SKIPPED, DNS_QUERY_NO_SERVERS,
DNS_QUERY_TIMEOUT, DNS_QUERY_TIMEOUT,
DNS_QUERY_ATTEMPTS_MAX, DNS_QUERY_ATTEMPTS_MAX,
DNS_QUERY_INVALID_REPLY, DNS_QUERY_INVALID_REPLY,
DNS_QUERY_RESOURCES,
} DnsQueryState; } DnsQueryState;
struct DnsQueryTransaction { struct DnsQueryTransaction {
@ -55,7 +56,13 @@ struct DnsQueryTransaction {
sd_event_source *timeout_event_source; sd_event_source *timeout_event_source;
unsigned n_attempts; unsigned n_attempts;
DnsPacket *packet; DnsPacket *sent, *received;
/* TCP connection logic */
int tcp_fd;
sd_event_source *tcp_event_source;
size_t tcp_written, tcp_read;
be16_t tcp_read_size;
LIST_FIELDS(DnsQueryTransaction, transactions_by_query); LIST_FIELDS(DnsQueryTransaction, transactions_by_query);
LIST_FIELDS(DnsQueryTransaction, transactions_by_scope); LIST_FIELDS(DnsQueryTransaction, transactions_by_scope);
@ -71,8 +78,7 @@ struct DnsQuery {
sd_event_source *timeout_event_source; sd_event_source *timeout_event_source;
uint16_t rcode; DnsPacket *received;
DnsPacket *packet;
sd_bus_message *request; sd_bus_message *request;
unsigned char request_family; unsigned char request_family;
@ -92,6 +98,6 @@ void dns_query_finish(DnsQuery *q);
DnsQueryTransaction* dns_query_transaction_free(DnsQueryTransaction *t); DnsQueryTransaction* dns_query_transaction_free(DnsQueryTransaction *t);
int dns_query_transaction_start(DnsQueryTransaction *t); int dns_query_transaction_start(DnsQueryTransaction *t);
int dns_query_transaction_reply(DnsQueryTransaction *t, DnsPacket *p); void dns_query_transaction_reply(DnsQueryTransaction *t, DnsPacket *p);
DEFINE_TRIVIAL_CLEANUP_FUNC(DnsQuery*, dns_query_free); DEFINE_TRIVIAL_CLEANUP_FUNC(DnsQuery*, dns_query_free);

View File

@ -19,7 +19,10 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>. along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/ ***/
#include <netinet/tcp.h>
#include "strv.h" #include "strv.h"
#include "socket-util.h"
#include "resolved-dns-domain.h" #include "resolved-dns-domain.h"
#include "resolved-dns-scope.h" #include "resolved-dns-scope.h"
@ -92,10 +95,14 @@ int dns_scope_send(DnsScope *s, DnsPacket *p) {
srv = dns_scope_get_server(s); srv = dns_scope_get_server(s);
if (!srv) if (!srv)
return 0; return -ESRCH;
if (s->link) {
if (p->size > s->link->mtu)
return -EMSGSIZE;
if (s->link)
ifindex = s->link->ifindex; ifindex = s->link->ifindex;
}
if (srv->family == AF_INET) if (srv->family == AF_INET)
r = manager_dns_ipv4_send(s->manager, srv, ifindex, p); r = manager_dns_ipv4_send(s->manager, srv, ifindex, p);
@ -110,6 +117,52 @@ int dns_scope_send(DnsScope *s, DnsPacket *p) {
return 1; return 1;
} }
int dns_scope_tcp_socket(DnsScope *s) {
_cleanup_close_ int fd = -1;
union sockaddr_union sa = {};
socklen_t salen;
int one, ifindex, ret;
DnsServer *srv;
int r;
assert(s);
srv = dns_scope_get_server(s);
if (!srv)
return -ESRCH;
if (s->link)
ifindex = s->link->ifindex;
sa.sa.sa_family = srv->family;
if (srv->family == AF_INET) {
sa.in.sin_port = htobe16(53);
sa.in.sin_addr = srv->address.in;
salen = sizeof(sa.in);
} else if (srv->family == AF_INET6) {
sa.in6.sin6_port = htobe16(53);
sa.in6.sin6_addr = srv->address.in6;
sa.in6.sin6_scope_id = ifindex;
salen = sizeof(sa.in6);
} else
return -EAFNOSUPPORT;
fd = socket(srv->family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (fd < 0)
return -errno;
one = 1;
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
r = connect(fd, &sa.sa, salen);
if (r < 0 && errno != EINPROGRESS)
return -errno;
ret = fd;
fd = -1;
return ret;
}
DnsScopeMatch dns_scope_test(DnsScope *s, const char *domain) { DnsScopeMatch dns_scope_test(DnsScope *s, const char *domain) {
char **i; char **i;

View File

@ -63,6 +63,8 @@ int dns_scope_new(Manager *m, DnsScope **ret, DnsScopeType t);
DnsScope* dns_scope_free(DnsScope *s); DnsScope* dns_scope_free(DnsScope *s);
int dns_scope_send(DnsScope *s, DnsPacket *p); int dns_scope_send(DnsScope *s, DnsPacket *p);
int dns_scope_tcp_socket(DnsScope *s);
DnsScopeMatch dns_scope_test(DnsScope *s, const char *domain); DnsScopeMatch dns_scope_test(DnsScope *s, const char *domain);
DnsServer *dns_scope_get_server(DnsScope *s); DnsServer *dns_scope_get_server(DnsScope *s);

View File

@ -577,7 +577,7 @@ int manager_dns_ipv4_recv(Manager *m, DnsPacket **ret) {
l = recvmsg(fd, &mh, 0); l = recvmsg(fd, &mh, 0);
if (l < 0) { if (l < 0) {
if (errno == EAGAIN) if (errno == EAGAIN || errno == EINTR)
return 0; return 0;
return -errno; return -errno;
@ -626,7 +626,7 @@ int manager_dns_ipv6_recv(Manager *m, DnsPacket **ret) {
l = recvmsg(fd, &mh, 0); l = recvmsg(fd, &mh, 0);
if (l < 0) { if (l < 0) {
if (errno == EAGAIN) if (errno == EAGAIN || errno == EINTR)
return 0; return 0;
return -errno; return -errno;
@ -657,7 +657,8 @@ static int on_dns_ipv4_packet(sd_event_source *s, int fd, uint32_t revents, void
if (!t) if (!t)
return 0; return 0;
return dns_query_transaction_reply(t, p); dns_query_transaction_reply(t, p);
return 0;
} }
static int on_dns_ipv6_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) { static int on_dns_ipv6_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
@ -674,7 +675,8 @@ static int on_dns_ipv6_packet(sd_event_source *s, int fd, uint32_t revents, void
if (!t) if (!t)
return 0; return 0;
return dns_query_transaction_reply(t, p); dns_query_transaction_reply(t, p);
return 0;
} }
int manager_dns_ipv4_fd(Manager *m) { int manager_dns_ipv4_fd(Manager *m) {

View File

@ -63,4 +63,5 @@
#define BUS_ERROR_NO_NAME_SERVERS "org.freedesktop.resolve1.NoNameServers" #define BUS_ERROR_NO_NAME_SERVERS "org.freedesktop.resolve1.NoNameServers"
#define BUS_ERROR_INVALID_REPLY "org.freedesktop.resolve1.InvalidReply" #define BUS_ERROR_INVALID_REPLY "org.freedesktop.resolve1.InvalidReply"
#define BUS_ERROR_NO_SUCH_RR "org.freedesktop.resolve1.NoSuchRR" #define BUS_ERROR_NO_SUCH_RR "org.freedesktop.resolve1.NoSuchRR"
#define BUS_ERROR_NO_RESOURCES "org.freedesktop.resolve1.NoResources"
#define _BUS_ERROR_DNS "org.freedesktop.resolve1.DnsError." #define _BUS_ERROR_DNS "org.freedesktop.resolve1.DnsError."