Merge libnetddns into libnetd_resolv

libnetddns is the library for DNS-over-TLS and is statically
linked to netd. Deprecate it and move them to libnetd_resolv
as a more general DNS library for netd.

This change comprises:

[1] Clean up netd/server/dns/*. Move all DnsTls* files to
    netd/resolv/ to parts of libnetd_resolv library.

[2] Export DnsTls* classes being visible for netd. It will only
    be temporary for a while.

[3] Remove the libssl dependency in netd. The relevant stuff is
    moved to libnetd_resolv.

Note that DnsTls* classes are still required for DnsProxyListener
and ResolverController to manipulate private DNS servers even after
this change.

Bug: 113628807
Test: as follows
    - built, flashed, booted
    - system/netd/tests/runtests.sh
    - DNS-over-TLS in live network passed

Change-Id: Ieac5889b4ebe737f876b3dcbe1a8da2b2b1b629d
diff --git a/resolv/include/netd_resolv/DnsTlsQueryMap.h b/resolv/include/netd_resolv/DnsTlsQueryMap.h
new file mode 100644
index 0000000..4c8010c
--- /dev/null
+++ b/resolv/include/netd_resolv/DnsTlsQueryMap.h
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _DNS_DNSTLSQUERYMAP_H
+#define _DNS_DNSTLSQUERYMAP_H
+
+#include <future>
+#include <map>
+#include <mutex>
+#include <vector>
+
+#include <android-base/thread_annotations.h>
+#include <netdutils/Slice.h>
+
+#include "DnsTlsServer.h"
+#include "params.h"
+
+namespace android {
+namespace net {
+
+using netdutils::Slice;
+
+// Keeps track of queries and responses.  This class matches responses with queries.
+// All methods are thread-safe and non-blocking.
+class LIBNETD_RESOLV_TLS_EXPORT DnsTlsQueryMap {
+public:
+    struct Query {
+        // The new ID number assigned to this query.
+        uint16_t newId;
+        // A query that has been passed to recordQuery(), with its original ID number.
+        const Slice query;
+    };
+
+    typedef DnsTlsServer::Response Response;
+    typedef DnsTlsServer::Result Result;
+
+    struct QueryFuture {
+        QueryFuture(Query query, std::future<Result> result) :
+                query(query), result(std::move(result)) {}
+        Query query;
+        // A future which will resolve to the result of this query.
+        std::future<Result> result;
+    };
+
+    // Returns an object containing everything needed to complete processing of
+    // this query, or null if the query could not be recorded.
+    std::unique_ptr<QueryFuture> recordQuery(const Slice query);
+
+    // Process a response, including a new ID.  If the response
+    // is not recognized as matching any query, it will be ignored.
+    void onResponse(std::vector<uint8_t> response);
+
+    // Clear all map contents.  This causes all pending queries to resolve with failure.
+    void clear();
+
+    // Get all pending queries.  This returns a shallow copy, mostly for thread-safety.
+    std::vector<Query> getAll();
+
+    // Mark a query has having been retried.  If the query hits the retry limit, it will
+    // be expired at the next call to cleanup.
+    void markTried(uint16_t newId);
+    void cleanup();
+
+    // Returns true if there are no pending queries.
+    bool empty();
+
+private:
+    std::mutex mLock;
+
+    struct QueryPromise {
+        QueryPromise(Query query) : query(query) {}
+        Query query;
+        // Number of times the query has been tried.  Limited to kMaxTries.
+        int tries = 0;
+        // A promise whose future is returned by recordQuery()
+        // It is fulfilled by onResponse().
+        std::promise<Result> result;
+    };
+
+    // The maximum number of times we will send a query before abandoning it.
+    static constexpr int kMaxTries = 3;
+
+    // Outstanding queries by newId.
+    std::map<uint16_t, QueryPromise> mQueries GUARDED_BY(mLock);
+
+    // Get a "newId" number that is not currently in use.  Returns -1 if there are none.
+    int32_t getFreeId() REQUIRES(mLock);
+
+    // Fulfill the result with an error code.
+    static void expire(QueryPromise* _Nonnull p);
+};
+
+}  // end of namespace net
+}  // end of namespace android
+
+#endif  // _DNS_DNSTLSQUERYMAP_H