]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolved-dns-stream.c
man/systemd-sysext: list ephemeral/ephemeral-import in the list of options
[thirdparty/systemd.git] / src / resolve / resolved-dns-stream.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
623a4c97 2
f5947a5e 3#include <unistd.h>
623a4c97 4
284d7641
DDM
5#include "sd-event.h"
6
b5efdb8a 7#include "alloc-util.h"
284d7641 8#include "errno-util.h"
3ffd4af2 9#include "fd-util.h"
bd1ae178 10#include "iovec-util.h"
284d7641 11#include "log.h"
f5947a5e 12#include "missing_network.h"
284d7641 13#include "ordered-set.h"
68527d30
DDM
14#include "resolved-dns-packet.h"
15#include "resolved-dns-server.h"
623a4c97 16#include "resolved-dns-stream.h"
be28f72d 17#include "resolved-manager.h"
284d7641
DDM
18#include "set.h"
19#include "time-util.h"
623a4c97 20
623a4c97
LP
21#define DNS_STREAMS_MAX 128
22
b412af57
LP
23#define DNS_QUERIES_PER_STREAM 32
24
623a4c97
LP
25static void dns_stream_stop(DnsStream *s) {
26 assert(s);
27
97935302
ZJS
28 s->io_event_source = sd_event_source_disable_unref(s->io_event_source);
29 s->timeout_event_source = sd_event_source_disable_unref(s->timeout_event_source);
623a4c97 30 s->fd = safe_close(s->fd);
808089ae
LP
31
32 /* Disconnect us from the server object if we are now not usable anymore */
33 dns_stream_detach(s);
623a4c97
LP
34}
35
36static int dns_stream_update_io(DnsStream *s) {
eff10773 37 uint32_t f = 0;
623a4c97
LP
38
39 assert(s);
40
41 if (s->write_packet && s->n_written < sizeof(s->write_size) + s->write_packet->size)
42 f |= EPOLLOUT;
98767d75
IT
43 else if (!ordered_set_isempty(s->write_queue)) {
44 dns_packet_unref(s->write_packet);
45 s->write_packet = ordered_set_steal_first(s->write_queue);
46 s->write_size = htobe16(s->write_packet->size);
47 s->n_written = 0;
48 f |= EPOLLOUT;
49 }
b412af57
LP
50
51 /* Let's read a packet if we haven't queued any yet. Except if we already hit a limit of parallel
52 * queries for this connection. */
53 if ((!s->read_packet || s->n_read < sizeof(s->read_size) + s->read_packet->size) &&
54 set_size(s->queries) < DNS_QUERIES_PER_STREAM)
623a4c97
LP
55 f |= EPOLLIN;
56
eff10773
JB
57 s->requested_events = f;
58
ba6aaf57
IT
59#if ENABLE_DNS_OVER_TLS
60 /* For handshake and clean closing purposes, TLS can override requested events */
57bdb749 61 if (s->dnstls_events != 0)
ba6aaf57
IT
62 f = s->dnstls_events;
63#endif
64
623a4c97
LP
65 return sd_event_source_set_io_events(s->io_event_source, f);
66}
67
b914e211 68static int dns_stream_complete(DnsStream *s, int error) {
d973d94d
LP
69 _cleanup_(dns_stream_unrefp) _unused_ DnsStream *ref = dns_stream_ref(s); /* Protect stream while we process it */
70
623a4c97 71 assert(s);
f447d9e3
LP
72 assert(error >= 0);
73
74 /* Error is > 0 when the connection failed for some reason in the network stack. It's == 0 if we sent
5238e957 75 * and received exactly one packet each (in the LLMNR client case). */
623a4c97 76
56ddbf10 77#if ENABLE_DNS_OVER_TLS
6016fcb0 78 if (s->encrypted) {
5d67a7ae
IT
79 int r;
80
6016fcb0
IT
81 r = dnstls_stream_shutdown(s, error);
82 if (r != -EAGAIN)
5d67a7ae
IT
83 dns_stream_stop(s);
84 } else
85#endif
86 dns_stream_stop(s);
623a4c97 87
7172e4ee
LP
88 dns_stream_detach(s);
89
623a4c97
LP
90 if (s->complete)
91 s->complete(s, error);
b30bf55d
LP
92 else /* the default action if no completion function is set is to close the stream */
93 dns_stream_unref(s);
623a4c97
LP
94
95 return 0;
96}
97
b914e211 98static int dns_stream_identify(DnsStream *s) {
fb29cdbe 99 CMSG_BUFFER_TYPE(CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo))
08ab1861 100 + CMSG_SPACE(int) + /* for the TTL */
fb29cdbe 101 + EXTRA_CMSG_SPACE /* kernel appears to require extra space */) control;
b914e211
LP
102 struct msghdr mh = {};
103 struct cmsghdr *cmsg;
104 socklen_t sl;
105 int r;
106
107 assert(s);
108
109 if (s->identified)
110 return 0;
111
112 /* Query the local side */
113 s->local_salen = sizeof(s->local);
114 r = getsockname(s->fd, &s->local.sa, &s->local_salen);
115 if (r < 0)
116 return -errno;
117 if (s->local.sa.sa_family == AF_INET6 && s->ifindex <= 0)
118 s->ifindex = s->local.in6.sin6_scope_id;
119
120 /* Query the remote side */
121 s->peer_salen = sizeof(s->peer);
122 r = getpeername(s->fd, &s->peer.sa, &s->peer_salen);
123 if (r < 0)
124 return -errno;
125 if (s->peer.sa.sa_family == AF_INET6 && s->ifindex <= 0)
126 s->ifindex = s->peer.in6.sin6_scope_id;
127
128 /* Check consistency */
129 assert(s->peer.sa.sa_family == s->local.sa.sa_family);
130 assert(IN_SET(s->peer.sa.sa_family, AF_INET, AF_INET6));
131
132 /* Query connection meta information */
133 sl = sizeof(control);
134 if (s->peer.sa.sa_family == AF_INET) {
135 r = getsockopt(s->fd, IPPROTO_IP, IP_PKTOPTIONS, &control, &sl);
136 if (r < 0)
137 return -errno;
138 } else if (s->peer.sa.sa_family == AF_INET6) {
139
140 r = getsockopt(s->fd, IPPROTO_IPV6, IPV6_2292PKTOPTIONS, &control, &sl);
141 if (r < 0)
142 return -errno;
143 } else
144 return -EAFNOSUPPORT;
145
146 mh.msg_control = &control;
147 mh.msg_controllen = sl;
2a1288ff
LP
148
149 CMSG_FOREACH(cmsg, &mh) {
b914e211
LP
150
151 if (cmsg->cmsg_level == IPPROTO_IPV6) {
152 assert(s->peer.sa.sa_family == AF_INET6);
153
154 switch (cmsg->cmsg_type) {
155
156 case IPV6_PKTINFO: {
b1d02191 157 struct in6_pktinfo *i = CMSG_TYPED_DATA(cmsg, struct in6_pktinfo);
b914e211
LP
158
159 if (s->ifindex <= 0)
160 s->ifindex = i->ipi6_ifindex;
161 break;
162 }
163
164 case IPV6_HOPLIMIT:
b1d02191 165 s->ttl = *CMSG_TYPED_DATA(cmsg, int);
b914e211
LP
166 break;
167 }
168
169 } else if (cmsg->cmsg_level == IPPROTO_IP) {
170 assert(s->peer.sa.sa_family == AF_INET);
171
172 switch (cmsg->cmsg_type) {
173
174 case IP_PKTINFO: {
b1d02191 175 struct in_pktinfo *i = CMSG_TYPED_DATA(cmsg, struct in_pktinfo);
b914e211
LP
176
177 if (s->ifindex <= 0)
178 s->ifindex = i->ipi_ifindex;
179 break;
180 }
181
182 case IP_TTL:
b1d02191 183 s->ttl = *CMSG_TYPED_DATA(cmsg, int);
b914e211
LP
184 break;
185 }
186 }
187 }
188
189 /* The Linux kernel sets the interface index to the loopback
190 * device if the connection came from the local host since it
191 * avoids the routing table in such a case. Let's unset the
192 * interface index in such a case. */
a5f03596 193 if (s->ifindex == LOOPBACK_IFINDEX)
b914e211
LP
194 s->ifindex = 0;
195
196 /* If we don't know the interface index still, we look for the
197 * first local interface with a matching address. Yuck! */
198 if (s->ifindex <= 0)
b1dea5cf 199 s->ifindex = manager_find_ifindex(s->manager, s->local.sa.sa_family, sockaddr_in_addr(&s->local.sa));
b914e211
LP
200
201 if (s->protocol == DNS_PROTOCOL_LLMNR && s->ifindex > 0) {
b914e211 202 /* Make sure all packets for this connection are sent on the same interface */
5d0fe423
LP
203 r = socket_set_unicast_if(s->fd, s->local.sa.sa_family, s->ifindex);
204 if (r < 0)
be6aa742 205 log_debug_errno(r, "Failed to invoke IP_UNICAST_IF/IPV6_UNICAST_IF: %m");
b914e211
LP
206 }
207
208 s->identified = true;
209
210 return 0;
211}
212
6016fcb0 213ssize_t dns_stream_writev(DnsStream *s, const struct iovec *iov, size_t iovcnt, int flags) {
e6dc5556 214 ssize_t m;
5699e4c2 215 int r;
91ccab1e
IT
216
217 assert(s);
218 assert(iov);
219
56ddbf10 220#if ENABLE_DNS_OVER_TLS
aa892849
JB
221 if (s->encrypted && !(flags & DNS_STREAM_WRITE_TLS_DATA))
222 return dnstls_stream_writev(s, iov, iovcnt);
5d67a7ae 223#endif
aa892849 224
91ccab1e
IT
225 if (s->tfo_salen > 0) {
226 struct msghdr hdr = {
227 .msg_iov = (struct iovec*) iov,
228 .msg_iovlen = iovcnt,
229 .msg_name = &s->tfo_address.sa,
230 .msg_namelen = s->tfo_salen
231 };
232
e6dc5556
LP
233 m = sendmsg(s->fd, &hdr, MSG_FASTOPEN);
234 if (m < 0) {
5699e4c2
LP
235 if (ERRNO_IS_NOT_SUPPORTED(errno)) {
236 /* MSG_FASTOPEN not supported? Then try to connect() traditionally */
237 r = RET_NERRNO(connect(s->fd, &s->tfo_address.sa, s->tfo_salen));
238 s->tfo_salen = 0; /* connection is made */
239 if (r < 0 && r != -EINPROGRESS)
240 return r;
241
242 return -EAGAIN; /* In case of EINPROGRESS, EAGAIN or success: return EAGAIN, so that caller calls us again */
e6dc5556
LP
243 }
244 if (errno == EINPROGRESS)
245 return -EAGAIN;
246
247 return -errno;
91ccab1e
IT
248 } else
249 s->tfo_salen = 0; /* connection is made */
f6c9c5f8 250 } else {
e6dc5556
LP
251 m = writev(s->fd, iov, iovcnt);
252 if (m < 0)
253 return -errno;
f6c9c5f8 254 }
91ccab1e 255
e6dc5556 256 return m;
91ccab1e
IT
257}
258
5d67a7ae
IT
259static ssize_t dns_stream_read(DnsStream *s, void *buf, size_t count) {
260 ssize_t ss;
261
56ddbf10 262#if ENABLE_DNS_OVER_TLS
6016fcb0
IT
263 if (s->encrypted)
264 ss = dnstls_stream_read(s, buf, count);
265 else
5d67a7ae 266#endif
f6c9c5f8 267 {
5d67a7ae 268 ss = read(s->fd, buf, count);
f6c9c5f8 269 if (ss < 0)
94fdb4d9 270 return -errno;
f6c9c5f8 271 }
5d67a7ae
IT
272
273 return ss;
274}
275
623a4c97 276static int on_stream_timeout(sd_event_source *es, usec_t usec, void *userdata) {
99534007 277 DnsStream *s = ASSERT_PTR(userdata);
623a4c97 278
b914e211 279 return dns_stream_complete(s, ETIMEDOUT);
623a4c97
LP
280}
281
624f907e
YW
282static DnsPacket *dns_stream_take_read_packet(DnsStream *s) {
283 assert(s);
284
4aa61298
YW
285 /* Note, dns_stream_update() should be called after this is called. When this is called, the
286 * stream may be already full and the EPOLLIN flag is dropped from the stream IO event source.
287 * Even this makes a room to read in the stream, this does not call dns_stream_update(), hence
288 * EPOLLIN flag is not set automatically. So, to read further packets from the stream,
289 * dns_stream_update() must be called explicitly. Currently, this is only called from
839a70c3 290 * on_stream_io(), and there dns_stream_update() is called. */
4aa61298 291
624f907e
YW
292 if (!s->read_packet)
293 return NULL;
294
295 if (s->n_read < sizeof(s->read_size))
296 return NULL;
297
298 if (s->n_read < sizeof(s->read_size) + be16toh(s->read_size))
299 return NULL;
300
301 s->n_read = 0;
302 return TAKE_PTR(s->read_packet);
303}
304
839a70c3
JB
305static int on_stream_io(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
306 _cleanup_(dns_stream_unrefp) DnsStream *s = dns_stream_ref(userdata); /* Protect stream while we process it */
5971dffd 307 bool progressed = false;
623a4c97
LP
308 int r;
309
310 assert(s);
311
56ddbf10 312#if ENABLE_DNS_OVER_TLS
6016fcb0 313 if (s->encrypted) {
04c4d919 314 r = dnstls_stream_on_io(s, revents);
ba6aaf57 315 if (r == DNSTLS_STREAM_CLOSED)
6016fcb0 316 return 0;
b2cf6704 317 if (r == -EAGAIN)
ba6aaf57 318 return dns_stream_update_io(s);
b2cf6704 319 if (r < 0)
6016fcb0 320 return dns_stream_complete(s, -r);
b2cf6704
LP
321
322 r = dns_stream_update_io(s);
323 if (r < 0)
324 return r;
5d67a7ae
IT
325 }
326#endif
327
91ccab1e
IT
328 /* only identify after connecting */
329 if (s->tfo_salen == 0) {
330 r = dns_stream_identify(s);
331 if (r < 0)
332 return dns_stream_complete(s, -r);
333 }
b914e211 334
ddd710a3
RP
335 if (revents & EPOLLERR) {
336 socklen_t errlen = sizeof(r);
337 if (getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &r, &errlen) == 0)
338 return dns_stream_complete(s, r);
339 }
340
623a4c97
LP
341 if ((revents & EPOLLOUT) &&
342 s->write_packet &&
343 s->n_written < sizeof(s->write_size) + s->write_packet->size) {
344
cf1e6e62
ZJS
345 struct iovec iov[] = {
346 IOVEC_MAKE(&s->write_size, sizeof(s->write_size)),
347 IOVEC_MAKE(DNS_PACKET_DATA(s->write_packet), s->write_packet->size),
348 };
623a4c97 349
986235a9 350 iovec_increment(iov, ELEMENTSOF(iov), s->n_written);
623a4c97 351
cf1e6e62 352 ssize_t ss = dns_stream_writev(s, iov, ELEMENTSOF(iov), 0);
623a4c97 353 if (ss < 0) {
8add30a0 354 if (!ERRNO_IS_TRANSIENT(ss))
f6c9c5f8 355 return dns_stream_complete(s, -ss);
5971dffd
LP
356 } else {
357 progressed = true;
623a4c97 358 s->n_written += ss;
5971dffd 359 }
623a4c97
LP
360
361 /* Are we done? If so, disable the event source for EPOLLOUT */
362 if (s->n_written >= sizeof(s->write_size) + s->write_packet->size) {
363 r = dns_stream_update_io(s);
364 if (r < 0)
b914e211 365 return dns_stream_complete(s, -r);
623a4c97
LP
366 }
367 }
368
facc9439
LP
369 while (s->identified && /* Only read data once we identified the peer, because we cannot fill in the DNS packet meta info otherwise */
370 (revents & (EPOLLIN|EPOLLHUP|EPOLLRDHUP)) &&
839a70c3
JB
371 (!s->read_packet ||
372 s->n_read < sizeof(s->read_size) + s->read_packet->size)) {
623a4c97
LP
373
374 if (s->n_read < sizeof(s->read_size)) {
375 ssize_t ss;
376
5d67a7ae 377 ss = dns_stream_read(s, (uint8_t*) &s->read_size + s->n_read, sizeof(s->read_size) - s->n_read);
623a4c97 378 if (ss < 0) {
8add30a0 379 if (!ERRNO_IS_TRANSIENT(ss))
f6c9c5f8 380 return dns_stream_complete(s, -ss);
839a70c3 381 break;
623a4c97 382 } else if (ss == 0)
b914e211 383 return dns_stream_complete(s, ECONNRESET);
5971dffd
LP
384 else {
385 progressed = true;
623a4c97 386 s->n_read += ss;
5971dffd 387 }
623a4c97
LP
388 }
389
390 if (s->n_read >= sizeof(s->read_size)) {
391
392 if (be16toh(s->read_size) < DNS_PACKET_HEADER_SIZE)
b914e211 393 return dns_stream_complete(s, EBADMSG);
623a4c97
LP
394
395 if (s->n_read < sizeof(s->read_size) + be16toh(s->read_size)) {
396 ssize_t ss;
397
398 if (!s->read_packet) {
51027656 399 r = dns_packet_new(&s->read_packet, s->protocol, be16toh(s->read_size), DNS_PACKET_SIZE_MAX);
623a4c97 400 if (r < 0)
b914e211 401 return dns_stream_complete(s, -r);
623a4c97
LP
402
403 s->read_packet->size = be16toh(s->read_size);
404 s->read_packet->ipproto = IPPROTO_TCP;
405 s->read_packet->family = s->peer.sa.sa_family;
406 s->read_packet->ttl = s->ttl;
407 s->read_packet->ifindex = s->ifindex;
ba4e0427 408 s->read_packet->timestamp = now(CLOCK_BOOTTIME);
623a4c97
LP
409
410 if (s->read_packet->family == AF_INET) {
411 s->read_packet->sender.in = s->peer.in.sin_addr;
412 s->read_packet->sender_port = be16toh(s->peer.in.sin_port);
413 s->read_packet->destination.in = s->local.in.sin_addr;
414 s->read_packet->destination_port = be16toh(s->local.in.sin_port);
415 } else {
416 assert(s->read_packet->family == AF_INET6);
417 s->read_packet->sender.in6 = s->peer.in6.sin6_addr;
418 s->read_packet->sender_port = be16toh(s->peer.in6.sin6_port);
419 s->read_packet->destination.in6 = s->local.in6.sin6_addr;
420 s->read_packet->destination_port = be16toh(s->local.in6.sin6_port);
421
422 if (s->read_packet->ifindex == 0)
423 s->read_packet->ifindex = s->peer.in6.sin6_scope_id;
424 if (s->read_packet->ifindex == 0)
425 s->read_packet->ifindex = s->local.in6.sin6_scope_id;
426 }
427 }
428
5d67a7ae 429 ss = dns_stream_read(s,
623a4c97
LP
430 (uint8_t*) DNS_PACKET_DATA(s->read_packet) + s->n_read - sizeof(s->read_size),
431 sizeof(s->read_size) + be16toh(s->read_size) - s->n_read);
432 if (ss < 0) {
8add30a0 433 if (!ERRNO_IS_TRANSIENT(ss))
99521cab 434 return dns_stream_complete(s, -ss);
839a70c3 435 break;
623a4c97 436 } else if (ss == 0)
b914e211 437 return dns_stream_complete(s, ECONNRESET);
623a4c97
LP
438 else
439 s->n_read += ss;
440 }
441
18230451
YW
442 /* Are we done? If so, call the packet handler and re-enable EPOLLIN for the
443 * event source if necessary. */
624f907e
YW
444 _cleanup_(dns_packet_unrefp) DnsPacket *p = dns_stream_take_read_packet(s);
445 if (p) {
18230451 446 assert(s->on_packet);
624f907e 447 r = s->on_packet(s, p);
18230451
YW
448 if (r < 0)
449 return r;
98767d75
IT
450
451 r = dns_stream_update_io(s);
452 if (r < 0)
453 return dns_stream_complete(s, -r);
a5e2a488
YW
454
455 s->packet_received = true;
839a70c3
JB
456
457 /* If we just disabled the read event, stop reading */
458 if (!FLAGS_SET(s->requested_events, EPOLLIN))
459 break;
623a4c97
LP
460 }
461 }
462 }
463
eff10773
JB
464 /* Complete the stream if finished reading and writing one packet, and there's nothing
465 * else left to write. */
466 if (s->type == DNS_STREAM_LLMNR_SEND && s->packet_received &&
467 !FLAGS_SET(s->requested_events, EPOLLOUT))
468 return dns_stream_complete(s, 0);
623a4c97 469
5971dffd
LP
470 /* If we did something, let's restart the timeout event source */
471 if (progressed && s->timeout_event_source) {
e1158539 472 r = sd_event_source_set_time_relative(s->timeout_event_source, DNS_STREAM_ESTABLISHED_TIMEOUT_USEC);
5971dffd 473 if (r < 0)
be6aa742 474 log_warning_errno(r, "Couldn't restart TCP connection timeout, ignoring: %m");
5971dffd
LP
475 }
476
623a4c97
LP
477 return 0;
478}
479
8301aa0b 480static DnsStream *dns_stream_free(DnsStream *s) {
98767d75 481 DnsPacket *p;
98767d75 482
8301aa0b 483 assert(s);
b30bf55d 484
623a4c97
LP
485 dns_stream_stop(s);
486
487 if (s->manager) {
488 LIST_REMOVE(streams, s->manager->dns_streams, s);
652ba568 489 s->manager->n_dns_streams[s->type]--;
623a4c97
LP
490 }
491
56ddbf10 492#if ENABLE_DNS_OVER_TLS
6016fcb0
IT
493 if (s->encrypted)
494 dnstls_stream_free(s);
5d67a7ae
IT
495#endif
496
90e74a66 497 ORDERED_SET_FOREACH(p, s->write_queue)
98767d75
IT
498 dns_packet_unref(ordered_set_remove(s->write_queue, p));
499
623a4c97
LP
500 dns_packet_unref(s->write_packet);
501 dns_packet_unref(s->read_packet);
98767d75
IT
502 dns_server_unref(s->server);
503
504 ordered_set_free(s->write_queue);
623a4c97 505
6b430fdb 506 return mfree(s);
623a4c97
LP
507}
508
8301aa0b 509DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsStream, dns_stream, dns_stream_free);
623a4c97 510
b27a32a0
LP
511int dns_stream_new(
512 Manager *m,
513 DnsStream **ret,
652ba568 514 DnsStreamType type,
b27a32a0
LP
515 DnsProtocol protocol,
516 int fd,
e1158539 517 const union sockaddr_union *tfo_address,
624f907e 518 int (on_packet)(DnsStream*, DnsPacket*),
18230451 519 int (complete)(DnsStream*, int), /* optional */
e1158539 520 usec_t connect_timeout_usec) {
b27a32a0 521
b30bf55d 522 _cleanup_(dns_stream_unrefp) DnsStream *s = NULL;
623a4c97
LP
523 int r;
524
525 assert(m);
499aa1d3 526 assert(ret);
652ba568
LP
527 assert(type >= 0);
528 assert(type < _DNS_STREAM_TYPE_MAX);
529 assert(protocol >= 0);
530 assert(protocol < _DNS_PROTOCOL_MAX);
623a4c97 531 assert(fd >= 0);
18230451 532 assert(on_packet);
623a4c97 533
652ba568 534 if (m->n_dns_streams[type] > DNS_STREAMS_MAX)
623a4c97
LP
535 return -EBUSY;
536
898892e8 537 s = new(DnsStream, 1);
623a4c97
LP
538 if (!s)
539 return -ENOMEM;
540
898892e8
LP
541 *s = (DnsStream) {
542 .n_ref = 1,
254d1313 543 .fd = -EBADF,
898892e8 544 .protocol = protocol,
1c089741 545 .type = type,
898892e8
LP
546 };
547
98767d75
IT
548 r = ordered_set_ensure_allocated(&s->write_queue, &dns_packet_hash_ops);
549 if (r < 0)
550 return r;
551
623a4c97
LP
552 r = sd_event_add_io(m->event, &s->io_event_source, fd, EPOLLIN, on_stream_io, s);
553 if (r < 0)
554 return r;
555
aa4a9deb
LP
556 (void) sd_event_source_set_description(s->io_event_source, "dns-stream-io");
557
39cf0351 558 r = sd_event_add_time_relative(
9a015429
LP
559 m->event,
560 &s->timeout_event_source,
ba4e0427 561 CLOCK_BOOTTIME,
e1158539 562 connect_timeout_usec, 0,
9a015429 563 on_stream_timeout, s);
623a4c97
LP
564 if (r < 0)
565 return r;
566
aa4a9deb
LP
567 (void) sd_event_source_set_description(s->timeout_event_source, "dns-stream-timeout");
568
623a4c97 569 LIST_PREPEND(streams, m->dns_streams, s);
652ba568 570 m->n_dns_streams[type]++;
623a4c97 571 s->manager = m;
08e254c8 572
623a4c97 573 s->fd = fd;
18230451
YW
574 s->on_packet = on_packet;
575 s->complete = complete;
08e254c8 576
91ccab1e
IT
577 if (tfo_address) {
578 s->tfo_address = *tfo_address;
a0233fcd 579 s->tfo_salen = sockaddr_len(tfo_address);
91ccab1e
IT
580 }
581
1cc6c93a 582 *ret = TAKE_PTR(s);
623a4c97
LP
583
584 return 0;
585}
586
587int dns_stream_write_packet(DnsStream *s, DnsPacket *p) {
98767d75
IT
588 int r;
589
623a4c97 590 assert(s);
499aa1d3 591 assert(p);
623a4c97 592
98767d75
IT
593 r = ordered_set_put(s->write_queue, p);
594 if (r < 0)
595 return r;
623a4c97 596
98767d75 597 dns_packet_ref(p);
623a4c97
LP
598
599 return dns_stream_update_io(s);
600}
aa337a5e 601
808089ae
LP
602void dns_stream_detach(DnsStream *s) {
603 assert(s);
604
605 if (!s->server)
606 return;
607
608 if (s->server->stream != s)
609 return;
610
611 dns_server_unref_stream(s->server);
612}
7addc530
LP
613
614DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
615 dns_stream_hash_ops,
616 void,
617 trivial_hash_func,
618 trivial_compare_func,
619 dns_stream_unref);
620
621int dns_stream_disconnect_all(Manager *m) {
fe5a1afb 622 _cleanup_set_free_ Set *closed = NULL;
7addc530
LP
623 int r;
624
625 assert(m);
626
627 /* Terminates all TCP connections (called after system suspend for example, to speed up recovery) */
628
629 log_info("Closing all remaining TCP connections.");
630
631 bool restart;
632 do {
633 restart = false;
634
635 LIST_FOREACH(streams, s, m->dns_streams) {
636 r = set_ensure_put(&closed, &dns_stream_hash_ops, s);
637 if (r < 0)
638 return log_oom();
639 if (r > 0) {
640 /* Haven't seen this one before. Close it. */
641 dns_stream_ref(s);
642 (void) dns_stream_complete(s, ECONNRESET);
643
644 /* This might have a ripple effect, let's hence no look at the list further,
645 * but scan from the beginning again */
646 restart = true;
647 break;
648 }
649 }
650 } while (restart);
651
652 return 0;
653}