• Home
Name Date Size #Lines LOC

..--

aidl/dnsresolver/03-May-2024-12890

binder/android/net/03-May-2024-25252

dns_responder/03-May-2024-2,0041,585

include/netd_resolv/03-May-2024-25591

tests/03-May-2024-318178

Android.bpD03-May-20245.1 KiB221211

AndroidTest.xmlD03-May-20241.8 KiB3914

Dns64Configuration.cppD03-May-20248.3 KiB234144

Dns64Configuration.hD03-May-20244.4 KiB12367

DnsProxyListener.cppD03-May-202443.6 KiB1,222946

DnsProxyListener.hD03-May-20244.4 KiB151101

DnsResolver.cppD03-May-20242.4 KiB8352

DnsResolver.hD03-May-20241.3 KiB5226

DnsResolverService.cppD03-May-202410.7 KiB298193

DnsResolverService.hD03-May-20242.6 KiB7338

DnsTlsDispatcher.cppD03-May-20246 KiB181128

DnsTlsDispatcher.hD03-May-20244.7 KiB11044

DnsTlsQueryMap.cppD03-May-20244.1 KiB151113

DnsTlsQueryMap.hD03-May-20243.4 KiB10748

DnsTlsServer.cppD03-May-20244.6 KiB13491

DnsTlsServer.hD03-May-20242.4 KiB7931

DnsTlsSessionCache.cppD03-May-20242.3 KiB7848

DnsTlsSessionCache.hD03-May-20242 KiB6123

DnsTlsSocket.cppD03-May-202417.6 KiB549425

DnsTlsSocket.hD03-May-20245.7 KiB13751

DnsTlsSocketFactory.hD03-May-20241.6 KiB5025

DnsTlsTransport.cppD03-May-20247 KiB215150

DnsTlsTransport.hD03-May-20243.1 KiB9446

IDnsTlsSocket.hD03-May-20241.7 KiB4917

IDnsTlsSocketFactory.hD03-May-20241.3 KiB4318

IDnsTlsSocketObserver.hD03-May-20241.2 KiB3913

LockedQueue.hD03-May-20241.3 KiB5326

NOTICED03-May-202421.6 KiB419349

PrivateDnsConfiguration.cppD03-May-202411.8 KiB316228

PrivateDnsConfiguration.hD03-May-20243.2 KiB9346

README.mdD03-May-20246.6 KiB135104

ResolverController.cppD03-May-202415.6 KiB383306

ResolverController.hD03-May-20242.2 KiB6835

ResolverEventReporter.cppD03-May-20245.2 KiB12966

ResolverEventReporter.hD03-May-20242.5 KiB6932

ResolverStats.hD03-May-20244.3 KiB12279

dns_tls_test.cppD03-May-202432.9 KiB964741

dnsresolver_binder_test.cppD03-May-202414.5 KiB370259

getaddrinfo.cppD03-May-202457.9 KiB1,8251,298

getaddrinfo.hD03-May-2024931 265

gethnamaddr.cppD03-May-202433.3 KiB945728

gethnamaddr.hD03-May-20241.1 KiB286

hostent.hD03-May-20242.9 KiB7332

libnetd_resolv.map.txtD03-May-2024852 2825

libnetd_resolv_test.cppD03-May-202425.7 KiB592452

res_cache.cppD03-May-202464.4 KiB2,0261,232

res_comp.cppD03-May-20247.7 KiB21378

res_debug.cppD03-May-202421.8 KiB541349

res_debug.hD03-May-2024708 223

res_init.cppD03-May-202414.5 KiB398248

res_mkquery.cppD03-May-20249.6 KiB251140

res_query.cppD03-May-202414.6 KiB381192

res_send.cppD03-May-202446.2 KiB1,308901

res_send.hD03-May-2024893 244

res_state.cppD03-May-20244 KiB13171

res_state_ext.hD03-May-2024519 2316

res_stats.cppD03-May-20246.6 KiB166118

resolv_cache.hD03-May-20243.2 KiB7623

resolv_private.hD03-May-202410.3 KiB241123

resolv_static.hD03-May-2024819 3419

resolver_test.cppD03-May-2024139.7 KiB3,4722,624

sethostent.cppD03-May-20246.7 KiB205126

stats.protoD03-May-20248.4 KiB249222

README.md

