]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
5eef597e MP |
2 | |
3 | #include <netinet/tcp.h> | |
4 | ||
db2df898 MP |
5 | #include "alloc-util.h" |
6 | #include "fd-util.h" | |
7 | #include "io-util.h" | |
5eef597e MP |
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 | ||
bb4f798a MB |
14 | #define DNS_QUERIES_PER_STREAM 32 |
15 | ||
5eef597e MP |
16 | static 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); | |
6e866b33 MB |
22 | |
23 | /* Disconnect us from the server object if we are now not usable anymore */ | |
24 | dns_stream_detach(s); | |
5eef597e MP |
25 | } |
26 | ||
27 | static 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; | |
b012e921 MB |
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 | } | |
bb4f798a MB |
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) | |
5eef597e MP |
46 | f |= EPOLLIN; |
47 | ||
6e866b33 MB |
48 | #if ENABLE_DNS_OVER_TLS |
49 | /* For handshake and clean closing purposes, TLS can override requested events */ | |
bb4f798a | 50 | if (s->dnstls_events != 0) |
6e866b33 MB |
51 | f = s->dnstls_events; |
52 | #endif | |
53 | ||
5eef597e MP |
54 | return sd_event_source_set_io_events(s->io_event_source, f); |
55 | } | |
56 | ||
57 | static int dns_stream_complete(DnsStream *s, int error) { | |
6e866b33 MB |
58 | _cleanup_(dns_stream_unrefp) _unused_ DnsStream *ref = dns_stream_ref(s); /* Protect stream while we process it */ |
59 | ||
5eef597e | 60 | assert(s); |
bb4f798a MB |
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 | |
64 | * and receieved exactly one packet each (in the LLMNR client case). */ | |
5eef597e | 65 | |
b012e921 | 66 | #if ENABLE_DNS_OVER_TLS |
6e866b33 | 67 | if (s->encrypted) { |
b012e921 MB |
68 | int r; |
69 | ||
6e866b33 MB |
70 | r = dnstls_stream_shutdown(s, error); |
71 | if (r != -EAGAIN) | |
b012e921 MB |
72 | dns_stream_stop(s); |
73 | } else | |
74 | #endif | |
75 | dns_stream_stop(s); | |
5eef597e | 76 | |
6e866b33 MB |
77 | dns_stream_detach(s); |
78 | ||
5eef597e MP |
79 | if (s->complete) |
80 | s->complete(s, error); | |
5a920b42 MP |
81 | else /* the default action if no completion function is set is to close the stream */ |
82 | dns_stream_unref(s); | |
5eef597e MP |
83 | |
84 | return 0; | |
85 | } | |
86 | ||
87 | static int dns_stream_identify(DnsStream *s) { | |
88 | union { | |
89 | struct cmsghdr header; /* For alignment */ | |
90 | uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo)) | |
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; | |
86f210e9 MP |
139 | |
140 | CMSG_FOREACH(cmsg, &mh) { | |
5eef597e MP |
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. */ | |
184 | if (s->ifindex == LOOPBACK_IFINDEX) | |
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) | |
f47781d8 | 199 | log_debug_errno(errno, "Failed to invoke IP_UNICAST_IF: %m"); |
5eef597e MP |
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) | |
f47781d8 | 203 | log_debug_errno(errno, "Failed to invoke IPV6_UNICAST_IF: %m"); |
5eef597e MP |
204 | } |
205 | } | |
206 | ||
207 | s->identified = true; | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
6e866b33 MB |
212 | ssize_t dns_stream_writev(DnsStream *s, const struct iovec *iov, size_t iovcnt, int flags) { |
213 | ssize_t m; | |
b012e921 MB |
214 | |
215 | assert(s); | |
216 | assert(iov); | |
217 | ||
218 | #if ENABLE_DNS_OVER_TLS | |
6e866b33 | 219 | if (s->encrypted && !(flags & DNS_STREAM_WRITE_TLS_DATA)) { |
b012e921 MB |
220 | ssize_t ss; |
221 | size_t i; | |
222 | ||
6e866b33 | 223 | m = 0; |
b012e921 | 224 | for (i = 0; i < iovcnt; i++) { |
6e866b33 MB |
225 | ss = dnstls_stream_write(s, iov[i].iov_base, iov[i].iov_len); |
226 | if (ss < 0) | |
227 | return ss; | |
b012e921 | 228 | |
6e866b33 | 229 | m += ss; |
b012e921 MB |
230 | if (ss != (ssize_t) iov[i].iov_len) |
231 | continue; | |
232 | } | |
233 | } else | |
234 | #endif | |
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 | ||
6e866b33 MB |
243 | m = sendmsg(s->fd, &hdr, MSG_FASTOPEN); |
244 | if (m < 0) { | |
b012e921 MB |
245 | if (errno == EOPNOTSUPP) { |
246 | s->tfo_salen = 0; | |
6e866b33 | 247 | if (connect(s->fd, &s->tfo_address.sa, s->tfo_salen) < 0) |
b012e921 MB |
248 | return -errno; |
249 | ||
6e866b33 MB |
250 | return -EAGAIN; |
251 | } | |
252 | if (errno == EINPROGRESS) | |
253 | return -EAGAIN; | |
254 | ||
255 | return -errno; | |
b012e921 MB |
256 | } else |
257 | s->tfo_salen = 0; /* connection is made */ | |
258 | } else { | |
6e866b33 MB |
259 | m = writev(s->fd, iov, iovcnt); |
260 | if (m < 0) | |
261 | return -errno; | |
b012e921 MB |
262 | } |
263 | ||
6e866b33 | 264 | return m; |
b012e921 MB |
265 | } |
266 | ||
267 | static ssize_t dns_stream_read(DnsStream *s, void *buf, size_t count) { | |
268 | ssize_t ss; | |
269 | ||
270 | #if ENABLE_DNS_OVER_TLS | |
6e866b33 MB |
271 | if (s->encrypted) |
272 | ss = dnstls_stream_read(s, buf, count); | |
273 | else | |
b012e921 MB |
274 | #endif |
275 | { | |
276 | ss = read(s->fd, buf, count); | |
277 | if (ss < 0) | |
6e866b33 | 278 | return -errno; |
b012e921 MB |
279 | } |
280 | ||
281 | return ss; | |
282 | } | |
283 | ||
5eef597e MP |
284 | static int on_stream_timeout(sd_event_source *es, usec_t usec, void *userdata) { |
285 | DnsStream *s = userdata; | |
286 | ||
287 | assert(s); | |
288 | ||
289 | return dns_stream_complete(s, ETIMEDOUT); | |
290 | } | |
291 | ||
292 | static int on_stream_io(sd_event_source *es, int fd, uint32_t revents, void *userdata) { | |
6e866b33 | 293 | _cleanup_(dns_stream_unrefp) DnsStream *s = dns_stream_ref(userdata); /* Protect stream while we process it */ |
bb4f798a | 294 | bool progressed = false; |
5eef597e MP |
295 | int r; |
296 | ||
297 | assert(s); | |
298 | ||
b012e921 | 299 | #if ENABLE_DNS_OVER_TLS |
6e866b33 MB |
300 | if (s->encrypted) { |
301 | r = dnstls_stream_on_io(s, revents); | |
302 | if (r == DNSTLS_STREAM_CLOSED) | |
303 | return 0; | |
304 | if (r == -EAGAIN) | |
305 | return dns_stream_update_io(s); | |
306 | if (r < 0) | |
307 | return dns_stream_complete(s, -r); | |
b012e921 | 308 | |
6e866b33 MB |
309 | r = dns_stream_update_io(s); |
310 | if (r < 0) | |
311 | return r; | |
b012e921 MB |
312 | } |
313 | #endif | |
314 | ||
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 | } | |
5eef597e MP |
321 | |
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 | ||
6e866b33 MB |
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); | |
5eef597e MP |
331 | |
332 | IOVEC_INCREMENT(iov, 2, s->n_written); | |
333 | ||
b012e921 | 334 | ss = dns_stream_writev(s, iov, 2, 0); |
5eef597e | 335 | if (ss < 0) { |
b012e921 MB |
336 | if (!IN_SET(-ss, EINTR, EAGAIN)) |
337 | return dns_stream_complete(s, -ss); | |
bb4f798a MB |
338 | } else { |
339 | progressed = true; | |
5eef597e | 340 | s->n_written += ss; |
bb4f798a | 341 | } |
5eef597e MP |
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) | |
347 | return dns_stream_complete(s, -r); | |
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 | ||
b012e921 | 358 | ss = dns_stream_read(s, (uint8_t*) &s->read_size + s->n_read, sizeof(s->read_size) - s->n_read); |
5eef597e | 359 | if (ss < 0) { |
b012e921 MB |
360 | if (!IN_SET(-ss, EINTR, EAGAIN)) |
361 | return dns_stream_complete(s, -ss); | |
5eef597e MP |
362 | } else if (ss == 0) |
363 | return dns_stream_complete(s, ECONNRESET); | |
bb4f798a MB |
364 | else { |
365 | progressed = true; | |
5eef597e | 366 | s->n_read += ss; |
bb4f798a | 367 | } |
5eef597e MP |
368 | } |
369 | ||
370 | if (s->n_read >= sizeof(s->read_size)) { | |
371 | ||
372 | if (be16toh(s->read_size) < DNS_PACKET_HEADER_SIZE) | |
373 | return dns_stream_complete(s, EBADMSG); | |
374 | ||
375 | if (s->n_read < sizeof(s->read_size) + be16toh(s->read_size)) { | |
376 | ssize_t ss; | |
377 | ||
378 | if (!s->read_packet) { | |
f5e65279 | 379 | r = dns_packet_new(&s->read_packet, s->protocol, be16toh(s->read_size), DNS_PACKET_SIZE_MAX); |
5eef597e MP |
380 | if (r < 0) |
381 | return dns_stream_complete(s, -r); | |
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 | ||
b012e921 | 408 | ss = dns_stream_read(s, |
5eef597e MP |
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) { | |
6e866b33 MB |
412 | if (!IN_SET(-ss, EINTR, EAGAIN)) |
413 | return dns_stream_complete(s, -ss); | |
5eef597e MP |
414 | } else if (ss == 0) |
415 | return dns_stream_complete(s, ECONNRESET); | |
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)) { | |
5eef597e MP |
422 | /* If there's a packet handler |
423 | * installed, call that. Note that | |
424 | * this is optional... */ | |
b012e921 MB |
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); | |
5eef597e MP |
434 | } |
435 | } | |
436 | } | |
437 | ||
bb4f798a MB |
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) && | |
5eef597e MP |
443 | (s->read_packet && s->n_read >= sizeof(s->read_size) + s->read_packet->size)) |
444 | return dns_stream_complete(s, 0); | |
445 | ||
bb4f798a MB |
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 | ||
5eef597e MP |
453 | return 0; |
454 | } | |
455 | ||
6e866b33 | 456 | static DnsStream *dns_stream_free(DnsStream *s) { |
b012e921 MB |
457 | DnsPacket *p; |
458 | Iterator i; | |
459 | ||
6e866b33 | 460 | assert(s); |
5a920b42 | 461 | |
5eef597e MP |
462 | dns_stream_stop(s); |
463 | ||
464 | if (s->manager) { | |
465 | LIST_REMOVE(streams, s->manager->dns_streams, s); | |
bb4f798a | 466 | s->manager->n_dns_streams[s->type]--; |
5eef597e MP |
467 | } |
468 | ||
b012e921 | 469 | #if ENABLE_DNS_OVER_TLS |
6e866b33 MB |
470 | if (s->encrypted) |
471 | dnstls_stream_free(s); | |
b012e921 MB |
472 | #endif |
473 | ||
474 | ORDERED_SET_FOREACH(p, s->write_queue, i) | |
475 | dns_packet_unref(ordered_set_remove(s->write_queue, p)); | |
476 | ||
5eef597e MP |
477 | dns_packet_unref(s->write_packet); |
478 | dns_packet_unref(s->read_packet); | |
b012e921 MB |
479 | dns_server_unref(s->server); |
480 | ||
481 | ordered_set_free(s->write_queue); | |
5eef597e | 482 | |
8a584da2 | 483 | return mfree(s); |
5eef597e MP |
484 | } |
485 | ||
6e866b33 | 486 | DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsStream, dns_stream, dns_stream_free); |
5a920b42 | 487 | |
6e866b33 MB |
488 | int dns_stream_new( |
489 | Manager *m, | |
490 | DnsStream **ret, | |
bb4f798a | 491 | DnsStreamType type, |
6e866b33 MB |
492 | DnsProtocol protocol, |
493 | int fd, | |
494 | const union sockaddr_union *tfo_address) { | |
5eef597e | 495 | |
5a920b42 | 496 | _cleanup_(dns_stream_unrefp) DnsStream *s = NULL; |
5eef597e MP |
497 | int r; |
498 | ||
499 | assert(m); | |
6e866b33 | 500 | assert(ret); |
bb4f798a MB |
501 | assert(type >= 0); |
502 | assert(type < _DNS_STREAM_TYPE_MAX); | |
503 | assert(protocol >= 0); | |
504 | assert(protocol < _DNS_PROTOCOL_MAX); | |
5eef597e MP |
505 | assert(fd >= 0); |
506 | ||
bb4f798a | 507 | if (m->n_dns_streams[type] > DNS_STREAMS_MAX) |
5eef597e MP |
508 | return -EBUSY; |
509 | ||
6e866b33 | 510 | s = new(DnsStream, 1); |
5eef597e MP |
511 | if (!s) |
512 | return -ENOMEM; | |
513 | ||
6e866b33 MB |
514 | *s = (DnsStream) { |
515 | .n_ref = 1, | |
516 | .fd = -1, | |
517 | .protocol = protocol, | |
518 | }; | |
519 | ||
b012e921 MB |
520 | r = ordered_set_ensure_allocated(&s->write_queue, &dns_packet_hash_ops); |
521 | if (r < 0) | |
522 | return r; | |
523 | ||
5eef597e MP |
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 | ||
4c89c718 MP |
528 | (void) sd_event_source_set_description(s->io_event_source, "dns-stream-io"); |
529 | ||
5eef597e MP |
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); | |
536 | if (r < 0) | |
537 | return r; | |
538 | ||
4c89c718 MP |
539 | (void) sd_event_source_set_description(s->timeout_event_source, "dns-stream-timeout"); |
540 | ||
5eef597e | 541 | LIST_PREPEND(streams, m->dns_streams, s); |
bb4f798a | 542 | m->n_dns_streams[type]++; |
5eef597e | 543 | s->manager = m; |
6e866b33 | 544 | |
5eef597e | 545 | s->fd = fd; |
6e866b33 | 546 | |
b012e921 MB |
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 | ||
b012e921 MB |
552 | *ret = TAKE_PTR(s); |
553 | ||
554 | return 0; | |
555 | } | |
556 | ||
5eef597e | 557 | int dns_stream_write_packet(DnsStream *s, DnsPacket *p) { |
b012e921 MB |
558 | int r; |
559 | ||
5eef597e | 560 | assert(s); |
6e866b33 | 561 | assert(p); |
5eef597e | 562 | |
b012e921 MB |
563 | r = ordered_set_put(s->write_queue, p); |
564 | if (r < 0) | |
565 | return r; | |
5eef597e | 566 | |
b012e921 | 567 | dns_packet_ref(p); |
5eef597e MP |
568 | |
569 | return dns_stream_update_io(s); | |
570 | } | |
6e866b33 MB |
571 | |
572 | DnsPacket *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 | } | |
587 | ||
588 | void 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 | } |