blob: c4ae45076ac65b966de9282b17143263d6bffd7c [file] [log] [blame]
Mike Yubab3daa2018-10-19 22:11:43 +08001/*
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
Ken Chen5471dca2019-04-15 15:25:35 +080017#define LOG_TAG "resolv"
Mike Yubab3daa2018-10-19 22:11:43 +080018
Bernie Innocentiec4219b2019-01-30 11:16:36 +090019#include "DnsTlsQueryMap.h"
Mike Yubab3daa2018-10-19 22:11:43 +080020
chenbruceaff85842019-05-31 15:46:42 +080021#include <android-base/logging.h>
Mike Yubab3daa2018-10-19 22:11:43 +080022
23namespace android {
24namespace net {
25
Bernie Innocentiec4219b2019-01-30 11:16:36 +090026std::unique_ptr<DnsTlsQueryMap::QueryFuture> DnsTlsQueryMap::recordQuery(
27 const netdutils::Slice query) {
Mike Yubab3daa2018-10-19 22:11:43 +080028 std::lock_guard guard(mLock);
29
30 // Store the query so it can be matched to the response or reissued.
31 if (query.size() < 2) {
chenbruceaff85842019-05-31 15:46:42 +080032 LOG(WARNING) << "Query is too short";
Mike Yubab3daa2018-10-19 22:11:43 +080033 return nullptr;
34 }
35 int32_t newId = getFreeId();
36 if (newId < 0) {
chenbruceaff85842019-05-31 15:46:42 +080037 LOG(WARNING) << "All query IDs are in use";
Mike Yubab3daa2018-10-19 22:11:43 +080038 return nullptr;
39 }
Mike Yu7e08b852019-10-18 18:27:43 +080040
41 // Make a copy of the query.
42 std::vector<uint8_t> tmp(query.base(), query.base() + query.size());
43 Query q = {.newId = static_cast<uint16_t>(newId), .query = std::move(tmp)};
44
45 const auto [it, inserted] = mQueries.try_emplace(newId, q);
Mike Yubab3daa2018-10-19 22:11:43 +080046 if (!inserted) {
chenbruceaff85842019-05-31 15:46:42 +080047 LOG(ERROR) << "Failed to store pending query";
Mike Yubab3daa2018-10-19 22:11:43 +080048 return nullptr;
49 }
50 return std::make_unique<QueryFuture>(q, it->second.result.get_future());
51}
52
53void DnsTlsQueryMap::expire(QueryPromise* p) {
54 Result r = { .code = Response::network_error };
55 p->result.set_value(r);
56}
57
58void DnsTlsQueryMap::markTried(uint16_t newId) {
59 std::lock_guard guard(mLock);
60 auto it = mQueries.find(newId);
61 if (it != mQueries.end()) {
62 it->second.tries++;
63 }
64}
65
66void DnsTlsQueryMap::cleanup() {
67 std::lock_guard guard(mLock);
68 for (auto it = mQueries.begin(); it != mQueries.end();) {
69 auto& p = it->second;
70 if (p.tries >= kMaxTries) {
71 expire(&p);
72 it = mQueries.erase(it);
73 } else {
74 ++it;
75 }
76 }
77}
78
79int32_t DnsTlsQueryMap::getFreeId() {
80 if (mQueries.empty()) {
81 return 0;
82 }
83 uint16_t maxId = mQueries.rbegin()->first;
84 if (maxId < UINT16_MAX) {
85 return maxId + 1;
86 }
87 if (mQueries.size() == UINT16_MAX + 1) {
88 // Map is full.
89 return -1;
90 }
91 // Linear scan.
92 uint16_t nextId = 0;
93 for (auto& pair : mQueries) {
94 uint16_t id = pair.first;
95 if (id != nextId) {
96 // Found a gap.
97 return nextId;
98 }
99 nextId = id + 1;
100 }
101 // Unreachable (but the compiler isn't smart enough to prove it).
102 return -1;
103}
104
105std::vector<DnsTlsQueryMap::Query> DnsTlsQueryMap::getAll() {
106 std::lock_guard guard(mLock);
107 std::vector<DnsTlsQueryMap::Query> queries;
108 for (auto& q : mQueries) {
109 queries.push_back(q.second.query);
110 }
111 return queries;
112}
113
114bool DnsTlsQueryMap::empty() {
115 std::lock_guard guard(mLock);
116 return mQueries.empty();
117}
118
119void DnsTlsQueryMap::clear() {
120 std::lock_guard guard(mLock);
121 for (auto& q : mQueries) {
122 expire(&q.second);
123 }
124 mQueries.clear();
125}
126
127void DnsTlsQueryMap::onResponse(std::vector<uint8_t> response) {
chenbruceaff85842019-05-31 15:46:42 +0800128 LOG(VERBOSE) << "Got response of size " << response.size();
Mike Yubab3daa2018-10-19 22:11:43 +0800129 if (response.size() < 2) {
chenbruceaff85842019-05-31 15:46:42 +0800130 LOG(WARNING) << "Response is too short";
Mike Yubab3daa2018-10-19 22:11:43 +0800131 return;
132 }
133 uint16_t id = response[0] << 8 | response[1];
134 std::lock_guard guard(mLock);
135 auto it = mQueries.find(id);
136 if (it == mQueries.end()) {
chenbruceaff85842019-05-31 15:46:42 +0800137 LOG(WARNING) << "Discarding response: unknown ID " << id;
Mike Yubab3daa2018-10-19 22:11:43 +0800138 return;
139 }
140 Result r = { .code = Response::success, .response = std::move(response) };
141 // Rewrite ID to match the query
Mike Yu7e08b852019-10-18 18:27:43 +0800142 const uint8_t* data = it->second.query.query.data();
Mike Yubab3daa2018-10-19 22:11:43 +0800143 r.response[0] = data[0];
144 r.response[1] = data[1];
chenbruceaff85842019-05-31 15:46:42 +0800145 LOG(DEBUG) << "Sending result to dispatcher";
Mike Yubab3daa2018-10-19 22:11:43 +0800146 it->second.result.set_value(std::move(r));
147 mQueries.erase(it);
148}
149
150} // end of namespace net
151} // end of namespace android