1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef NETSYS_DNS_QUALITY_DIAG_H
17 #define NETSYS_DNS_QUALITY_DIAG_H
18 
19 #include <iostream>
20 #include <map>
21 
22 #include "dns_resolv_config.h"
23 #include "netnative_log_wrapper.h"
24 #include "dns_quality_event_handler.h"
25 #include "i_net_dns_health_callback.h"
26 #include "i_net_dns_result_callback.h"
27 #include "netsys_net_dns_result_data.h"
28 #include "dns_config_client.h"
29 
30 #if DNS_CONFIG_DEBUG
31 #ifdef DNS_CONFIG_PRINT
32 #undef DNS_CONFIG_PRINT
33 #endif
34 #define DNS_CONFIG_PRINT(fmt, ...) NETNATIVE_LOGI("DNS" fmt, ##__VA_ARGS__)
35 #else
36 #define DNS_CONFIG_PRINT(fmt, ...)
37 #endif
38 
39 namespace OHOS::nmd {
40 class DnsQualityDiag {
41 public:
42     ~DnsQualityDiag() = default;
43 
44     static DnsQualityDiag &GetInstance();
45 
46     // for net_conn_service
47     int32_t ReportDnsResult(uint16_t netId, uint16_t uid, uint32_t pid, int32_t usedtime, char* name,
48                             uint32_t size, int32_t failreason, QueryParam param, AddrInfo* addrinfo);
49 
50     int32_t RegisterResultListener(const sptr<NetsysNative::INetDnsResultCallback> &callback, uint32_t timeStep);
51 
52     int32_t UnregisterResultListener(const sptr<NetsysNative::INetDnsResultCallback> &callback);
53 
54     int32_t RegisterHealthListener(const sptr<NetsysNative::INetDnsHealthCallback> &callback);
55 
56     int32_t UnregisterHealthListener(const sptr<NetsysNative::INetDnsHealthCallback> &callback);
57 
58     int32_t SetLoopDelay(int32_t delay);
59 
60     int32_t HandleEvent(const AppExecFwk::InnerEvent::Pointer &event);
61 
62 private:
63     DnsQualityDiag();
64 
65     std::mutex cacheMutex_;
66 
67     std::mutex resultListenersMutex_;
68 
69     std::atomic_uint defaultNetId_;
70 
71     uint32_t monitor_loop_delay;
72 
73     uint32_t report_delay;
74 
75     std::atomic_bool handler_started;
76 
77     std::string queryAddr;
78 
79     std::list<sptr<NetsysNative::INetDnsResultCallback>> resultListeners_;
80 
81     std::list<sptr<NetsysNative::INetDnsHealthCallback>> healthListeners_;
82 
83     std::shared_ptr<DnsQualityEventHandler> handler_;
84 
85     std::list<NetsysNative::NetDnsResultReport> report_;
86 
87     int32_t SendHealthReport(NetsysNative::NetDnsHealthReport healthreport);
88     int32_t InitHandler();
89     int32_t query_default_host();
90     int32_t handle_dns_loop();
91     int32_t handle_dns_fail();
92     int32_t send_dns_report();
93     int32_t add_dns_report(std::shared_ptr<NetsysNative::NetDnsResultReport> report);
94     int32_t load_query_addr(const char* defaultAddr);
95     int32_t ParseReportAddr(uint32_t size, AddrInfo* addrinfo, NetsysNative::NetDnsResultReport &report);
96 };
97 } // namespace OHOS::nmd
98 #endif // NETSYS_DNS_QUALITY_DIAG_H
99