Mike Yu | 939e369 | 2019-10-29 17:28:34 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | */ |
| 17 | |
| 18 | #pragma once |
| 19 | |
Mike Yu | 939e369 | 2019-10-29 17:28:34 +0800 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Mike Yu | 939e369 | 2019-10-29 17:28:34 +0800 | [diff] [blame] | 23 | #include <netdutils/DumpWriter.h> |
| 24 | |
Mike Yu | a86a970 | 2020-12-21 19:30:25 +0800 | [diff] [blame] | 25 | #include "LockedQueue.h" |
| 26 | |
Mike Yu | 939e369 | 2019-10-29 17:28:34 +0800 | [diff] [blame] | 27 | namespace android::net { |
| 28 | |
Mike Yu | a86a970 | 2020-12-21 19:30:25 +0800 | [diff] [blame] | 29 | // This class stores query records in a locked ring buffer. It's thread-safe for concurrent access. |
Mike Yu | 939e369 | 2019-10-29 17:28:34 +0800 | [diff] [blame] | 30 | class DnsQueryLog { |
| 31 | public: |
| 32 | static constexpr std::string_view DUMP_KEYWORD = "querylog"; |
| 33 | |
| 34 | struct Record { |
| 35 | Record(uint32_t netId, uid_t uid, pid_t pid, const std::string& hostname, |
| 36 | const std::vector<std::string>& addrs, int timeTaken) |
| 37 | : netId(netId), |
| 38 | uid(uid), |
| 39 | pid(pid), |
| 40 | hostname(hostname), |
| 41 | addrs(addrs), |
| 42 | timeTaken(timeTaken) {} |
| 43 | const uint32_t netId; |
| 44 | const uid_t uid; |
| 45 | const pid_t pid; |
| 46 | const std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now(); |
| 47 | const std::string hostname; |
| 48 | const std::vector<std::string> addrs; |
| 49 | const int timeTaken; |
| 50 | }; |
| 51 | |
Mike Yu | 39df0b1 | 2019-10-31 16:12:23 +0800 | [diff] [blame] | 52 | // Allow the tests to set the capacity and the validaty time in milliseconds. |
| 53 | DnsQueryLog(size_t size = kDefaultLogSize, |
| 54 | std::chrono::milliseconds time = kDefaultValidityMinutes) |
Mike Yu | a86a970 | 2020-12-21 19:30:25 +0800 | [diff] [blame] | 55 | : mQueue(size), mValidityTimeMs(time) {} |
Mike Yu | 39df0b1 | 2019-10-31 16:12:23 +0800 | [diff] [blame] | 56 | |
Mike Yu | a86a970 | 2020-12-21 19:30:25 +0800 | [diff] [blame] | 57 | void push(Record&& record); |
| 58 | void dump(netdutils::DumpWriter& dw) const; |
Mike Yu | 939e369 | 2019-10-29 17:28:34 +0800 | [diff] [blame] | 59 | |
| 60 | private: |
Mike Yu | a86a970 | 2020-12-21 19:30:25 +0800 | [diff] [blame] | 61 | LockedRingBuffer<Record> mQueue; |
Mike Yu | 39df0b1 | 2019-10-31 16:12:23 +0800 | [diff] [blame] | 62 | const std::chrono::milliseconds mValidityTimeMs; |
Mike Yu | 939e369 | 2019-10-29 17:28:34 +0800 | [diff] [blame] | 63 | |
| 64 | // The capacity of the circular buffer. |
Mike Yu | 39df0b1 | 2019-10-31 16:12:23 +0800 | [diff] [blame] | 65 | static constexpr size_t kDefaultLogSize = 200; |
Mike Yu | 939e369 | 2019-10-29 17:28:34 +0800 | [diff] [blame] | 66 | |
Mike Yu | 39df0b1 | 2019-10-31 16:12:23 +0800 | [diff] [blame] | 67 | // Limit to dump the queries within last |kDefaultValidityMinutes| minutes. |
| 68 | static constexpr std::chrono::minutes kDefaultValidityMinutes{60}; |
Mike Yu | 939e369 | 2019-10-29 17:28:34 +0800 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | } // namespace android::net |