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
GetNetIdByIdentifier(const std::string & ident,std::list<int32_t> & netIdList)656 int32_t NetConnClient::GetNetIdByIdentifier(const std::string &ident, std::list<int32_t> &netIdList)
657 {
658 sptr<INetConnService> proxy = GetProxy();
659 if (proxy == nullptr) {
660 NETMGR_LOG_E("proxy is nullptr");
661 return NETMANAGER_ERR_GET_PROXY_FAIL;
662 }
663 return proxy->GetNetIdByIdentifier(ident, netIdList);
664 }
665
SetAppNet(int32_t netId)666 int32_t NetConnClient::SetAppNet(int32_t netId)
667 {
668 if (netId < MIN_VALID_NETID && netId != 0) {
669 return NET_CONN_ERR_INVALID_NETWORK;
670 }
671 sptr<INetConnService> proxy = GetProxy();
672 if (proxy == nullptr) {
673 NETMGR_LOG_E("proxy is nullptr");
674 return NETMANAGER_ERR_GET_PROXY_FAIL;
675 }
676 int32_t ret = proxy->SetAppNet(netId);
677 if (ret != NETMANAGER_SUCCESS) {
678 return ret;
679 }
680
681 SetNetForApp(netId);
682 return NETMANAGER_SUCCESS;
683 }
684
GetAppNet(int32_t & netId)685 int32_t NetConnClient::GetAppNet(int32_t &netId)
686 {
687 netId = GetNetForApp();
688 return NETMANAGER_SUCCESS;
689 }
690
RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> & callback)691 int32_t NetConnClient::RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback)
692 {
693 sptr<INetConnService> proxy = GetProxy();
694 if (proxy == nullptr) {
695 NETMGR_LOG_E("proxy is nullptr");
696 return NETMANAGER_ERR_GET_PROXY_FAIL;
697 }
698 return proxy->RegisterNetInterfaceCallback(callback);
699 }
700
GetNetInterfaceConfiguration(const std::string & iface,NetInterfaceConfiguration & config)701 int32_t NetConnClient::GetNetInterfaceConfiguration(const std::string &iface, NetInterfaceConfiguration &config)
702 {
703 sptr<INetConnService> proxy = GetProxy();
704 if (proxy == nullptr) {
705 NETMGR_LOG_E("proxy is nullptr");
706 return NETMANAGER_ERR_GET_PROXY_FAIL;
707 }
708 return proxy->GetNetInterfaceConfiguration(iface, config);
709 }
710
AddNetworkRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)711 int32_t NetConnClient::AddNetworkRoute(int32_t netId, const std::string &ifName,
712 const std::string &destination, const std::string &nextHop)
713 {
714 NETMGR_LOG_I("AddNetworkRoute client in.");
715 sptr<INetConnService> proxy = GetProxy();
716 if (proxy == nullptr) {
717 NETMGR_LOG_E("proxy is nullptr");
718 return NETMANAGER_ERR_GET_PROXY_FAIL;
719 }
720
721 return proxy->AddNetworkRoute(netId, ifName, destination, nextHop);
722 }
723
RemoveNetworkRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)724 int32_t NetConnClient::RemoveNetworkRoute(int32_t netId, const std::string &ifName,
725 const std::string &destination, const std::string &nextHop)
726 {
727 NETMGR_LOG_I("RemoveNetworkRoute client in.");
728 sptr<INetConnService> proxy = GetProxy();
729 if (proxy == nullptr) {
730 NETMGR_LOG_E("proxy is nullptr");
731 return NETMANAGER_ERR_GET_PROXY_FAIL;
732 }
733
734 return proxy->RemoveNetworkRoute(netId, ifName, destination, nextHop);
735 }
736
AddInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)737 int32_t NetConnClient::AddInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
738 int32_t prefixLength)
739 {
740 NETMGR_LOG_I("AddInterfaceAddress client in.");
741 sptr<INetConnService> proxy = GetProxy();
742 if (proxy == nullptr) {
743 NETMGR_LOG_E("proxy is nullptr");
744 return NETMANAGER_ERR_GET_PROXY_FAIL;
745 }
746
747 return proxy->AddInterfaceAddress(ifName, ipAddr, prefixLength);
748 }
749
DelInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)750 int32_t NetConnClient::DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
751 int32_t prefixLength)
752 {
753 NETMGR_LOG_I("DelInterfaceAddress client in.");
754 sptr<INetConnService> proxy = GetProxy();
755 if (proxy == nullptr) {
756 NETMGR_LOG_E("proxy is nullptr");
757 return NETMANAGER_ERR_GET_PROXY_FAIL;
758 }
759
760 return proxy->DelInterfaceAddress(ifName, ipAddr, prefixLength);
761 }
762
AddStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)763 int32_t NetConnClient::AddStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName)
764 {
765 NETMGR_LOG_I("AddStaticArp client in.");
766 sptr<INetConnService> proxy = GetProxy();
767 if (proxy == nullptr) {
768 NETMGR_LOG_E("proxy is nullptr");
769 return NETMANAGER_ERR_GET_PROXY_FAIL;
770 }
771
772 return proxy->AddStaticArp(ipAddr, macAddr, ifName);
773 }
774
DelStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)775 int32_t NetConnClient::DelStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName)
776 {
777 NETMGR_LOG_I("DelStaticArp 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->DelStaticArp(ipAddr, macAddr, ifName);
785 }
786
RegisterSlotType(uint32_t supplierId,int32_t type)787 int32_t NetConnClient::RegisterSlotType(uint32_t supplierId, int32_t type)
788 {
789 NETMGR_LOG_I("RegisterSlotType client in.supplierId[%{public}d] type[%{public}d]", supplierId, type);
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->RegisterSlotType(supplierId, type);
797 }
798
GetSlotType(std::string & type)799 int32_t NetConnClient::GetSlotType(std::string &type)
800 {
801 sptr<INetConnService> proxy = GetProxy();
802 if (proxy == nullptr) {
803 NETMGR_LOG_E("proxy is nullptr");
804 return NETMANAGER_ERR_GET_PROXY_FAIL;
805 }
806
807 return proxy->GetSlotType(type);
808 }
809
GetPinSetForHostName(const std::string & hostname,std::string & pins)810 int32_t NetConnClient::GetPinSetForHostName(const std::string &hostname, std::string &pins)
811 {
812 return NetworkSecurityConfig::GetInstance().GetPinSetForHostName(hostname, pins);
813 }
814
IsPinOpenMode(const std::string & hostname)815 bool NetConnClient::IsPinOpenMode(const std::string &hostname)
816 {
817 return NetworkSecurityConfig::GetInstance().IsPinOpenMode(hostname);
818 }
819
GetTrustAnchorsForHostName(const std::string & hostname,std::vector<std::string> & certs)820 int32_t NetConnClient::GetTrustAnchorsForHostName(const std::string &hostname, std::vector<std::string> &certs)
821 {
822 return NetworkSecurityConfig::GetInstance().GetTrustAnchorsForHostName(hostname, certs);
823 }
824
FactoryResetNetwork()825 int32_t NetConnClient::FactoryResetNetwork()
826 {
827 sptr<INetConnService> proxy = GetProxy();
828 if (proxy == nullptr) {
829 NETMGR_LOG_E("proxy is nullptr");
830 return NETMANAGER_ERR_GET_PROXY_FAIL;
831 }
832
833 return proxy->FactoryResetNetwork();
834 }
835
RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> & callback)836 int32_t NetConnClient::RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> &callback)
837 {
838 sptr<INetConnService> proxy = GetProxy();
839 if (proxy == nullptr) {
840 NETMGR_LOG_E("proxy is nullptr");
841 return NETMANAGER_ERR_GET_PROXY_FAIL;
842 }
843 return proxy->RegisterNetFactoryResetCallback(callback);
844 }
845
IsPreferCellularUrl(const std::string & url,bool & preferCellular)846 int32_t NetConnClient::IsPreferCellularUrl(const std::string& url, bool& preferCellular)
847 {
848 sptr<INetConnService> proxy = GetProxy();
849 if (proxy == nullptr) {
850 NETMGR_LOG_E("proxy is nullptr");
851 return NETMANAGER_ERR_GET_PROXY_FAIL;
852 }
853 return proxy->IsPreferCellularUrl(url, preferCellular);
854 }
855
RegisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)856 int32_t NetConnClient::RegisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)
857 {
858 NETMGR_LOG_D("RegisterPreAirplaneCallback client in.");
859 sptr<INetConnService> proxy = GetProxy();
860 if (proxy == nullptr) {
861 NETMGR_LOG_E("proxy is nullptr");
862 return NETMANAGER_ERR_GET_PROXY_FAIL;
863 }
864
865 int32_t ret = proxy->RegisterPreAirplaneCallback(callback);
866 if (ret == NETMANAGER_SUCCESS) {
867 NETMGR_LOG_D("RegisterPreAirplaneCallback success, save callback.");
868 preAirplaneCallback_ = callback;
869 }
870
871 return ret;
872 }
873
UnregisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)874 int32_t NetConnClient::UnregisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)
875 {
876 NETMGR_LOG_D("UnregisterPreAirplaneCallback client in.");
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
883 int32_t ret = proxy->UnregisterPreAirplaneCallback(callback);
884 if (ret == NETMANAGER_SUCCESS) {
885 NETMGR_LOG_D("UnregisterPreAirplaneCallback success,delete callback.");
886 preAirplaneCallback_ = nullptr;
887 }
888
889 return ret;
890 }
891
UpdateSupplierScore(NetBearType bearerType,uint32_t detectionStatus,uint32_t & supplierId)892 int32_t NetConnClient::UpdateSupplierScore(NetBearType bearerType, uint32_t detectionStatus, uint32_t& supplierId)
893 {
894 sptr<INetConnService> proxy = GetProxy();
895 if (proxy == nullptr) {
896 NETMGR_LOG_E("proxy is nullptr.");
897 return NETMANAGER_ERR_GET_PROXY_FAIL;
898 }
899 return proxy->UpdateSupplierScore(bearerType, detectionStatus, supplierId);
900 }
901
ObtainTargetApiVersionForSelf()902 std::optional<int32_t> NetConnClient::ObtainTargetApiVersionForSelf()
903 {
904 void *handler = dlopen(LIB_NET_BUNDLE_UTILS_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE);
905 if (handler == nullptr) {
906 NETMGR_LOG_E("load lib failed, reason : %{public}s", dlerror());
907 return std::nullopt;
908 }
909 using GetNetBundleClass = INetBundle *(*)();
910 auto getNetBundle = (GetNetBundleClass)dlsym(handler, "GetNetBundle");
911 if (getNetBundle == nullptr) {
912 NETMGR_LOG_E("GetNetBundle failed, reason : %{public}s", dlerror());
913 dlclose(handler);
914 return std::nullopt;
915 }
916 auto netBundle = getNetBundle();
917 if (netBundle == nullptr) {
918 NETMGR_LOG_E("netBundle is nullptr");
919 dlclose(handler);
920 return std::nullopt;
921 }
922 auto result = netBundle->ObtainTargetApiVersionForSelf();
923 dlclose(handler);
924 return result;
925 }
926
IsAPIVersionSupported(int targetApiVersion)927 bool NetConnClient::IsAPIVersionSupported(int targetApiVersion)
928 {
929 static auto currentApiVersion = ObtainTargetApiVersionForSelf();
930 // Returns true by default in case can not get bundle info from bundle mgr.
931 return currentApiVersion.value_or(targetApiVersion) >= targetApiVersion;
932 }
933
ObtainBundleNameForSelf()934 std::optional<std::string> NetConnClient::ObtainBundleNameForSelf()
935 {
936 static auto bundleName = ObtainBundleNameFromBundleMgr();
937 return bundleName;
938 }
939
ObtainBundleNameFromBundleMgr()940 std::optional<std::string> NetConnClient::ObtainBundleNameFromBundleMgr()
941 {
942 void *handler = dlopen(LIB_NET_BUNDLE_UTILS_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE);
943 if (handler == nullptr) {
944 NETMGR_LOG_E("load lib failed, reason : %{public}s", dlerror());
945 return std::nullopt;
946 }
947 using GetNetBundleClass = INetBundle *(*)();
948 auto getNetBundle = (GetNetBundleClass)dlsym(handler, "GetNetBundle");
949 if (getNetBundle == nullptr) {
950 NETMGR_LOG_E("GetNetBundle failed, reason : %{public}s", dlerror());
951 dlclose(handler);
952 return std::nullopt;
953 }
954 auto netBundle = getNetBundle();
955 if (netBundle == nullptr) {
956 NETMGR_LOG_E("netBundle is nullptr");
957 dlclose(handler);
958 return std::nullopt;
959 }
960 auto result = netBundle->ObtainBundleNameForSelf();
961 dlclose(handler);
962 return result;
963 }
964 } // namespace NetManagerStandard
965 } // namespace OHOS
966