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 #include <time.h> 22 23 #include "dns_resolv_config.h" 24 #include "netnative_log_wrapper.h" 25 #include "dns_quality_event_handler.h" 26 #include "i_net_dns_health_callback.h" 27 #include "i_net_dns_result_callback.h" 28 #include "netsys_net_dns_result_data.h" 29 #include "dns_config_client.h" 30 31 #define FROM_CACHE_FLAG (1 << 0) 32 #define VPN_NET_FLAG (1 << 1) 33 #define IPV4_NO_ANSWER_FLAG (1 << 2) 34 #define IPV4_CNAME_FLAG (1 << 3) 35 #define IPV6_NO_ANSWER_FLAG (1 << 4) 36 #define IPV6_CNAME_FLAG (1 << 5) 37 38 namespace OHOS::nmd { 39 40 struct DnsAbnormalInfo { 41 uint32_t eventfailcause; 42 NetsysNative::NetDnsQueryResultReport report; 43 }; 44 45 class DnsQualityDiag { 46 public: 47 ~DnsQualityDiag() = default; 48 49 static DnsQualityDiag &GetInstance(); 50 51 // for net_conn_service 52 int32_t ReportDnsResult(uint16_t netId, uint16_t uid, uint32_t pid, int32_t usedtime, char* name, 53 uint32_t size, int32_t failreason, QueryParam param, AddrInfo* addrinfo); 54 55 int32_t ReportDnsQueryResult(PostDnsQueryParam queryParam, AddrInfo* addrinfo, uint8_t addrSize); 56 57 int32_t ReportDnsQueryAbnormal(uint32_t eventfailcause, PostDnsQueryParam queryParam, AddrInfo* addrinfo); 58 59 int32_t RegisterResultListener(const sptr<NetsysNative::INetDnsResultCallback> &callback, uint32_t timeStep); 60 61 int32_t UnregisterResultListener(const sptr<NetsysNative::INetDnsResultCallback> &callback); 62 63 int32_t RegisterHealthListener(const sptr<NetsysNative::INetDnsHealthCallback> &callback); 64 65 int32_t UnregisterHealthListener(const sptr<NetsysNative::INetDnsHealthCallback> &callback); 66 67 int32_t SetLoopDelay(int32_t delay); 68 69 int32_t HandleEvent(const AppExecFwk::InnerEvent::Pointer &event); 70 71 private: 72 DnsQualityDiag(); 73 74 std::mutex cacheMutex_; 75 76 std::mutex resultListenersMutex_; 77 78 std::mutex dnsAbnormalTimeMutex_; 79 80 std::atomic_uint defaultNetId_; 81 82 uint32_t monitor_loop_delay; 83 84 uint32_t report_delay; 85 86 uint32_t last_dns_abnormal_report_time = 0; 87 88 std::atomic_bool handler_started; 89 90 std::string queryAddr; 91 92 std::list<sptr<NetsysNative::INetDnsResultCallback>> resultListeners_; 93 94 std::list<sptr<NetsysNative::INetDnsHealthCallback>> healthListeners_; 95 96 std::shared_ptr<DnsQualityEventHandler> handler_; 97 98 std::list<NetsysNative::NetDnsResultReport> report_; 99 100 std::list<NetsysNative::NetDnsQueryResultReport> dnsQueryReport_; 101 102 int32_t SendHealthReport(NetsysNative::NetDnsHealthReport healthreport); 103 int32_t InitHandler(); 104 int32_t query_default_host(); 105 int32_t handle_dns_loop(); 106 int32_t handle_dns_fail(); 107 int32_t send_dns_report(); 108 int32_t add_dns_report(std::shared_ptr<NetsysNative::NetDnsResultReport> report); 109 int32_t add_dns_query_report(std::shared_ptr<NetsysNative::NetDnsQueryResultReport> report); 110 int32_t load_query_addr(const char* defaultAddr); 111 void GetDefaultDnsServerList(int32_t uid, std::vector<std::string> &servers); 112 int32_t ParseReportAddr(uint32_t size, AddrInfo* addrinfo, NetsysNative::NetDnsResultReport &report); 113 void FillDnsQueryResultReport(NetsysNative::NetDnsQueryResultReport &report, 114 PostDnsQueryParam &queryParam); 115 int32_t ParseDnsQueryReportAddr(uint8_t size, 116 AddrInfo* addrinfo, NetsysNative::NetDnsQueryResultReport &report); 117 int32_t handle_dns_abnormal(std::shared_ptr<DnsAbnormalInfo> abnormalInfo); 118 }; 119 } // namespace OHOS::nmd 120 #endif // NETSYS_DNS_QUALITY_DIAG_H 121