Skip to content

Commit eff1077

Browse files
committed
resolved: Make event flags logic robust for DoT
Since when handling a DNS over TLS stream, the TLS library can override the requested events through dnstls_events for handshake/shutdown purposes, obtaining the event flags through sd_event_source_get_io_events and checking for EPOLLIN or EPOLLOUT does not really tell us whether we want to read/write a packet. Instead, it could just be OpenSSL/GnuTLS doing something else. To make the logic more robust (and simpler), save the flags that tell us whether we want to read/write a packet, and check them instead of the IO flags. (& use uint32_t for the flags like in sd_event_source_set_io_events prototype)
1 parent bb7031b commit eff1077

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

src/resolve/resolved-dns-stream.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static void dns_stream_stop(DnsStream *s) {
2727
}
2828

2929
static int dns_stream_update_io(DnsStream *s) {
30-
int f = 0;
30+
uint32_t f = 0;
3131

3232
assert(s);
3333

@@ -47,6 +47,8 @@ static int dns_stream_update_io(DnsStream *s) {
4747
set_size(s->queries) < DNS_QUERIES_PER_STREAM)
4848
f |= EPOLLIN;
4949

50+
s->requested_events = f;
51+
5052
#if ENABLE_DNS_OVER_TLS
5153
/* For handshake and clean closing purposes, TLS can override requested events */
5254
if (s->dnstls_events != 0)
@@ -452,19 +454,11 @@ static int on_stream_io_impl(DnsStream *s, uint32_t revents) {
452454
}
453455
}
454456

455-
if (s->type == DNS_STREAM_LLMNR_SEND && s->packet_received) {
456-
uint32_t events;
457-
458-
/* Complete the stream if finished reading and writing one packet, and there's nothing
459-
* else left to write. */
460-
461-
r = sd_event_source_get_io_events(s->io_event_source, &events);
462-
if (r < 0)
463-
return r;
464-
465-
if (!FLAGS_SET(events, EPOLLOUT))
466-
return dns_stream_complete(s, 0);
467-
}
457+
/* Complete the stream if finished reading and writing one packet, and there's nothing
458+
* else left to write. */
459+
if (s->type == DNS_STREAM_LLMNR_SEND && s->packet_received &&
460+
!FLAGS_SET(s->requested_events, EPOLLOUT))
461+
return dns_stream_complete(s, 0);
468462

469463
/* If we did something, let's restart the timeout event source */
470464
if (progressed && s->timeout_event_source) {
@@ -499,10 +493,7 @@ static int on_stream_io(sd_event_source *es, int fd, uint32_t revents, void *use
499493
uint32_t events;
500494

501495
/* Make sure the stream still wants to process more data... */
502-
r = sd_event_source_get_io_events(s->io_event_source, &events);
503-
if (r < 0)
504-
return r;
505-
if (!FLAGS_SET(events, EPOLLIN))
496+
if (!FLAGS_SET(s->requested_events, EPOLLIN))
506497
break;
507498

508499
r = on_stream_io_impl(s, EPOLLIN);

src/resolve/resolved-dns-stream.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ struct DnsStream {
6161
uint32_t ttl;
6262
bool identified;
6363
bool packet_received; /* At least one packet is received. Used by LLMNR. */
64+
uint32_t requested_events;
6465

6566
/* only when using TCP fast open */
6667
union sockaddr_union tfo_address;
6768
socklen_t tfo_salen;
6869

6970
#if ENABLE_DNS_OVER_TLS
7071
DnsTlsStreamData dnstls_data;
71-
int dnstls_events;
72+
uint32_t dnstls_events;
7273
#endif
7374

7475
sd_event_source *io_event_source;

0 commit comments

Comments
 (0)