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