1 /*
2 * Copyright (c) 2021-2023 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
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21
22 #include "fwmark_client.h"
23 #include "net_conn_service_proxy.h"
24 #include "net_manager_constants.h"
25 #include "net_mgr_log_wrapper.h"
26 #include "net_supplier_callback_stub.h"
27 #include "netsys_sock_client.h"
28 #include "network_security_config.h"
29
30 static constexpr const int32_t MIN_VALID_NETID = 100;
31 static constexpr uint32_t WAIT_FOR_SERVICE_TIME_MS = 500;
32 static constexpr uint32_t MAX_GET_SERVICE_COUNT = 10;
33
34 namespace OHOS {
35 namespace NetManagerStandard {
NetConnClient()36 NetConnClient::NetConnClient() : NetConnService_(nullptr), deathRecipient_(nullptr)
37 {
38 buffer_[RESERVED_BUFFER_SIZE-1] = '\0';
39 }
40
~NetConnClient()41 NetConnClient::~NetConnClient() {}
42
GetInstance()43 NetConnClient &NetConnClient::GetInstance()
44 {
45 static NetConnClient gInstance;
46 return gInstance;
47 }
48
SystemReady()49 int32_t NetConnClient::SystemReady()
50 {
51 sptr<INetConnService> proxy = GetProxy();
52 if (proxy == nullptr) {
53 NETMGR_LOG_E("proxy is nullptr");
54 return NETMANAGER_ERR_GET_PROXY_FAIL;
55 }
56 return proxy->SystemReady();
57 }
58
SetInternetPermission(uint32_t uid,uint8_t allow)59 int32_t NetConnClient::SetInternetPermission(uint32_t uid, uint8_t allow)
60 {
61 sptr<INetConnService> proxy = GetProxy();
62 if (proxy == nullptr) {
63 NETMGR_LOG_E("proxy is nullptr");
64 return NETMANAGER_ERR_GET_PROXY_FAIL;
65 }
66 return proxy->SetInternetPermission(uid, allow);
67 }
68
RegisterNetSupplier(NetBearType bearerType,const std::string & ident,const std::set<NetCap> & netCaps,uint32_t & supplierId)69 int32_t NetConnClient::RegisterNetSupplier(NetBearType bearerType, const std::string &ident,
70 const std::set<NetCap> &netCaps, uint32_t &supplierId)
71 {
72 NETMGR_LOG_D("RegisterNetSupplier client in.");
73 sptr<INetConnService> proxy = GetProxy();
74 if (proxy == nullptr) {
75 NETMGR_LOG_E("proxy is nullptr");
76 return NETMANAGER_ERR_GET_PROXY_FAIL;
77 }
78
79 return proxy->RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
80 }
81
UnregisterNetSupplier(uint32_t supplierId)82 int32_t NetConnClient::UnregisterNetSupplier(uint32_t supplierId)
83 {
84 NETMGR_LOG_D("UnregisterNetSupplier client in.");
85 sptr<INetConnService> proxy = GetProxy();
86 if (proxy == nullptr) {
87 NETMGR_LOG_E("proxy is nullptr");
88 return NETMANAGER_ERR_GET_PROXY_FAIL;
89 }
90 netSupplierCallback_.erase(supplierId);
91 return proxy->UnregisterNetSupplier(supplierId);
92 }
93
RegisterNetSupplierCallback(uint32_t supplierId,const sptr<NetSupplierCallbackBase> & callback)94 int32_t NetConnClient::RegisterNetSupplierCallback(uint32_t supplierId, const sptr<NetSupplierCallbackBase> &callback)
95 {
96 NETMGR_LOG_D("RegisterNetSupplierCallback client in.");
97 sptr<INetConnService> proxy = GetProxy();
98 if (proxy == nullptr) {
99 NETMGR_LOG_E("proxy is nullptr");
100 return NETMANAGER_ERR_GET_PROXY_FAIL;
101 }
102 sptr<NetSupplierCallbackStub> ptr = std::make_unique<NetSupplierCallbackStub>().release();
103 ptr->RegisterSupplierCallbackImpl(callback);
104 netSupplierCallback_[supplierId] = ptr;
105 return proxy->RegisterNetSupplierCallback(supplierId, ptr);
106 }
107
RegisterNetConnCallback(const sptr<INetConnCallback> & callback)108 int32_t NetConnClient::RegisterNetConnCallback(const sptr<INetConnCallback> &callback)
109 {
110 NETMGR_LOG_D("RegisterNetConnCallback client in.");
111 sptr<INetConnService> proxy = GetProxy();
112 if (proxy == nullptr) {
113 NETMGR_LOG_E("The parameter of proxy is nullptr");
114 return NETMANAGER_ERR_GET_PROXY_FAIL;
115 }
116 int32_t ret = proxy->RegisterNetConnCallback(callback);
117 if (ret == NETMANAGER_SUCCESS) {
118 NETMGR_LOG_D("RegisterNetConnCallback success, save callback.");
119 registerConnTupleList_.push_back(std::make_tuple(nullptr, callback, 0));
120 }
121
122 return ret;
123 }
124
RegisterNetConnCallback(const sptr<NetSpecifier> & netSpecifier,const sptr<INetConnCallback> & callback,const uint32_t & timeoutMS)125 int32_t NetConnClient::RegisterNetConnCallback(const sptr<NetSpecifier> &netSpecifier,
126 const sptr<INetConnCallback> &callback, const uint32_t &timeoutMS)
127 {
128 NETMGR_LOG_D("RegisterNetConnCallback with timeout client in.");
129 if (netSpecifier == nullptr || !netSpecifier->SpecifierIsValid()) {
130 NETMGR_LOG_E("The parameter of netSpecifier is invalid");
131 return NETMANAGER_ERR_PARAMETER_ERROR;
132 }
133 sptr<INetConnService> proxy = GetProxy();
134 if (proxy == nullptr) {
135 NETMGR_LOG_E("The parameter of proxy is nullptr");
136 return NETMANAGER_ERR_GET_PROXY_FAIL;
137 }
138 int32_t ret = proxy->RegisterNetConnCallback(netSpecifier, callback, timeoutMS);
139 if (ret == NETMANAGER_SUCCESS) {
140 NETMGR_LOG_D("RegisterNetConnCallback success, save netSpecifier and callback and timeoutMS.");
141 registerConnTupleList_.push_back(std::make_tuple(netSpecifier, callback, timeoutMS));
142 }
143
144 return ret;
145 }
146
UnregisterNetConnCallback(const sptr<INetConnCallback> & callback)147 int32_t NetConnClient::UnregisterNetConnCallback(const sptr<INetConnCallback> &callback)
148 {
149 NETMGR_LOG_D("UnregisterNetConnCallback client in.");
150 sptr<INetConnService> proxy = GetProxy();
151 if (proxy == nullptr) {
152 NETMGR_LOG_E("proxy is nullptr");
153 return NETMANAGER_ERR_GET_PROXY_FAIL;
154 }
155 int32_t ret = proxy->UnregisterNetConnCallback(callback);
156 if (ret == NETMANAGER_SUCCESS) {
157 NETMGR_LOG_D("UnregisterNetConnCallback success, delete callback.");
158 for (auto it = registerConnTupleList_.begin(); it != registerConnTupleList_.end(); ++it) {
159 if (std::get<1>(*it)->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) {
160 registerConnTupleList_.erase(it);
161 break;
162 }
163 }
164 }
165
166 return ret;
167 }
168
RegisterNetDetectionCallback(int32_t netId,const sptr<INetDetectionCallback> & callback)169 int32_t NetConnClient::RegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback)
170 {
171 NETMGR_LOG_I("RegisterNetDetectionCallback client in.");
172 sptr<INetConnService> proxy = GetProxy();
173 if (proxy == nullptr) {
174 NETMGR_LOG_E("proxy is nullptr");
175 return NETMANAGER_ERR_GET_PROXY_FAIL;
176 }
177
178 return proxy->RegisterNetDetectionCallback(netId, callback);
179 }
180
UnRegisterNetDetectionCallback(int32_t netId,const sptr<INetDetectionCallback> & callback)181 int32_t NetConnClient::UnRegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback)
182 {
183 NETMGR_LOG_I("UnRegisterNetDetectionCallback client in.");
184 sptr<INetConnService> proxy = GetProxy();
185 if (proxy == nullptr) {
186 NETMGR_LOG_E("proxy is nullptr");
187 return NETMANAGER_ERR_GET_PROXY_FAIL;
188 }
189
190 return proxy->UnRegisterNetDetectionCallback(netId, callback);
191 }
192
UpdateNetSupplierInfo(uint32_t supplierId,const sptr<NetSupplierInfo> & netSupplierInfo)193 int32_t NetConnClient::UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo)
194 {
195 NETMGR_LOG_I("UpdateNetSupplierInfo client in.");
196 sptr<INetConnService> proxy = GetProxy();
197 if (proxy == nullptr) {
198 NETMGR_LOG_E("proxy is nullptr");
199 return NETMANAGER_ERR_GET_PROXY_FAIL;
200 }
201
202 return proxy->UpdateNetSupplierInfo(supplierId, netSupplierInfo);
203 }
204
UpdateNetLinkInfo(uint32_t supplierId,const sptr<NetLinkInfo> & netLinkInfo)205 int32_t NetConnClient::UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo)
206 {
207 NETMGR_LOG_I("UpdateNetLinkInfo client in.");
208 sptr<INetConnService> proxy = GetProxy();
209 if (proxy == nullptr) {
210 NETMGR_LOG_E("proxy is nullptr");
211 return NETMANAGER_ERR_GET_PROXY_FAIL;
212 }
213
214 return proxy->UpdateNetLinkInfo(supplierId, netLinkInfo);
215 }
216
GetDefaultNet(NetHandle & netHandle)217 int32_t NetConnClient::GetDefaultNet(NetHandle &netHandle)
218 {
219 NETMGR_LOG_D("GetDefaultNet client in.");
220 sptr<INetConnService> proxy = GetProxy();
221 if (proxy == nullptr) {
222 NETMGR_LOG_E("proxy is nullptr");
223 return NETMANAGER_ERR_GET_PROXY_FAIL;
224 }
225
226 int32_t netId = 0;
227 int32_t result = proxy->GetDefaultNet(netId);
228 if (result != NETMANAGER_SUCCESS) {
229 NETMGR_LOG_D("fail to get default net.");
230 return result;
231 }
232 netHandle.SetNetId(netId);
233 NETMGR_LOG_D("GetDefaultNet client out.");
234 return NETMANAGER_SUCCESS;
235 }
236
HasDefaultNet(bool & flag)237 int32_t NetConnClient::HasDefaultNet(bool &flag)
238 {
239 NETMGR_LOG_D("HasDefaultNet client in.");
240 sptr<INetConnService> proxy = GetProxy();
241 if (proxy == nullptr) {
242 NETMGR_LOG_E("proxy is nullptr");
243 return NETMANAGER_ERR_GET_PROXY_FAIL;
244 }
245 return proxy->HasDefaultNet(flag);
246 }
247
GetAllNets(std::list<sptr<NetHandle>> & netList)248 int32_t NetConnClient::GetAllNets(std::list<sptr<NetHandle>> &netList)
249 {
250 sptr<INetConnService> proxy = GetProxy();
251 if (proxy == nullptr) {
252 NETMGR_LOG_E("proxy is nullptr");
253 return NETMANAGER_ERR_GET_PROXY_FAIL;
254 }
255
256 std::list<int32_t> netIdList;
257 int32_t result = proxy->GetAllNets(netIdList);
258 if (result != NETMANAGER_SUCCESS) {
259 return result;
260 }
261 std::list<int32_t>::iterator iter;
262 for (iter = netIdList.begin(); iter != netIdList.end(); ++iter) {
263 sptr<NetHandle> netHandle = std::make_unique<NetHandle>(*iter).release();
264 if (netHandle != nullptr) {
265 netList.push_back(netHandle);
266 }
267 }
268 return NETMANAGER_SUCCESS;
269 }
270
GetConnectionProperties(const NetHandle & netHandle,NetLinkInfo & info)271 int32_t NetConnClient::GetConnectionProperties(const NetHandle &netHandle, NetLinkInfo &info)
272 {
273 sptr<INetConnService> proxy = GetProxy();
274 if (proxy == nullptr) {
275 NETMGR_LOG_E("proxy is nullptr");
276 return NETMANAGER_ERR_GET_PROXY_FAIL;
277 }
278
279 return proxy->GetConnectionProperties(netHandle.GetNetId(), info);
280 }
281
GetNetCapabilities(const NetHandle & netHandle,NetAllCapabilities & netAllCap)282 int32_t NetConnClient::GetNetCapabilities(const NetHandle &netHandle, NetAllCapabilities &netAllCap)
283 {
284 sptr<INetConnService> proxy = GetProxy();
285 if (proxy == nullptr) {
286 NETMGR_LOG_E("proxy is nullptr");
287 return NETMANAGER_ERR_GET_PROXY_FAIL;
288 }
289
290 return proxy->GetNetCapabilities(netHandle.GetNetId(), netAllCap);
291 }
292
GetAddressesByName(const std::string & host,int32_t netId,std::vector<INetAddr> & addrList)293 int32_t NetConnClient::GetAddressesByName(const std::string &host, int32_t netId, std::vector<INetAddr> &addrList)
294 {
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->GetAddressesByName(host, netId, addrList);
302 }
303
GetAddressByName(const std::string & host,int32_t netId,INetAddr & addr)304 int32_t NetConnClient::GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr)
305 {
306 sptr<INetConnService> proxy = GetProxy();
307 if (proxy == nullptr) {
308 NETMGR_LOG_E("proxy is nullptr");
309 return NETMANAGER_ERR_GET_PROXY_FAIL;
310 }
311
312 return proxy->GetAddressByName(host, netId, addr);
313 }
314
BindSocket(int32_t socketFd,int32_t netId)315 int32_t NetConnClient::BindSocket(int32_t socketFd, int32_t netId)
316 {
317 if (netId < MIN_VALID_NETID) {
318 NETMGR_LOG_E("netId is invalid.");
319 return NET_CONN_ERR_INVALID_NETWORK;
320 }
321 std::shared_ptr<nmd::FwmarkClient> fwmarkClient_ = std::make_shared<nmd::FwmarkClient>();
322 if (fwmarkClient_ == nullptr) {
323 NETMGR_LOG_E("fwmarkClient_ is nullptr");
324 return NETMANAGER_ERR_PARAMETER_ERROR;
325 }
326 fwmarkClient_->BindSocket(socketFd, netId);
327 return NETMANAGER_SUCCESS;
328 }
329
NetDetection(const NetHandle & netHandle)330 int32_t NetConnClient::NetDetection(const NetHandle &netHandle)
331 {
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
338 return proxy->NetDetection(netHandle.GetNetId());
339 }
340
GetProxy()341 sptr<INetConnService> NetConnClient::GetProxy()
342 {
343 std::lock_guard lock(mutex_);
344
345 if (NetConnService_) {
346 NETMGR_LOG_D("get proxy is ok");
347 return NetConnService_;
348 }
349
350 NETMGR_LOG_D("execute GetSystemAbilityManager");
351 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
352 if (sam == nullptr) {
353 NETMGR_LOG_E("GetProxy(), get SystemAbilityManager failed");
354 return nullptr;
355 }
356
357 sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
358 if (remote == nullptr) {
359 NETMGR_LOG_E("get Remote service failed");
360 return nullptr;
361 }
362
363 deathRecipient_ = new (std::nothrow) NetConnDeathRecipient(*this);
364 if (deathRecipient_ == nullptr) {
365 NETMGR_LOG_E("get deathRecipient_ failed");
366 return nullptr;
367 }
368 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
369 NETMGR_LOG_E("add death recipient failed");
370 return nullptr;
371 }
372
373 NetConnService_ = iface_cast<INetConnService>(remote);
374 if (NetConnService_ == nullptr) {
375 NETMGR_LOG_E("get Remote service proxy failed");
376 return nullptr;
377 }
378
379 return NetConnService_;
380 }
381
SetAirplaneMode(bool state)382 int32_t NetConnClient::SetAirplaneMode(bool state)
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->SetAirplaneMode(state);
391 }
392
RecoverCallback()393 void NetConnClient::RecoverCallback()
394 {
395 uint32_t count = 0;
396 while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
397 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_SERVICE_TIME_MS));
398 count++;
399 }
400 auto proxy = GetProxy();
401 NETMGR_LOG_W("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
402 if (proxy != nullptr && !registerConnTupleList_.empty()) {
403 for (auto mem : registerConnTupleList_) {
404 sptr<NetSpecifier> specifier = std::get<0>(mem);
405 sptr<INetConnCallback> callback = std::get<1>(mem);
406 uint32_t timeoutMS = std::get<2>(mem);
407 if (specifier != nullptr && timeoutMS != 0) {
408 int32_t ret = proxy->RegisterNetConnCallback(specifier, callback, timeoutMS);
409 NETMGR_LOG_D("Register result hasNetSpecifier_ and timeoutMS_ %{public}d", ret);
410 } else if (specifier != nullptr) {
411 int32_t ret = proxy->RegisterNetConnCallback(specifier, callback, 0);
412 NETMGR_LOG_D("Register result hasNetSpecifier_ %{public}d", ret);
413 } else if (callback != nullptr) {
414 int32_t ret = proxy->RegisterNetConnCallback(callback);
415 NETMGR_LOG_D("Register result %{public}d", ret);
416 }
417 }
418 }
419 }
420
OnRemoteDied(const wptr<IRemoteObject> & remote)421 void NetConnClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
422 {
423 NETMGR_LOG_D("on remote died");
424 if (remote == nullptr) {
425 NETMGR_LOG_E("remote object is nullptr");
426 return;
427 }
428
429 std::lock_guard lock(mutex_);
430 if (NetConnService_ == nullptr) {
431 NETMGR_LOG_E("NetConnService_ is nullptr");
432 return;
433 }
434
435 sptr<IRemoteObject> local = NetConnService_->AsObject();
436 if (local != remote.promote()) {
437 NETMGR_LOG_E("proxy and stub is not same remote object");
438 return;
439 }
440
441 local->RemoveDeathRecipient(deathRecipient_);
442 NetConnService_ = nullptr;
443
444 if (!registerConnTupleList_.empty()) {
445 NETMGR_LOG_I("on remote died recover callback");
446 std::thread t([this]() {
447 RecoverCallback();
448 });
449 std::string threadName = "netconnRecoverCallback";
450 pthread_setname_np(t.native_handle(), threadName.c_str());
451 t.detach();
452 }
453 }
454
IsDefaultNetMetered(bool & isMetered)455 int32_t NetConnClient::IsDefaultNetMetered(bool &isMetered)
456 {
457 sptr<INetConnService> proxy = GetProxy();
458 if (proxy == nullptr) {
459 NETMGR_LOG_E("proxy is nullptr");
460 return NETMANAGER_ERR_GET_PROXY_FAIL;
461 }
462 return proxy->IsDefaultNetMetered(isMetered);
463 }
464
SetGlobalHttpProxy(const HttpProxy & httpProxy)465 int32_t NetConnClient::SetGlobalHttpProxy(const HttpProxy &httpProxy)
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 return proxy->SetGlobalHttpProxy(httpProxy);
473 }
474
RegisterAppHttpProxyCallback(std::function<void (const HttpProxy & httpProxy)> callback,uint32_t & callbackid)475 void NetConnClient::RegisterAppHttpProxyCallback(std::function<void(const HttpProxy &httpProxy)> callback,
476 uint32_t &callbackid)
477 {
478 uint32_t id = currentCallbackId_;
479 std::lock_guard<std::mutex> lock(appHttpProxyCbMapMutex_);
480
481 currentCallbackId_++;
482 appHttpProxyCbMap_[id] = callback;
483 callbackid = id;
484 NETMGR_LOG_I("registerCallback id:%{public}d.", id);
485 }
486
UnregisterAppHttpProxyCallback(uint32_t callbackid)487 void NetConnClient::UnregisterAppHttpProxyCallback(uint32_t callbackid)
488 {
489 NETMGR_LOG_I("unregisterCallback callbackid:%{public}d.", callbackid);
490 std::lock_guard<std::mutex> lock(appHttpProxyCbMapMutex_);
491 appHttpProxyCbMap_.erase(callbackid);
492 }
493
SetAppHttpProxy(const HttpProxy & httpProxy)494 int32_t NetConnClient::SetAppHttpProxy(const HttpProxy &httpProxy)
495 {
496 NETMGR_LOG_I("AppHttpProxy:%{public}s:%{public}d",
497 httpProxy.GetHost().empty() ? "" : httpProxy.GetHost().c_str(),
498 httpProxy.GetPort());
499
500 if (appHttpProxy_ != httpProxy) {
501 appHttpProxy_ = httpProxy;
502 std::lock_guard<std::mutex> lock(appHttpProxyCbMapMutex_);
503 for (const auto &pair : appHttpProxyCbMap_) {
504 pair.second(httpProxy);
505 }
506 }
507
508 return NETMANAGER_SUCCESS;
509 }
510
GetGlobalHttpProxy(HttpProxy & httpProxy)511 int32_t NetConnClient::GetGlobalHttpProxy(HttpProxy &httpProxy)
512 {
513 sptr<INetConnService> proxy = GetProxy();
514 if (proxy == nullptr) {
515 NETMGR_LOG_E("proxy is nullptr");
516 return NETMANAGER_ERR_GET_PROXY_FAIL;
517 }
518 return proxy->GetGlobalHttpProxy(httpProxy);
519 }
520
GetDefaultHttpProxy(HttpProxy & httpProxy)521 int32_t NetConnClient::GetDefaultHttpProxy(HttpProxy &httpProxy)
522 {
523 if (!appHttpProxy_.GetHost().empty()) {
524 httpProxy = appHttpProxy_;
525 NETMGR_LOG_D("Return AppHttpProxy:%{public}s:%{public}d",
526 httpProxy.GetHost().c_str(), httpProxy.GetPort());
527 return NETMANAGER_SUCCESS;
528 }
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 int32_t bindNetId = 0;
536 GetAppNet(bindNetId);
537 return proxy->GetDefaultHttpProxy(bindNetId, httpProxy);
538 }
539
GetNetIdByIdentifier(const std::string & ident,std::list<int32_t> & netIdList)540 int32_t NetConnClient::GetNetIdByIdentifier(const std::string &ident, std::list<int32_t> &netIdList)
541 {
542 sptr<INetConnService> proxy = GetProxy();
543 if (proxy == nullptr) {
544 NETMGR_LOG_E("proxy is nullptr");
545 return NETMANAGER_ERR_GET_PROXY_FAIL;
546 }
547 return proxy->GetNetIdByIdentifier(ident, netIdList);
548 }
549
SetAppNet(int32_t netId)550 int32_t NetConnClient::SetAppNet(int32_t netId)
551 {
552 if (netId < MIN_VALID_NETID && netId != 0) {
553 return NET_CONN_ERR_INVALID_NETWORK;
554 }
555 sptr<INetConnService> proxy = GetProxy();
556 if (proxy == nullptr) {
557 NETMGR_LOG_E("proxy is nullptr");
558 return NETMANAGER_ERR_GET_PROXY_FAIL;
559 }
560 int32_t ret = proxy->SetAppNet(netId);
561 if (ret != NETMANAGER_SUCCESS) {
562 return ret;
563 }
564
565 SetNetForApp(netId);
566 return NETMANAGER_SUCCESS;
567 }
568
GetAppNet(int32_t & netId)569 int32_t NetConnClient::GetAppNet(int32_t &netId)
570 {
571 netId = GetNetForApp();
572 return NETMANAGER_SUCCESS;
573 }
574
RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> & callback)575 int32_t NetConnClient::RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback)
576 {
577 sptr<INetConnService> proxy = GetProxy();
578 if (proxy == nullptr) {
579 NETMGR_LOG_E("proxy is nullptr");
580 return NETMANAGER_ERR_GET_PROXY_FAIL;
581 }
582 return proxy->RegisterNetInterfaceCallback(callback);
583 }
584
GetNetInterfaceConfiguration(const std::string & iface,NetInterfaceConfiguration & config)585 int32_t NetConnClient::GetNetInterfaceConfiguration(const std::string &iface, NetInterfaceConfiguration &config)
586 {
587 sptr<INetConnService> proxy = GetProxy();
588 if (proxy == nullptr) {
589 NETMGR_LOG_E("proxy is nullptr");
590 return NETMANAGER_ERR_GET_PROXY_FAIL;
591 }
592 return proxy->GetNetInterfaceConfiguration(iface, config);
593 }
594
AddNetworkRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)595 int32_t NetConnClient::AddNetworkRoute(int32_t netId, const std::string &ifName,
596 const std::string &destination, const std::string &nextHop)
597 {
598 NETMGR_LOG_I("AddNetworkRoute client in.");
599 sptr<INetConnService> proxy = GetProxy();
600 if (proxy == nullptr) {
601 NETMGR_LOG_E("proxy is nullptr");
602 return NETMANAGER_ERR_GET_PROXY_FAIL;
603 }
604
605 return proxy->AddNetworkRoute(netId, ifName, destination, nextHop);
606 }
607
RemoveNetworkRoute(int32_t netId,const std::string & ifName,const std::string & destination,const std::string & nextHop)608 int32_t NetConnClient::RemoveNetworkRoute(int32_t netId, const std::string &ifName,
609 const std::string &destination, const std::string &nextHop)
610 {
611 NETMGR_LOG_I("RemoveNetworkRoute client in.");
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
618 return proxy->RemoveNetworkRoute(netId, ifName, destination, nextHop);
619 }
620
AddInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)621 int32_t NetConnClient::AddInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
622 int32_t prefixLength)
623 {
624 NETMGR_LOG_I("AddInterfaceAddress client in.");
625 sptr<INetConnService> proxy = GetProxy();
626 if (proxy == nullptr) {
627 NETMGR_LOG_E("proxy is nullptr");
628 return NETMANAGER_ERR_GET_PROXY_FAIL;
629 }
630
631 return proxy->AddInterfaceAddress(ifName, ipAddr, prefixLength);
632 }
633
DelInterfaceAddress(const std::string & ifName,const std::string & ipAddr,int32_t prefixLength)634 int32_t NetConnClient::DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
635 int32_t prefixLength)
636 {
637 NETMGR_LOG_I("DelInterfaceAddress client in.");
638 sptr<INetConnService> proxy = GetProxy();
639 if (proxy == nullptr) {
640 NETMGR_LOG_E("proxy is nullptr");
641 return NETMANAGER_ERR_GET_PROXY_FAIL;
642 }
643
644 return proxy->DelInterfaceAddress(ifName, ipAddr, prefixLength);
645 }
646
AddStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)647 int32_t NetConnClient::AddStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName)
648 {
649 NETMGR_LOG_I("AddStaticArp client in.");
650 sptr<INetConnService> proxy = GetProxy();
651 if (proxy == nullptr) {
652 NETMGR_LOG_E("proxy is nullptr");
653 return NETMANAGER_ERR_GET_PROXY_FAIL;
654 }
655
656 return proxy->AddStaticArp(ipAddr, macAddr, ifName);
657 }
658
DelStaticArp(const std::string & ipAddr,const std::string & macAddr,const std::string & ifName)659 int32_t NetConnClient::DelStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName)
660 {
661 NETMGR_LOG_I("DelStaticArp client in.");
662 sptr<INetConnService> proxy = GetProxy();
663 if (proxy == nullptr) {
664 NETMGR_LOG_E("proxy is nullptr");
665 return NETMANAGER_ERR_GET_PROXY_FAIL;
666 }
667
668 return proxy->DelStaticArp(ipAddr, macAddr, ifName);
669 }
670
RegisterSlotType(uint32_t supplierId,int32_t type)671 int32_t NetConnClient::RegisterSlotType(uint32_t supplierId, int32_t type)
672 {
673 NETMGR_LOG_I("RegisterSlotType client in.supplierId[%{public}d] type[%{public}d]", supplierId, type);
674 sptr<INetConnService> proxy = GetProxy();
675 if (proxy == nullptr) {
676 NETMGR_LOG_E("proxy is nullptr");
677 return NETMANAGER_ERR_GET_PROXY_FAIL;
678 }
679
680 return proxy->RegisterSlotType(supplierId, type);
681 }
682
GetSlotType(std::string & type)683 int32_t NetConnClient::GetSlotType(std::string &type)
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
691 return proxy->GetSlotType(type);
692 }
693
GetPinSetForHostName(const std::string & hostname,std::string & pins)694 int32_t NetConnClient::GetPinSetForHostName(const std::string &hostname, std::string &pins)
695 {
696 return NetworkSecurityConfig::GetInstance().GetPinSetForHostName(hostname, pins);
697 }
698
GetTrustAnchorsForHostName(const std::string & hostname,std::vector<std::string> & certs)699 int32_t NetConnClient::GetTrustAnchorsForHostName(const std::string &hostname, std::vector<std::string> &certs)
700 {
701 return NetworkSecurityConfig::GetInstance().GetTrustAnchorsForHostName(hostname, certs);
702 }
703
FactoryResetNetwork()704 int32_t NetConnClient::FactoryResetNetwork()
705 {
706 sptr<INetConnService> proxy = GetProxy();
707 if (proxy == nullptr) {
708 NETMGR_LOG_E("proxy is nullptr");
709 return NETMANAGER_ERR_GET_PROXY_FAIL;
710 }
711
712 return proxy->FactoryResetNetwork();
713 }
714
RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> & callback)715 int32_t NetConnClient::RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> &callback)
716 {
717 sptr<INetConnService> proxy = GetProxy();
718 if (proxy == nullptr) {
719 NETMGR_LOG_E("proxy is nullptr");
720 return NETMANAGER_ERR_GET_PROXY_FAIL;
721 }
722 return proxy->RegisterNetFactoryResetCallback(callback);
723 }
724
IsPreferCellularUrl(const std::string & url,bool & preferCellular)725 int32_t NetConnClient::IsPreferCellularUrl(const std::string& url, bool& preferCellular)
726 {
727 sptr<INetConnService> proxy = GetProxy();
728 if (proxy == nullptr) {
729 NETMGR_LOG_E("proxy is nullptr");
730 return NETMANAGER_ERR_GET_PROXY_FAIL;
731 }
732 return proxy->IsPreferCellularUrl(url, preferCellular);
733 }
734 } // namespace NetManagerStandard
735 } // namespace OHOS
736