1 /**
2 * Copyright (c) 2021, 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 #include "unsolicited_event_listener.h"
18
19 #include <thread>
20
21 #include <android-base/chrono_utils.h>
22 #include <android-base/format.h>
23
24 namespace android::net::resolv::aidl {
25
26 using ::aidl::android::net::resolv::aidl::DnsHealthEventParcel;
27 using ::aidl::android::net::resolv::aidl::IDnsResolverUnsolicitedEventListener;
28 using ::aidl::android::net::resolv::aidl::Nat64PrefixEventParcel;
29 using ::aidl::android::net::resolv::aidl::PrivateDnsValidationEventParcel;
30 using android::base::Error;
31 using android::base::Result;
32 using android::base::ScopedLockAssertion;
33 using std::chrono::milliseconds;
34
35 constexpr milliseconds kEventTimeoutMs{5000};
36
onDnsHealthEvent(const DnsHealthEventParcel & event)37 ::ndk::ScopedAStatus UnsolicitedEventListener::onDnsHealthEvent(const DnsHealthEventParcel& event) {
38 std::lock_guard lock(mMutex);
39 if (event.netId == mNetId) mDnsHealthResultRecords.push(event.healthResult);
40 mCv.notify_all();
41 return ::ndk::ScopedAStatus::ok();
42 }
43
onNat64PrefixEvent(const Nat64PrefixEventParcel & event)44 ::ndk::ScopedAStatus UnsolicitedEventListener::onNat64PrefixEvent(
45 const Nat64PrefixEventParcel& event) {
46 std::lock_guard lock(mMutex);
47 mUnexpectedNat64PrefixUpdates++;
48 if (event.netId == mNetId) {
49 mNat64PrefixAddress = (event.prefixOperation ==
50 IDnsResolverUnsolicitedEventListener::PREFIX_OPERATION_ADDED)
51 ? event.prefixAddress
52 : "";
53 }
54 mCv.notify_all();
55 return ::ndk::ScopedAStatus::ok();
56 }
57
onPrivateDnsValidationEvent(const PrivateDnsValidationEventParcel & event)58 ::ndk::ScopedAStatus UnsolicitedEventListener::onPrivateDnsValidationEvent(
59 const PrivateDnsValidationEventParcel& event) {
60 {
61 std::lock_guard lock(mMutex);
62 // keep updating the server to have latest validation status.
63 mValidationRecords.insert_or_assign({event.netId, event.ipAddress, event.protocol},
64 event.validation);
65 }
66 mCv.notify_all();
67 return ::ndk::ScopedAStatus::ok();
68 }
69
waitForPrivateDnsValidation(const std::string & serverAddr,int validation,int protocol)70 bool UnsolicitedEventListener::waitForPrivateDnsValidation(const std::string& serverAddr,
71 int validation, int protocol) {
72 std::unique_lock lock(mMutex);
73 return mCv.wait_for(lock, kEventTimeoutMs, [&]() REQUIRES(mMutex) {
74 return findAndRemoveValidationRecord({mNetId, serverAddr, protocol}, validation);
75 });
76 }
77
findAndRemoveValidationRecord(const ServerKey & key,int value)78 bool UnsolicitedEventListener::findAndRemoveValidationRecord(const ServerKey& key, int value) {
79 auto it = mValidationRecords.find(key);
80 if (it != mValidationRecords.end() && it->second == value) {
81 mValidationRecords.erase(it);
82 return true;
83 }
84 return false;
85 }
86
waitForNat64Prefix(int operation,const milliseconds & timeout)87 bool UnsolicitedEventListener::waitForNat64Prefix(int operation, const milliseconds& timeout) {
88 const auto now = std::chrono::steady_clock::now();
89
90 std::unique_lock lock(mMutex);
91 ScopedLockAssertion assume_lock(mMutex);
92
93 if (mCv.wait_for(lock, timeout, [&]() REQUIRES(mMutex) {
94 return (operation == IDnsResolverUnsolicitedEventListener::PREFIX_OPERATION_ADDED &&
95 !mNat64PrefixAddress.empty()) ||
96 (operation == IDnsResolverUnsolicitedEventListener::PREFIX_OPERATION_REMOVED &&
97 mNat64PrefixAddress.empty());
98 })) {
99 mUnexpectedNat64PrefixUpdates--;
100 return true;
101 }
102
103 // Timeout.
104 return false;
105 }
106
popDnsHealthResult()107 Result<int> UnsolicitedEventListener::popDnsHealthResult() {
108 std::unique_lock lock(mMutex);
109 ScopedLockAssertion assume_lock(mMutex);
110
111 if (!mCv.wait_for(lock, kEventTimeoutMs,
112 [&]() REQUIRES(mMutex) { return !mDnsHealthResultRecords.empty(); })) {
113 return Error() << "Dns health result record is empty";
114 }
115
116 auto ret = mDnsHealthResultRecords.front();
117 mDnsHealthResultRecords.pop();
118 return ret;
119 }
120
121 } // namespace android::net::resolv::aidl
122