mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: [musl] TCP DNS outline
Date: Tue, 20 Sep 2022 00:37:45 -0400	[thread overview]
Message-ID: <20220920043744.GJ9709@brightrain.aerifal.cx> (raw)

What follow are some notes I've put together in preparation for TCP
fallback addition to DNS query core. Open to comments/concerns.



Current DNS query core state machine outline:

Single socket fd for sending/receiving UDP messages.
One or more answer buffers: answers[nqueries]
Lengths of accepted answers, 0 if none: alens[nqueries]

On each loop iteration, queries that don't yet have answers get
[re]sent if retry interval is exceeded.

UDP socket fd is poll()ed with timeout, then messages are recvfrom'd
and processed until no more are available. For each:

- It is read into the lowest-indexed answer buffer that's not yet
  filled.

- Query id is used to determine which question it goes with. If none
  or if that question already has an answer, it's discarded and
  processing continues to next packet.

- RCODE is checked; if non-conclusive the packet is ignored and
  processing continues to next packet (possibly with early retry, but
  this is not core to the logic)

- If answer it accepted, it's moved to the right answer slot (possibly
  the one it's already in) and the size is recorded, marking the slot
  as having an accepted answer.

This process continues until all questions have answers or timeout.



Changes for TCP:

Additional state:
For polling multiple socket fds: struct pollfd [nqueries+1]. This
requires nqueries be well-bounded, but it's always 1 or 2 anyway. The
+1 is for the UDP socket. The pollfd array itself can store socket fds
so we don't need separate storage for them.
Buffer positions for handling partial read: pos[nqueries]

Logic changes:

An answer slot that's being queried by TCP is always considered in-use
for the purpose of receiving UDP. If all (both) queries have switched
to TCP, no UDP will be accepted. UDP received for a slot that's
already in "TCP mode" will be dropped just like if the slot already
have a UDP answe.

When checking the validity (see above: query id, RCODE) of a UDP
answer, if the TC bit is set, a predicate will be evaluated to decide
whether to accept the truncated answer. If so, there is no change. If
not, a new TCP socket will be opened to the nameserver address that
issued the TC'd answer, and added to the pollfd set. (This should
probably use TCP fastopen API if the kernel supports it, and only fall
back if not. I think this means that, if the MSG_FASTOPEN sendmsg
succeeds, we can go straight to polling for read, and only need to
connect and poll for write first if it doesn't.) Buffer position is
initialized to -2 (2 bytes prior to payload start) for the prepended
BE16 length field that's not part of the answer and that will be read
into a separate location via iovec.

When poll reports a TCP socket writable, send the corresponding query
payload (looping to send the whole thing; assume it won't block after
partial send of <280 bytes). Then switch the pollfd events to only
poll for read.

When poll reports a TCP socket ready for read, use buffer position and
readv or recvmsg to iovec-read into answer size and answer payload,
advancing buffer position accordingly. If we reach the answer length,
or hit the full answer buffer size without reaching it, close the
connection and deem the answer completed. If we get EOF or socket
error or see an inconclusive rcode, close the connection and deem the
query failed (alens[i]=0).


                 reply	other threads:[~2022-09-20  4:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220920043744.GJ9709@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).