• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "net_conn_client.h"
17 #include <thread>
18 #include <dlfcn.h>
19 
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 
23 #include "fwmark_client.h"
24 #include "net_conn_service_proxy.h"
25 #include "net_manager_constants.h"
26 #include "net_mgr_log_wrapper.h"
27 #include "net_bundle.h"
28 #include "net_supplier_callback_stub.h"
29 #include "netsys_sock_client.h"
30 #include "network_security_config.h"
31 
32 static constexpr const int32_t MIN_VALID_NETID = 100;
33 static constexpr const int32_t MIN_VALID_INTERNAL_NETID = 1;
34 static constexpr const int32_t MAX_VALID_INTERNAL_NETID = 50;
35 static constexpr uint32_t WAIT_FOR_SERVICE_TIME_MS = 500;
36 static constexpr uint32_t MAX_GET_SERVICE_COUNT = 10;
37 static const std::string LIB_NET_BUNDLE_UTILS_PATH = "libnet_bundle_utils.z.so";
38 
39 namespace OHOS {
40 namespace NetManagerStandard {
NetConnClient()41 NetConnClient::NetConnClient() : NetConnService_(nullptr), deathRecipient_(nullptr)
42 {
43     buffer_[RESERVED_BUFFER_SIZE-1] = '\0';
44 }
45 
~NetConnClient()46 NetConnClient::~NetConnClient()
47 {
48     DlCloseRemoveDeathRecipient();
49 }
50 
GetInstance()51 NetConnClient &NetConnClient::GetInstance()
52 {
53     static NetConnClient gInstance;
54     return gInstance;
55 }
56 
SystemReady()57 int32_t NetConnClient::SystemReady()
58 {
59     sptr<INetConnService> proxy = GetProxy();
60     if (proxy == nullptr) {
61         NETMGR_LOG_E("proxy is nullptr");
62         return NETMANAGER_ERR_GET_PROXY_FAIL;
63     }
64     return proxy->SystemReady();
65 }
66 
SetInternetPermission(uint32_t uid,uint8_t allow)67 int32_t NetConnClient::SetInternetPermission(uint32_t uid, uint8_t allow)
68 {
69     uint8_t oldAllow;
70     bool ret = netPermissionMap_.Find(uid, oldAllow);
71     if (ret && allow == oldAllow) {
72         return NETMANAGER_SUCCESS;
73     }
74 
75     sptr<INetConnService> proxy = GetProxy();
76     if (proxy == nullptr) {
77         NETMGR_LOG_E("proxy is nullptr");
78         return NETMANAGER_ERR_GET_PROXY_FAIL;
79     }
80     int32_t result = proxy->SetInternetPermission(uid, allow);
81     if (result == NETMANAGER_SUCCESS) {
82         netPermissionMap_.EnsureInsert(uid, allow);
83     }
84     return result;
85 }
86 
EnableVnicNetwork(const sptr<NetLinkInfo> & netLinkInfo,const std::set<int32_t> & uids)87 int32_t NetConnClient::EnableVnicNetwork(const sptr<NetLinkInfo> &netLinkInfo, const std::set<int32_t> &uids)
88 {
89     NETMGR_LOG_D("EnableVnicNetwork client in.");
90     sptr<INetConnService> proxy = GetProxy();
91     if (proxy == nullptr) {
92         NETMGR_LOG_E("proxy is nullptr");
93         return NETMANAGER_ERR_GET_PROXY_FAIL;
94     }
95 
96     return proxy->EnableVnicNetwork(netLinkInfo, uids);
97 }
98 
DisableVnicNetwork()99 int32_t NetConnClient::DisableVnicNetwork()
100 {
101     NETMGR_LOG_D("DisableVnicNetwork client in.");
102     sptr<INetConnService> proxy = GetProxy();
103     if (proxy == nullptr) {
104         NETMGR_LOG_E("proxy is nullptr");
105         return NETMANAGER_ERR_GET_PROXY_FAIL;
106     }
107 
108     return proxy->DisableVnicNetwork();
109 }
110 
RegisterNetSupplier(NetBearType bearerType,const std::string & ident,const std::set<NetCap> & netCaps,uint32_t & supplierId)111 int32_t NetConnClient::RegisterNetSupplier(NetBearType bearerType, const std::string &ident,
112                                            const std::set<NetCap> &netCaps, uint32_t &supplierId)
113 {
114     NETMGR_LOG_D("RegisterNetSupplier client in.");
115     sptr<INetConnService> proxy = GetProxy();
116     if (proxy == nullptr) {
117         NETMGR_LOG_E("proxy is nullptr");
118         return NETMANAGER_ERR_GET_PROXY_FAIL;
119     }
120 
121     return proxy->RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
122 }
123 
UnregisterNetSupplier(uint32_t supplierId)124 int32_t NetConnClient::UnregisterNetSupplier(uint32_t supplierId)
125 {
126     NETMGR_LOG_D("UnregisterNetSupplier client in.");
127     sptr<INetConnService> proxy = GetProxy();
128     if (proxy == nullptr) {
129         NETMGR_LOG_E("proxy is nullptr");
130         return NETMANAGER_ERR_GET_PROXY_FAIL;
131     }
132     netSupplierCallback_.erase(supplierId);
133     return proxy->UnregisterNetSupplier(supplierId);
134 }
135 
RegisterNetSupplierCallback(uint32_t supplierId,const sptr<NetSupplierCallbackBase> & callback)136 int32_t NetConnClient::RegisterNetSupplierCallback(uint32_t supplierId, const sptr<NetSupplierCallbackBase> &callback)
137 {
138     NETMGR_LOG_D("RegisterNetSupplierCallback client in.");
139     sptr<INetConnService> proxy = GetProxy();
140     if (proxy == nullptr) {
141         NETMGR_LOG_E("proxy is nullptr");
142         return NETMANAGER_ERR_GET_PROXY_FAIL;
143     }
144     sptr<NetSupplierCallbackStub> ptr = std::make_unique<NetSupplierCallbackStub>().release();
145     ptr->RegisterSupplierCallbackImpl(callback);
146     netSupplierCallback_[supplierId] = ptr;
147     return proxy->RegisterNetSupplierCallback(supplierId, ptr);
148 }
149 
RegisterNetConnCallback(const sptr<INetConnCallback> callback)150 int32_t NetConnClient::RegisterNetConnCallback(const sptr<INetConnCallback> callback)
151 {
152     NETMGR_LOG_D("RegisterNetConnCallback client in.");
153     sptr<INetConnService> proxy = GetProxy();
154     if (proxy == nullptr) {
155         NETMGR_LOG_E("The parameter of proxy is nullptr");
156         return NETMANAGER_ERR_GET_PROXY_FAIL;
157     }
158     int32_t ret = proxy->RegisterNetConnCallback(callback);
159     if (ret == NETMANAGER_SUCCESS) {
160         NETMGR_LOG_D("RegisterNetConnCallback success, save callback.");
161         std::lock_guard<std::mutex> locker(registerConnTupleListMutex_);
162         registerConnTupleList_.push_back(std::make_tuple(nullptr, callback, 0));
163     }
164 
165     return ret;
166 }
167 
RegisterNetConnCallback(const sptr<NetSpecifier> & netSpecifier,const sptr<INetConnCallback> callback,const uint32_t & timeoutMS)168 int32_t NetConnClient::RegisterNetConnCallback(const sptr<NetSpecifier> &netSpecifier,
169                                                const sptr<INetConnCallback> callback, const uint32_t &timeoutMS)
170 {
171     NETMGR_LOG_D("RegisterNetConnCallback with timeout client in.");
172     if (netSpecifier == nullptr || !netSpecifier->SpecifierIsValid()) {
173         NETMGR_LOG_E("The parameter of netSpecifier is invalid");
174         return NETMANAGER_ERR_PARAMETER_ERROR;
175     }
176     sptr<INetConnService> proxy = GetProxy();
177     if (proxy == nullptr) {
178         NETMGR_LOG_E("The parameter of proxy is nullptr");
179         return NETMANAGER_ERR_GET_PROXY_FAIL;
180     }
181     int32_t ret = proxy->RegisterNetConnCallback(netSpecifier, callback, timeoutMS);
182     if (ret == NETMANAGER_SUCCESS) {
183         NETMGR_LOG_D("RegisterNetConnCallback success, save netSpecifier and callback and timeoutMS.");
184         std::lock_guard<std::mutex> locker(registerConnTupleListMutex_);
185         registerConnTupleList_.push_back(std::make_tuple(netSpecifier, callback, timeoutMS));
186     }
187 
188     return ret;
189 }
190 
RequestNetConnection(const sptr<NetSpecifier> netSpecifier,const sptr<INetConnCallback> callback,const uint32_t timeoutMS)191 int32_t NetConnClient::RequestNetConnection(const sptr<NetSpecifier> netSpecifier,
192                                             const sptr<INetConnCallback> callback, const uint32_t timeoutMS)
193 {
194     NETMGR_LOG_D("RequestNetConnection with timeout client in.");
195     if (netSpecifier == nullptr || !netSpecifier->SpecifierIsValid()) {
196         NETMGR_LOG_E("The parameter of netSpecifier is invalid");
197         return NETMANAGER_ERR_PARAMETER_ERROR;
198     }
199     sptr<INetConnService> proxy = GetProxy();
200     if (proxy == nullptr) {
201         NETMGR_LOG_E("The parameter of proxy is nullptr");
202         return NETMANAGER_ERR_GET_PROXY_FAIL;
203     }
204     int32_t ret = proxy->RequestNetConnection(netSpecifier, callback, timeoutMS);
205     if (ret == NETMANAGER_SUCCESS) {
206         NETMGR_LOG_D("RequestNetConnection success, save netSpecifier and callback and timeoutMS.");
207         std::lock_guard<std::mutex> locker(registerConnTupleListMutex_);
208         registerConnTupleList_.push_back(std::make_tuple(netSpecifier, callback, timeoutMS));
209     }
210 
211     return ret;
212 }
213 
UnregisterNetConnCallback(const sptr<INetConnCallback> & callback)214 int32_t NetConnClient::UnregisterNetConnCallback(const sptr<INetConnCallback> &callback)
215 {
216     NETMGR_LOG_D("UnregisterNetConnCallback client in.");
217     sptr<INetConnService> proxy = GetProxy();
218     if (proxy == nullptr) {
219         NETMGR_LOG_E("proxy is nullptr");
220         return NETMANAGER_ERR_GET_PROXY_FAIL;
221     }
222     int32_t ret = proxy->UnregisterNetConnCallback(callback);
223     if (ret == NETMANAGER_SUCCESS) {
224         NETMGR_LOG_D("UnregisterNetConnCallback success, delete callback.");
225         std::lock_guard<std::mutex> locker(registerConnTupleListMutex_);
226         for (auto it = registerConnTupleList_.begin(); it != registerConnTupleList_.end(); ++it) {
227             if (std::get<1>(*it)->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) {
228                 registerConnTupleList_.erase(it);
229                 break;
230             }
231         }
232     }
233 
234     return ret;
235 }
236 
RegisterNetDetectionCallback(int32_t netId,const sptr<INetDetectionCallback> & callback)237 int32_t NetConnClient::RegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback)
238 {
239     NETMGR_LOG_I("RegisterNetDetectionCallback client in.");
240     sptr<INetConnService> proxy = GetProxy();
241     if (proxy == nullptr) {
242         NETMGR_LOG_E("proxy is nullptr");
243         return NETMANAGER_ERR_GET_PROXY_FAIL;
244     }
245 
246     return proxy->RegisterNetDetectionCallback(netId, callback);
247 }
248 
UnRegisterNetDetectionCallback(int32_t netId,const sptr<INetDetectionCallback> & callback)249 int32_t NetConnClient::UnRegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback)
250 {
251     NETMGR_LOG_I("UnRegisterNetDetectionCallback client in.");
252     sptr<INetConnService> proxy = GetProxy();
253     if (proxy == nullptr) {
254         NETMGR_LOG_E("proxy is nullptr");
255         return NETMANAGER_ERR_GET_PROXY_FAIL;
256     }
257 
258     return proxy->UnRegisterNetDetectionCallback(netId, callback);
259 }
260 
UpdateNetSupplierInfo(uint32_t supplierId,const sptr<NetSupplierInfo> & netSupplierInfo)261 int32_t NetConnClient::UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo)
262 {
263     NETMGR_LOG_I("UpdateNetSupplierInfo client in.");
264     sptr<INetConnService> proxy = GetProxy();
265     if (proxy == nullptr) {
266         NETMGR_LOG_E("proxy is nullptr");
267         return NETMANAGER_ERR_GET_PROXY_FAIL;
268     }
269 
270     return proxy->UpdateNetSupplierInfo(supplierId, netSupplierInfo);
271 }
272 
UpdateNetLinkInfo(uint32_t supplierId,const sptr<NetLinkInfo> & netLinkInfo)273 int32_t NetConnClient::UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo)
274 {
275     NETMGR_LOG_I("UpdateNetLinkInfo client in.");
276     sptr<INetConnService> proxy = GetProxy();
277     if (proxy == nullptr) {
278         NETMGR_LOG_E("proxy is nullptr");
279         return NETMANAGER_ERR_GET_PROXY_FAIL;
280     }
281 
282     return proxy->UpdateNetLinkInfo(supplierId, netLinkInfo);
283 }
284 
GetDefaultNet(NetHandle & netHandle)285 int32_t NetConnClient::GetDefaultNet(NetHandle &netHandle)
286 {
287     NETMGR_LOG_D("GetDefaultNet client in.");
288     sptr<INetConnService> proxy = GetProxy();
289     if (proxy == nullptr) {
290         NETMGR_LOG_E("proxy is nullptr");
291         return NETMANAGER_ERR_GET_PROXY_FAIL;
292     }
293 
294     int32_t netId = 0;
295     int32_t result = proxy->GetDefaultNet(netId);
296     if (result != NETMANAGER_SUCCESS) {
297         NETMGR_LOG_D("fail to get default net.");
298         return result;
299     }
300     netHandle.SetNetId(netId);
301     NETMGR_LOG_D("GetDefaultNet client out.");
302     return NETMANAGER_SUCCESS;
303 }
304 
HasDefaultNet(bool & flag)305 int32_t NetConnClient::HasDefaultNet(bool &flag)
306 {
307     NETMGR_LOG_D("HasDefaultNet client in.");
308     sptr<INetConnService> proxy = GetProxy();
309     if (proxy == nullptr) {
310         NETMGR_LOG_E("proxy is nullptr");
311         return NETMANAGER_ERR_GET_PROXY_FAIL;
312     }
313     return proxy->HasDefaultNet(flag);
314 }
315 
GetAllNets(std::list<sptr<NetHandle>> & netList)316 int32_t NetConnClient::GetAllNets(std::list<sptr<NetHandle>> &netList)
317 {
318     sptr<INetConnService> proxy = GetProxy();
319     if (proxy == nullptr) {
320         NETMGR_LOG_E("proxy is nullptr");
321         return NETMANAGER_ERR_GET_PROXY_FAIL;
322     }
323 
324     std::list<int32_t> netIdList;
325     int32_t result = proxy->GetAllNets(netIdList);
326     if (result != NETMANAGER_SUCCESS) {
327         return result;
328     }
329     std::list<int32_t>::iterator iter;
330     for (iter = netIdList.begin(); iter != netIdList.end(); ++iter) {
331         sptr<NetHandle> netHandle = std::make_unique<NetHandle>(*iter).release();
332         if (netHandle != nullptr) {
333             netList.push_back(netHandle);
334         }
335     }
336     return NETMANAGER_SUCCESS;
337 }
338 
GetConnectionProperties(const NetHandle & netHandle,NetLinkInfo & info)339 int32_t NetConnClient::GetConnectionProperties(const NetHandle &netHandle, NetLinkInfo &info)
340 {
341     sptr<INetConnService> proxy = GetProxy();
342     if (proxy == nullptr) {
343         NETMGR_LOG_E("proxy is nullptr");
344         return NETMANAGER_ERR_GET_PROXY_FAIL;
345     }
346 
347     return proxy->GetConnectionProperties(netHandle.GetNetId(), info);
348 }
349 
GetNetCapabilities(const NetHandle & netHandle,NetAllCapabilities & netAllCap)350 int32_t NetConnClient::GetNetCapabilities(const NetHandle &netHandle, NetAllCapabilities &netAllCap)
351 {
352     sptr<INetConnService> proxy = GetProxy();
353     if (proxy == nullptr) {
354         NETMGR_LOG_E("proxy is nullptr");
355         return NETMANAGER_ERR_GET_PROXY_FAIL;
356     }
357 
358     return proxy->GetNetCapabilities(netHandle.GetNetId(), netAllCap);
359 }
360 
GetAddressesByName(const std::string & host,int32_t netId,std::vector<INetAddr> & addrList)361 int32_t NetConnClient::GetAddressesByName(const std::string &host, int32_t netId, std::vector<INetAddr> &addrList)
362 {
363     sptr<INetConnService> proxy = GetProxy();
364     if (proxy == nullptr) {
365         NETMGR_LOG_E("proxy is nullptr");
366         return NETMANAGER_ERR_GET_PROXY_FAIL;
367     }
368 
369     return proxy->GetAddressesByName(host, netId, addrList);
370 }
371 
GetAddressByName(const std::string & host,int32_t netId,INetAddr & addr)372 int32_t NetConnClient::GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr)
373 {
374     sptr<INetConnService> proxy = GetProxy();
375     if (proxy == nullptr) {
376         NETMGR_LOG_E("proxy is nullptr");
377         return NETMANAGER_ERR_GET_PROXY_FAIL;
378     }
379 
380     return proxy->GetAddressByName(host, netId, addr);
381 }
382 
GetIfaceNameIdentMaps(NetBearType bearerType,SafeMap<std::string,std::string> & ifaceNameIdentMaps)383 int32_t NetConnClient::GetIfaceNameIdentMaps(NetBearType bearerType,
384                                              SafeMap<std::string, std::string> &ifaceNameIdentMaps)
385 {
386     sptr<INetConnService> proxy = GetProxy();
387     if (proxy == nullptr) {
388         NETMGR_LOG_E("proxy is nullptr");
389         return NETMANAGER_ERR_GET_PROXY_FAIL;
390     }
391     return proxy->GetIfaceNameIdentMaps(bearerType, ifaceNameIdentMaps);
392 }
393 
BindSocket(int32_t socketFd,int32_t netId)394 int32_t NetConnClient::BindSocket(int32_t socketFd, int32_t netId)
395 {
396     // default netId begin whit 100, inner virtual interface netId between 1 and 50
397     if (netId < MIN_VALID_INTERNAL_NETID || (netId > MAX_VALID_INTERNAL_NETID && netId < MIN_VALID_NETID)) {
398         NETMGR_LOG_E("netId is invalid.");
399         return NET_CONN_ERR_INVALID_NETWORK;
400     }
401     std::shared_ptr<nmd::FwmarkClient> fwmarkClient_ = std::make_shared<nmd::FwmarkClient>();
402     if (fwmarkClient_ == nullptr) {
403         NETMGR_LOG_E("fwmarkClient_ is nullptr");
404         return NETMANAGER_ERR_PARAMETER_ERROR;
405     }
406     fwmarkClient_->BindSocket(socketFd, netId);
407     return NETMANAGER_SUCCESS;
408 }
409 
NetDetection(const NetHandle & netHandle)410 int32_t NetConnClient::NetDetection(const NetHandle &netHandle)
411 {
412     sptr<INetConnService> proxy = GetProxy();
413     if (proxy == nullptr) {
414         NETMGR_LOG_E("proxy is nullptr");
415         return NETMANAGER_ERR_GET_PROXY_FAIL;
416     }
417 
418     return proxy->NetDetection(netHandle.GetNetId());
419 }
420 
GetProxy()421 sptr<INetConnService> NetConnClient::GetProxy()
422 {
423     std::lock_guard lock(mutex_);
424 
425     if (NetConnService_) {
426         NETMGR_LOG_D("get proxy is ok");
427         return NetConnService_;
428     }
429 
430     NETMGR_LOG_D("execute GetSystemAbilityManager");
431     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
432     if (sam == nullptr) {
433         NETMGR_LOG_E("GetProxy(), get SystemAbilityManager failed");
434         return nullptr;
435     }
436 
437     sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
438     if (remote == nullptr) {
439         NETMGR_LOG_E("get Remote service failed");
440         return nullptr;
441     }
442 
443     deathRecipient_ = new (std::nothrow) NetConnDeathRecipient(*this);
444     if (deathRecipient_ == nullptr) {
445         NETMGR_LOG_E("get deathRecipient_ failed");
446         return nullptr;
447     }
448     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
449         NETMGR_LOG_E("add death recipient failed");
450         return nullptr;
451     }
452 
453     NetConnService_ = iface_cast<INetConnService>(remote);
454     if (NetConnService_ == nullptr) {
455         NETMGR_LOG_E("get Remote service proxy failed");
456         return nullptr;
457     }
458 
459     return NetConnService_;
460 }
461 
SetAirplaneMode(bool state)462 int32_t NetConnClient::SetAirplaneMode(bool state)
463 {
464     NETMGR_LOG_I("SetAirplaneMode client in.");
465     sptr<INetConnService> proxy = GetProxy();
466     if (proxy == nullptr) {
467         NETMGR_LOG_E("proxy is nullptr");
468         return NETMANAGER_ERR_GET_PROXY_FAIL;
469     }
470 
471     return proxy->SetAirplaneMode(state);
472 }
473 
RecoverCallbackAndGlobalProxy()474 void NetConnClient::RecoverCallbackAndGlobalProxy()
475 {
476     uint32_t count = 0;
477     while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
478         std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_SERVICE_TIME_MS));
479         count++;
480     }
481     auto proxy = GetProxy();
482     NETMGR_LOG_W("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
483     if (proxy != nullptr) {
484         for (auto mem : registerConnTupleList_) {
485             sptr<NetSpecifier> specifier = std::get<0>(mem);
486             sptr<INetConnCallback> callback = std::get<1>(mem);
487             uint32_t timeoutMS = std::get<2>(mem);
488             bool isInternalDefault = specifier != nullptr &&
489                 specifier->netCapabilities_.netCaps_.count(NetManagerStandard::NET_CAPABILITY_INTERNAL_DEFAULT) > 0;
490             int32_t ret = NETMANAGER_SUCCESS;
491             if (specifier != nullptr && timeoutMS != 0) {
492                 ret = isInternalDefault ? proxy->RequestNetConnection(specifier, callback, timeoutMS) :
493                     proxy->RegisterNetConnCallback(specifier, callback, timeoutMS);
494                 NETMGR_LOG_D("Register result hasNetSpecifier_ and timeoutMS_ %{public}d", ret);
495             } else if (specifier != nullptr) {
496                 ret = isInternalDefault ? proxy->RequestNetConnection(specifier, callback, 0) :
497                     proxy->RegisterNetConnCallback(specifier, callback, 0);
498                 NETMGR_LOG_D("Register result hasNetSpecifier_ %{public}d", ret);
499             } else if (callback != nullptr) {
500                 int32_t ret = proxy->RegisterNetConnCallback(callback);
501                 NETMGR_LOG_D("Register netconn result %{public}d", ret);
502             }
503         }
504     }
505     if (proxy != nullptr && preAirplaneCallback_ != nullptr) {
506         int32_t ret = proxy->RegisterPreAirplaneCallback(preAirplaneCallback_);
507         NETMGR_LOG_D("Register pre airplane result %{public}d", ret);
508     }
509 
510     if (proxy != nullptr && !globalHttpProxy_.GetHost().empty()) {
511         int32_t ret = proxy->SetGlobalHttpProxy(globalHttpProxy_);
512         NETMGR_LOG_D("globalHttpProxy_ Register result %{public}d", ret);
513     }
514 }
515 
OnRemoteDied(const wptr<IRemoteObject> & remote)516 void NetConnClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
517 {
518     NETMGR_LOG_D("on remote died");
519     if (remote == nullptr) {
520         NETMGR_LOG_E("remote object is nullptr");
521         return;
522     }
523 
524     std::lock_guard lock(mutex_);
525     if (NetConnService_ == nullptr) {
526         NETMGR_LOG_E("NetConnService_ is nullptr");
527         return;
528     }
529 
530     sptr<IRemoteObject> local = NetConnService_->AsObject();
531     if (local != remote.promote()) {
532         NETMGR_LOG_E("proxy and stub is not same remote object");
533         return;
534     }
535 
536     local->RemoveDeathRecipient(deathRecipient_);
537     NetConnService_ = nullptr;
538 
539     if (!registerConnTupleList_.empty() || preAirplaneCallback_ != nullptr || !globalHttpProxy_.GetHost().empty()) {
540         NETMGR_LOG_I("on remote died recover callback");
541         std::thread t([this]() {
542             RecoverCallbackAndGlobalProxy();
543         });
544         std::string threadName = "netconnRecoverCallback";
545         pthread_setname_np(t.native_handle(), threadName.c_str());
546         t.detach();
547     }
548 }
549 
DlCloseRemoveDeathRecipient()550 void NetConnClient::DlCloseRemoveDeathRecipient()
551 {
552     sptr<INetConnService> proxy = GetProxy();
553     if (proxy == nullptr) {
554         NETMGR_LOG_E("proxy is nullptr");
555         return;
556     }
557 
558     auto serviceRemote = proxy->AsObject();
559     if (serviceRemote == nullptr) {
560         NETMGR_LOG_E("serviceRemote is nullptr");
561         return;
562     }
563 
564     serviceRemote->RemoveDeathRecipient(deathRecipient_);
565     NETMGR_LOG_I("RemoveDeathRecipient success");
566 }
567 
IsDefaultNetMetered(bool & isMetered)568 int32_t NetConnClient::IsDefaultNetMetered(bool &isMetered)
569 {
570     sptr<INetConnService> proxy = GetProxy();
571     if (proxy == nullptr) {
572         NETMGR_LOG_E("proxy is nullptr");
573         return NETMANAGER_ERR_GET_PROXY_FAIL;
574     }
575     return proxy->IsDefaultNetMetered(isMetered);
576 }
577 
SetGlobalHttpProxy(const HttpProxy & httpProxy)578 int32_t NetConnClient::SetGlobalHttpProxy(const HttpProxy &httpProxy)
579 {
580     sptr<INetConnService> proxy = GetProxy();
581     if (proxy == nullptr) {
582         NETMGR_LOG_E("proxy is nullptr");
583         return NETMANAGER_ERR_GET_PROXY_FAIL;
584     }
585     if (globalHttpProxy_ != httpProxy) {
586         globalHttpProxy_ = httpProxy;
587     }
588     return proxy->SetGlobalHttpProxy(httpProxy);
589 }
590 
RegisterAppHttpProxyCallback(std::function<void (const HttpProxy & httpProxy)> callback,uint32_t & callbackid)591 void NetConnClient::RegisterAppHttpProxyCallback(std::function<void(const HttpProxy &httpProxy)> callback,
592                                                  uint32_t &callbackid)
593 {
594     std::lock_guard<std::mutex> lock(appHttpProxyCbMapMutex_);
595     uint32_t id = currentCallbackId_;
596     currentCallbackId_++;
597     appHttpProxyCbMap_[id] = callback;
598     callbackid = id;
599     if (callback && !appHttpProxy_.GetHost().empty()) {
600         callback(appHttpProxy_);
601     }
602     NETMGR_LOG_I("registerCallback id:%{public}d.", id);
603 }
604 
UnregisterAppHttpProxyCallback(uint32_t callbackid)605 void NetConnClient::UnregisterAppHttpProxyCallback(uint32_t callbackid)
606 {
607     NETMGR_LOG_I("unregisterCallback callbackid:%{public}d.", callbackid);
608     std::lock_guard<std::mutex> lock(appHttpProxyCbMapMutex_);
609     appHttpProxyCbMap_.erase(callbackid);
610 }
611 
SetAppHttpProxy(const HttpProxy & httpProxy)612 int32_t NetConnClient::SetAppHttpProxy(const HttpProxy &httpProxy)
613 {
614     NETMGR_LOG_I("Enter AppHttpProxy");
615 
616     if (appHttpProxy_ != httpProxy) {
617         appHttpProxy_ = httpProxy;
618         std::lock_guard<std::mutex> lock(appHttpProxyCbMapMutex_);
619         for (const auto &pair : appHttpProxyCbMap_) {
620             pair.second(httpProxy);
621         }
622     }
623 
624     return NETMANAGER_SUCCESS;
625 }
626 
GetGlobalHttpProxy(HttpProxy & httpProxy)627 int32_t NetConnClient::GetGlobalHttpProxy(HttpProxy &httpProxy)
628 {
629     sptr<INetConnService> proxy = GetProxy();
630     if (proxy == nullptr) {
631         NETMGR_LOG_E("proxy is nullptr");
632         return NETMANAGER_ERR_GET_PROXY_FAIL;
633     }
634     return proxy->GetGlobalHttpProxy(httpProxy);
635 }
636 
GetDefaultHttpProxy(HttpProxy & httpProxy)637 int32_t NetConnClient::GetDefaultHttpProxy(HttpProxy &httpProxy)
638 {
639     if (!appHttpProxy_.GetHost().empty()) {
640         httpProxy = appHttpProxy_;
641         NETMGR_LOG_D("Return AppHttpProxy:%{public}s:%{public}d",
642                      httpProxy.GetHost().c_str(), httpProxy.GetPort());
643         return NETMANAGER_SUCCESS;
644     }
645 
646     sptr<INetConnService> proxy = GetProxy();
647     if (proxy == nullptr) {
648         NETMGR_LOG_E("proxy is nullptr");
649         return NETMANAGER_ERR_GET_PROXY_FAIL;
650     }
651     int32_t bindNetId = 0;
652     GetAppNet(bindNetId);
653     return proxy->GetDefaultHttpProxy(bindNetId, httpProxy);
654 }
655 
SetPacUrl(const std::string & pacUrl)656 int32_t NetConnClient::SetPacUrl(const std::string &pacUrl)
657 {
658     NETMGR_LOG_I("Enter SetPacUrl");
659 
660     sptr<INetConnService> proxy = GetProxy();
661     if (proxy == nullptr) {
662         NETMGR_LOG_E("proxy is nullptr");
663         return NETMANAGER_ERR_GET_PROXY_FAIL;
664     }
665     return proxy->SetPacUrl(pacUrl);
666 }
667 
GetPacUrl(std::string & pacUrl)668 int32_t NetConnClient::GetPacUrl(std::string &pacUrl)
669 {
670     NETMGR_LOG_I("Enter GetPacUrl");
671 
672     sptr<INetConnService> proxy = GetProxy();
673     if (proxy == nullptr) {
674         NETMGR_LOG_E("proxy is nullptr");
675         return NETMANAGER_ERR_GET_PROXY_FAIL;
676     }
677     return proxy->GetPacUrl(pacUrl);
678 }
679 
GetNetIdByIdentifier(const std::string & ident,std::list<int32_t> & netIdList)680 int32_t NetConnClient::GetNetIdByIdentifier(const std::string &ident, std::list<int32_t> &netIdList)
681 {
682     sptr<INetConnService> proxy = GetProxy();
683     if (proxy == nullptr) {
684         NETMGR_LOG_E("proxy is nullptr");
685         return NETMANAGER_ERR_GET_PROXY_FAIL;
686     }
687     return proxy->GetNetIdByIdentifier(ident, netIdList);
688 }
689 
SetAppNet(int32_t netId)690 int32_t NetConnClient::SetAppNet(int32_t netId)
691 {
692     if (netId < MIN_VALID_NETID && netId != 0) {
693         return NET_CONN_ERR_INVALID_NETWORK;
694     }
695     sptr<INetConnService> proxy = GetProxy();
696     if (proxy == nullptr) {
697         NETMGR_LOG_E("proxy is nullptr");
698         return NETMANAGER_ERR_GET_PROXY_FAIL;
699     }
700     int32_t ret = proxy->SetAppNet(netId);
701     if (ret != NETMANAGER_SUCCESS) {
702         return ret;
703     }
704 
705     SetNetForApp(netId);
706     return NETMANAGER_SUCCESS;
707 }
708 
GetAppNet(int32_t & netId)709 int32_t NetConnClient::GetAppNet(int32_t &netId)
710 {
711     netId = GetNetForApp();
712     return NETMANAGER_SUCCESS;
713 }
714 
RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> & callback)715 int32_t NetConnClient::RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback)
716 {
717     sptr<INetConnService> proxy = GetProxy();
718     if (proxy == nullptr) {
719         NETMGR_LOG_E("proxy is nullptr");
720         return NETMANAGER_ERR_GET_PROXY_FAIL;
721     }
722     return proxy->RegisterNetInterfaceCallback(callback);
723 }
724 
GetNetInterfaceConfiguration(const std::string & iface,NetInterfaceConfiguration & config)725 int32_t NetConnClient::GetNetInterfaceConfiguration(const std::string &iface, NetInterfaceConfiguration &config)
726 {
727     sptr<INetConnService> proxy = GetProxy();
728     if (proxy == nullptr) {
729         NETMGR_LOG_E("proxy is nullptr");
730         return NETMANAGER_ERR_GET_PROXY_FAIL;
731     }
732     return proxy->GetNetInterfaceConfiguration(iface, config);
733 }
734 
AddNetworkRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)735 int32_t NetConnClient::AddNetworkRoute(int32_t netId, const std::string &ifName,
736                                        const std::string &destination, const std::string &nextHop)
737 {
738     NETMGR_LOG_I("AddNetworkRoute client in.");
739     sptr<INetConnService> proxy = GetProxy();
740     if (proxy == nullptr) {
741         NETMGR_LOG_E("proxy is nullptr");
742         return NETMANAGER_ERR_GET_PROXY_FAIL;
743     }
744 
745     return proxy->AddNetworkRoute(netId, ifName, destination, nextHop);
746 }
747 
RemoveNetworkRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)748 int32_t NetConnClient::RemoveNetworkRoute(int32_t netId, const std::string &ifName,
749                                           const std::string &destination, const std::string &nextHop)
750 {
751     NETMGR_LOG_I("RemoveNetworkRoute client in.");
752     sptr<INetConnService> proxy = GetProxy();
753     if (proxy == nullptr) {
754         NETMGR_LOG_E("proxy is nullptr");
755         return NETMANAGER_ERR_GET_PROXY_FAIL;
756     }
757 
758     return proxy->RemoveNetworkRoute(netId, ifName, destination, nextHop);
759 }
760 
AddInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)761 int32_t NetConnClient::AddInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
762                                            int32_t prefixLength)
763 {
764     NETMGR_LOG_I("AddInterfaceAddress client in.");
765     sptr<INetConnService> proxy = GetProxy();
766     if (proxy == nullptr) {
767         NETMGR_LOG_E("proxy is nullptr");
768         return NETMANAGER_ERR_GET_PROXY_FAIL;
769     }
770 
771     return proxy->AddInterfaceAddress(ifName, ipAddr, prefixLength);
772 }
773 
DelInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)774 int32_t NetConnClient::DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
775                                            int32_t prefixLength)
776 {
777     NETMGR_LOG_I("DelInterfaceAddress client in.");
778     sptr<INetConnService> proxy = GetProxy();
779     if (proxy == nullptr) {
780         NETMGR_LOG_E("proxy is nullptr");
781         return NETMANAGER_ERR_GET_PROXY_FAIL;
782     }
783 
784     return proxy->DelInterfaceAddress(ifName, ipAddr, prefixLength);
785 }
786 
AddStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)787 int32_t NetConnClient::AddStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName)
788 {
789     NETMGR_LOG_I("AddStaticArp client in.");
790     sptr<INetConnService> proxy = GetProxy();
791     if (proxy == nullptr) {
792         NETMGR_LOG_E("proxy is nullptr");
793         return NETMANAGER_ERR_GET_PROXY_FAIL;
794     }
795 
796     return proxy->AddStaticArp(ipAddr, macAddr, ifName);
797 }
798 
DelStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)799 int32_t NetConnClient::DelStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName)
800 {
801     NETMGR_LOG_I("DelStaticArp client in.");
802     sptr<INetConnService> proxy = GetProxy();
803     if (proxy == nullptr) {
804         NETMGR_LOG_E("proxy is nullptr");
805         return NETMANAGER_ERR_GET_PROXY_FAIL;
806     }
807 
808     return proxy->DelStaticArp(ipAddr, macAddr, ifName);
809 }
810 
RegisterSlotType(uint32_t supplierId,int32_t type)811 int32_t NetConnClient::RegisterSlotType(uint32_t supplierId, int32_t type)
812 {
813     NETMGR_LOG_I("RegisterSlotType client in.supplierId[%{public}d] type[%{public}d]", supplierId, type);
814     sptr<INetConnService> proxy = GetProxy();
815     if (proxy == nullptr) {
816         NETMGR_LOG_E("proxy is nullptr");
817         return NETMANAGER_ERR_GET_PROXY_FAIL;
818     }
819 
820     return proxy->RegisterSlotType(supplierId, type);
821 }
822 
GetSlotType(std::string & type)823 int32_t NetConnClient::GetSlotType(std::string &type)
824 {
825     sptr<INetConnService> proxy = GetProxy();
826     if (proxy == nullptr) {
827         NETMGR_LOG_E("proxy is nullptr");
828         return NETMANAGER_ERR_GET_PROXY_FAIL;
829     }
830 
831     return proxy->GetSlotType(type);
832 }
833 
GetPinSetForHostName(const std::string & hostname,std::string & pins)834 int32_t NetConnClient::GetPinSetForHostName(const std::string &hostname, std::string &pins)
835 {
836     return NetworkSecurityConfig::GetInstance().GetPinSetForHostName(hostname, pins);
837 }
838 
IsPinOpenMode(const std::string & hostname)839 bool NetConnClient::IsPinOpenMode(const std::string &hostname)
840 {
841     return NetworkSecurityConfig::GetInstance().IsPinOpenMode(hostname);
842 }
843 
IsPinOpenModeVerifyRootCa(const std::string & hostname)844 bool NetConnClient::IsPinOpenModeVerifyRootCa(const std::string &hostname)
845 {
846     return NetworkSecurityConfig::GetInstance().IsPinOpenModeVerifyRootCa(hostname);
847 }
848 
GetTrustAnchorsForHostName(const std::string & hostname,std::vector<std::string> & certs)849 int32_t NetConnClient::GetTrustAnchorsForHostName(const std::string &hostname, std::vector<std::string> &certs)
850 {
851     return NetworkSecurityConfig::GetInstance().GetTrustAnchorsForHostName(hostname, certs);
852 }
853 
FactoryResetNetwork()854 int32_t NetConnClient::FactoryResetNetwork()
855 {
856     sptr<INetConnService> proxy = GetProxy();
857     if (proxy == nullptr) {
858         NETMGR_LOG_E("proxy is nullptr");
859         return NETMANAGER_ERR_GET_PROXY_FAIL;
860     }
861 
862     return proxy->FactoryResetNetwork();
863 }
864 
RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> & callback)865 int32_t NetConnClient::RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> &callback)
866 {
867     sptr<INetConnService> proxy = GetProxy();
868     if (proxy == nullptr) {
869         NETMGR_LOG_E("proxy is nullptr");
870         return NETMANAGER_ERR_GET_PROXY_FAIL;
871     }
872     return proxy->RegisterNetFactoryResetCallback(callback);
873 }
874 
IsPreferCellularUrl(const std::string & url,bool & preferCellular)875 int32_t NetConnClient::IsPreferCellularUrl(const std::string& url, bool& preferCellular)
876 {
877     sptr<INetConnService> proxy = GetProxy();
878     if (proxy == nullptr) {
879         NETMGR_LOG_E("proxy is nullptr");
880         return NETMANAGER_ERR_GET_PROXY_FAIL;
881     }
882     return proxy->IsPreferCellularUrl(url, preferCellular);
883 }
884 
RegisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)885 int32_t NetConnClient::RegisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)
886 {
887     NETMGR_LOG_D("RegisterPreAirplaneCallback client in.");
888     sptr<INetConnService> proxy = GetProxy();
889     if (proxy == nullptr) {
890         NETMGR_LOG_E("proxy is nullptr");
891         return NETMANAGER_ERR_GET_PROXY_FAIL;
892     }
893 
894     int32_t ret = proxy->RegisterPreAirplaneCallback(callback);
895     if (ret == NETMANAGER_SUCCESS) {
896         NETMGR_LOG_D("RegisterPreAirplaneCallback success, save callback.");
897         preAirplaneCallback_ = callback;
898     }
899 
900     return ret;
901 }
902 
UnregisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)903 int32_t NetConnClient::UnregisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)
904 {
905     NETMGR_LOG_D("UnregisterPreAirplaneCallback client in.");
906     sptr<INetConnService> proxy = GetProxy();
907     if (proxy == nullptr) {
908         NETMGR_LOG_E("proxy is nullptr");
909         return NETMANAGER_ERR_GET_PROXY_FAIL;
910     }
911 
912     int32_t ret = proxy->UnregisterPreAirplaneCallback(callback);
913     if (ret == NETMANAGER_SUCCESS) {
914         NETMGR_LOG_D("UnregisterPreAirplaneCallback success,delete callback.");
915         preAirplaneCallback_ = nullptr;
916     }
917 
918     return ret;
919 }
920 
UpdateSupplierScore(NetBearType bearerType,uint32_t detectionStatus,uint32_t & supplierId)921 int32_t NetConnClient::UpdateSupplierScore(NetBearType bearerType, uint32_t detectionStatus, uint32_t& supplierId)
922 {
923     sptr<INetConnService> proxy = GetProxy();
924     if (proxy == nullptr) {
925         NETMGR_LOG_E("proxy is nullptr.");
926         return NETMANAGER_ERR_GET_PROXY_FAIL;
927     }
928     return proxy->UpdateSupplierScore(bearerType, detectionStatus, supplierId);
929 }
930 
ObtainTargetApiVersionForSelf()931 std::optional<int32_t> NetConnClient::ObtainTargetApiVersionForSelf()
932 {
933     void *handler = dlopen(LIB_NET_BUNDLE_UTILS_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE);
934     if (handler == nullptr) {
935         NETMGR_LOG_E("load lib failed, reason : %{public}s", dlerror());
936         return std::nullopt;
937     }
938     using GetNetBundleClass = INetBundle *(*)();
939     auto getNetBundle = (GetNetBundleClass)dlsym(handler, "GetNetBundle");
940     if (getNetBundle == nullptr) {
941         NETMGR_LOG_E("GetNetBundle failed, reason : %{public}s", dlerror());
942         dlclose(handler);
943         return std::nullopt;
944     }
945     auto netBundle = getNetBundle();
946     if (netBundle == nullptr) {
947         NETMGR_LOG_E("netBundle is nullptr");
948         dlclose(handler);
949         return std::nullopt;
950     }
951     auto result = netBundle->ObtainTargetApiVersionForSelf();
952     dlclose(handler);
953     return result;
954 }
955 
IsAPIVersionSupported(int targetApiVersion)956 bool NetConnClient::IsAPIVersionSupported(int targetApiVersion)
957 {
958     static auto currentApiVersion = ObtainTargetApiVersionForSelf();
959     // Returns true by default in case can not get bundle info from bundle mgr.
960     return currentApiVersion.value_or(targetApiVersion) >= targetApiVersion;
961 }
962 
ObtainBundleNameForSelf()963 std::optional<std::string> NetConnClient::ObtainBundleNameForSelf()
964 {
965     static auto bundleName = ObtainBundleNameFromBundleMgr();
966     return bundleName;
967 }
968 
ObtainBundleNameFromBundleMgr()969 std::optional<std::string> NetConnClient::ObtainBundleNameFromBundleMgr()
970 {
971     void *handler = dlopen(LIB_NET_BUNDLE_UTILS_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE);
972     if (handler == nullptr) {
973         NETMGR_LOG_E("load lib failed, reason : %{public}s", dlerror());
974         return std::nullopt;
975     }
976     using GetNetBundleClass = INetBundle *(*)();
977     auto getNetBundle = (GetNetBundleClass)dlsym(handler, "GetNetBundle");
978     if (getNetBundle == nullptr) {
979         NETMGR_LOG_E("GetNetBundle failed, reason : %{public}s", dlerror());
980         dlclose(handler);
981         return std::nullopt;
982     }
983     auto netBundle = getNetBundle();
984     if (netBundle == nullptr) {
985         NETMGR_LOG_E("netBundle is nullptr");
986         dlclose(handler);
987         return std::nullopt;
988     }
989     auto result = netBundle->ObtainBundleNameForSelf();
990     dlclose(handler);
991     return result;
992 }
993 } // namespace NetManagerStandard
994 } // namespace OHOS
995