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 const std::string LIB_NET_BUNDLE_UTILS_PATH = "libnet_bundle_utils.z.so";
36
37 namespace OHOS {
38 namespace NetManagerStandard {
NetConnClient()39 NetConnClient::NetConnClient() : NetConnService_(nullptr), deathRecipient_(nullptr)
40 {
41 buffer_[RESERVED_BUFFER_SIZE-1] = '\0';
42 }
43
~NetConnClient()44 NetConnClient::~NetConnClient()
45 {
46 DlCloseRemoveDeathRecipient();
47 }
48
GetInstance()49 NetConnClient &NetConnClient::GetInstance()
50 {
51 auto temp = std::atomic_load_explicit(&instance_, std::memory_order_acquire);
52 if (temp == nullptr) {
53 std::lock_guard locker(instanceMtx_);
54 temp = std::atomic_load_explicit(&instance_, std::memory_order_relaxed);
55 if (temp == nullptr) {
56 temp = std::make_shared<NetConnClient>();
57 std::atomic_store_explicit(&instance_, temp, std::memory_order_release);
58 }
59 }
60 return *temp;
61 }
62
SubscribeSystemAbility()63 void NetConnClient::SubscribeSystemAbility()
64 {
65 if (saStatusListener_ != nullptr) {
66 NETMGR_LOG_D("No duplicate subscribe.");
67 return;
68 }
69 saStatusListener_ = sptr<NetConnAbilityListener>(new NetConnAbilityListener());
70 if (saStatusListener_ == nullptr) {
71 NETMGR_LOG_E("NetConnAbilityListener create failed.");
72 return;
73 }
74 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
75 if (sam == nullptr) {
76 NETMGR_LOG_E("SubscribeSystemAbility sam is null.");
77 return;
78 }
79 int32_t result =
80 sam->SubscribeSystemAbility(static_cast<int32_t>(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID), saStatusListener_);
81 if (result != ERR_OK) {
82 NETMGR_LOG_E("NetConnAbilityListener subscribe failed, code %{public}d.", result);
83 }
84 }
85
UnsubscribeSystemAbility()86 void NetConnClient::UnsubscribeSystemAbility()
87 {
88 if (saStatusListener_ == nullptr) {
89 NETMGR_LOG_I("NetConnAbilityListener is nullptr.");
90 return;
91 }
92 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
93 if (sam == nullptr) {
94 NETMGR_LOG_E("UnsubscribeSystemAbility sam is null.");
95 return;
96 }
97 int32_t result =
98 sam->UnSubscribeSystemAbility(static_cast<int32_t>(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID), saStatusListener_);
99 if (result != ERR_OK) {
100 NETMGR_LOG_E("NetConnAbilityListener Unsubscribe failed, code %{public}d.", result);
101 }
102 }
103
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)104 void NetConnAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
105 {
106 std::lock_guard<std::mutex>(this->mutex_);
107 if (systemAbilityId == COMM_NET_CONN_MANAGER_SYS_ABILITY_ID) {
108 NETMGR_LOG_I("net conn manager sa is added.");
109 NetConnClient::GetInstance().RecoverCallbackAndGlobalProxy();
110 }
111 }
112
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)113 void NetConnAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
114 {
115 std::lock_guard<std::mutex>(this->mutex_);
116 if (systemAbilityId == COMM_NET_CONN_MANAGER_SYS_ABILITY_ID) {
117 NETMGR_LOG_I("net conn manager sa is removed.");
118 }
119 }
120
SystemReady()121 int32_t NetConnClient::SystemReady()
122 {
123 sptr<INetConnService> proxy = GetProxy();
124 if (proxy == nullptr) {
125 NETMGR_LOG_E("proxy is nullptr");
126 return NETMANAGER_ERR_GET_PROXY_FAIL;
127 }
128 return proxy->SystemReady();
129 }
130
SetInternetPermission(uint32_t uid,uint8_t allow)131 int32_t NetConnClient::SetInternetPermission(uint32_t uid, uint8_t allow)
132 {
133 uint8_t oldAllow;
134 bool ret = netPermissionMap_.Find(uid, oldAllow);
135 if (ret && allow == oldAllow) {
136 return NETMANAGER_SUCCESS;
137 }
138
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 int32_t result = proxy->SetInternetPermission(uid, allow);
145 if (result == NETMANAGER_SUCCESS) {
146 netPermissionMap_.EnsureInsert(uid, allow);
147 }
148 return result;
149 }
150
EnableVnicNetwork(const sptr<NetLinkInfo> & netLinkInfo,const std::set<int32_t> & uids)151 int32_t NetConnClient::EnableVnicNetwork(const sptr<NetLinkInfo> &netLinkInfo, const std::set<int32_t> &uids)
152 {
153 NETMGR_LOG_D("EnableVnicNetwork client in.");
154 sptr<INetConnService> proxy = GetProxy();
155 if (proxy == nullptr) {
156 NETMGR_LOG_E("proxy is nullptr");
157 return NETMANAGER_ERR_GET_PROXY_FAIL;
158 }
159
160 return proxy->EnableVnicNetwork(netLinkInfo, uids);
161 }
162
DisableVnicNetwork()163 int32_t NetConnClient::DisableVnicNetwork()
164 {
165 NETMGR_LOG_D("DisableVnicNetwork client in.");
166 sptr<INetConnService> proxy = GetProxy();
167 if (proxy == nullptr) {
168 NETMGR_LOG_E("proxy is nullptr");
169 return NETMANAGER_ERR_GET_PROXY_FAIL;
170 }
171
172 return proxy->DisableVnicNetwork();
173 }
174
EnableDistributedClientNet(const std::string & virnicAddr,const std::string & iif)175 int32_t NetConnClient::EnableDistributedClientNet(const std::string &virnicAddr, const std::string &iif)
176 {
177 NETMGR_LOG_D("EnableDistributedClientNet client in.");
178 sptr<INetConnService> proxy = GetProxy();
179 if (proxy == nullptr) {
180 NETMGR_LOG_E("proxy is nullptr");
181 return NETMANAGER_ERR_GET_PROXY_FAIL;
182 }
183
184 return proxy->EnableDistributedClientNet(virnicAddr, iif);
185 }
186
EnableDistributedServerNet(const std::string & iif,const std::string & devIface,const std::string & dstAddr)187 int32_t NetConnClient::EnableDistributedServerNet(const std::string &iif, const std::string &devIface,
188 const std::string &dstAddr)
189 {
190 NETMGR_LOG_D("EnableDistributedServerNet client in.");
191 sptr<INetConnService> proxy = GetProxy();
192 if (proxy == nullptr) {
193 NETMGR_LOG_E("proxy is nullptr");
194 return NETMANAGER_ERR_GET_PROXY_FAIL;
195 }
196
197 return proxy->EnableDistributedServerNet(iif, devIface, dstAddr);
198 }
199
DisableDistributedNet(bool isServer)200 int32_t NetConnClient::DisableDistributedNet(bool isServer)
201 {
202 NETMGR_LOG_D("DisableDistributedNet client in.");
203 sptr<INetConnService> proxy = GetProxy();
204 if (proxy == nullptr) {
205 NETMGR_LOG_E("proxy is nullptr");
206 return NETMANAGER_ERR_GET_PROXY_FAIL;
207 }
208
209 return proxy->DisableDistributedNet(isServer);
210 }
211
RegisterNetSupplier(NetBearType bearerType,const std::string & ident,const std::set<NetCap> & netCaps,uint32_t & supplierId)212 int32_t NetConnClient::RegisterNetSupplier(NetBearType bearerType, const std::string &ident,
213 const std::set<NetCap> &netCaps, uint32_t &supplierId)
214 {
215 NETMGR_LOG_D("RegisterNetSupplier client in.");
216 sptr<INetConnService> proxy = GetProxy();
217 if (proxy == nullptr) {
218 NETMGR_LOG_E("proxy is nullptr");
219 return NETMANAGER_ERR_GET_PROXY_FAIL;
220 }
221
222 return proxy->RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
223 }
224
UnregisterNetSupplier(uint32_t supplierId)225 int32_t NetConnClient::UnregisterNetSupplier(uint32_t supplierId)
226 {
227 NETMGR_LOG_D("UnregisterNetSupplier client in.");
228 sptr<INetConnService> proxy = GetProxy();
229 if (proxy == nullptr) {
230 NETMGR_LOG_E("proxy is nullptr");
231 return NETMANAGER_ERR_GET_PROXY_FAIL;
232 }
233 {
234 std::lock_guard<std::mutex> lock(netSupplierCallbackMutex_);
235 netSupplierCallback_.erase(supplierId);
236 }
237 return proxy->UnregisterNetSupplier(supplierId);
238 }
239
RegisterNetSupplierCallback(uint32_t supplierId,const sptr<NetSupplierCallbackBase> & callback)240 int32_t NetConnClient::RegisterNetSupplierCallback(uint32_t supplierId, const sptr<NetSupplierCallbackBase> &callback)
241 {
242 NETMGR_LOG_D("RegisterNetSupplierCallback client in.");
243 sptr<INetConnService> proxy = GetProxy();
244 if (proxy == nullptr) {
245 NETMGR_LOG_E("proxy is nullptr");
246 return NETMANAGER_ERR_GET_PROXY_FAIL;
247 }
248 sptr<NetSupplierCallbackStub> ptr = std::make_unique<NetSupplierCallbackStub>().release();
249 ptr->RegisterSupplierCallbackImpl(callback);
250 {
251 std::lock_guard<std::mutex> lock(netSupplierCallbackMutex_);
252 netSupplierCallback_[supplierId] = ptr;
253 }
254 return proxy->RegisterNetSupplierCallback(supplierId, ptr);
255 }
256
RegisterNetConnCallback(const sptr<INetConnCallback> callback)257 int32_t NetConnClient::RegisterNetConnCallback(const sptr<INetConnCallback> callback)
258 {
259 NETMGR_LOG_D("RegisterNetConnCallback client in.");
260 sptr<INetConnService> proxy = GetProxy();
261 if (proxy == nullptr) {
262 NETMGR_LOG_E("The parameter of proxy is nullptr");
263 return NETMANAGER_ERR_GET_PROXY_FAIL;
264 }
265 int32_t ret = proxy->RegisterNetConnCallback(callback);
266 if (ret == NETMANAGER_SUCCESS) {
267 NETMGR_LOG_D("RegisterNetConnCallback success, save callback.");
268 std::lock_guard<std::mutex> locker(registerConnTupleListMutex_);
269 registerConnTupleList_.push_back(std::make_tuple(nullptr, callback, 0));
270 }
271
272 return ret;
273 }
274
RegisterNetConnCallback(const sptr<NetSpecifier> & netSpecifier,const sptr<INetConnCallback> callback,const uint32_t & timeoutMS)275 int32_t NetConnClient::RegisterNetConnCallback(const sptr<NetSpecifier> &netSpecifier,
276 const sptr<INetConnCallback> callback, const uint32_t &timeoutMS)
277 {
278 NETMGR_LOG_D("RegisterNetConnCallback with timeout client in.");
279 if (netSpecifier == nullptr || !netSpecifier->SpecifierIsValid()) {
280 NETMGR_LOG_E("The parameter of netSpecifier is invalid");
281 return NETMANAGER_ERR_PARAMETER_ERROR;
282 }
283 sptr<INetConnService> proxy = GetProxy();
284 if (proxy == nullptr) {
285 NETMGR_LOG_E("The parameter of proxy is nullptr");
286 return NETMANAGER_ERR_GET_PROXY_FAIL;
287 }
288 int32_t ret = proxy->RegisterNetConnCallback(netSpecifier, callback, timeoutMS);
289 if (ret == NETMANAGER_SUCCESS) {
290 NETMGR_LOG_D("RegisterNetConnCallback success, save netSpecifier and callback and timeoutMS.");
291 std::lock_guard<std::mutex> locker(registerConnTupleListMutex_);
292 registerConnTupleList_.push_back(std::make_tuple(netSpecifier, callback, timeoutMS));
293 }
294
295 return ret;
296 }
297
RequestNetConnection(const sptr<NetSpecifier> netSpecifier,const sptr<INetConnCallback> callback,const uint32_t timeoutMS)298 int32_t NetConnClient::RequestNetConnection(const sptr<NetSpecifier> netSpecifier,
299 const sptr<INetConnCallback> callback, const uint32_t timeoutMS)
300 {
301 NETMGR_LOG_D("RequestNetConnection with timeout client in.");
302 if (netSpecifier == nullptr || !netSpecifier->SpecifierIsValid()) {
303 NETMGR_LOG_E("The parameter of netSpecifier is invalid");
304 return NETMANAGER_ERR_PARAMETER_ERROR;
305 }
306 sptr<INetConnService> proxy = GetProxy();
307 if (proxy == nullptr) {
308 NETMGR_LOG_E("The parameter of proxy is nullptr");
309 return NETMANAGER_ERR_GET_PROXY_FAIL;
310 }
311 int32_t ret = proxy->RequestNetConnection(netSpecifier, callback, timeoutMS);
312 if (ret == NETMANAGER_SUCCESS) {
313 NETMGR_LOG_D("RequestNetConnection success, save netSpecifier and callback and timeoutMS.");
314 std::lock_guard<std::mutex> locker(registerConnTupleListMutex_);
315 registerConnTupleList_.push_back(std::make_tuple(netSpecifier, callback, timeoutMS));
316 }
317
318 return ret;
319 }
320
UnregisterNetConnCallback(const sptr<INetConnCallback> & callback)321 int32_t NetConnClient::UnregisterNetConnCallback(const sptr<INetConnCallback> &callback)
322 {
323 NETMGR_LOG_D("UnregisterNetConnCallback client in.");
324 sptr<INetConnService> proxy = GetProxy();
325 if (proxy == nullptr) {
326 NETMGR_LOG_E("proxy is nullptr");
327 return NETMANAGER_ERR_GET_PROXY_FAIL;
328 }
329 int32_t ret = proxy->UnregisterNetConnCallback(callback);
330 if (ret == NETMANAGER_SUCCESS) {
331 NETMGR_LOG_D("UnregisterNetConnCallback success, delete callback.");
332 std::lock_guard<std::mutex> locker(registerConnTupleListMutex_);
333 for (auto it = registerConnTupleList_.begin(); it != registerConnTupleList_.end(); ++it) {
334 if (std::get<1>(*it)->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) {
335 registerConnTupleList_.erase(it);
336 break;
337 }
338 }
339 }
340
341 return ret;
342 }
343
RegisterNetDetectionCallback(int32_t netId,const sptr<INetDetectionCallback> & callback)344 int32_t NetConnClient::RegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback)
345 {
346 NETMGR_LOG_I("RegisterNetDetectionCallback client in.");
347 sptr<INetConnService> proxy = GetProxy();
348 if (proxy == nullptr) {
349 NETMGR_LOG_E("proxy is nullptr");
350 return NETMANAGER_ERR_GET_PROXY_FAIL;
351 }
352
353 return proxy->RegisterNetDetectionCallback(netId, callback);
354 }
355
UnRegisterNetDetectionCallback(int32_t netId,const sptr<INetDetectionCallback> & callback)356 int32_t NetConnClient::UnRegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback)
357 {
358 NETMGR_LOG_I("UnRegisterNetDetectionCallback client in.");
359 sptr<INetConnService> proxy = GetProxy();
360 if (proxy == nullptr) {
361 NETMGR_LOG_E("proxy is nullptr");
362 return NETMANAGER_ERR_GET_PROXY_FAIL;
363 }
364
365 return proxy->UnRegisterNetDetectionCallback(netId, callback);
366 }
367
UpdateNetCaps(const std::set<NetCap> & netCaps,const uint32_t supplierId)368 int32_t NetConnClient::UpdateNetCaps(const std::set<NetCap> &netCaps, const uint32_t supplierId)
369 {
370 NETMGR_LOG_I("Update net caps.");
371 auto proxy = GetProxy();
372 if (proxy == nullptr) {
373 NETMGR_LOG_E("proxy is nullptr");
374 return NETMANAGER_ERR_GET_PROXY_FAIL;
375 }
376 return proxy->UpdateNetCaps(netCaps, supplierId);
377 }
378
UpdateNetSupplierInfo(uint32_t supplierId,const sptr<NetSupplierInfo> & netSupplierInfo)379 int32_t NetConnClient::UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo)
380 {
381 NETMGR_LOG_I("UpdateNetSupplierInfo client in.");
382 sptr<INetConnService> proxy = GetProxy();
383 if (proxy == nullptr) {
384 NETMGR_LOG_E("proxy is nullptr");
385 return NETMANAGER_ERR_GET_PROXY_FAIL;
386 }
387
388 return proxy->UpdateNetSupplierInfo(supplierId, netSupplierInfo);
389 }
390
UpdateNetLinkInfo(uint32_t supplierId,const sptr<NetLinkInfo> & netLinkInfo)391 int32_t NetConnClient::UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo)
392 {
393 NETMGR_LOG_I("UpdateNetLinkInfo client in.");
394 sptr<INetConnService> proxy = GetProxy();
395 if (proxy == nullptr) {
396 NETMGR_LOG_E("proxy is nullptr");
397 return NETMANAGER_ERR_GET_PROXY_FAIL;
398 }
399
400 return proxy->UpdateNetLinkInfo(supplierId, netLinkInfo);
401 }
402
GetDefaultNet(NetHandle & netHandle)403 int32_t NetConnClient::GetDefaultNet(NetHandle &netHandle)
404 {
405 NETMGR_LOG_D("GetDefaultNet client in.");
406 sptr<INetConnService> proxy = GetProxy();
407 if (proxy == nullptr) {
408 NETMGR_LOG_E("proxy is nullptr");
409 return NETMANAGER_ERR_GET_PROXY_FAIL;
410 }
411
412 int32_t netId = 0;
413 int32_t result = proxy->GetDefaultNet(netId);
414 if (result != NETMANAGER_SUCCESS) {
415 NETMGR_LOG_D("fail to get default net.");
416 return result;
417 }
418 netHandle.SetNetId(netId);
419 NETMGR_LOG_D("GetDefaultNet client out.");
420 return NETMANAGER_SUCCESS;
421 }
422
HasDefaultNet(bool & flag)423 int32_t NetConnClient::HasDefaultNet(bool &flag)
424 {
425 NETMGR_LOG_D("HasDefaultNet client in.");
426 sptr<INetConnService> proxy = GetProxy();
427 if (proxy == nullptr) {
428 NETMGR_LOG_E("proxy is nullptr");
429 return NETMANAGER_ERR_GET_PROXY_FAIL;
430 }
431 return proxy->HasDefaultNet(flag);
432 }
433
GetAllNets(std::list<sptr<NetHandle>> & netList)434 int32_t NetConnClient::GetAllNets(std::list<sptr<NetHandle>> &netList)
435 {
436 sptr<INetConnService> proxy = GetProxy();
437 if (proxy == nullptr) {
438 NETMGR_LOG_E("proxy is nullptr");
439 return NETMANAGER_ERR_GET_PROXY_FAIL;
440 }
441
442 std::list<int32_t> netIdList;
443 int32_t result = proxy->GetAllNets(netIdList);
444 if (result != NETMANAGER_SUCCESS) {
445 return result;
446 }
447 std::list<int32_t>::iterator iter;
448 for (iter = netIdList.begin(); iter != netIdList.end(); ++iter) {
449 sptr<NetHandle> netHandle = std::make_unique<NetHandle>(*iter).release();
450 if (netHandle != nullptr) {
451 netList.push_back(netHandle);
452 }
453 }
454 return NETMANAGER_SUCCESS;
455 }
456
GetConnectionProperties(const NetHandle & netHandle,NetLinkInfo & info)457 int32_t NetConnClient::GetConnectionProperties(const NetHandle &netHandle, NetLinkInfo &info)
458 {
459 sptr<INetConnService> proxy = GetProxy();
460 if (proxy == nullptr) {
461 NETMGR_LOG_E("proxy is nullptr");
462 return NETMANAGER_ERR_GET_PROXY_FAIL;
463 }
464
465 return proxy->GetConnectionProperties(netHandle.GetNetId(), info);
466 }
467
GetNetCapabilities(const NetHandle & netHandle,NetAllCapabilities & netAllCap)468 int32_t NetConnClient::GetNetCapabilities(const NetHandle &netHandle, NetAllCapabilities &netAllCap)
469 {
470 sptr<INetConnService> proxy = GetProxy();
471 if (proxy == nullptr) {
472 NETMGR_LOG_E("proxy is nullptr");
473 return NETMANAGER_ERR_GET_PROXY_FAIL;
474 }
475
476 return proxy->GetNetCapabilities(netHandle.GetNetId(), netAllCap);
477 }
478
GetAddressesByName(const std::string & host,int32_t netId,std::vector<INetAddr> & addrList)479 int32_t NetConnClient::GetAddressesByName(const std::string &host, int32_t netId, std::vector<INetAddr> &addrList)
480 {
481 sptr<INetConnService> proxy = GetProxy();
482 if (proxy == nullptr) {
483 NETMGR_LOG_E("proxy is nullptr");
484 return NETMANAGER_ERR_GET_PROXY_FAIL;
485 }
486
487 return proxy->GetAddressesByName(host, netId, addrList);
488 }
489
GetAddressByName(const std::string & host,int32_t netId,INetAddr & addr)490 int32_t NetConnClient::GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr)
491 {
492 sptr<INetConnService> proxy = GetProxy();
493 if (proxy == nullptr) {
494 NETMGR_LOG_E("proxy is nullptr");
495 return NETMANAGER_ERR_GET_PROXY_FAIL;
496 }
497
498 return proxy->GetAddressByName(host, netId, addr);
499 }
500
GetIfaceNameIdentMaps(NetBearType bearerType,SafeMap<std::string,std::string> & ifaceNameIdentMaps)501 int32_t NetConnClient::GetIfaceNameIdentMaps(NetBearType bearerType,
502 SafeMap<std::string, std::string> &ifaceNameIdentMaps)
503 {
504 sptr<INetConnService> proxy = GetProxy();
505 if (proxy == nullptr) {
506 NETMGR_LOG_E("proxy is nullptr");
507 return NETMANAGER_ERR_GET_PROXY_FAIL;
508 }
509 return proxy->GetIfaceNameIdentMaps(bearerType, ifaceNameIdentMaps);
510 }
511
BindSocket(int32_t socketFd,int32_t netId)512 int32_t NetConnClient::BindSocket(int32_t socketFd, int32_t netId)
513 {
514 // default netId begin whit 100, inner virtual interface netId between 1 and 50
515 if (netId < MIN_VALID_INTERNAL_NETID || (netId > MAX_VALID_INTERNAL_NETID && netId < MIN_VALID_NETID)) {
516 NETMGR_LOG_E("netId is invalid.");
517 return NET_CONN_ERR_INVALID_NETWORK;
518 }
519 std::shared_ptr<nmd::FwmarkClient> fwmarkClient_ = std::make_shared<nmd::FwmarkClient>();
520 if (fwmarkClient_ == nullptr) {
521 NETMGR_LOG_E("fwmarkClient_ is nullptr");
522 return NETMANAGER_ERR_PARAMETER_ERROR;
523 }
524 fwmarkClient_->BindSocket(socketFd, netId);
525 return NETMANAGER_SUCCESS;
526 }
527
NetDetection(const NetHandle & netHandle)528 int32_t NetConnClient::NetDetection(const NetHandle &netHandle)
529 {
530 sptr<INetConnService> proxy = GetProxy();
531 if (proxy == nullptr) {
532 NETMGR_LOG_E("proxy is nullptr");
533 return NETMANAGER_ERR_GET_PROXY_FAIL;
534 }
535
536 return proxy->NetDetection(netHandle.GetNetId());
537 }
538
GetProxy()539 sptr<INetConnService> NetConnClient::GetProxy()
540 {
541 std::lock_guard lock(mutex_);
542
543 SubscribeSystemAbility();
544 if (NetConnService_) {
545 NETMGR_LOG_D("get proxy is ok");
546 return NetConnService_;
547 }
548
549 NETMGR_LOG_D("execute GetSystemAbilityManager");
550 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
551 if (sam == nullptr) {
552 NETMGR_LOG_E("GetProxy(), get SystemAbilityManager failed");
553 return nullptr;
554 }
555
556 sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
557 if (remote == nullptr) {
558 NETMGR_LOG_E("get Remote service failed");
559 return nullptr;
560 }
561
562 deathRecipient_ = new (std::nothrow) NetConnDeathRecipient(*this);
563 if (deathRecipient_ == nullptr) {
564 NETMGR_LOG_E("get deathRecipient_ failed");
565 return nullptr;
566 }
567 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
568 NETMGR_LOG_E("add death recipient failed");
569 return nullptr;
570 }
571
572 NetConnService_ = iface_cast<INetConnService>(remote);
573 if (NetConnService_ == nullptr) {
574 NETMGR_LOG_E("get Remote service proxy failed");
575 return nullptr;
576 }
577
578 return NetConnService_;
579 }
580
SetAirplaneMode(bool state)581 int32_t NetConnClient::SetAirplaneMode(bool state)
582 {
583 NETMGR_LOG_I("SetAirplaneMode client in.");
584 sptr<INetConnService> proxy = GetProxy();
585 if (proxy == nullptr) {
586 NETMGR_LOG_E("proxy is nullptr");
587 return NETMANAGER_ERR_GET_PROXY_FAIL;
588 }
589
590 return proxy->SetAirplaneMode(state);
591 }
592
RecoverCallbackAndGlobalProxy()593 void NetConnClient::RecoverCallbackAndGlobalProxy()
594 {
595 std::list<std::tuple<sptr<NetSpecifier>, sptr<INetConnCallback>, uint32_t>> registerConnTupleListTmp;
596 {
597 std::lock_guard<std::mutex> locker(registerConnTupleListMutex_);
598 registerConnTupleListTmp = registerConnTupleList_;
599 }
600 if (registerConnTupleListTmp.empty() && globalHttpProxy_.GetHost().empty() &&
601 preAirplaneCallback_ == nullptr) {
602 NETMGR_LOG_W("no need recovery");
603 return;
604 }
605 auto proxy = GetProxy();
606 NETMGR_LOG_W("Get proxy %{public}s", proxy == nullptr ? "failed" : "success");
607 if (proxy != nullptr) {
608 for (auto mem : registerConnTupleListTmp) {
609 sptr<NetSpecifier> specifier = std::get<0>(mem);
610 sptr<INetConnCallback> callback = std::get<1>(mem);
611 uint32_t timeoutMS = std::get<2>(mem);
612 bool isInternalDefault = specifier != nullptr &&
613 specifier->netCapabilities_.netCaps_.count(NetManagerStandard::NET_CAPABILITY_INTERNAL_DEFAULT) > 0;
614 int32_t ret = NETMANAGER_SUCCESS;
615 if (specifier != nullptr && timeoutMS != 0) {
616 ret = isInternalDefault ? proxy->RequestNetConnection(specifier, callback, timeoutMS) :
617 proxy->RegisterNetConnCallback(specifier, callback, timeoutMS);
618 NETMGR_LOG_D("Register result hasNetSpecifier_ and timeoutMS_ %{public}d", ret);
619 } else if (specifier != nullptr) {
620 ret = isInternalDefault ? proxy->RequestNetConnection(specifier, callback, 0) :
621 proxy->RegisterNetConnCallback(specifier, callback, 0);
622 NETMGR_LOG_D("Register result hasNetSpecifier_ %{public}d", ret);
623 } else if (callback != nullptr) {
624 int32_t ret = proxy->RegisterNetConnCallback(callback);
625 NETMGR_LOG_D("Register netconn result %{public}d", ret);
626 }
627 }
628 }
629 if (proxy != nullptr && preAirplaneCallback_ != nullptr) {
630 int32_t ret = proxy->RegisterPreAirplaneCallback(preAirplaneCallback_);
631 NETMGR_LOG_D("Register pre airplane result %{public}d", ret);
632 }
633
634 if (proxy != nullptr && !globalHttpProxy_.GetHost().empty()) {
635 int32_t ret = proxy->SetGlobalHttpProxy(globalHttpProxy_);
636 NETMGR_LOG_D("globalHttpProxy_ Register result %{public}d", ret);
637 }
638 }
639
OnRemoteDied(const wptr<IRemoteObject> & remote)640 void NetConnClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
641 {
642 NETMGR_LOG_D("on remote died");
643 if (remote == nullptr) {
644 NETMGR_LOG_E("remote object is nullptr");
645 return;
646 }
647
648 std::lock_guard lock(mutex_);
649 if (NetConnService_ == nullptr) {
650 NETMGR_LOG_E("NetConnService_ is nullptr");
651 return;
652 }
653
654 sptr<IRemoteObject> local = NetConnService_->AsObject();
655 if (local != remote.promote()) {
656 NETMGR_LOG_E("proxy and stub is not same remote object");
657 return;
658 }
659
660 local->RemoveDeathRecipient(deathRecipient_);
661 NetConnService_ = nullptr;
662 }
663
DlCloseRemoveDeathRecipient()664 void NetConnClient::DlCloseRemoveDeathRecipient()
665 {
666 UnsubscribeSystemAbility();
667 sptr<INetConnService> proxy = GetProxy();
668 if (proxy == nullptr) {
669 NETMGR_LOG_E("proxy is nullptr");
670 return;
671 }
672
673 auto serviceRemote = proxy->AsObject();
674 if (serviceRemote == nullptr) {
675 NETMGR_LOG_E("serviceRemote is nullptr");
676 return;
677 }
678
679 serviceRemote->RemoveDeathRecipient(deathRecipient_);
680 NETMGR_LOG_I("RemoveDeathRecipient success");
681 }
682
IsDefaultNetMetered(bool & isMetered)683 int32_t NetConnClient::IsDefaultNetMetered(bool &isMetered)
684 {
685 sptr<INetConnService> proxy = GetProxy();
686 if (proxy == nullptr) {
687 NETMGR_LOG_E("proxy is nullptr");
688 return NETMANAGER_ERR_GET_PROXY_FAIL;
689 }
690 return proxy->IsDefaultNetMetered(isMetered);
691 }
692
SetGlobalHttpProxy(const HttpProxy & httpProxy)693 int32_t NetConnClient::SetGlobalHttpProxy(const HttpProxy &httpProxy)
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 if (globalHttpProxy_ != httpProxy) {
701 globalHttpProxy_ = httpProxy;
702 }
703 return proxy->SetGlobalHttpProxy(httpProxy);
704 }
705
RegisterAppHttpProxyCallback(std::function<void (const HttpProxy & httpProxy)> callback,uint32_t & callbackid)706 void NetConnClient::RegisterAppHttpProxyCallback(std::function<void(const HttpProxy &httpProxy)> callback,
707 uint32_t &callbackid)
708 {
709 std::lock_guard<std::mutex> lock(appHttpProxyCbMapMutex_);
710 uint32_t id = currentCallbackId_;
711 currentCallbackId_++;
712 appHttpProxyCbMap_[id] = callback;
713 callbackid = id;
714 if (callback && !appHttpProxy_.GetHost().empty()) {
715 callback(appHttpProxy_);
716 }
717 NETMGR_LOG_I("registerCallback id:%{public}d.", id);
718 }
719
UnregisterAppHttpProxyCallback(uint32_t callbackid)720 void NetConnClient::UnregisterAppHttpProxyCallback(uint32_t callbackid)
721 {
722 NETMGR_LOG_I("unregisterCallback callbackid:%{public}d.", callbackid);
723 std::lock_guard<std::mutex> lock(appHttpProxyCbMapMutex_);
724 appHttpProxyCbMap_.erase(callbackid);
725 }
726
SetAppHttpProxy(const HttpProxy & httpProxy)727 int32_t NetConnClient::SetAppHttpProxy(const HttpProxy &httpProxy)
728 {
729 NETMGR_LOG_I("Enter AppHttpProxy");
730
731 if (appHttpProxy_ != httpProxy) {
732 appHttpProxy_ = httpProxy;
733 std::lock_guard<std::mutex> lock(appHttpProxyCbMapMutex_);
734 for (const auto &pair : appHttpProxyCbMap_) {
735 pair.second(httpProxy);
736 }
737 }
738
739 return NETMANAGER_SUCCESS;
740 }
741
GetGlobalHttpProxy(HttpProxy & httpProxy)742 int32_t NetConnClient::GetGlobalHttpProxy(HttpProxy &httpProxy)
743 {
744 sptr<INetConnService> proxy = GetProxy();
745 if (proxy == nullptr) {
746 NETMGR_LOG_E("proxy is nullptr");
747 return NETMANAGER_ERR_GET_PROXY_FAIL;
748 }
749 return proxy->GetGlobalHttpProxy(httpProxy);
750 }
751
GetDefaultHttpProxy(HttpProxy & httpProxy)752 int32_t NetConnClient::GetDefaultHttpProxy(HttpProxy &httpProxy)
753 {
754 if (!appHttpProxy_.GetHost().empty()) {
755 httpProxy = appHttpProxy_;
756 NETMGR_LOG_D("Return AppHttpProxy:%{public}s:%{public}d",
757 httpProxy.GetHost().c_str(), httpProxy.GetPort());
758 return NETMANAGER_SUCCESS;
759 }
760
761 sptr<INetConnService> proxy = GetProxy();
762 if (proxy == nullptr) {
763 NETMGR_LOG_E("proxy is nullptr");
764 return NETMANAGER_ERR_GET_PROXY_FAIL;
765 }
766 int32_t bindNetId = 0;
767 GetAppNet(bindNetId);
768 return proxy->GetDefaultHttpProxy(bindNetId, httpProxy);
769 }
770
SetPacUrl(const std::string & pacUrl)771 int32_t NetConnClient::SetPacUrl(const std::string &pacUrl)
772 {
773 NETMGR_LOG_I("Enter SetPacUrl");
774
775 sptr<INetConnService> proxy = GetProxy();
776 if (proxy == nullptr) {
777 NETMGR_LOG_E("proxy is nullptr");
778 return NETMANAGER_ERR_GET_PROXY_FAIL;
779 }
780 return proxy->SetPacUrl(pacUrl);
781 }
782
GetPacUrl(std::string & pacUrl)783 int32_t NetConnClient::GetPacUrl(std::string &pacUrl)
784 {
785 NETMGR_LOG_I("Enter GetPacUrl");
786
787 sptr<INetConnService> proxy = GetProxy();
788 if (proxy == nullptr) {
789 NETMGR_LOG_E("proxy is nullptr");
790 return NETMANAGER_ERR_GET_PROXY_FAIL;
791 }
792 return proxy->GetPacUrl(pacUrl);
793 }
794
GetNetIdByIdentifier(const std::string & ident,std::list<int32_t> & netIdList)795 int32_t NetConnClient::GetNetIdByIdentifier(const std::string &ident, std::list<int32_t> &netIdList)
796 {
797 sptr<INetConnService> proxy = GetProxy();
798 if (proxy == nullptr) {
799 NETMGR_LOG_E("proxy is nullptr");
800 return NETMANAGER_ERR_GET_PROXY_FAIL;
801 }
802 return proxy->GetNetIdByIdentifier(ident, netIdList);
803 }
804
SetAppNet(int32_t netId)805 int32_t NetConnClient::SetAppNet(int32_t netId)
806 {
807 if (netId < MIN_VALID_NETID && netId != 0) {
808 return NET_CONN_ERR_INVALID_NETWORK;
809 }
810 sptr<INetConnService> proxy = GetProxy();
811 if (proxy == nullptr) {
812 NETMGR_LOG_E("proxy is nullptr");
813 return NETMANAGER_ERR_GET_PROXY_FAIL;
814 }
815 int32_t ret = proxy->SetAppNet(netId);
816 if (ret != NETMANAGER_SUCCESS) {
817 return ret;
818 }
819
820 SetNetForApp(netId);
821 return NETMANAGER_SUCCESS;
822 }
823
GetAppNet(int32_t & netId)824 int32_t NetConnClient::GetAppNet(int32_t &netId)
825 {
826 netId = GetNetForApp();
827 return NETMANAGER_SUCCESS;
828 }
829
RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> & callback)830 int32_t NetConnClient::RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback)
831 {
832 sptr<INetConnService> proxy = GetProxy();
833 if (proxy == nullptr) {
834 NETMGR_LOG_E("proxy is nullptr");
835 return NETMANAGER_ERR_GET_PROXY_FAIL;
836 }
837 return proxy->RegisterNetInterfaceCallback(callback);
838 }
839
UnregisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> & callback)840 int32_t NetConnClient::UnregisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback)
841 {
842 sptr<INetConnService> proxy = GetProxy();
843 if (proxy == nullptr) {
844 NETMGR_LOG_E("proxy is nullptr");
845 return NETMANAGER_ERR_GET_PROXY_FAIL;
846 }
847 return proxy->UnregisterNetInterfaceCallback(callback);
848 }
849
GetNetInterfaceConfiguration(const std::string & iface,NetInterfaceConfiguration & config)850 int32_t NetConnClient::GetNetInterfaceConfiguration(const std::string &iface, NetInterfaceConfiguration &config)
851 {
852 sptr<INetConnService> proxy = GetProxy();
853 if (proxy == nullptr) {
854 NETMGR_LOG_E("proxy is nullptr");
855 return NETMANAGER_ERR_GET_PROXY_FAIL;
856 }
857 return proxy->GetNetInterfaceConfiguration(iface, config);
858 }
859
SetNetInterfaceIpAddress(const std::string & iface,const std::string & ipAddress)860 int32_t NetConnClient::SetNetInterfaceIpAddress(const std::string &iface, const std::string &ipAddress)
861 {
862 sptr<INetConnService> proxy = GetProxy();
863 if (proxy == nullptr) {
864 NETMGR_LOG_E("proxy is nullptr");
865 return NETMANAGER_ERR_GET_PROXY_FAIL;
866 }
867 return proxy->SetNetInterfaceIpAddress(iface, ipAddress);
868 }
869
SetInterfaceUp(const std::string & iface)870 int32_t NetConnClient::SetInterfaceUp(const std::string &iface)
871 {
872 sptr<INetConnService> proxy = GetProxy();
873 if (proxy == nullptr) {
874 NETMGR_LOG_E("proxy is nullptr");
875 return NETMANAGER_ERR_GET_PROXY_FAIL;
876 }
877 return proxy->SetInterfaceUp(iface);
878 }
879
SetInterfaceDown(const std::string & iface)880 int32_t NetConnClient::SetInterfaceDown(const std::string &iface)
881 {
882 sptr<INetConnService> proxy = GetProxy();
883 if (proxy == nullptr) {
884 NETMGR_LOG_E("proxy is nullptr");
885 return NETMANAGER_ERR_GET_PROXY_FAIL;
886 }
887 return proxy->SetInterfaceDown(iface);
888 }
889
AddNetworkRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)890 int32_t NetConnClient::AddNetworkRoute(int32_t netId, const std::string &ifName,
891 const std::string &destination, const std::string &nextHop)
892 {
893 NETMGR_LOG_I("AddNetworkRoute client in.");
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
900 return proxy->AddNetworkRoute(netId, ifName, destination, nextHop);
901 }
902
RemoveNetworkRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)903 int32_t NetConnClient::RemoveNetworkRoute(int32_t netId, const std::string &ifName,
904 const std::string &destination, const std::string &nextHop)
905 {
906 NETMGR_LOG_I("RemoveNetworkRoute client in.");
907 sptr<INetConnService> proxy = GetProxy();
908 if (proxy == nullptr) {
909 NETMGR_LOG_E("proxy is nullptr");
910 return NETMANAGER_ERR_GET_PROXY_FAIL;
911 }
912
913 return proxy->RemoveNetworkRoute(netId, ifName, destination, nextHop);
914 }
915
AddInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)916 int32_t NetConnClient::AddInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
917 int32_t prefixLength)
918 {
919 NETMGR_LOG_I("AddInterfaceAddress client in.");
920 sptr<INetConnService> proxy = GetProxy();
921 if (proxy == nullptr) {
922 NETMGR_LOG_E("proxy is nullptr");
923 return NETMANAGER_ERR_GET_PROXY_FAIL;
924 }
925
926 return proxy->AddInterfaceAddress(ifName, ipAddr, prefixLength);
927 }
928
DelInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)929 int32_t NetConnClient::DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
930 int32_t prefixLength)
931 {
932 NETMGR_LOG_I("DelInterfaceAddress client in.");
933 sptr<INetConnService> proxy = GetProxy();
934 if (proxy == nullptr) {
935 NETMGR_LOG_E("proxy is nullptr");
936 return NETMANAGER_ERR_GET_PROXY_FAIL;
937 }
938
939 return proxy->DelInterfaceAddress(ifName, ipAddr, prefixLength);
940 }
941
AddStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)942 int32_t NetConnClient::AddStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName)
943 {
944 NETMGR_LOG_I("AddStaticArp client in.");
945 sptr<INetConnService> proxy = GetProxy();
946 if (proxy == nullptr) {
947 NETMGR_LOG_E("proxy is nullptr");
948 return NETMANAGER_ERR_GET_PROXY_FAIL;
949 }
950
951 return proxy->AddStaticArp(ipAddr, macAddr, ifName);
952 }
953
DelStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)954 int32_t NetConnClient::DelStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName)
955 {
956 NETMGR_LOG_I("DelStaticArp client in.");
957 sptr<INetConnService> proxy = GetProxy();
958 if (proxy == nullptr) {
959 NETMGR_LOG_E("proxy is nullptr");
960 return NETMANAGER_ERR_GET_PROXY_FAIL;
961 }
962
963 return proxy->DelStaticArp(ipAddr, macAddr, ifName);
964 }
965
RegisterSlotType(uint32_t supplierId,int32_t type)966 int32_t NetConnClient::RegisterSlotType(uint32_t supplierId, int32_t type)
967 {
968 NETMGR_LOG_I("RegisterSlotType client in.supplierId[%{public}d] type[%{public}d]", supplierId, type);
969 sptr<INetConnService> proxy = GetProxy();
970 if (proxy == nullptr) {
971 NETMGR_LOG_E("proxy is nullptr");
972 return NETMANAGER_ERR_GET_PROXY_FAIL;
973 }
974
975 return proxy->RegisterSlotType(supplierId, type);
976 }
977
GetSlotType(std::string & type)978 int32_t NetConnClient::GetSlotType(std::string &type)
979 {
980 sptr<INetConnService> proxy = GetProxy();
981 if (proxy == nullptr) {
982 NETMGR_LOG_E("proxy is nullptr");
983 return NETMANAGER_ERR_GET_PROXY_FAIL;
984 }
985
986 return proxy->GetSlotType(type);
987 }
988
GetPinSetForHostName(const std::string & hostname,std::string & pins)989 int32_t NetConnClient::GetPinSetForHostName(const std::string &hostname, std::string &pins)
990 {
991 return NetworkSecurityConfig::GetInstance().GetPinSetForHostName(hostname, pins);
992 }
993
TrustUser0Ca()994 bool NetConnClient::TrustUser0Ca()
995 {
996 return NetworkSecurityConfig::GetInstance().TrustUser0Ca();
997 }
998
TrustUserCa()999 bool NetConnClient::TrustUserCa()
1000 {
1001 return NetworkSecurityConfig::GetInstance().TrustUserCa();
1002 }
1003
IsUserDnsCache()1004 bool NetConnClient::IsUserDnsCache()
1005 {
1006 return NetworkSecurityConfig::GetInstance().IsUserDnsCache();
1007 }
1008
IsPinOpenMode(const std::string & hostname)1009 bool NetConnClient::IsPinOpenMode(const std::string &hostname)
1010 {
1011 return NetworkSecurityConfig::GetInstance().IsPinOpenMode(hostname);
1012 }
1013
IsPinOpenModeVerifyRootCa(const std::string & hostname)1014 bool NetConnClient::IsPinOpenModeVerifyRootCa(const std::string &hostname)
1015 {
1016 return NetworkSecurityConfig::GetInstance().IsPinOpenModeVerifyRootCa(hostname);
1017 }
1018
GetTrustAnchorsForHostName(const std::string & hostname,std::vector<std::string> & certs)1019 int32_t NetConnClient::GetTrustAnchorsForHostName(const std::string &hostname, std::vector<std::string> &certs)
1020 {
1021 return NetworkSecurityConfig::GetInstance().GetTrustAnchorsForHostName(hostname, certs);
1022 }
1023
IsCleartextPermitted(bool & cleartextPermitted)1024 int32_t NetConnClient::IsCleartextPermitted(bool &cleartextPermitted)
1025 {
1026 return NetworkSecurityConfig::GetInstance().IsCleartextPermitted(cleartextPermitted);
1027 }
1028
IsCleartextPermitted(const std::string & hostname,bool & cleartextPermitted)1029 int32_t NetConnClient::IsCleartextPermitted(const std::string &hostname, bool &cleartextPermitted)
1030 {
1031 return NetworkSecurityConfig::GetInstance().IsCleartextPermitted(hostname, cleartextPermitted);
1032 }
1033
FactoryResetNetwork()1034 int32_t NetConnClient::FactoryResetNetwork()
1035 {
1036 sptr<INetConnService> proxy = GetProxy();
1037 if (proxy == nullptr) {
1038 NETMGR_LOG_E("proxy is nullptr");
1039 return NETMANAGER_ERR_GET_PROXY_FAIL;
1040 }
1041
1042 return proxy->FactoryResetNetwork();
1043 }
1044
RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> & callback)1045 int32_t NetConnClient::RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> &callback)
1046 {
1047 sptr<INetConnService> proxy = GetProxy();
1048 if (proxy == nullptr) {
1049 NETMGR_LOG_E("proxy is nullptr");
1050 return NETMANAGER_ERR_GET_PROXY_FAIL;
1051 }
1052 return proxy->RegisterNetFactoryResetCallback(callback);
1053 }
1054
IsPreferCellularUrl(const std::string & url,bool & preferCellular)1055 int32_t NetConnClient::IsPreferCellularUrl(const std::string& url, bool& preferCellular)
1056 {
1057 sptr<INetConnService> proxy = GetProxy();
1058 if (proxy == nullptr) {
1059 NETMGR_LOG_E("proxy is nullptr");
1060 return NETMANAGER_ERR_GET_PROXY_FAIL;
1061 }
1062 return proxy->IsPreferCellularUrl(url, preferCellular);
1063 }
1064
RegisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)1065 int32_t NetConnClient::RegisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)
1066 {
1067 NETMGR_LOG_D("RegisterPreAirplaneCallback client in.");
1068 sptr<INetConnService> proxy = GetProxy();
1069 if (proxy == nullptr) {
1070 NETMGR_LOG_E("proxy is nullptr");
1071 return NETMANAGER_ERR_GET_PROXY_FAIL;
1072 }
1073
1074 int32_t ret = proxy->RegisterPreAirplaneCallback(callback);
1075 if (ret == NETMANAGER_SUCCESS) {
1076 NETMGR_LOG_D("RegisterPreAirplaneCallback success, save callback.");
1077 preAirplaneCallback_ = callback;
1078 }
1079
1080 return ret;
1081 }
1082
UnregisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)1083 int32_t NetConnClient::UnregisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback)
1084 {
1085 NETMGR_LOG_D("UnregisterPreAirplaneCallback client in.");
1086 sptr<INetConnService> proxy = GetProxy();
1087 if (proxy == nullptr) {
1088 NETMGR_LOG_E("proxy is nullptr");
1089 return NETMANAGER_ERR_GET_PROXY_FAIL;
1090 }
1091
1092 int32_t ret = proxy->UnregisterPreAirplaneCallback(callback);
1093 if (ret == NETMANAGER_SUCCESS) {
1094 NETMGR_LOG_D("UnregisterPreAirplaneCallback success,delete callback.");
1095 preAirplaneCallback_ = nullptr;
1096 }
1097
1098 return ret;
1099 }
1100
DecreaseSupplierScore(NetBearType bearerType,const std::string & ident,uint32_t & supplierId)1101 int32_t NetConnClient::DecreaseSupplierScore(NetBearType bearerType,
1102 const std::string &ident, uint32_t& supplierId)
1103 {
1104 sptr<INetConnService> proxy = GetProxy();
1105 if (proxy == nullptr) {
1106 NETMGR_LOG_E("proxy is nullptr.");
1107 return NETMANAGER_ERR_GET_PROXY_FAIL;
1108 }
1109 return proxy->DecreaseSupplierScore(bearerType, ident, supplierId);
1110 }
1111
IncreaseSupplierScore(uint32_t supplierId)1112 int32_t NetConnClient::IncreaseSupplierScore(uint32_t supplierId)
1113 {
1114 sptr<INetConnService> proxy = GetProxy();
1115 if (proxy == nullptr) {
1116 NETMGR_LOG_E("proxy is nullptr.");
1117 return NETMANAGER_ERR_GET_PROXY_FAIL;
1118 }
1119 return proxy->IncreaseSupplierScore(supplierId);
1120 }
1121
ObtainTargetApiVersionForSelf()1122 std::optional<int32_t> NetConnClient::ObtainTargetApiVersionForSelf()
1123 {
1124 void *handler = dlopen(LIB_NET_BUNDLE_UTILS_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE);
1125 if (handler == nullptr) {
1126 NETMGR_LOG_E("load lib failed, reason : %{public}s", dlerror());
1127 return std::nullopt;
1128 }
1129 using GetNetBundleClass = INetBundle *(*)();
1130 auto getNetBundle = (GetNetBundleClass)dlsym(handler, "GetNetBundle");
1131 if (getNetBundle == nullptr) {
1132 NETMGR_LOG_E("GetNetBundle failed, reason : %{public}s", dlerror());
1133 dlclose(handler);
1134 return std::nullopt;
1135 }
1136 auto netBundle = getNetBundle();
1137 if (netBundle == nullptr) {
1138 NETMGR_LOG_E("netBundle is nullptr");
1139 dlclose(handler);
1140 return std::nullopt;
1141 }
1142 auto result = netBundle->ObtainTargetApiVersionForSelf();
1143 dlclose(handler);
1144 return result;
1145 }
1146
IsAPIVersionSupported(int targetApiVersion)1147 bool NetConnClient::IsAPIVersionSupported(int targetApiVersion)
1148 {
1149 static auto currentApiVersion = ObtainTargetApiVersionForSelf();
1150 // Returns true by default in case can not get bundle info from bundle mgr.
1151 return currentApiVersion.value_or(targetApiVersion) >= targetApiVersion;
1152 }
1153
ObtainBundleNameForSelf()1154 std::optional<std::string> NetConnClient::ObtainBundleNameForSelf()
1155 {
1156 static auto bundleName = ObtainBundleNameFromBundleMgr();
1157 return bundleName;
1158 }
1159
ObtainBundleNameFromBundleMgr()1160 std::optional<std::string> NetConnClient::ObtainBundleNameFromBundleMgr()
1161 {
1162 void *handler = dlopen(LIB_NET_BUNDLE_UTILS_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE);
1163 if (handler == nullptr) {
1164 NETMGR_LOG_E("load lib failed, reason : %{public}s", dlerror());
1165 return std::nullopt;
1166 }
1167 using GetNetBundleClass = INetBundle *(*)();
1168 auto getNetBundle = (GetNetBundleClass)dlsym(handler, "GetNetBundle");
1169 if (getNetBundle == nullptr) {
1170 NETMGR_LOG_E("GetNetBundle failed, reason : %{public}s", dlerror());
1171 dlclose(handler);
1172 return std::nullopt;
1173 }
1174 auto netBundle = getNetBundle();
1175 if (netBundle == nullptr) {
1176 NETMGR_LOG_E("netBundle is nullptr");
1177 dlclose(handler);
1178 return std::nullopt;
1179 }
1180 auto result = netBundle->ObtainBundleNameForSelf();
1181 dlclose(handler);
1182 return result;
1183 }
1184
CloseSocketsUid(int32_t netId,uint32_t uid)1185 int32_t NetConnClient::CloseSocketsUid(int32_t netId, uint32_t uid)
1186 {
1187 sptr<INetConnService> proxy = GetProxy();
1188 if (proxy == nullptr) {
1189 NETMGR_LOG_E("proxy is nullptr");
1190 return NETMANAGER_ERR_GET_PROXY_FAIL;
1191 }
1192 return proxy->CloseSocketsUid(netId, uid);
1193 }
1194
GetSpecificNet(NetBearType bearerType,std::list<int32_t> & netIdList)1195 int32_t NetConnClient::GetSpecificNet(NetBearType bearerType, std::list<int32_t> &netIdList)
1196 {
1197 sptr<INetConnService> proxy= GetProxy();
1198 if (proxy == nullptr) {
1199 NETMGR_LOG_E("proxy is nullptr");
1200 return NETMANAGER_ERR_GET_PROXY_FAIL;
1201 }
1202 return proxy->GetSpecificNet(bearerType, netIdList);
1203 }
1204
GetSpecificNetByIdent(NetBearType bearerType,const std::string & ident,std::list<int32_t> & netIdList)1205 int32_t NetConnClient::GetSpecificNetByIdent(NetBearType bearerType, const std::string &ident,
1206 std::list<int32_t> &netIdList)
1207 {
1208 sptr<INetConnService> proxy = GetProxy();
1209 if (proxy == nullptr) {
1210 NETMGR_LOG_E("GetSpecificNetByIdent proxy is nullptr");
1211 return NETMANAGER_ERR_GET_PROXY_FAIL;
1212 }
1213 return proxy->GetSpecificNetByIdent(bearerType, ident, netIdList);
1214 }
1215
SetAppIsFrozened(uint32_t uid,bool isFrozened)1216 int32_t NetConnClient::SetAppIsFrozened(uint32_t uid, bool isFrozened)
1217 {
1218 sptr<INetConnService> proxy= GetProxy();
1219 if (proxy == nullptr) {
1220 NETMGR_LOG_E("proxy is nullptr");
1221 return NETMANAGER_ERR_GET_PROXY_FAIL;
1222 }
1223 return proxy->SetAppIsFrozened(uid, isFrozened);
1224 }
1225
EnableAppFrozenedCallbackLimitation(bool flag)1226 int32_t NetConnClient::EnableAppFrozenedCallbackLimitation(bool flag)
1227 {
1228 sptr<INetConnService> proxy= GetProxy();
1229 if (proxy == nullptr) {
1230 NETMGR_LOG_E("proxy is nullptr");
1231 return NETMANAGER_ERR_GET_PROXY_FAIL;
1232 }
1233 return proxy->EnableAppFrozenedCallbackLimitation(flag);
1234 }
1235
1236 } // namespace NetManagerStandard
1237 } // namespace OHOS
1238