• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024-2024 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 #include <arpa/inet.h>
17 #include <cstring>
18 #include <fcntl.h>
19 #include <fstream>
20 #include <future>
21 #include <list>
22 #include <memory>
23 #include <netdb.h>
24 #include <regex>
25 #include <securec.h>
26 #include <sys/socket.h>
27 #include <thread>
28 #include <pthread.h>
29 #include <unistd.h>
30 
31 #include "probe_thread.h"
32 #include "dns_config_client.h"
33 #include "event_report.h"
34 #include "fwmark_client.h"
35 #include "netmanager_base_common_utils.h"
36 #include "netsys_controller.h"
37 #include "net_http_proxy_tracker.h"
38 #include "net_mgr_log_wrapper.h"
39 #include "net_manager_constants.h"
40 
41 namespace OHOS {
42 namespace NetManagerStandard {
43 
NetProbeThread(const std::shared_ptr<ProbeThread> & probeThread)44 static void NetProbeThread(const std::shared_ptr<ProbeThread> &probeThread)
45 {
46     if (probeThread == nullptr) {
47         NETMGR_LOG_E("probeThread is nullptr");
48         return;
49     }
50     ProbeType type = probeThread->GetProbeType();
51     probeThread->SendHttpProbe(type);
52 }
53 
ProbeThread(uint32_t netId,NetBearType bearType,const NetLinkInfo & netLinkInfo,std::shared_ptr<TinyCountDownLatch> latch,std::shared_ptr<TinyCountDownLatch> latchAll,ProbeType probeType,std::string httpUrl,std::string httpsUrl)54 ProbeThread::ProbeThread(uint32_t netId, NetBearType bearType, const NetLinkInfo &netLinkInfo,
55     std::shared_ptr<TinyCountDownLatch> latch, std::shared_ptr<TinyCountDownLatch> latchAll,
56     ProbeType probeType, std::string httpUrl, std::string httpsUrl)
57     : netId_(netId), probeType_(probeType), latch_(latch), latchAll_(latchAll), httpProbeUrl_(httpUrl),
58     httpsProbeUrl_(httpsUrl)
59 {
60     httpProbe_ = std::make_unique<NetHttpProbe>(netId, bearType, netLinkInfo, probeType);
61 }
62 
~ProbeThread()63 ProbeThread::~ProbeThread()
64 {
65     if (thread_.joinable()) {
66         thread_.join();
67     }
68 }
69 
Start()70 void ProbeThread::Start()
71 {
72     NETMGR_LOG_D("Start net[%{public}d] monitor in", netId_);
73     isDetecting_ = true;
74     std::shared_ptr<ProbeThread> probeThead = shared_from_this();
75     std::shared_ptr<ProbeThread> temp;
76     std::atomic_store_explicit(&temp, std::move(probeThead), std::memory_order_release);
77     thread_ = std::thread([thread = std::atomic_load_explicit(&temp, std::memory_order_acquire)] {
78         return NetProbeThread(thread);
79     });
80     std::string threadName = "netDetectThread";
81     pthread_setname_np(thread_.native_handle(), threadName.c_str());
82     thread_.detach();
83 }
84 
SendHttpProbe(ProbeType probeType)85 void ProbeThread::SendHttpProbe(ProbeType probeType)
86 {
87     if (httpProbeUrl_.empty() || httpsProbeUrl_.empty()) {
88         NETMGR_LOG_E("Net:[%{public}d] httpProbeUrl is empty", netId_);
89         isDetecting_ = false;
90         latch_->CountDown();
91         latchAll_->CountDown();
92         return;
93     }
94 
95     if (httpProbe_ == nullptr) {
96         NETMGR_LOG_E("Net:[%{public}d] httpProbe_ is nullptr", netId_);
97         isDetecting_ = false;
98         latch_->CountDown();
99         latchAll_->CountDown();
100         return;
101     }
102 
103     if (httpProbe_->SendProbe(probeType, httpProbeUrl_, httpsProbeUrl_) != NETMANAGER_SUCCESS) {
104         NETMGR_LOG_E("Net:[%{public}d] send probe failed.", netId_);
105         isDetecting_ = false;
106         latch_->CountDown();
107         latchAll_->CountDown();
108         return;
109     }
110 
111     if (IsConclusiveResult()) {
112         isDetecting_ = false;
113         while (latch_->GetCount() > 0) {
114             latch_->CountDown();
115         }
116         while (latchAll_->GetCount() > 0) {
117             latchAll_->CountDown();
118         }
119     }
120     isDetecting_ = false;
121     if (latch_->GetCount() > 0) {
122         latch_->CountDown();
123     }
124     if (latchAll_->GetCount() > 0) {
125         latchAll_->CountDown();
126     }
127 }
128 
IsConclusiveResult()129 bool ProbeThread::IsConclusiveResult()
130 {
131     if ((probeType_ == ProbeType::PROBE_HTTP || probeType_ == ProbeType::PROBE_HTTP_FALLBACK) &&
132         httpProbe_->GetHttpProbeResult().IsNeedPortal()) {
133         NETMGR_LOG_I("http url detection result: portal");
134         return true;
135     }
136     if ((probeType_ == ProbeType::PROBE_HTTPS || probeType_ == ProbeType::PROBE_HTTPS_FALLBACK) &&
137         httpProbe_->GetHttpsProbeResult().IsSuccessful()) {
138         NETMGR_LOG_I("https url detection result: success");
139         return true;
140     }
141     return false;
142 }
143 
UpdateGlobalHttpProxy(const HttpProxy & httpProxy)144 void ProbeThread::UpdateGlobalHttpProxy(const HttpProxy &httpProxy)
145 {
146     if (httpProbe_) {
147         httpProbe_->UpdateGlobalHttpProxy(httpProxy);
148     }
149 }
150 
ProbeWithoutGlobalHttpProxy()151 void ProbeThread::ProbeWithoutGlobalHttpProxy()
152 {
153     httpProbe_->ProbeWithoutGlobalHttpProxy();
154 }
155 
GetHttpProbeResult()156 NetHttpProbeResult ProbeThread::GetHttpProbeResult()
157 {
158     return httpProbe_->GetHttpProbeResult();
159 }
160 
GetHttpsProbeResult()161 NetHttpProbeResult ProbeThread::GetHttpsProbeResult()
162 {
163     return httpProbe_->GetHttpsProbeResult();
164 }
165 
IsDetecting()166 bool ProbeThread::IsDetecting()
167 {
168     return isDetecting_.load();
169 }
170 
GetProbeType()171 ProbeType ProbeThread::GetProbeType()
172 {
173     return probeType_;
174 }
175 
176 }
177 }