Loading Android.bp +3 −0 Original line number Diff line number Diff line Loading @@ -27,6 +27,8 @@ aidl_interface { "binder/android/net/ResolverHostsParcel.aidl", "binder/android/net/ResolverOptionsParcel.aidl", "binder/android/net/ResolverParamsParcel.aidl", // New AIDL classes should go into android.net.resolv.aidl so they can be clearly identified "binder/android/net/resolv/aidl/**/*.aidl", ], imports: [ "netd_event_listener_interface", Loading Loading @@ -54,6 +56,7 @@ aidl_interface { "4", "5", "6", "7", ], } Loading DnsQueryLog.cpp +3 −19 Original line number Diff line number Diff line Loading @@ -17,7 +17,7 @@ #include "DnsQueryLog.h" #include <android-base/stringprintf.h> #include "util.h" namespace android::net { Loading Loading @@ -45,25 +45,10 @@ std::string maskIps(const std::vector<std::string>& ips) { return ret.empty() ? "" : ret.substr(0, ret.length() - 2); } // Return the readable string format "hr:min:sec.ms". std::string timestampToString(const std::chrono::system_clock::time_point& ts) { using std::chrono::duration_cast; using std::chrono::milliseconds; const auto time_sec = std::chrono::system_clock::to_time_t(ts); char buf[32]; std::strftime(buf, sizeof(buf), "%H:%M:%S", std::localtime(&time_sec)); int ms = duration_cast<milliseconds>(ts.time_since_epoch()).count() % 1000; return android::base::StringPrintf("%s.%03d", buf, ms); } } // namespace void DnsQueryLog::push(Record&& record) { std::lock_guard guard(mLock); mQueue.push_back(std::move(record)); if (mQueue.size() > mCapacity) { mQueue.pop_front(); } mQueue.push(std::move(record)); } void DnsQueryLog::dump(netdutils::DumpWriter& dw) const { Loading @@ -71,8 +56,7 @@ void DnsQueryLog::dump(netdutils::DumpWriter& dw) const { netdutils::ScopedIndent indentStats(dw); const auto now = std::chrono::system_clock::now(); std::lock_guard guard(mLock); for (const auto& record : mQueue) { for (const auto& record : mQueue.copy()) { if (now - record.timestamp > mValidityTimeMs) continue; const std::string maskedHostname = maskHostname(record.hostname); Loading DnsQueryLog.h +7 −9 Original line number Diff line number Diff line Loading @@ -17,16 +17,16 @@ #pragma once #include <deque> #include <string> #include <vector> #include <android-base/thread_annotations.h> #include <netdutils/DumpWriter.h> #include "LockedQueue.h" namespace android::net { // A circular buffer based class used for query logging. It's thread-safe for concurrent access. // This class stores query records in a locked ring buffer. It's thread-safe for concurrent access. class DnsQueryLog { public: static constexpr std::string_view DUMP_KEYWORD = "querylog"; Loading @@ -52,15 +52,13 @@ class DnsQueryLog { // Allow the tests to set the capacity and the validaty time in milliseconds. DnsQueryLog(size_t size = kDefaultLogSize, std::chrono::milliseconds time = kDefaultValidityMinutes) : mCapacity(size), mValidityTimeMs(time) {} : mQueue(size), mValidityTimeMs(time) {} void push(Record&& record) EXCLUDES(mLock); void dump(netdutils::DumpWriter& dw) const EXCLUDES(mLock); void push(Record&& record); void dump(netdutils::DumpWriter& dw) const; private: mutable std::mutex mLock; std::deque<Record> mQueue GUARDED_BY(mLock); const size_t mCapacity; LockedRingBuffer<Record> mQueue; const std::chrono::milliseconds mValidityTimeMs; // The capacity of the circular buffer. Loading DnsResolverService.cpp +11 −0 Original line number Diff line number Diff line Loading @@ -32,6 +32,7 @@ #include "DnsResolver.h" #include "Experiments.h" #include "NetdPermissions.h" // PERM_* #include "PrivateDnsConfiguration.h" #include "ResolverEventReporter.h" #include "resolv_cache.h" Loading Loading @@ -117,6 +118,8 @@ binder_status_t DnsResolverService::dump(int fd, const char** args, uint32_t num gDnsResolv->resolverCtrl.dump(dw, netId); dw.blankline(); } PrivateDnsConfiguration::getInstance().dump(dw); Experiments::getInstance()->dump(dw); return STATUS_OK; } Loading @@ -138,6 +141,14 @@ binder_status_t DnsResolverService::dump(int fd, const char** args, uint32_t num return statusFromErrcode(res); } ::ndk::ScopedAStatus DnsResolverService::registerUnsolicitedEventListener( const std::shared_ptr< aidl::android::net::resolv::aidl::IDnsResolverUnsolicitedEventListener>&) { ENFORCE_NETWORK_STACK_PERMISSIONS(); return ::ndk::ScopedAStatus(AStatus_newOk()); } ::ndk::ScopedAStatus DnsResolverService::checkAnyPermission( const std::vector<const char*>& permissions) { // TODO: Remove callback and move this to unnamed namespace after libbiner_ndk supports Loading DnsResolverService.h +4 −0 Original line number Diff line number Diff line Loading @@ -39,6 +39,10 @@ class DnsResolverService : public aidl::android::net::BnDnsResolver { ::ndk::ScopedAStatus registerEventListener( const std::shared_ptr<aidl::android::net::metrics::INetdEventListener>& listener) override; ::ndk::ScopedAStatus registerUnsolicitedEventListener( const std::shared_ptr< aidl::android::net::resolv::aidl::IDnsResolverUnsolicitedEventListener>& listener) override; // Resolver commands. ::ndk::ScopedAStatus setResolverConfiguration( Loading Loading
Android.bp +3 −0 Original line number Diff line number Diff line Loading @@ -27,6 +27,8 @@ aidl_interface { "binder/android/net/ResolverHostsParcel.aidl", "binder/android/net/ResolverOptionsParcel.aidl", "binder/android/net/ResolverParamsParcel.aidl", // New AIDL classes should go into android.net.resolv.aidl so they can be clearly identified "binder/android/net/resolv/aidl/**/*.aidl", ], imports: [ "netd_event_listener_interface", Loading Loading @@ -54,6 +56,7 @@ aidl_interface { "4", "5", "6", "7", ], } Loading
DnsQueryLog.cpp +3 −19 Original line number Diff line number Diff line Loading @@ -17,7 +17,7 @@ #include "DnsQueryLog.h" #include <android-base/stringprintf.h> #include "util.h" namespace android::net { Loading Loading @@ -45,25 +45,10 @@ std::string maskIps(const std::vector<std::string>& ips) { return ret.empty() ? "" : ret.substr(0, ret.length() - 2); } // Return the readable string format "hr:min:sec.ms". std::string timestampToString(const std::chrono::system_clock::time_point& ts) { using std::chrono::duration_cast; using std::chrono::milliseconds; const auto time_sec = std::chrono::system_clock::to_time_t(ts); char buf[32]; std::strftime(buf, sizeof(buf), "%H:%M:%S", std::localtime(&time_sec)); int ms = duration_cast<milliseconds>(ts.time_since_epoch()).count() % 1000; return android::base::StringPrintf("%s.%03d", buf, ms); } } // namespace void DnsQueryLog::push(Record&& record) { std::lock_guard guard(mLock); mQueue.push_back(std::move(record)); if (mQueue.size() > mCapacity) { mQueue.pop_front(); } mQueue.push(std::move(record)); } void DnsQueryLog::dump(netdutils::DumpWriter& dw) const { Loading @@ -71,8 +56,7 @@ void DnsQueryLog::dump(netdutils::DumpWriter& dw) const { netdutils::ScopedIndent indentStats(dw); const auto now = std::chrono::system_clock::now(); std::lock_guard guard(mLock); for (const auto& record : mQueue) { for (const auto& record : mQueue.copy()) { if (now - record.timestamp > mValidityTimeMs) continue; const std::string maskedHostname = maskHostname(record.hostname); Loading
DnsQueryLog.h +7 −9 Original line number Diff line number Diff line Loading @@ -17,16 +17,16 @@ #pragma once #include <deque> #include <string> #include <vector> #include <android-base/thread_annotations.h> #include <netdutils/DumpWriter.h> #include "LockedQueue.h" namespace android::net { // A circular buffer based class used for query logging. It's thread-safe for concurrent access. // This class stores query records in a locked ring buffer. It's thread-safe for concurrent access. class DnsQueryLog { public: static constexpr std::string_view DUMP_KEYWORD = "querylog"; Loading @@ -52,15 +52,13 @@ class DnsQueryLog { // Allow the tests to set the capacity and the validaty time in milliseconds. DnsQueryLog(size_t size = kDefaultLogSize, std::chrono::milliseconds time = kDefaultValidityMinutes) : mCapacity(size), mValidityTimeMs(time) {} : mQueue(size), mValidityTimeMs(time) {} void push(Record&& record) EXCLUDES(mLock); void dump(netdutils::DumpWriter& dw) const EXCLUDES(mLock); void push(Record&& record); void dump(netdutils::DumpWriter& dw) const; private: mutable std::mutex mLock; std::deque<Record> mQueue GUARDED_BY(mLock); const size_t mCapacity; LockedRingBuffer<Record> mQueue; const std::chrono::milliseconds mValidityTimeMs; // The capacity of the circular buffer. Loading
DnsResolverService.cpp +11 −0 Original line number Diff line number Diff line Loading @@ -32,6 +32,7 @@ #include "DnsResolver.h" #include "Experiments.h" #include "NetdPermissions.h" // PERM_* #include "PrivateDnsConfiguration.h" #include "ResolverEventReporter.h" #include "resolv_cache.h" Loading Loading @@ -117,6 +118,8 @@ binder_status_t DnsResolverService::dump(int fd, const char** args, uint32_t num gDnsResolv->resolverCtrl.dump(dw, netId); dw.blankline(); } PrivateDnsConfiguration::getInstance().dump(dw); Experiments::getInstance()->dump(dw); return STATUS_OK; } Loading @@ -138,6 +141,14 @@ binder_status_t DnsResolverService::dump(int fd, const char** args, uint32_t num return statusFromErrcode(res); } ::ndk::ScopedAStatus DnsResolverService::registerUnsolicitedEventListener( const std::shared_ptr< aidl::android::net::resolv::aidl::IDnsResolverUnsolicitedEventListener>&) { ENFORCE_NETWORK_STACK_PERMISSIONS(); return ::ndk::ScopedAStatus(AStatus_newOk()); } ::ndk::ScopedAStatus DnsResolverService::checkAnyPermission( const std::vector<const char*>& permissions) { // TODO: Remove callback and move this to unnamed namespace after libbiner_ndk supports Loading
DnsResolverService.h +4 −0 Original line number Diff line number Diff line Loading @@ -39,6 +39,10 @@ class DnsResolverService : public aidl::android::net::BnDnsResolver { ::ndk::ScopedAStatus registerEventListener( const std::shared_ptr<aidl::android::net::metrics::INetdEventListener>& listener) override; ::ndk::ScopedAStatus registerUnsolicitedEventListener( const std::shared_ptr< aidl::android::net::resolv::aidl::IDnsResolverUnsolicitedEventListener>& listener) override; // Resolver commands. ::ndk::ScopedAStatus setResolverConfiguration( Loading