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
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20
21 #include "fwmark_client.h"
22 #include "net_conn_service_proxy.h"
23 #include "net_manager_constants.h"
24 #include "net_mgr_log_wrapper.h"
25 #include "net_supplier_callback_stub.h"
26 #include "netsys_sock_client.h"
27
28 static constexpr const int32_t MIN_VALID_NETID = 100;
29
30 namespace OHOS {
31 namespace NetManagerStandard {
NetConnClient()32 NetConnClient::NetConnClient() : NetConnService_(nullptr), deathRecipient_(nullptr) {}
33
~NetConnClient()34 NetConnClient::~NetConnClient() {}
35
GetInstance()36 NetConnClient &NetConnClient::GetInstance()
37 {
38 static NetConnClient gInstance;
39 return gInstance;
40 }
41
SystemReady()42 int32_t NetConnClient::SystemReady()
43 {
44 sptr<INetConnService> proxy = GetProxy();
45 if (proxy == nullptr) {
46 NETMGR_LOG_E("proxy is nullptr");
47 return NETMANAGER_ERR_GET_PROXY_FAIL;
48 }
49 return proxy->SystemReady();
50 }
51
SetInternetPermission(uint32_t uid,uint8_t allow)52 int32_t NetConnClient::SetInternetPermission(uint32_t uid, uint8_t allow)
53 {
54 sptr<INetConnService> proxy = GetProxy();
55 if (proxy == nullptr) {
56 NETMGR_LOG_E("proxy is nullptr");
57 return NETMANAGER_ERR_GET_PROXY_FAIL;
58 }
59
60 return proxy->SetInternetPermission(uid, allow);
61 }
62
RegisterNetSupplier(NetBearType bearerType,const std::string & ident,const std::set<NetCap> & netCaps,uint32_t & supplierId)63 int32_t NetConnClient::RegisterNetSupplier(NetBearType bearerType, const std::string &ident,
64 const std::set<NetCap> &netCaps, uint32_t &supplierId)
65 {
66 NETMGR_LOG_D("RegisterNetSupplier client in.");
67 sptr<INetConnService> proxy = GetProxy();
68 if (proxy == nullptr) {
69 NETMGR_LOG_E("proxy is nullptr");
70 return NETMANAGER_ERR_GET_PROXY_FAIL;
71 }
72
73 return proxy->RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
74 }
75
UnregisterNetSupplier(uint32_t supplierId)76 int32_t NetConnClient::UnregisterNetSupplier(uint32_t supplierId)
77 {
78 NETMGR_LOG_D("UnregisterNetSupplier client in.");
79 sptr<INetConnService> proxy = GetProxy();
80 if (proxy == nullptr) {
81 NETMGR_LOG_E("proxy is nullptr");
82 return NETMANAGER_ERR_GET_PROXY_FAIL;
83 }
84 netSupplierCallback_.erase(supplierId);
85 return proxy->UnregisterNetSupplier(supplierId);
86 }
87
RegisterNetSupplierCallback(uint32_t supplierId,const sptr<NetSupplierCallbackBase> & callback)88 int32_t NetConnClient::RegisterNetSupplierCallback(uint32_t supplierId, const sptr<NetSupplierCallbackBase> &callback)
89 {
90 NETMGR_LOG_D("RegisterNetSupplierCallback client in.");
91 sptr<INetConnService> proxy = GetProxy();
92 if (proxy == nullptr) {
93 NETMGR_LOG_E("proxy is nullptr");
94 return NETMANAGER_ERR_GET_PROXY_FAIL;
95 }
96 sptr<NetSupplierCallbackStub> ptr = std::make_unique<NetSupplierCallbackStub>().release();
97 ptr->RegisterSupplierCallbackImpl(callback);
98 netSupplierCallback_[supplierId] = ptr;
99 return proxy->RegisterNetSupplierCallback(supplierId, ptr);
100 }
101
RegisterNetConnCallback(const sptr<INetConnCallback> & callback)102 int32_t NetConnClient::RegisterNetConnCallback(const sptr<INetConnCallback> &callback)
103 {
104 NETMGR_LOG_D("RegisterNetConnCallback client in.");
105 sptr<INetConnService> proxy = GetProxy();
106 if (proxy == nullptr) {
107 NETMGR_LOG_E("The parameter of proxy is nullptr");
108 return NETMANAGER_ERR_GET_PROXY_FAIL;
109 }
110
111 return proxy->RegisterNetConnCallback(callback);
112 }
113
RegisterNetConnCallback(const sptr<NetSpecifier> & netSpecifier,const sptr<INetConnCallback> & callback,const uint32_t & timeoutMS)114 int32_t NetConnClient::RegisterNetConnCallback(const sptr<NetSpecifier> &netSpecifier,
115 const sptr<INetConnCallback> &callback, const uint32_t &timeoutMS)
116 {
117 NETMGR_LOG_D("RegisterNetConnCallback with timeout client in.");
118 if (netSpecifier == nullptr || !netSpecifier->SpecifierIsValid()) {
119 NETMGR_LOG_E("The parameter of netSpecifier is invalid");
120 return NETMANAGER_ERR_PARAMETER_ERROR;
121 }
122 sptr<INetConnService> proxy = GetProxy();
123 if (proxy == nullptr) {
124 NETMGR_LOG_E("The parameter of proxy is nullptr");
125 return NETMANAGER_ERR_GET_PROXY_FAIL;
126 }
127
128 return proxy->RegisterNetConnCallback(netSpecifier, callback, timeoutMS);
129 }
130
UnregisterNetConnCallback(const sptr<INetConnCallback> & callback)131 int32_t NetConnClient::UnregisterNetConnCallback(const sptr<INetConnCallback> &callback)
132 {
133 NETMGR_LOG_D("UnregisterNetConnCallback client in.");
134 sptr<INetConnService> proxy = GetProxy();
135 if (proxy == nullptr) {
136 NETMGR_LOG_E("proxy is nullptr");
137 return NETMANAGER_ERR_GET_PROXY_FAIL;
138 }
139
140 return proxy->UnregisterNetConnCallback(callback);
141 }
142
UpdateNetSupplierInfo(uint32_t supplierId,const sptr<NetSupplierInfo> & netSupplierInfo)143 int32_t NetConnClient::UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo)
144 {
145 NETMGR_LOG_D("UpdateNetSupplierInfo client in.");
146 sptr<INetConnService> proxy = GetProxy();
147 if (proxy == nullptr) {
148 NETMGR_LOG_E("proxy is nullptr");
149 return NETMANAGER_ERR_GET_PROXY_FAIL;
150 }
151
152 return proxy->UpdateNetSupplierInfo(supplierId, netSupplierInfo);
153 }
154
UpdateNetLinkInfo(uint32_t supplierId,const sptr<NetLinkInfo> & netLinkInfo)155 int32_t NetConnClient::UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo)
156 {
157 NETMGR_LOG_D("UpdateNetLinkInfo client in.");
158 sptr<INetConnService> proxy = GetProxy();
159 if (proxy == nullptr) {
160 NETMGR_LOG_E("proxy is nullptr");
161 return NETMANAGER_ERR_GET_PROXY_FAIL;
162 }
163
164 return proxy->UpdateNetLinkInfo(supplierId, netLinkInfo);
165 }
166
GetDefaultNet(NetHandle & netHandle)167 int32_t NetConnClient::GetDefaultNet(NetHandle &netHandle)
168 {
169 NETMGR_LOG_D("GetDefaultNet client in.");
170 sptr<INetConnService> proxy = GetProxy();
171 if (proxy == nullptr) {
172 NETMGR_LOG_E("proxy is nullptr");
173 return NETMANAGER_ERR_GET_PROXY_FAIL;
174 }
175
176 int32_t netId = 0;
177 int32_t result = proxy->GetDefaultNet(netId);
178 if (result != NETMANAGER_SUCCESS) {
179 NETMGR_LOG_D("fail to get default net.");
180 return result;
181 }
182 netHandle.SetNetId(netId);
183 NETMGR_LOG_D("GetDefaultNet client out.");
184 return NETMANAGER_SUCCESS;
185 }
186
HasDefaultNet(bool & flag)187 int32_t NetConnClient::HasDefaultNet(bool &flag)
188 {
189 NETMGR_LOG_D("HasDefaultNet client in.");
190 sptr<INetConnService> proxy = GetProxy();
191 if (proxy == nullptr) {
192 NETMGR_LOG_E("proxy is nullptr");
193 return NETMANAGER_ERR_GET_PROXY_FAIL;
194 }
195 return proxy->HasDefaultNet(flag);
196 }
197
GetAllNets(std::list<sptr<NetHandle>> & netList)198 int32_t NetConnClient::GetAllNets(std::list<sptr<NetHandle>> &netList)
199 {
200 sptr<INetConnService> proxy = GetProxy();
201 if (proxy == nullptr) {
202 NETMGR_LOG_E("proxy is nullptr");
203 return NETMANAGER_ERR_GET_PROXY_FAIL;
204 }
205
206 std::list<int32_t> netIdList;
207 int32_t result = proxy->GetAllNets(netIdList);
208 if (result != NETMANAGER_SUCCESS) {
209 return result;
210 }
211 std::list<int32_t>::iterator iter;
212 for (iter = netIdList.begin(); iter != netIdList.end(); ++iter) {
213 sptr<NetHandle> netHandle = std::make_unique<NetHandle>(*iter).release();
214 if (netHandle != nullptr) {
215 netList.push_back(netHandle);
216 }
217 }
218 return NETMANAGER_SUCCESS;
219 }
220
GetConnectionProperties(const NetHandle & netHandle,NetLinkInfo & info)221 int32_t NetConnClient::GetConnectionProperties(const NetHandle &netHandle, NetLinkInfo &info)
222 {
223 sptr<INetConnService> proxy = GetProxy();
224 if (proxy == nullptr) {
225 NETMGR_LOG_E("proxy is nullptr");
226 return NETMANAGER_ERR_GET_PROXY_FAIL;
227 }
228
229 return proxy->GetConnectionProperties(netHandle.GetNetId(), info);
230 }
231
GetNetCapabilities(const NetHandle & netHandle,NetAllCapabilities & netAllCap)232 int32_t NetConnClient::GetNetCapabilities(const NetHandle &netHandle, NetAllCapabilities &netAllCap)
233 {
234 sptr<INetConnService> proxy = GetProxy();
235 if (proxy == nullptr) {
236 NETMGR_LOG_E("proxy is nullptr");
237 return NETMANAGER_ERR_GET_PROXY_FAIL;
238 }
239
240 return proxy->GetNetCapabilities(netHandle.GetNetId(), netAllCap);
241 }
242
GetAddressesByName(const std::string & host,int32_t netId,std::vector<INetAddr> & addrList)243 int32_t NetConnClient::GetAddressesByName(const std::string &host, int32_t netId, std::vector<INetAddr> &addrList)
244 {
245 sptr<INetConnService> proxy = GetProxy();
246 if (proxy == nullptr) {
247 NETMGR_LOG_E("proxy is nullptr");
248 return NETMANAGER_ERR_GET_PROXY_FAIL;
249 }
250
251 return proxy->GetAddressesByName(host, netId, addrList);
252 }
253
GetAddressByName(const std::string & host,int32_t netId,INetAddr & addr)254 int32_t NetConnClient::GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr)
255 {
256 sptr<INetConnService> proxy = GetProxy();
257 if (proxy == nullptr) {
258 NETMGR_LOG_E("proxy is nullptr");
259 return NETMANAGER_ERR_GET_PROXY_FAIL;
260 }
261
262 return proxy->GetAddressByName(host, netId, addr);
263 }
264
BindSocket(int32_t socket_fd,int32_t netId)265 int32_t NetConnClient::BindSocket(int32_t socket_fd, int32_t netId)
266 {
267 if (netId < MIN_VALID_NETID) {
268 return NET_CONN_ERR_INVALID_NETWORK;
269 }
270 std::shared_ptr<nmd::FwmarkClient> fwmarkClient_ = std::make_shared<nmd::FwmarkClient>();
271 if (fwmarkClient_ == nullptr) {
272 NETMGR_LOG_E("fwmarkClient_ is nullptr");
273 return NETMANAGER_ERR_PARAMETER_ERROR;
274 }
275 fwmarkClient_->BindSocket(socket_fd, netId);
276 return NETMANAGER_SUCCESS;
277 }
278
NetDetection(const NetHandle & netHandle)279 int32_t NetConnClient::NetDetection(const NetHandle &netHandle)
280 {
281 sptr<INetConnService> proxy = GetProxy();
282 if (proxy == nullptr) {
283 NETMGR_LOG_E("proxy is nullptr");
284 return NETMANAGER_ERR_GET_PROXY_FAIL;
285 }
286
287 return proxy->NetDetection(netHandle.GetNetId());
288 }
289
GetProxy()290 sptr<INetConnService> NetConnClient::GetProxy()
291 {
292 std::lock_guard lock(mutex_);
293
294 if (NetConnService_) {
295 NETMGR_LOG_D("get proxy is ok");
296 return NetConnService_;
297 }
298
299 NETMGR_LOG_D("execute GetSystemAbilityManager");
300 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
301 if (sam == nullptr) {
302 NETMGR_LOG_E("GetProxy(), get SystemAbilityManager failed");
303 return nullptr;
304 }
305
306 sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
307 if (remote == nullptr) {
308 NETMGR_LOG_E("get Remote service failed");
309 return nullptr;
310 }
311
312 deathRecipient_ = (std::make_unique<NetConnDeathRecipient>(*this)).release();
313 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
314 NETMGR_LOG_E("add death recipient failed");
315 return nullptr;
316 }
317
318 NetConnService_ = iface_cast<INetConnService>(remote);
319 if (NetConnService_ == nullptr) {
320 NETMGR_LOG_E("get Remote service proxy failed");
321 return nullptr;
322 }
323
324 return NetConnService_;
325 }
326
SetAirplaneMode(bool state)327 int32_t NetConnClient::SetAirplaneMode(bool state)
328 {
329 sptr<INetConnService> proxy = GetProxy();
330 if (proxy == nullptr) {
331 NETMGR_LOG_E("proxy is nullptr");
332 return NETMANAGER_ERR_GET_PROXY_FAIL;
333 }
334
335 return proxy->SetAirplaneMode(state);
336 }
337
OnRemoteDied(const wptr<IRemoteObject> & remote)338 void NetConnClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
339 {
340 NETMGR_LOG_D("on remote died");
341 if (remote == nullptr) {
342 NETMGR_LOG_E("remote object is nullptr");
343 return;
344 }
345
346 std::lock_guard lock(mutex_);
347 if (NetConnService_ == nullptr) {
348 NETMGR_LOG_E("NetConnService_ is nullptr");
349 return;
350 }
351
352 sptr<IRemoteObject> local = NetConnService_->AsObject();
353 if (local != remote.promote()) {
354 NETMGR_LOG_E("proxy and stub is not same remote object");
355 return;
356 }
357
358 local->RemoveDeathRecipient(deathRecipient_);
359 NetConnService_ = nullptr;
360 }
361
IsDefaultNetMetered(bool & isMetered)362 int32_t NetConnClient::IsDefaultNetMetered(bool &isMetered)
363 {
364 sptr<INetConnService> proxy = GetProxy();
365 if (proxy == nullptr) {
366 NETMGR_LOG_E("proxy is nullptr");
367 return NETMANAGER_ERR_GET_PROXY_FAIL;
368 }
369 return proxy->IsDefaultNetMetered(isMetered);
370 }
371
SetGlobalHttpProxy(const HttpProxy & httpProxy)372 int32_t NetConnClient::SetGlobalHttpProxy(const HttpProxy &httpProxy)
373 {
374 sptr<INetConnService> proxy = GetProxy();
375 if (proxy == nullptr) {
376 NETMGR_LOG_E("proxy is nullptr");
377 return NETMANAGER_ERR_GET_PROXY_FAIL;
378 }
379 return proxy->SetGlobalHttpProxy(httpProxy);
380 }
381
GetGlobalHttpProxy(HttpProxy & httpProxy)382 int32_t NetConnClient::GetGlobalHttpProxy(HttpProxy &httpProxy)
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 return proxy->GetGlobalHttpProxy(httpProxy);
390 }
391
GetDefaultHttpProxy(HttpProxy & httpProxy)392 int32_t NetConnClient::GetDefaultHttpProxy(HttpProxy &httpProxy)
393 {
394 sptr<INetConnService> proxy = GetProxy();
395 if (proxy == nullptr) {
396 NETMGR_LOG_E("proxy is nullptr");
397 return NETMANAGER_ERR_GET_PROXY_FAIL;
398 }
399 int32_t bindNetId = 0;
400 GetAppNet(bindNetId);
401 return proxy->GetDefaultHttpProxy(bindNetId, httpProxy);
402 }
403
GetNetIdByIdentifier(const std::string & ident,std::list<int32_t> & netIdList)404 int32_t NetConnClient::GetNetIdByIdentifier(const std::string &ident, std::list<int32_t> &netIdList)
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 return proxy->GetNetIdByIdentifier(ident, netIdList);
412 }
413
SetAppNet(int32_t netId)414 int32_t NetConnClient::SetAppNet(int32_t netId)
415 {
416 if (netId < MIN_VALID_NETID && netId != 0) {
417 return NET_CONN_ERR_INVALID_NETWORK;
418 }
419 sptr<INetConnService> proxy = GetProxy();
420 if (proxy == nullptr) {
421 NETMGR_LOG_E("proxy is nullptr");
422 return NETMANAGER_ERR_GET_PROXY_FAIL;
423 }
424 int32_t ret = proxy->SetAppNet(netId);
425 if (ret != NETMANAGER_SUCCESS) {
426 return ret;
427 }
428
429 SetNetForApp(netId);
430 return NETMANAGER_SUCCESS;
431 }
432
GetAppNet(int32_t & netId)433 int32_t NetConnClient::GetAppNet(int32_t &netId)
434 {
435 netId = GetNetForApp();
436 return NETMANAGER_SUCCESS;
437 }
438
RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> & callback)439 int32_t NetConnClient::RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback)
440 {
441 sptr<INetConnService> proxy = GetProxy();
442 if (proxy == nullptr) {
443 NETMGR_LOG_E("proxy is nullptr");
444 return NETMANAGER_ERR_GET_PROXY_FAIL;
445 }
446 return proxy->RegisterNetInterfaceCallback(callback);
447 }
448
GetNetInterfaceConfiguration(const std::string & iface,NetInterfaceConfiguration & config)449 int32_t NetConnClient::GetNetInterfaceConfiguration(const std::string &iface, NetInterfaceConfiguration &config)
450 {
451 sptr<INetConnService> proxy = GetProxy();
452 if (proxy == nullptr) {
453 NETMGR_LOG_E("proxy is nullptr");
454 return NETMANAGER_ERR_GET_PROXY_FAIL;
455 }
456 return proxy->GetNetInterfaceConfiguration(iface, config);
457 }
458 } // namespace NetManagerStandard
459 } // namespace OHOS
460