]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
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" |
f5947a5e | 9 | #include "missing_network.h" |
623a4c97 | 10 | #include "resolved-dns-stream.h" |
be28f72d | 11 | #include "resolved-manager.h" |
623a4c97 LP |
12 | |
13 | #define DNS_STREAM_TIMEOUT_USEC (10 * USEC_PER_SEC) | |
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 | ||
21 | s->io_event_source = sd_event_source_unref(s->io_event_source); | |
22 | s->timeout_event_source = sd_event_source_unref(s->timeout_event_source); | |
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) { | |
30 | int f = 0; | |
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 | ||
ba6aaf57 IT |
50 | #if ENABLE_DNS_OVER_TLS |
51 | /* For handshake and clean closing purposes, TLS can override requested events */ | |
57bdb749 | 52 | if (s->dnstls_events != 0) |
ba6aaf57 IT |
53 | f = s->dnstls_events; |
54 | #endif | |
55 | ||
623a4c97 LP |
56 | return sd_event_source_set_io_events(s->io_event_source, f); |
57 | } | |
58 | ||
b914e211 | 59 | static int dns_stream_complete(DnsStream *s, int error) { |
d973d94d LP |
60 | _cleanup_(dns_stream_unrefp) _unused_ DnsStream *ref = dns_stream_ref(s); /* Protect stream while we process it */ |
61 | ||
623a4c97 | 62 | assert(s); |
f447d9e3 LP |
63 | assert(error >= 0); |
64 | ||
65 | /* Error is > 0 when the connection failed for some reason in the network stack. It's == 0 if we sent | |
5238e957 | 66 | * and received exactly one packet each (in the LLMNR client case). */ |
623a4c97 | 67 | |
56ddbf10 | 68 | #if ENABLE_DNS_OVER_TLS |
6016fcb0 | 69 | if (s->encrypted) { |
5d67a7ae IT |
70 | int r; |
71 | ||
6016fcb0 IT |
72 | r = dnstls_stream_shutdown(s, error); |
73 | if (r != -EAGAIN) | |
5d67a7ae IT |
74 | dns_stream_stop(s); |
75 | } else | |
76 | #endif | |
77 | dns_stream_stop(s); | |
623a4c97 | 78 | |
7172e4ee LP |
79 | dns_stream_detach(s); |
80 | ||
623a4c97 LP |
81 | if (s->complete) |
82 | s->complete(s, error); | |
b30bf55d LP |
83 | else /* the default action if no completion function is set is to close the stream */ |
84 | dns_stream_unref(s); | |
623a4c97 LP |
85 | |
86 | return 0; | |
87 | } | |
88 | ||
b914e211 | 89 | static int dns_stream_identify(DnsStream *s) { |
fb29cdbe | 90 | CMSG_BUFFER_TYPE(CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo)) |
08ab1861 | 91 | + CMSG_SPACE(int) + /* for the TTL */ |
fb29cdbe | 92 | + EXTRA_CMSG_SPACE /* kernel appears to require extra space */) control; |
b914e211 LP |
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) { | |
13adf353 | 193 | be32_t ifindex = htobe32(s->ifindex); |
b914e211 LP |
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 | 212 | ssize_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 |
267 | static 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 |
284 | static 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 | ||
292 | static 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 | 456 | static 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 | 486 | DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsStream, dns_stream, dns_stream_free); |
623a4c97 | 487 | |
b27a32a0 LP |
488 | int 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, | |
1c089741 | 518 | .type = type, |
898892e8 LP |
519 | }; |
520 | ||
98767d75 IT |
521 | r = ordered_set_ensure_allocated(&s->write_queue, &dns_packet_hash_ops); |
522 | if (r < 0) | |
523 | return r; | |
524 | ||
623a4c97 LP |
525 | r = sd_event_add_io(m->event, &s->io_event_source, fd, EPOLLIN, on_stream_io, s); |
526 | if (r < 0) | |
527 | return r; | |
528 | ||
aa4a9deb LP |
529 | (void) sd_event_source_set_description(s->io_event_source, "dns-stream-io"); |
530 | ||
9a015429 LP |
531 | r = sd_event_add_time( |
532 | m->event, | |
533 | &s->timeout_event_source, | |
534 | clock_boottime_or_monotonic(), | |
535 | now(clock_boottime_or_monotonic()) + DNS_STREAM_TIMEOUT_USEC, 0, | |
536 | on_stream_timeout, s); | |
623a4c97 LP |
537 | if (r < 0) |
538 | return r; | |
539 | ||
aa4a9deb LP |
540 | (void) sd_event_source_set_description(s->timeout_event_source, "dns-stream-timeout"); |
541 | ||
623a4c97 | 542 | LIST_PREPEND(streams, m->dns_streams, s); |
652ba568 | 543 | m->n_dns_streams[type]++; |
623a4c97 | 544 | s->manager = m; |
08e254c8 | 545 | |
623a4c97 | 546 | s->fd = fd; |
08e254c8 | 547 | |
91ccab1e IT |
548 | if (tfo_address) { |
549 | s->tfo_address = *tfo_address; | |
550 | s->tfo_salen = tfo_address->sa.sa_family == AF_INET6 ? sizeof(tfo_address->in6) : sizeof(tfo_address->in); | |
551 | } | |
552 | ||
1cc6c93a | 553 | *ret = TAKE_PTR(s); |
623a4c97 LP |
554 | |
555 | return 0; | |
556 | } | |
557 | ||
558 | int dns_stream_write_packet(DnsStream *s, DnsPacket *p) { | |
98767d75 IT |
559 | int r; |
560 | ||
623a4c97 | 561 | assert(s); |
499aa1d3 | 562 | assert(p); |
623a4c97 | 563 | |
98767d75 IT |
564 | r = ordered_set_put(s->write_queue, p); |
565 | if (r < 0) | |
566 | return r; | |
623a4c97 | 567 | |
98767d75 | 568 | dns_packet_ref(p); |
623a4c97 LP |
569 | |
570 | return dns_stream_update_io(s); | |
571 | } | |
aa337a5e LP |
572 | |
573 | DnsPacket *dns_stream_take_read_packet(DnsStream *s) { | |
574 | assert(s); | |
575 | ||
576 | if (!s->read_packet) | |
577 | return NULL; | |
578 | ||
579 | if (s->n_read < sizeof(s->read_size)) | |
580 | return NULL; | |
581 | ||
582 | if (s->n_read < sizeof(s->read_size) + be16toh(s->read_size)) | |
583 | return NULL; | |
584 | ||
585 | s->n_read = 0; | |
586 | return TAKE_PTR(s->read_packet); | |
587 | } | |
808089ae LP |
588 | |
589 | void dns_stream_detach(DnsStream *s) { | |
590 | assert(s); | |
591 | ||
592 | if (!s->server) | |
593 | return; | |
594 | ||
595 | if (s->server->stream != s) | |
596 | return; | |
597 | ||
598 | dns_server_unref_stream(s->server); | |
599 | } |