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 #include "DnsQueryLog.h"
19
20 #include "util.h"
21
22 namespace android::net {
23
24 namespace {
25
maskHostname(const std::string & hostname)26 std::string maskHostname(const std::string& hostname) {
27 // Boundary issue is handled in substr().
28 return hostname.substr(0, 1) + "***";
29 }
30
31 // Return the string of masked addresses of the first v4 address and the first v6 address.
maskIps(const std::vector<std::string> & ips)32 std::string maskIps(const std::vector<std::string>& ips) {
33 std::string ret;
34 bool v4Found = false, v6Found = false;
35 for (const auto& ip : ips) {
36 if (auto pos = ip.find_first_of(':'); pos != ip.npos && !v6Found) {
37 ret += ip.substr(0, pos + 1) + "***, ";
38 v6Found = true;
39 } else if (auto pos = ip.find_first_of('.'); pos != ip.npos && !v4Found) {
40 ret += ip.substr(0, pos + 1) + "***, ";
41 v4Found = true;
42 }
43 if (v6Found && v4Found) break;
44 }
45 return ret.empty() ? "" : ret.substr(0, ret.length() - 2);
46 }
47
48 } // namespace
49
push(Record && record)50 void DnsQueryLog::push(Record&& record) {
51 mQueue.push(std::move(record));
52 }
53
getLogSizeFromSysProp()54 uint64_t DnsQueryLog::getLogSizeFromSysProp() {
55 const uint64_t logSize = android::base::GetUintProperty<uint64_t>(
56 "persist.net.dns_query_log_size", kDefaultLogSize);
57 return logSize <= kMaxLogSize ? logSize : kDefaultLogSize;
58 }
59
dump(netdutils::DumpWriter & dw) const60 void DnsQueryLog::dump(netdutils::DumpWriter& dw) const {
61 dw.println("DNS query log:");
62 netdutils::ScopedIndent indentStats(dw);
63
64 for (const auto& record : mQueue.copy()) {
65 const std::string maskedHostname = maskHostname(record.hostname);
66 const std::string maskedIpsStr = maskIps(record.addrs);
67 const std::string time = timestampToString(record.timestamp);
68 dw.println("time=%s netId=%u uid=%u pid=%d hostname=%s answer=[%s] (%dms)", time.c_str(),
69 record.netId, record.uid, record.pid, maskedHostname.c_str(),
70 maskedIpsStr.c_str(), record.timeTaken);
71 }
72 }
73
74 } // namespace android::net
75