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()); |
| 49 | auto result = xport->transport.query(query); |
| 50 | DnsTlsTransport::Response code = result.code; |
| 51 | if (code == DnsTlsTransport::Response::success) { |
| 52 | if (result.response.size() > ans.size()) { |
| 53 | ALOGV("Response too large: %zu > %zu", result.response.size(), ans.size()); |
| 54 | code = DnsTlsTransport::Response::limit_error; |
| 55 | } else { |
| 56 | ALOGV("Got response successfully"); |
| 57 | *resplen = result.response.size(); |
| 58 | netdutils::copy(ans, netdutils::makeSlice(result.response)); |
| 59 | } |
| 60 | } else { |
| 61 | ALOGV("Query failed: %u", (unsigned int)code); |
| 62 | } |
| 63 | |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 64 | auto now = std::chrono::steady_clock::now(); |
| 65 | { |
| 66 | std::lock_guard<std::mutex> guard(sLock); |
| 67 | --xport->useCount; |
| 68 | xport->lastUsed = now; |
| 69 | cleanup(now); |
| 70 | } |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame^] | 71 | return code; |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame^] | 74 | // This timeout effectively controls how long to keep SSL session tickets. |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 75 | static constexpr std::chrono::minutes IDLE_TIMEOUT(5); |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 76 | void DnsTlsDispatcher::cleanup(std::chrono::time_point<std::chrono::steady_clock> now) { |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame^] | 77 | // To avoid scanning mStore after every query, return early if a cleanup has been |
| 78 | // performed recently. |
| 79 | if (now - mLastCleanup < IDLE_TIMEOUT) { |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 80 | return; |
| 81 | } |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame^] | 82 | for (auto it = mStore.begin(); it != mStore.end();) { |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 83 | auto& s = it->second; |
| 84 | if (s->useCount == 0 && now - s->lastUsed > IDLE_TIMEOUT) { |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame^] | 85 | it = mStore.erase(it); |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 86 | } else { |
| 87 | ++it; |
| 88 | } |
| 89 | } |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame^] | 90 | mLastCleanup = now; |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame^] | 93 | } // end of namespace net |
| 94 | } // end of namespace android |