• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "dns_result_call_back.h"
16 #include "net_manager_constants.h"
17 #include "net_mgr_log_wrapper.h"
18 #include "net_conn_service.h"
19 
20 namespace OHOS {
21 namespace NetManagerStandard {
22 constexpr double FAIL_RATE = 0.2;
OnDnsResultReport(uint32_t size,const std::list<NetsysNative::NetDnsResultReport> netDnsResultReport)23 int32_t NetDnsResultCallback::OnDnsResultReport(uint32_t size,
24     const std::list<NetsysNative::NetDnsResultReport> netDnsResultReport)
25 {
26     NETMGR_LOG_D("Dns Result Report interface is called");
27     netDnsResult_.Clear();
28     IterateDnsReportResults(netDnsResultReport);
29     netDnsResult_.Iterate([](int32_t netid, NetDnsResult dnsResult) {
30         double failRate = static_cast<double>(dnsResult.failReports_) / dnsResult.totalReports_;
31         NETMGR_LOG_I("netId_: %{public}d, totalReports_: %{public}d, failReports_: %{public}d, failrate : %{public}f",
32                      netid, dnsResult.totalReports_, dnsResult.failReports_, failRate);
33         if (failRate > FAIL_RATE) {
34             NETMGR_LOG_D("Netdetection for dns fail, netId:%{public}d,totalReports:%{public}d, failReports:%{public}d",
35                          netid, dnsResult.totalReports_, dnsResult.failReports_);
36             int32_t result = NetConnService::GetInstance()->NetDetectionForDnsHealth(netid, false);
37             if (result != 0) {
38                 NETMGR_LOG_E("NetDetectionForDnsHealth failed");
39             }
40         } else {
41             NETMGR_LOG_D("Netdetection for dns success, netId:%{public}d, totalReports:%{public}d,"
42                          "failReports:%{public}d", netid, dnsResult.totalReports_, dnsResult.failReports_);
43             int32_t result = NetConnService::GetInstance()->NetDetectionForDnsHealth(netid, true);
44             if (result != 0) {
45                 NETMGR_LOG_E("NetDetectionForDnsHealth failed");
46             }
47         }
48     });
49     return NETMANAGER_SUCCESS;
50 }
51 
GetDumpMessageForDnsResult(std::string & message)52 void NetDnsResultCallback::GetDumpMessageForDnsResult(std::string &message)
53 {
54     message.append("Dns result Info:\n");
55     netDnsResult_.Iterate([&message](int32_t netid, NetDnsResult dnsResult) {
56         message.append("\tnetId: " + std::to_string(netid) + "\n");
57         message.append("\ttotalReports: " + std::to_string(dnsResult.totalReports_) + "\n");
58         message.append("\tfailReports: " + std::to_string(dnsResult.failReports_) + "\n");
59     });
60 }
61 
IterateDnsReportResults(const std::list<NetsysNative::NetDnsResultReport> netDnsResultReport)62 void NetDnsResultCallback::IterateDnsReportResults(
63     const std::list<NetsysNative::NetDnsResultReport> netDnsResultReport)
64 {
65     int32_t defaultNetid = 0;
66     int32_t result = NetConnService::GetInstance()->GetDefaultNet(defaultNetid);
67     NETMGR_LOG_I("GetDefaultNet result: %{public}d, defaultNetid: %{public}d", result, defaultNetid);
68     for (auto &it : netDnsResultReport) {
69         NETMGR_LOG_D("netId_: %{public}d, queryResult_: %{public}d, pid_ : %{public}d",
70                      it.netid_, it.queryresult_, it.pid_);
71         NetDnsResult existResult;
72         bool ret =  netDnsResult_.Find(it.netid_, existResult);
73         if (!ret && it.netid_ == 0) {
74             NetDnsResult newDefaultResult;
75             if (!netDnsResult_.Find(defaultNetid, newDefaultResult)) {
76                 NetDnsResult defaultResult;
77                 defaultResult.totalReports_ = 1;
78                 defaultResult.failReports_ = it.queryresult_ == 0 ? 0 : 1;
79                 netDnsResult_.EnsureInsert(defaultNetid, defaultResult);
80             } else {
81                 newDefaultResult = netDnsResult_.ReadVal(defaultNetid);
82                 newDefaultResult.totalReports_++;
83                 newDefaultResult.failReports_ += it.queryresult_ == 0 ? 0 : 1;
84                 netDnsResult_.EnsureInsert(defaultNetid, newDefaultResult);
85             }
86         } else if (!ret) {
87             NetDnsResult newResult;
88             newResult.totalReports_ = 1;
89             newResult.failReports_ = it.queryresult_ == 0 ? 0 : 1;
90             netDnsResult_.EnsureInsert(it.netid_, newResult);
91         } else {
92             existResult = netDnsResult_.ReadVal(it.netid_);
93             existResult.totalReports_++;
94             existResult.failReports_ += it.queryresult_ == 0 ? 0 : 1;
95             netDnsResult_.EnsureInsert(it.netid_, existResult);
96         }
97     }
98 }
99 } // namespace NetManagerStandard
100 } // namespace OHOS
101