1# DNS-over-TLS query forwarder design
2
3## Overview
4
5The DNS-over-TLS query forwarder consists of five classes:
6 * `DnsTlsDispatcher`
7 * `DnsTlsTransport`
8 * `DnsTlsQueryMap`
9 * `DnsTlsSessionCache`
10 * `DnsTlsSocket`
11
12`DnsTlsDispatcher` is a singleton class whose `query` method is the DnsTls's
13only public interface.  `DnsTlsDispatcher` is just a table holding the
14`DnsTlsTransport` for each server (represented by a `DnsTlsServer` struct) and
15network.  `DnsTlsDispatcher` also blocks each query thread, waiting on a
16`std::future` returned by `DnsTlsTransport` that represents the response.
17
18`DnsTlsTransport` sends each query over a `DnsTlsSocket`, opening a
19new one if necessary.  It also has to listen for responses from the
20`DnsTlsSocket`, which happen on a different thread.
21`IDnsTlsSocketObserver` is an interface defining how `DnsTlsSocket` returns
22responses to `DnsTlsTransport`.
23
24`DnsTlsQueryMap` and `DnsTlsSessionCache` are helper classes owned by `DnsTlsTransport`.
25`DnsTlsQueryMap` handles ID renumbering and query-response pairing.
26`DnsTlsSessionCache` allows TLS session resumption.
27
28`DnsTlsSocket` interleaves all queries onto a single socket, and reports all
29responses to `DnsTlsTransport` (through the `IDnsTlsObserver` interface).  It doesn't
30know anything about which queries correspond to which responses, and does not retain
31state to indicate whether there is an outstanding query.
32
33## Threading
34
35### Overall patterns
36
37For clarity, each of the five classes in this design is thread-safe and holds one lock.
38Classes that spawn a helper thread call `thread::join()` in their destructor to ensure
39that it is cleaned up appropriately.
40
41All the classes here make full use of Clang thread annotations (and also null-pointer
42annotations) to minimize the likelihood of a latent threading bug.  The unit tests are
43also heavily threaded to exercise this functionality.
44
45This code creates O(1) threads per socket, and does not create a new thread for each
46query or response.  However, DnsProxyListener does create a thread for each query.
47
48### Threading in `DnsTlsSocket`
49
50`DnsTlsSocket` can receive queries on any thread, and send them over a
51"reliable datagram pipe" (`socketpair()` in `SOCK_SEQPACKET` mode).
52The query method writes a struct (containing a pointer to the query) to the pipe
53from its thread, and the loop thread (which owns the SSL socket)
54reads off the other end of the pipe.  The pipe doesn't actually have a queue "inside";
55instead, any queueing happens by blocking the query thread until the
56socket thread can read the datagram off the other end.
57
58We need to pass messages between threads using a pipe, and not a condition variable
59or a thread-safe queue, because the socket thread has to be blocked
60in `poll()` waiting for data from the server, but also has to be woken
61up on inputs from the query threads.  Therefore, inputs from the query
62threads have to arrive on a socket, so that `poll()` can listen for them.
63(There can only be a single thread because [you can't use different threads
64to read and write in OpenSSL](https://www.openssl.org/blog/blog/2017/02/21/threads/)).
65
66## ID renumbering
67
68`DnsTlsDispatcher` accepts queries that have colliding ID numbers and still sends them on
69a single socket.  To avoid confusion at the server, `DnsTlsQueryMap` assigns each
70query a new ID for transmission, records the mapping from input IDs to sent IDs, and
71applies the inverse mapping to responses before returning them to the caller.
72
73`DnsTlsQueryMap` assigns each new query the ID number one greater than the largest
74ID number of an outstanding query.  This means that ID numbers are initially sequential
75and usually small.  If the largest possible ID number is already in use,
76`DnsTlsQueryMap` will scan the ID space to find an available ID, or fail the query
77if there are no available IDs.  Queries will not block waiting for an ID number to
78become available.
79
80## Time constants
81
82`DnsTlsSocket` imposes a 20-second inactivity timeout.  A socket that has been idle for
8320 seconds will be closed.  This sets the limit of tolerance for slow replies,
84which could happen as a result of malfunctioning authoritative DNS servers.
85If there are any pending queries, `DnsTlsTransport` will retry them.
86
87`DnsTlsQueryMap` imposes a retry limit of 3.  `DnsTlsTransport` will retry the query up
88to 3 times before reporting failure to `DnsTlsDispatcher`.
89This limit helps to ensure proper functioning in the case of a recursive resolver that
90is malfunctioning or is flooded with requests that are stalled due to malfunctioning
91authoritative servers.
92
93`DnsTlsDispatcher` maintains a 5-minute timeout.  Any `DnsTlsTransport` that has had no
94outstanding queries for 5 minutes will be destroyed at the next query on a different
95transport.
96This sets the limit on how long session tickets will be preserved during idle periods,
97because each `DnsTlsTransport` owns a `DnsTlsSessionCache`.  Imposing this timeout
98increases latency on the first query after an idle period, but also helps to avoid
99unbounded memory usage.
100
101`DnsTlsSessionCache` sets a limit of 5 sessions in each cache, expiring the oldest one
102when the limit is reached.  However, because the client code does not currently
103reuse sessions more than once, it should not be possible to hit this limit.
104
105## Testing
106
107Unit tests are in `dns_tls_test.cpp`. They cover all the classes except
108`DnsTlsSocket` (which requires `CAP_NET_ADMIN` because it uses `setsockopt(SO_MARK)`) and
109`DnsTlsSessionCache` (which requires integration with libssl).  These classes are
110exercised by the integration tests in `../tests/resolv_test.cpp`.
111
112### Dependency Injection
113
114For unit testing, we would like to be able to mock out `DnsTlsSocket`.  This is
115particularly required for unit testing of `DnsTlsDispatcher` and `DnsTlsTransport`.
116To make these unit tests possible, this code uses a dependency injection pattern:
117`DnsTlsSocket` is produced by a `DnsTlsSocketFactory`, and both of these have a
118defined interface.
119
120`DnsTlsDispatcher`'s constructor takes an `IDnsTlsSocketFactory`,
121which in production is a `DnsTlsSocketFactory`.  However, in unit tests, we can
122substitute a test factory that returns a fake socket, so that the unit tests can
123run without actually connecting over TLS to a test server.  (The integration tests
124do actual TLS.)
125
126## Logging
127
128This code uses `ALOGV` throughout for low-priority logging, and does not use
129`ALOGD`.  `ALOGV` is disabled by default, unless activated by `#define LOG_NDEBUG 0`.
130(`ALOGD` is not disabled by default, requiring extra measures to avoid spamming the
131system log in production builds.)
132
133## Reference
134 * [BoringSSL API docs](https://commondatastorage.googleapis.com/chromium-boringssl-docs/headers.html)
135