DnsTlsQueryMap.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef _DNS_DNSTLSQUERYMAP_H
  17. #define _DNS_DNSTLSQUERYMAP_H
  18. #include <future>
  19. #include <map>
  20. #include <mutex>
  21. #include <vector>
  22. #include <android-base/thread_annotations.h>
  23. #include <netdutils/Slice.h>
  24. #include "DnsTlsServer.h"
  25. namespace android {
  26. namespace net {
  27. // Keeps track of queries and responses. This class matches responses with queries.
  28. // All methods are thread-safe and non-blocking.
  29. class DnsTlsQueryMap {
  30. public:
  31. struct Query {
  32. // The new ID number assigned to this query.
  33. uint16_t newId;
  34. // A query that has been passed to recordQuery(), with its original ID number.
  35. const netdutils::Slice query;
  36. };
  37. typedef DnsTlsServer::Response Response;
  38. typedef DnsTlsServer::Result Result;
  39. struct QueryFuture {
  40. QueryFuture(Query query, std::future<Result> result)
  41. : query(query), result(std::move(result)) {}
  42. Query query;
  43. // A future which will resolve to the result of this query.
  44. std::future<Result> result;
  45. };
  46. // Returns an object containing everything needed to complete processing of
  47. // this query, or null if the query could not be recorded.
  48. std::unique_ptr<QueryFuture> recordQuery(const netdutils::Slice query);
  49. // Process a response, including a new ID. If the response
  50. // is not recognized as matching any query, it will be ignored.
  51. void onResponse(std::vector<uint8_t> response);
  52. // Clear all map contents. This causes all pending queries to resolve with failure.
  53. void clear();
  54. // Get all pending queries. This returns a shallow copy, mostly for thread-safety.
  55. std::vector<Query> getAll();
  56. // Mark a query has having been retried. If the query hits the retry limit, it will
  57. // be expired at the next call to cleanup.
  58. void markTried(uint16_t newId);
  59. void cleanup();
  60. // Returns true if there are no pending queries.
  61. bool empty();
  62. private:
  63. std::mutex mLock;
  64. struct QueryPromise {
  65. QueryPromise(Query query) : query(query) {}
  66. Query query;
  67. // Number of times the query has been tried. Limited to kMaxTries.
  68. int tries = 0;
  69. // A promise whose future is returned by recordQuery()
  70. // It is fulfilled by onResponse().
  71. std::promise<Result> result;
  72. };
  73. // The maximum number of times we will send a query before abandoning it.
  74. static constexpr int kMaxTries = 3;
  75. // Outstanding queries by newId.
  76. std::map<uint16_t, QueryPromise> mQueries GUARDED_BY(mLock);
  77. // Get a "newId" number that is not currently in use. Returns -1 if there are none.
  78. int32_t getFreeId() REQUIRES(mLock);
  79. // Fulfill the result with an error code.
  80. static void expire(QueryPromise* _Nonnull p);
  81. };
  82. } // end of namespace net
  83. } // end of namespace android
  84. #endif // _DNS_DNSTLSQUERYMAP_H