Before anyone asks: I've seen When do DNS queries use TCP instead of UDP? and it doesn't answer my question.
All I keep hearing is "if the answer is too long, DNS will use TCP". This does not explain how it happens though.
So here's the situation: DNS client asks for resolution of a record using UDP. The record is too long for UDP:
- server answers with specific opcode, to have client switch to TCP
- server doesn't answer at all, and client re-tries over TCP
- server opens TCP connection to client (stupid, if you count NAT, but who knows?)
- client somehow (?) 'knows' that given query should be run over TCP so it doesn't bother with UDP in the first place
- DNS pixies magically turn UDP into TCP when needed
I've been looking all over the internet for the answer, but there's lot of noise (see above), and I can't seem to write proper Google query for that (nor can I find the info in RFCs, for that matter).
The client does not know in advance that the response will be too large, so it will query the server via UDP.
The server will respond via UDP and will include as much as possible and set the truncated header bit ("TC" http://www.networksorcery.com/enp/protocol/dns.htm).
The client can then resend the request via TCP and get the full response.
See also: https://www.rfc-editor.org/rfc/rfc5966
And: https://www.ietf.org/rfc/rfc2181.txt
And as mentioned in the comments, of course DNS zone transfers are always using TCP.