• 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 
16 #include "networkvpn_client.h"
17 
18 #include <thread>
19 #ifdef SUPPORT_SYSVPN
20 #include <vector>
21 #endif // SUPPORT_SYSVPN
22 
23 #include "fwmark_client.h"
24 #include "iservice_registry.h"
25 #include "net_manager_constants.h"
26 #include "netmgr_ext_log_wrapper.h"
27 #include "system_ability_definition.h"
28 #include "network_vpn_service_proxy.h"
29 #include "system_ability_status_change_stub.h"
30 
31 namespace OHOS {
32 namespace NetManagerStandard {
33 
34 static constexpr uint32_t WAIT_FOR_SERVICE_TIME_MS = 500;
35 static constexpr uint32_t MAX_GET_SERVICE_COUNT = 10;
36 
37 class NetworkVpnClient::SystemAbilityListener : public SystemAbilityStatusChangeStub {
38 public:
39     SystemAbilityListener() = default;
40     ~SystemAbilityListener() = default;
41     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
42     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
43 };
44 
OnVpnMultiUserSetUp()45 int32_t VpnSetUpEventCallback::OnVpnMultiUserSetUp()
46 {
47     NETMGR_EXT_LOG_I("vpn multiple user setup event.");
48     NetworkVpnClient::GetInstance().multiUserSetUpEvent();
49     return NETMANAGER_EXT_SUCCESS;
50 }
51 
OnVpnStateChanged(bool isConnected)52 int32_t VpnEventCallbackCollection::OnVpnStateChanged(bool isConnected)
53 {
54     std::shared_lock<std::shared_mutex> lock(vpnEventCbMutex_);
55     std::list<sptr<IVpnEventCallback>> tmpList = vpnEventCbList_;
56     lock.unlock();
57     for (auto iter = tmpList.begin(); iter != tmpList.end(); iter++) {
58         (*iter)->OnVpnStateChanged(isConnected);
59     }
60     return NETMANAGER_EXT_SUCCESS;
61 }
62 
OnMultiVpnStateChanged(bool isConnected,const std::string & bundleName,const std::string & vpnId)63 int32_t VpnEventCallbackCollection::OnMultiVpnStateChanged(
64     bool isConnected, const std::string &bundleName, const std::string &vpnId)
65 {
66     std::shared_lock<std::shared_mutex> lock(vpnEventCbMutex_);
67     std::list<sptr<IVpnEventCallback>> tmpList = vpnEventCbList_;
68     lock.unlock();
69     for (auto iter = tmpList.begin(); iter != tmpList.end(); iter++) {
70         (*iter)->OnMultiVpnStateChanged(isConnected, bundleName, vpnId);
71     }
72     return NETMANAGER_EXT_SUCCESS;
73 }
74 
OnVpnMultiUserSetUp()75 int32_t VpnEventCallbackCollection::OnVpnMultiUserSetUp()
76 {
77     std::shared_lock<std::shared_mutex> lock(vpnEventCbMutex_);
78     std::list<sptr<IVpnEventCallback>> tmpList = vpnEventCbList_;
79     lock.unlock();
80     for (auto iter = tmpList.begin(); iter != tmpList.end(); iter++) {
81         (*iter)->OnVpnMultiUserSetUp();
82     }
83     return NETMANAGER_EXT_SUCCESS;
84 }
85 
RegisterCallback(sptr<IVpnEventCallback> callback)86 int32_t VpnEventCallbackCollection::RegisterCallback(sptr<IVpnEventCallback> callback)
87 {
88     std::unique_lock<std::shared_mutex> lock(vpnEventCbMutex_);
89     for (auto iter = vpnEventCbList_.begin(); iter != vpnEventCbList_.end(); iter++) {
90         if ((*iter)->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) {
91             return NETMANAGER_EXT_ERR_OPERATION_FAILED;
92         }
93     }
94     vpnEventCbList_.push_back(callback);
95     return NETMANAGER_EXT_SUCCESS;
96 }
97 
UnregisterCallback(sptr<IVpnEventCallback> callback)98 int32_t VpnEventCallbackCollection::UnregisterCallback(sptr<IVpnEventCallback> callback)
99 {
100     std::unique_lock<std::shared_mutex> lock(vpnEventCbMutex_);
101     for (auto iter = vpnEventCbList_.begin(); iter != vpnEventCbList_.end(); iter++) {
102         if ((*iter)->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) {
103             vpnEventCbList_.erase(iter);
104             break;
105         }
106     }
107     return NETMANAGER_EXT_SUCCESS;
108 }
109 
GetCallbackNum()110 int32_t VpnEventCallbackCollection::GetCallbackNum()
111 {
112     std::shared_lock<std::shared_mutex> lock(vpnEventCbMutex_);
113     return vpnEventCbList_.size();
114 }
115 
NetworkVpnClient()116 NetworkVpnClient::NetworkVpnClient() : saStatusChangeListener_(nullptr)
117 {
118     Subscribe();
119 }
120 
~NetworkVpnClient()121 NetworkVpnClient::~NetworkVpnClient()
122 {
123     Unsubscribe();
124     UnregisterVpnEventCbCollection();
125 #ifdef SUPPORT_SYSVPN
126     UnregisterMultiVpnEventCbCollection();
127 #endif
128 }
129 
GetInstance()130 NetworkVpnClient &NetworkVpnClient::GetInstance()
131 {
132     static NetworkVpnClient instance;
133     return instance;
134 }
135 
Subscribe()136 void NetworkVpnClient::Subscribe()
137 {
138     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
139     if (samgrProxy != nullptr) {
140         saStatusChangeListener_ = sptr<SystemAbilityListener>::MakeSptr();
141         samgrProxy->SubscribeSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID, saStatusChangeListener_);
142     }
143 }
144 
Unsubscribe()145 void NetworkVpnClient::Unsubscribe()
146 {
147     if (saStatusChangeListener_ != nullptr) {
148         auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
149         if (samgrProxy != nullptr) {
150             samgrProxy->UnSubscribeSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID, saStatusChangeListener_);
151         }
152     }
153 }
154 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)155 void NetworkVpnClient::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
156 {
157     switch (systemAbilityId) {
158         case COMM_VPN_MANAGER_SYS_ABILITY_ID: {
159             NetworkVpnClient::GetInstance().SetVpnSaState(true);
160             NetworkVpnClient::GetInstance().RegisterVpnEventCbCollection();
161 #ifdef SUPPORT_SYSVPN
162             NetworkVpnClient::GetInstance().RegisterMultiVpnEventCbCollection();
163 #endif
164             break;
165         }
166         default:
167             break;
168     }
169 }
170 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)171 void NetworkVpnClient::SystemAbilityListener::OnRemoveSystemAbility(
172     int32_t systemAbilityId, const std::string &deviceId)
173 {
174     switch (systemAbilityId) {
175         case COMM_VPN_MANAGER_SYS_ABILITY_ID:
176             NetworkVpnClient::GetInstance().SetVpnSaState(false);
177             break;
178         default:
179             break;
180     }
181 }
182 
Prepare(bool & isExistVpn,bool & isRun,std::string & pkg)183 int32_t NetworkVpnClient::Prepare(bool &isExistVpn, bool &isRun, std::string &pkg)
184 {
185     sptr<INetworkVpnService> proxy = GetProxy();
186     if (proxy == nullptr) {
187         NETMGR_EXT_LOG_E("Prepare proxy is nullptr");
188         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
189     }
190     return proxy->Prepare(isExistVpn, isRun, pkg);
191 }
192 
Protect(int32_t socketFd,bool isVpnExtCall)193 int32_t NetworkVpnClient::Protect(int32_t socketFd, bool isVpnExtCall)
194 {
195     if (socketFd <= 0) {
196         NETMGR_EXT_LOG_E("Invalid socket file discriptor");
197         return NETWORKVPN_ERROR_INVALID_FD;
198     }
199 
200     sptr<INetworkVpnService> proxy = GetProxy();
201     if (proxy == nullptr) {
202         NETMGR_EXT_LOG_E("Protect proxy is nullptr");
203         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
204     }
205     int32_t result = proxy->Protect(isVpnExtCall);
206     if (result != NETMANAGER_EXT_SUCCESS) {
207         return result;
208     }
209     nmd::FwmarkClient fwmarkClient;
210     int32_t protectResult = fwmarkClient.ProtectFromVpn(socketFd);
211     if (protectResult == NETMANAGER_ERROR) {
212         return NETWORKVPN_ERROR_INVALID_FD;
213     }
214     return protectResult;
215 }
216 
SetUpVpn(sptr<VpnConfig> config,int32_t & tunFd,bool isVpnExtCall)217 int32_t NetworkVpnClient::SetUpVpn(sptr<VpnConfig> config, int32_t &tunFd, bool isVpnExtCall)
218 {
219     if (config == nullptr) {
220         NETMGR_EXT_LOG_E("SetUpVpn param config is nullptr");
221         return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
222     }
223 
224     sptr<INetworkVpnService> proxy = GetProxy();
225     if (proxy == nullptr) {
226         NETMGR_EXT_LOG_E("SetUpVpn proxy is nullptr");
227         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
228     }
229     NETMGR_EXT_LOG_I("enter SetUpVpn 1, %{public}d", isVpnExtCall);
230     int32_t result = proxy->SetUpVpn(*config, isVpnExtCall);
231     if (result != NETMANAGER_EXT_SUCCESS) {
232         tunFd = 0;
233         return result;
234     }
235     clientVpnConfig_.first = config;
236     clientVpnConfig_.second = isVpnExtCall;
237 #ifdef SUPPORT_SYSVPN
238     vpnInterface_.SetSupportMultiVpn(!config->vpnId_.empty());
239 #endif // SUPPORT_SYSVPN
240     tunFd = vpnInterface_.GetVpnInterfaceFd();
241     if (tunFd <= 0) {
242         return NETMANAGER_EXT_ERR_INTERNAL;
243     }
244 
245     if (vpnEventCallback_ != nullptr) {
246         UnregisterVpnEvent(vpnEventCallback_);
247     }
248     vpnEventCallback_ = new (std::nothrow) VpnSetUpEventCallback();
249     if (vpnEventCallback_ == nullptr) {
250         NETMGR_EXT_LOG_E("vpnEventCallback_ is nullptr");
251         return NETMANAGER_EXT_ERR_INTERNAL;
252     }
253     RegisterVpnEvent(vpnEventCallback_);
254     return NETMANAGER_EXT_SUCCESS;
255 }
256 
DestroyVpn(bool isVpnExtCall)257 int32_t NetworkVpnClient::DestroyVpn(bool isVpnExtCall)
258 {
259     vpnInterface_.CloseVpnInterfaceFd();
260     if (vpnEventCallback_ != nullptr) {
261         UnregisterVpnEvent(vpnEventCallback_);
262         vpnEventCallback_ = nullptr;
263     }
264 
265     sptr<INetworkVpnService> proxy = GetProxy();
266     if (proxy == nullptr) {
267         NETMGR_EXT_LOG_E("DestroyVpn proxy is nullptr");
268         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
269     }
270     return proxy->DestroyVpn(isVpnExtCall);
271 }
272 
273 #ifdef SUPPORT_SYSVPN
GetVpnCertData(const int32_t certType,std::vector<int8_t> & certData)274 int32_t NetworkVpnClient::GetVpnCertData(const int32_t certType, std::vector<int8_t> &certData)
275 {
276     sptr<INetworkVpnService> proxy = GetProxy();
277     if (proxy == nullptr) {
278         NETMGR_EXT_LOG_E("GetVpnCertData proxy is nullptr");
279         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
280     }
281     return proxy->GetVpnCertData(certType, certData);
282 }
283 
DestroyVpn(const std::string & vpnId)284 int32_t NetworkVpnClient::DestroyVpn(const std::string &vpnId)
285 {
286     if (vpnId.empty()) {
287         NETMGR_EXT_LOG_E("DestroyVpn vpnId is empty");
288         return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
289     }
290     vpnInterface_.CloseVpnInterfaceFd();
291     if (vpnEventCallback_ != nullptr) {
292         UnregisterVpnEvent(vpnEventCallback_);
293         vpnEventCallback_ = nullptr;
294     }
295 
296     sptr<INetworkVpnService> proxy = GetProxy();
297     if (proxy == nullptr) {
298         NETMGR_EXT_LOG_E("DestroyVpn proxy is nullptr");
299         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
300     }
301     return proxy->DestroyVpn(vpnId);
302 }
303 
SetUpVpn(const sptr<SysVpnConfig> & config,bool isVpnExtCall)304 int32_t NetworkVpnClient::SetUpVpn(const sptr<SysVpnConfig> &config, bool isVpnExtCall)
305 {
306     if (config == nullptr) {
307         NETMGR_EXT_LOG_E("SetUpVpn param config is nullptr");
308         return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
309     }
310     sptr<INetworkVpnService> proxy = GetProxy();
311     if (proxy == nullptr) {
312         NETMGR_EXT_LOG_E("SetUpVpn proxy is nullptr");
313         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
314     }
315     NETMGR_EXT_LOG_I("SetUpVpn id=%{public}s", config->vpnId_.c_str());
316     return proxy->SetUpSysVpn(config, isVpnExtCall);
317 }
318 
AddSysVpnConfig(sptr<SysVpnConfig> & config)319 int32_t NetworkVpnClient::AddSysVpnConfig(sptr<SysVpnConfig> &config)
320 {
321     if (config == nullptr) {
322         NETMGR_EXT_LOG_E("AddSysVpnConfig config is null");
323         return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
324     }
325     sptr<INetworkVpnService> proxy = GetProxy();
326     if (proxy == nullptr) {
327         NETMGR_EXT_LOG_E("AddSysVpnConfig proxy is nullptr");
328         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
329     }
330     return proxy->AddSysVpnConfig(config);
331 }
332 
DeleteSysVpnConfig(const std::string & vpnId)333 int32_t NetworkVpnClient::DeleteSysVpnConfig(const std::string &vpnId)
334 {
335     if (vpnId.empty()) {
336         NETMGR_EXT_LOG_E("DeleteSysVpnConfig vpnId is empty");
337         return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
338     }
339     sptr<INetworkVpnService> proxy = GetProxy();
340     if (proxy == nullptr) {
341         NETMGR_EXT_LOG_E("DeleteSysVpnConfig proxy is nullptr");
342         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
343     }
344     return proxy->DeleteSysVpnConfig(vpnId);
345 }
346 
GetConnectedVpnAppInfo(std::vector<std::string> & bundleNameList)347 int32_t NetworkVpnClient::GetConnectedVpnAppInfo(std::vector<std::string> &bundleNameList)
348 {
349     sptr<INetworkVpnService> proxy = GetProxy();
350     if (proxy == nullptr) {
351         NETMGR_EXT_LOG_E("GetConnectedVpnAppInfo proxy is nullptr");
352         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
353     }
354     return proxy->GetConnectedVpnAppInfo(bundleNameList);
355 }
356 
GetSysVpnConfigList(std::vector<sptr<SysVpnConfig>> & vpnList)357 int32_t NetworkVpnClient::GetSysVpnConfigList(std::vector<sptr<SysVpnConfig>> &vpnList)
358 {
359     sptr<INetworkVpnService> proxy = GetProxy();
360     if (proxy == nullptr) {
361         NETMGR_EXT_LOG_E("GetSysVpnConfigList proxy is nullptr");
362         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
363     }
364     return proxy->GetSysVpnConfigList(vpnList);
365 }
366 
GetSysVpnConfig(sptr<SysVpnConfig> & config,const std::string & vpnId)367 int32_t NetworkVpnClient::GetSysVpnConfig(sptr<SysVpnConfig> &config, const std::string &vpnId)
368 {
369     if (vpnId.empty()) {
370         NETMGR_EXT_LOG_E("DeleteSysVpnConfig vpnId is empty");
371         return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
372     }
373     sptr<INetworkVpnService> proxy = GetProxy();
374     if (proxy == nullptr) {
375         NETMGR_EXT_LOG_E("GetSysVpnConfig proxy is nullptr");
376         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
377     }
378     return proxy->GetSysVpnConfig(config, vpnId);
379 }
380 
GetConnectedSysVpnConfig(sptr<SysVpnConfig> & config)381 int32_t NetworkVpnClient::GetConnectedSysVpnConfig(sptr<SysVpnConfig> &config)
382 {
383     sptr<INetworkVpnService> proxy = GetProxy();
384     if (proxy == nullptr) {
385         NETMGR_EXT_LOG_E("GetConnectedSysVpnConfig proxy is nullptr");
386         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
387     }
388     return proxy->GetConnectedSysVpnConfig(config);
389 }
390 
NotifyConnectStage(const std::string & stage,const int32_t & result)391 int32_t NetworkVpnClient::NotifyConnectStage(const std::string &stage, const int32_t &result)
392 {
393     sptr<INetworkVpnService> proxy = GetProxy();
394     if (proxy == nullptr) {
395         NETMGR_EXT_LOG_E("NotifyConnectStage proxy is nullptr");
396         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
397     }
398     return proxy->NotifyConnectStage(stage, result);
399 }
400 
GetSysVpnCertUri(const int32_t certType,std::string & certUri)401 int32_t NetworkVpnClient::GetSysVpnCertUri(const int32_t certType, std::string &certUri)
402 {
403     sptr<INetworkVpnService> proxy = GetProxy();
404     if (proxy == nullptr) {
405         NETMGR_EXT_LOG_E("GetSysVpnCertUri proxy is nullptr");
406         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
407     }
408     return proxy->GetSysVpnCertUri(certType, certUri);
409 }
410 
RegisterMultiVpnEvent(sptr<IVpnEventCallback> callback)411 int32_t NetworkVpnClient::RegisterMultiVpnEvent(sptr<IVpnEventCallback> callback)
412 {
413     if (callback == nullptr) {
414         NETMGR_EXT_LOG_E("RegisterMultiVpnEvent callback is null.");
415         return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
416     }
417     if (multiVpnEventCbCollection_ == nullptr) {
418         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
419     }
420     int ret = multiVpnEventCbCollection_->RegisterCallback(callback);
421     if (ret == NETMANAGER_EXT_SUCCESS && multiVpnEventCbCollection_->GetCallbackNum() == 1) {
422         RegisterMultiVpnEventCbCollection();
423     }
424     return ret;
425 }
426 
UnregisterMultiVpnEvent(sptr<IVpnEventCallback> callback)427 int32_t NetworkVpnClient::UnregisterMultiVpnEvent(sptr<IVpnEventCallback> callback)
428 {
429     if (callback == nullptr) {
430         NETMGR_EXT_LOG_E("UnregisterMultiVpnEvent callback is null.");
431         return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
432     }
433     if (multiVpnEventCbCollection_ != nullptr) {
434         int ret = multiVpnEventCbCollection_->UnregisterCallback(callback);
435         if (ret == NETMANAGER_EXT_SUCCESS && multiVpnEventCbCollection_->GetCallbackNum() == 0) {
436             UnregisterMultiVpnEventCbCollection();
437         }
438     }
439     return NETMANAGER_EXT_SUCCESS;
440 }
441 
RegisterMultiVpnEventCbCollection()442 void NetworkVpnClient::RegisterMultiVpnEventCbCollection()
443 {
444     if (multiVpnEventCbCollection_ == nullptr || multiVpnEventCbCollection_->GetCallbackNum() == 0 || !saStart_) {
445         return;
446     }
447     sptr<INetworkVpnService> proxy = GetProxy();
448     if (proxy == nullptr) {
449         NETMGR_EXT_LOG_E("RegisterMultiVpnEventCbCollection proxy is nullptr");
450         return;
451     }
452     proxy->RegisterMultiVpnEvent(multiVpnEventCbCollection_);
453 }
454 
UnregisterMultiVpnEventCbCollection()455 void NetworkVpnClient::UnregisterMultiVpnEventCbCollection()
456 {
457     if (multiVpnEventCbCollection_ != nullptr) {
458         sptr<INetworkVpnService> proxy = GetProxy();
459         if (proxy == nullptr) {
460             NETMGR_EXT_LOG_E("UnregisterMultiVpnEventCbCollection proxy is nullptr");
461             return;
462         }
463         proxy->UnregisterMultiVpnEvent(multiVpnEventCbCollection_);
464     }
465 }
466 #endif // SUPPORT_SYSVPN
467 
RegisterVpnEvent(sptr<IVpnEventCallback> callback)468 int32_t NetworkVpnClient::RegisterVpnEvent(sptr<IVpnEventCallback> callback)
469 {
470     if (callback == nullptr) {
471         NETMGR_EXT_LOG_E("RegisterVpnEvent callback is null.");
472         return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
473     }
474     if (vpnEventCbCollection_ == nullptr) {
475         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
476     }
477     int ret = vpnEventCbCollection_->RegisterCallback(callback);
478     if (ret == NETMANAGER_EXT_SUCCESS && vpnEventCbCollection_->GetCallbackNum() == 1) {
479         RegisterVpnEventCbCollection();
480     }
481     return ret;
482 }
483 
UnregisterVpnEvent(sptr<IVpnEventCallback> callback)484 int32_t NetworkVpnClient::UnregisterVpnEvent(sptr<IVpnEventCallback> callback)
485 {
486     if (callback == nullptr) {
487         NETMGR_EXT_LOG_E("UnregisterVpnEvent callback is null.");
488         return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
489     }
490     if (vpnEventCbCollection_ != nullptr) {
491         int ret = vpnEventCbCollection_->UnregisterCallback(callback);
492         if (ret == NETMANAGER_EXT_SUCCESS && vpnEventCbCollection_->GetCallbackNum() == 0) {
493             UnregisterVpnEventCbCollection();
494         }
495     }
496     return NETMANAGER_EXT_SUCCESS;
497 }
498 
RegisterVpnEventCbCollection()499 void NetworkVpnClient::RegisterVpnEventCbCollection()
500 {
501     if (vpnEventCbCollection_ == nullptr || vpnEventCbCollection_->GetCallbackNum() == 0 || !saStart_) {
502         return;
503     }
504     sptr<INetworkVpnService> proxy = GetProxy();
505     if (proxy == nullptr) {
506         NETMGR_EXT_LOG_E("RegisterVpnEventCbCollection proxy is nullptr");
507         return;
508     }
509     proxy->RegisterVpnEvent(vpnEventCbCollection_);
510 }
511 
UnregisterVpnEventCbCollection()512 void NetworkVpnClient::UnregisterVpnEventCbCollection()
513 {
514     if (vpnEventCbCollection_ != nullptr) {
515         sptr<INetworkVpnService> proxy = GetProxy();
516         if (proxy == nullptr) {
517             NETMGR_EXT_LOG_E("UnregisterVpnEventCbCollection proxy is nullptr");
518             return;
519         }
520         proxy->UnregisterVpnEvent(vpnEventCbCollection_);
521     }
522 }
523 
CreateVpnConnection(bool isVpnExtCall)524 int32_t NetworkVpnClient::CreateVpnConnection(bool isVpnExtCall)
525 {
526     sptr<INetworkVpnService> proxy = GetProxy();
527     if (proxy == nullptr) {
528         NETMGR_EXT_LOG_E("CreateVpnConnection proxy is nullptr");
529         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
530     }
531     return proxy->CreateVpnConnection(isVpnExtCall);
532 }
533 
RegisterBundleName(const std::string & bundleName,const std::string & abilityName)534 int32_t NetworkVpnClient::RegisterBundleName(const std::string &bundleName, const std::string &abilityName)
535 {
536     NETMGR_EXT_LOG_D("VpnClient::RegisterBundleName is %{public}s", bundleName.c_str());
537     sptr<INetworkVpnService> proxy = GetProxy();
538     if (proxy == nullptr) {
539         NETMGR_EXT_LOG_E("CreateVpnConnection proxy is nullptr");
540         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
541     }
542     return proxy->RegisterBundleName(bundleName, abilityName);
543 }
544 
GetProxy()545 sptr<INetworkVpnService> NetworkVpnClient::GetProxy()
546 {
547     std::lock_guard lock(mutex_);
548     if (networkVpnService_ != nullptr) {
549         return networkVpnService_;
550     }
551     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
552     if (sam == nullptr) {
553         NETMGR_EXT_LOG_E("get SystemAbilityManager failed");
554         return nullptr;
555     }
556     sptr<IRemoteObject> remote = sam->GetSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID);
557     if (remote == nullptr) {
558         NETMGR_EXT_LOG_E("get Remote vpn service failed");
559         return nullptr;
560     }
561     deathRecipient_ = new (std::nothrow) MonitorVpnServiceDead(*this);
562     if (deathRecipient_ == nullptr) {
563         NETMGR_EXT_LOG_E("deathRecipient_ is nullptr");
564         return nullptr;
565     }
566     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
567         NETMGR_EXT_LOG_E("add death recipient failed");
568         return nullptr;
569     }
570     networkVpnService_ = iface_cast<INetworkVpnService>(remote);
571     if (networkVpnService_ == nullptr) {
572         NETMGR_EXT_LOG_E("get Remote service proxy failed");
573         return nullptr;
574     }
575     return networkVpnService_;
576 }
577 
RecoverCallback()578 void NetworkVpnClient::RecoverCallback()
579 {
580     uint32_t count = 0;
581     while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
582         std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_SERVICE_TIME_MS));
583         count++;
584     }
585     auto proxy = GetProxy();
586     if (proxy != nullptr && clientVpnConfig_.first != nullptr) {
587         proxy->SetUpVpn(*clientVpnConfig_.first, clientVpnConfig_.second);
588     }
589     NETMGR_EXT_LOG_D("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
590 }
591 
OnRemoteDied(const wptr<IRemoteObject> & remote)592 void NetworkVpnClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
593 {
594     if (remote == nullptr) {
595         NETMGR_EXT_LOG_E("remote object is nullptr");
596         return;
597     }
598     {
599         std::lock_guard lock(mutex_);
600         if (networkVpnService_ == nullptr) {
601             NETMGR_EXT_LOG_E("networkVpnService_ is nullptr");
602             return;
603         }
604         sptr<IRemoteObject> local = networkVpnService_->AsObject();
605         if (local != remote.promote()) {
606             NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
607             return;
608         }
609         local->RemoveDeathRecipient(deathRecipient_);
610         networkVpnService_ = nullptr;
611     }
612 
613     if (vpnEventCallback_ != nullptr) {
614         NETMGR_EXT_LOG_D("on remote died recover callback");
615         std::thread t([this]() {
616             RecoverCallback();
617         });
618         std::string threadName = "networkvpnRecoverCallback";
619         pthread_setname_np(t.native_handle(), threadName.c_str());
620         t.detach();
621     }
622 }
623 
multiUserSetUpEvent()624 void NetworkVpnClient::multiUserSetUpEvent()
625 {
626     vpnInterface_.CloseVpnInterfaceFd();
627     if (vpnEventCallback_ != nullptr) {
628         UnregisterVpnEvent(vpnEventCallback_);
629         vpnEventCallback_ = nullptr;
630     }
631 }
632 
GetSelfAppName(std::string & selfAppName,std::string & selfBundleName)633 int32_t NetworkVpnClient::GetSelfAppName(std::string &selfAppName, std::string &selfBundleName)
634 {
635     auto proxy = GetProxy();
636     if (proxy == nullptr) {
637         NETMGR_EXT_LOG_E("GetSelfAppName proxy is nullptr");
638         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
639     }
640     return proxy->GetSelfAppName(selfAppName, selfBundleName);
641 }
642 
SetSelfVpnPid()643 int32_t NetworkVpnClient::SetSelfVpnPid()
644 {
645     auto proxy = GetProxy();
646     if (proxy == nullptr) {
647         NETMGR_EXT_LOG_E("SetSelfVpnPid proxy is nullptr");
648         return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
649     }
650     return proxy->SetSelfVpnPid();
651 }
652 
SetVpnSaState(bool state)653 void NetworkVpnClient::SetVpnSaState(bool state)
654 {
655     saStart_ = state;
656 }
657 } // namespace NetManagerStandard
658 } // namespace OHOS
659