Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 17 | #define LOG_TAG "DnsTlsDispatcher" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 20 | #include "dns/DnsTlsDispatcher.h" |
| 21 | |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 22 | #include "log/log.h" |
| 23 | |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace net { |
| 26 | |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 27 | using netdutils::Slice; |
| 28 | |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 29 | // static |
| 30 | std::mutex DnsTlsDispatcher::sLock; |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 31 | DnsTlsTransport::Response DnsTlsDispatcher::query(const DnsTlsServer& server, unsigned mark, |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 32 | const Slice query, |
| 33 | const Slice ans, int *resplen) { |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 34 | const Key key = std::make_pair(mark, server); |
| 35 | Transport* xport; |
| 36 | { |
| 37 | std::lock_guard<std::mutex> guard(sLock); |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 38 | auto it = mStore.find(key); |
| 39 | if (it == mStore.end()) { |
| 40 | xport = new Transport(server, mark, mFactory.get()); |
| 41 | mStore[key].reset(xport); |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 42 | } else { |
| 43 | xport = it->second.get(); |
| 44 | } |
| 45 | ++xport->useCount; |
| 46 | } |
| 47 | |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 48 | ALOGV("Sending query of length %zu", query.size()); |
Ben Schwartz | 3386076 | 2017-10-25 14:41:02 -0400 | [diff] [blame^] | 49 | auto res = xport->transport.query(query); |
| 50 | ALOGV("Awaiting response"); |
| 51 | const auto& result = res.get(); |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 52 | DnsTlsTransport::Response code = result.code; |
| 53 | if (code == DnsTlsTransport::Response::success) { |
| 54 | if (result.response.size() > ans.size()) { |
| 55 | ALOGV("Response too large: %zu > %zu", result.response.size(), ans.size()); |
| 56 | code = DnsTlsTransport::Response::limit_error; |
| 57 | } else { |
| 58 | ALOGV("Got response successfully"); |
| 59 | *resplen = result.response.size(); |
| 60 | netdutils::copy(ans, netdutils::makeSlice(result.response)); |
| 61 | } |
| 62 | } else { |
| 63 | ALOGV("Query failed: %u", (unsigned int)code); |
| 64 | } |
| 65 | |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 66 | auto now = std::chrono::steady_clock::now(); |
| 67 | { |
| 68 | std::lock_guard<std::mutex> guard(sLock); |
| 69 | --xport->useCount; |
| 70 | xport->lastUsed = now; |
| 71 | cleanup(now); |
| 72 | } |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 73 | return code; |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 76 | // This timeout effectively controls how long to keep SSL session tickets. |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 77 | static constexpr std::chrono::minutes IDLE_TIMEOUT(5); |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 78 | void DnsTlsDispatcher::cleanup(std::chrono::time_point<std::chrono::steady_clock> now) { |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 79 | // To avoid scanning mStore after every query, return early if a cleanup has been |
| 80 | // performed recently. |
| 81 | if (now - mLastCleanup < IDLE_TIMEOUT) { |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 82 | return; |
| 83 | } |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 84 | for (auto it = mStore.begin(); it != mStore.end();) { |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 85 | auto& s = it->second; |
| 86 | if (s->useCount == 0 && now - s->lastUsed > IDLE_TIMEOUT) { |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 87 | it = mStore.erase(it); |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 88 | } else { |
| 89 | ++it; |
| 90 | } |
| 91 | } |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 92 | mLastCleanup = now; |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 95 | } // end of namespace net |
| 96 | } // end of namespace android |