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