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