/* SPDX-License-Identifier: LGPL-2.1+ */
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-***/
#include <netinet/tcp.h>
if (s->write_packet && s->n_written < sizeof(s->write_size) + s->write_packet->size)
f |= EPOLLOUT;
+ else if (!ordered_set_isempty(s->write_queue)) {
+ dns_packet_unref(s->write_packet);
+ s->write_packet = ordered_set_steal_first(s->write_queue);
+ s->write_size = htobe16(s->write_packet->size);
+ s->n_written = 0;
+ f |= EPOLLOUT;
+ }
if (!s->read_packet || s->n_read < sizeof(s->read_size) + s->read_packet->size)
f |= EPOLLIN;
+#if ENABLE_DNS_OVER_TLS
+ /* For handshake and clean closing purposes, TLS can override requested events */
+ if (s->dnstls_events)
+ f = s->dnstls_events;
+#endif
+
return sd_event_source_set_io_events(s->io_event_source, f);
}
static int dns_stream_complete(DnsStream *s, int error) {
assert(s);
- dns_stream_stop(s);
+#if ENABLE_DNS_OVER_TLS
+ if (s->encrypted) {
+ int r;
+
+ r = dnstls_stream_shutdown(s, error);
+ if (r != -EAGAIN)
+ dns_stream_stop(s);
+ } else
+#endif
+ dns_stream_stop(s);
if (s->complete)
s->complete(s, error);
return 0;
}
+ssize_t dns_stream_writev(DnsStream *s, const struct iovec *iov, size_t iovcnt, int flags) {
+ ssize_t r;
+
+ assert(s);
+ assert(iov);
+
+#if ENABLE_DNS_OVER_TLS
+ if (s->encrypted && !(flags & DNS_STREAM_WRITE_TLS_DATA)) {
+ ssize_t ss;
+ size_t i;
+
+ r = 0;
+ for (i = 0; i < iovcnt; i++) {
+ ss = dnstls_stream_write(s, iov[i].iov_base, iov[i].iov_len);
+ if (ss < 0)
+ return ss;
+
+ r += ss;
+ if (ss != (ssize_t) iov[i].iov_len)
+ continue;
+ }
+ } else
+#endif
+ if (s->tfo_salen > 0) {
+ struct msghdr hdr = {
+ .msg_iov = (struct iovec*) iov,
+ .msg_iovlen = iovcnt,
+ .msg_name = &s->tfo_address.sa,
+ .msg_namelen = s->tfo_salen
+ };
+
+ r = sendmsg(s->fd, &hdr, MSG_FASTOPEN);
+ if (r < 0) {
+ if (errno == EOPNOTSUPP) {
+ s->tfo_salen = 0;
+ r = connect(s->fd, &s->tfo_address.sa, s->tfo_salen);
+ if (r < 0)
+ return -errno;
+
+ r = -EAGAIN;
+ } else if (errno == EINPROGRESS)
+ r = -EAGAIN;
+ else
+ r = -errno;
+ } else
+ s->tfo_salen = 0; /* connection is made */
+ } else {
+ r = writev(s->fd, iov, iovcnt);
+ if (r < 0)
+ r = -errno;
+ }
+
+ return r;
+}
+
+static ssize_t dns_stream_read(DnsStream *s, void *buf, size_t count) {
+ ssize_t ss;
+
+#if ENABLE_DNS_OVER_TLS
+ if (s->encrypted)
+ ss = dnstls_stream_read(s, buf, count);
+ else
+#endif
+ {
+ ss = read(s->fd, buf, count);
+ if (ss < 0)
+ ss = -errno;
+ }
+
+ return ss;
+}
+
static int on_stream_timeout(sd_event_source *es, usec_t usec, void *userdata) {
DnsStream *s = userdata;
assert(s);
- r = dns_stream_identify(s);
- if (r < 0)
- return dns_stream_complete(s, -r);
+#if ENABLE_DNS_OVER_TLS
+ if (s->encrypted) {
+ r = dnstls_stream_on_io(s, revents);
+
+ if (r == DNSTLS_STREAM_CLOSED)
+ return 0;
+ else if (r == -EAGAIN)
+ return dns_stream_update_io(s);
+ else if (r < 0) {
+ return dns_stream_complete(s, -r);
+ } else {
+ r = dns_stream_update_io(s);
+ if (r < 0)
+ return r;
+ }
+ }
+#endif
+
+ /* only identify after connecting */
+ if (s->tfo_salen == 0) {
+ r = dns_stream_identify(s);
+ if (r < 0)
+ return dns_stream_complete(s, -r);
+ }
if ((revents & EPOLLOUT) &&
s->write_packet &&
IOVEC_INCREMENT(iov, 2, s->n_written);
- ss = writev(fd, iov, 2);
+ ss = dns_stream_writev(s, iov, 2, 0);
if (ss < 0) {
- if (!IN_SET(errno, EINTR, EAGAIN))
- return dns_stream_complete(s, errno);
+ if (!IN_SET(-ss, EINTR, EAGAIN))
+ return dns_stream_complete(s, -ss);
} else
s->n_written += ss;
if (s->n_read < sizeof(s->read_size)) {
ssize_t ss;
- ss = read(fd, (uint8_t*) &s->read_size + s->n_read, sizeof(s->read_size) - s->n_read);
+ ss = dns_stream_read(s, (uint8_t*) &s->read_size + s->n_read, sizeof(s->read_size) - s->n_read);
if (ss < 0) {
- if (!IN_SET(errno, EINTR, EAGAIN))
- return dns_stream_complete(s, errno);
+ if (!IN_SET(-ss, EINTR, EAGAIN))
+ return dns_stream_complete(s, -ss);
} else if (ss == 0)
return dns_stream_complete(s, ECONNRESET);
else
}
}
- ss = read(fd,
+ ss = dns_stream_read(s,
(uint8_t*) DNS_PACKET_DATA(s->read_packet) + s->n_read - sizeof(s->read_size),
sizeof(s->read_size) + be16toh(s->read_size) - s->n_read);
if (ss < 0) {
- if (!IN_SET(errno, EINTR, EAGAIN))
- return dns_stream_complete(s, errno);
+ if (!IN_SET(-ss, EINTR, EAGAIN))
+ return dns_stream_complete(s, -ss);
} else if (ss == 0)
return dns_stream_complete(s, ECONNRESET);
else
/* Are we done? If so, disable the event source for EPOLLIN */
if (s->n_read >= sizeof(s->read_size) + be16toh(s->read_size)) {
- r = dns_stream_update_io(s);
- if (r < 0)
- return dns_stream_complete(s, -r);
-
/* If there's a packet handler
* installed, call that. Note that
* this is optional... */
- if (s->on_packet)
- return s->on_packet(s);
+ if (s->on_packet) {
+ r = s->on_packet(s);
+ if (r < 0)
+ return r;
+ }
+
+ r = dns_stream_update_io(s);
+ if (r < 0)
+ return dns_stream_complete(s, -r);
}
}
}
return 0;
}
-DnsStream *dns_stream_unref(DnsStream *s) {
- if (!s)
- return NULL;
-
- assert(s->n_ref > 0);
- s->n_ref--;
+static DnsStream *dns_stream_free(DnsStream *s) {
+ DnsPacket *p;
+ Iterator i;
- if (s->n_ref > 0)
- return NULL;
+ assert(s);
dns_stream_stop(s);
+ if (s->server && s->server->stream == s)
+ s->server->stream = NULL;
+
if (s->manager) {
LIST_REMOVE(streams, s->manager->dns_streams, s);
s->manager->n_dns_streams--;
}
+#if ENABLE_DNS_OVER_TLS
+ if (s->encrypted)
+ dnstls_stream_free(s);
+#endif
+
+ ORDERED_SET_FOREACH(p, s->write_queue, i)
+ dns_packet_unref(ordered_set_remove(s->write_queue, p));
+
dns_packet_unref(s->write_packet);
dns_packet_unref(s->read_packet);
+ dns_server_unref(s->server);
+
+ ordered_set_free(s->write_queue);
return mfree(s);
}
-DEFINE_TRIVIAL_CLEANUP_FUNC(DnsStream*, dns_stream_unref);
-
-DnsStream *dns_stream_ref(DnsStream *s) {
- if (!s)
- return NULL;
-
- assert(s->n_ref > 0);
- s->n_ref++;
+DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsStream, dns_stream, dns_stream_free);
- return s;
-}
-
-int dns_stream_new(Manager *m, DnsStream **ret, DnsProtocol protocol, int fd) {
+int dns_stream_new(Manager *m, DnsStream **ret, DnsProtocol protocol, int fd, const union sockaddr_union *tfo_address) {
_cleanup_(dns_stream_unrefp) DnsStream *s = NULL;
int r;
if (!s)
return -ENOMEM;
+ r = ordered_set_ensure_allocated(&s->write_queue, &dns_packet_hash_ops);
+ if (r < 0)
+ return r;
+
s->n_ref = 1;
s->fd = -1;
s->protocol = protocol;
LIST_PREPEND(streams, m->dns_streams, s);
s->manager = m;
s->fd = fd;
+ if (tfo_address) {
+ s->tfo_address = *tfo_address;
+ s->tfo_salen = tfo_address->sa.sa_family == AF_INET6 ? sizeof(tfo_address->in6) : sizeof(tfo_address->in);
+ }
+
m->n_dns_streams++;
*ret = TAKE_PTR(s);
}
int dns_stream_write_packet(DnsStream *s, DnsPacket *p) {
+ int r;
+
assert(s);
- if (s->write_packet)
- return -EBUSY;
+ r = ordered_set_put(s->write_queue, p);
+ if (r < 0)
+ return r;
- s->write_packet = dns_packet_ref(p);
- s->write_size = htobe16(p->size);
- s->n_written = 0;
+ dns_packet_ref(p);
return dns_stream_update_io(s);
}