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