• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "netsys_sock_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 
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 
SystemReady()36 int32_t NetConnClient::SystemReady()
37 {
38     sptr<INetConnService> proxy = GetProxy();
39     if (proxy == nullptr) {
40         NETMGR_LOG_E("proxy is nullptr");
41         return NETMANAGER_ERR_GET_PROXY_FAIL;
42     }
43     return proxy->SystemReady();
44 }
45 
RegisterNetSupplier(NetBearType bearerType,const std::string & ident,const std::set<NetCap> & netCaps,uint32_t & supplierId)46 int32_t NetConnClient::RegisterNetSupplier(NetBearType bearerType, const std::string &ident,
47                                            const std::set<NetCap> &netCaps, uint32_t &supplierId)
48 {
49     NETMGR_LOG_D("RegisterNetSupplier client in.");
50     sptr<INetConnService> proxy = GetProxy();
51     if (proxy == nullptr) {
52         NETMGR_LOG_E("proxy is nullptr");
53         return NETMANAGER_ERR_GET_PROXY_FAIL;
54     }
55 
56     return proxy->RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
57 }
58 
UnregisterNetSupplier(uint32_t supplierId)59 int32_t NetConnClient::UnregisterNetSupplier(uint32_t supplierId)
60 {
61     NETMGR_LOG_D("UnregisterNetSupplier client in.");
62     sptr<INetConnService> proxy = GetProxy();
63     if (proxy == nullptr) {
64         NETMGR_LOG_E("proxy is nullptr");
65         return NETMANAGER_ERR_GET_PROXY_FAIL;
66     }
67 
68     return proxy->UnregisterNetSupplier(supplierId);
69 }
70 
RegisterNetSupplierCallback(uint32_t supplierId,const sptr<NetSupplierCallbackBase> & callback)71 int32_t NetConnClient::RegisterNetSupplierCallback(uint32_t supplierId, const sptr<NetSupplierCallbackBase> &callback)
72 {
73     NETMGR_LOG_D("RegisterNetSupplierCallback client in.");
74     sptr<INetConnService> proxy = GetProxy();
75     if (proxy == nullptr) {
76         NETMGR_LOG_E("proxy is nullptr");
77         return NETMANAGER_ERR_GET_PROXY_FAIL;
78     }
79     sptr<NetSupplierCallbackStub> ptr = std::make_unique<NetSupplierCallbackStub>().release();
80     ptr->RegisterSupplierCallbackImpl(callback);
81     netSupplierCallback_[supplierId] = ptr;
82     return proxy->RegisterNetSupplierCallback(supplierId, ptr);
83 }
84 
RegisterNetConnCallback(const sptr<INetConnCallback> & callback)85 int32_t NetConnClient::RegisterNetConnCallback(const sptr<INetConnCallback> &callback)
86 {
87     NETMGR_LOG_D("RegisterNetConnCallback client in.");
88     sptr<INetConnService> proxy = GetProxy();
89     if (proxy == nullptr) {
90         NETMGR_LOG_E("The parameter of proxy is nullptr");
91         return NETMANAGER_ERR_GET_PROXY_FAIL;
92     }
93 
94     return proxy->RegisterNetConnCallback(callback);
95 }
96 
RegisterNetConnCallback(const sptr<NetSpecifier> & netSpecifier,const sptr<INetConnCallback> & callback,const uint32_t & timeoutMS)97 int32_t NetConnClient::RegisterNetConnCallback(const sptr<NetSpecifier> &netSpecifier,
98                                                const sptr<INetConnCallback> &callback, const uint32_t &timeoutMS)
99 {
100     NETMGR_LOG_D("RegisterNetConnCallback with timeout client in.");
101     if (netSpecifier == nullptr || !netSpecifier->SpecifierIsValid()) {
102         NETMGR_LOG_E("The parameter of netSpecifier is invalid");
103         return NETMANAGER_ERR_PARAMETER_ERROR;
104     }
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(netSpecifier, callback, timeoutMS);
112 }
113 
UnregisterNetConnCallback(const sptr<INetConnCallback> & callback)114 int32_t NetConnClient::UnregisterNetConnCallback(const sptr<INetConnCallback> &callback)
115 {
116     NETMGR_LOG_D("UnregisterNetConnCallback client in.");
117     sptr<INetConnService> proxy = GetProxy();
118     if (proxy == nullptr) {
119         NETMGR_LOG_E("proxy is nullptr");
120         return NETMANAGER_ERR_GET_PROXY_FAIL;
121     }
122 
123     return proxy->UnregisterNetConnCallback(callback);
124 }
125 
UpdateNetSupplierInfo(uint32_t supplierId,const sptr<NetSupplierInfo> & netSupplierInfo)126 int32_t NetConnClient::UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo)
127 {
128     NETMGR_LOG_D("UpdateNetSupplierInfo client in.");
129     sptr<INetConnService> proxy = GetProxy();
130     if (proxy == nullptr) {
131         NETMGR_LOG_E("proxy is nullptr");
132         return NETMANAGER_ERR_GET_PROXY_FAIL;
133     }
134 
135     return proxy->UpdateNetSupplierInfo(supplierId, netSupplierInfo);
136 }
137 
UpdateNetLinkInfo(uint32_t supplierId,const sptr<NetLinkInfo> & netLinkInfo)138 int32_t NetConnClient::UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo)
139 {
140     NETMGR_LOG_D("UpdateNetLinkInfo client in.");
141     sptr<INetConnService> proxy = GetProxy();
142     if (proxy == nullptr) {
143         NETMGR_LOG_E("proxy is nullptr");
144         return NETMANAGER_ERR_GET_PROXY_FAIL;
145     }
146 
147     return proxy->UpdateNetLinkInfo(supplierId, netLinkInfo);
148 }
149 
GetDefaultNet(NetHandle & netHandle)150 int32_t NetConnClient::GetDefaultNet(NetHandle &netHandle)
151 {
152     NETMGR_LOG_D("GetDefaultNet client in.");
153     sptr<INetConnService> proxy = GetProxy();
154     if (proxy == nullptr) {
155         NETMGR_LOG_E("proxy is nullptr");
156         return NETMANAGER_ERR_GET_PROXY_FAIL;
157     }
158 
159     int32_t netId = 0;
160     int32_t result = proxy->GetDefaultNet(netId);
161     if (result != NETMANAGER_SUCCESS) {
162         NETMGR_LOG_D("fail to get default net.");
163         return result;
164     }
165     netHandle.SetNetId(netId);
166     NETMGR_LOG_D("GetDefaultNet client out.");
167     return NETMANAGER_SUCCESS;
168 }
169 
HasDefaultNet(bool & flag)170 int32_t NetConnClient::HasDefaultNet(bool &flag)
171 {
172     NETMGR_LOG_D("HasDefaultNet client in.");
173     sptr<INetConnService> proxy = GetProxy();
174     if (proxy == nullptr) {
175         NETMGR_LOG_E("proxy is nullptr");
176         return NETMANAGER_ERR_GET_PROXY_FAIL;
177     }
178     return proxy->HasDefaultNet(flag);
179 }
180 
GetAllNets(std::list<sptr<NetHandle>> & netList)181 int32_t NetConnClient::GetAllNets(std::list<sptr<NetHandle>> &netList)
182 {
183     sptr<INetConnService> proxy = GetProxy();
184     if (proxy == nullptr) {
185         NETMGR_LOG_E("proxy is nullptr");
186         return NETMANAGER_ERR_GET_PROXY_FAIL;
187     }
188 
189     std::list<int32_t> netIdList;
190     int32_t result = proxy->GetAllNets(netIdList);
191     if (result != NETMANAGER_SUCCESS) {
192         return result;
193     }
194     std::list<int32_t>::iterator iter;
195     for (iter = netIdList.begin(); iter != netIdList.end(); ++iter) {
196         sptr<NetHandle> netHandle = std::make_unique<NetHandle>(*iter).release();
197         if (netHandle != nullptr) {
198             netList.push_back(netHandle);
199         }
200     }
201     return NETMANAGER_SUCCESS;
202 }
203 
GetConnectionProperties(const NetHandle & netHandle,NetLinkInfo & info)204 int32_t NetConnClient::GetConnectionProperties(const NetHandle &netHandle, NetLinkInfo &info)
205 {
206     sptr<INetConnService> proxy = GetProxy();
207     if (proxy == nullptr) {
208         NETMGR_LOG_E("proxy is nullptr");
209         return NETMANAGER_ERR_GET_PROXY_FAIL;
210     }
211 
212     return proxy->GetConnectionProperties(netHandle.GetNetId(), info);
213 }
214 
GetNetCapabilities(const NetHandle & netHandle,NetAllCapabilities & netAllCap)215 int32_t NetConnClient::GetNetCapabilities(const NetHandle &netHandle, NetAllCapabilities &netAllCap)
216 {
217     sptr<INetConnService> proxy = GetProxy();
218     if (proxy == nullptr) {
219         NETMGR_LOG_E("proxy is nullptr");
220         return NETMANAGER_ERR_GET_PROXY_FAIL;
221     }
222 
223     return proxy->GetNetCapabilities(netHandle.GetNetId(), netAllCap);
224 }
225 
GetAddressesByName(const std::string & host,int32_t netId,std::vector<INetAddr> & addrList)226 int32_t NetConnClient::GetAddressesByName(const std::string &host, int32_t netId, std::vector<INetAddr> &addrList)
227 {
228     sptr<INetConnService> proxy = GetProxy();
229     if (proxy == nullptr) {
230         NETMGR_LOG_E("proxy is nullptr");
231         return NETMANAGER_ERR_GET_PROXY_FAIL;
232     }
233 
234     return proxy->GetAddressesByName(host, netId, addrList);
235 }
236 
GetAddressByName(const std::string & host,int32_t netId,INetAddr & addr)237 int32_t NetConnClient::GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr)
238 {
239     sptr<INetConnService> proxy = GetProxy();
240     if (proxy == nullptr) {
241         NETMGR_LOG_E("proxy is nullptr");
242         return NETMANAGER_ERR_GET_PROXY_FAIL;
243     }
244 
245     return proxy->GetAddressByName(host, netId, addr);
246 }
247 
BindSocket(int32_t socket_fd,int32_t netId)248 int32_t NetConnClient::BindSocket(int32_t socket_fd, int32_t netId)
249 {
250     if (netId < MIN_VALID_NETID) {
251         return NET_CONN_ERR_INVALID_NETWORK;
252     }
253     std::shared_ptr<nmd::FwmarkClient> fwmarkClient_ = std::make_shared<nmd::FwmarkClient>();
254     if (fwmarkClient_ == nullptr) {
255         NETMGR_LOG_E("fwmarkClient_ is nullptr");
256         return NETMANAGER_ERR_PARAMETER_ERROR;
257     }
258     fwmarkClient_->BindSocket(socket_fd, netId);
259     return NETMANAGER_SUCCESS;
260 }
261 
NetDetection(const NetHandle & netHandle)262 int32_t NetConnClient::NetDetection(const NetHandle &netHandle)
263 {
264     sptr<INetConnService> proxy = GetProxy();
265     if (proxy == nullptr) {
266         NETMGR_LOG_E("proxy is nullptr");
267         return NETMANAGER_ERR_GET_PROXY_FAIL;
268     }
269 
270     return proxy->NetDetection(netHandle.GetNetId());
271 }
272 
GetProxy()273 sptr<INetConnService> NetConnClient::GetProxy()
274 {
275     std::lock_guard lock(mutex_);
276 
277     if (NetConnService_) {
278         NETMGR_LOG_D("get proxy is ok");
279         return NetConnService_;
280     }
281 
282     NETMGR_LOG_D("execute GetSystemAbilityManager");
283     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
284     if (sam == nullptr) {
285         NETMGR_LOG_E("GetProxy(), get SystemAbilityManager failed");
286         return nullptr;
287     }
288 
289     sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
290     if (remote == nullptr) {
291         NETMGR_LOG_E("get Remote service failed");
292         return nullptr;
293     }
294 
295     deathRecipient_ = (std::make_unique<NetConnDeathRecipient>(*this)).release();
296     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
297         NETMGR_LOG_E("add death recipient failed");
298         return nullptr;
299     }
300 
301     NetConnService_ = iface_cast<INetConnService>(remote);
302     if (NetConnService_ == nullptr) {
303         NETMGR_LOG_E("get Remote service proxy failed");
304         return nullptr;
305     }
306 
307     return NetConnService_;
308 }
309 
SetAirplaneMode(bool state)310 int32_t NetConnClient::SetAirplaneMode(bool state)
311 {
312     sptr<INetConnService> proxy = GetProxy();
313     if (proxy == nullptr) {
314         NETMGR_LOG_E("proxy is nullptr");
315         return NETMANAGER_ERR_GET_PROXY_FAIL;
316     }
317 
318     return proxy->SetAirplaneMode(state);
319 }
320 
OnRemoteDied(const wptr<IRemoteObject> & remote)321 void NetConnClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
322 {
323     NETMGR_LOG_D("on remote died");
324     if (remote == nullptr) {
325         NETMGR_LOG_E("remote object is nullptr");
326         return;
327     }
328 
329     std::lock_guard lock(mutex_);
330     if (NetConnService_ == nullptr) {
331         NETMGR_LOG_E("NetConnService_ is nullptr");
332         return;
333     }
334 
335     sptr<IRemoteObject> local = NetConnService_->AsObject();
336     if (local != remote.promote()) {
337         NETMGR_LOG_E("proxy and stub is not same remote object");
338         return;
339     }
340 
341     local->RemoveDeathRecipient(deathRecipient_);
342     NetConnService_ = nullptr;
343 }
344 
IsDefaultNetMetered(bool & isMetered)345 int32_t NetConnClient::IsDefaultNetMetered(bool &isMetered)
346 {
347     sptr<INetConnService> proxy = GetProxy();
348     if (proxy == nullptr) {
349         NETMGR_LOG_E("proxy is nullptr");
350         return NETMANAGER_ERR_GET_PROXY_FAIL;
351     }
352     return proxy->IsDefaultNetMetered(isMetered);
353 }
354 
SetGlobalHttpProxy(const HttpProxy & httpProxy)355 int32_t NetConnClient::SetGlobalHttpProxy(const HttpProxy &httpProxy)
356 {
357     sptr<INetConnService> proxy = GetProxy();
358     if (proxy == nullptr) {
359         NETMGR_LOG_E("proxy is nullptr");
360         return NETMANAGER_ERR_GET_PROXY_FAIL;
361     }
362     return proxy->SetGlobalHttpProxy(httpProxy);
363 }
364 
GetGlobalHttpProxy(HttpProxy & httpProxy)365 int32_t NetConnClient::GetGlobalHttpProxy(HttpProxy &httpProxy)
366 {
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     return proxy->GetGlobalHttpProxy(httpProxy);
373 }
374 
GetNetIdByIdentifier(const std::string & ident,int32_t & netId)375 int32_t NetConnClient::GetNetIdByIdentifier(const std::string &ident, int32_t &netId)
376 {
377     sptr<INetConnService> proxy = GetProxy();
378     if (proxy == nullptr) {
379         NETMGR_LOG_E("proxy is nullptr");
380         return NETMANAGER_ERR_GET_PROXY_FAIL;
381     }
382     return proxy->GetNetIdByIdentifier(ident, netId);
383 }
384 
SetAppNet(int32_t netId)385 int32_t NetConnClient::SetAppNet(int32_t netId)
386 {
387     if (netId < MIN_VALID_NETID && netId != 0) {
388         return NET_CONN_ERR_INVALID_NETWORK;
389     }
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     int32_t ret = proxy->SetAppNet(netId);
396     if (ret != NETMANAGER_SUCCESS) {
397         return ret;
398     }
399 
400     SetNetForApp(netId);
401     return NETMANAGER_SUCCESS;
402 }
403 
GetAppNet(int32_t & netId)404 int32_t NetConnClient::GetAppNet(int32_t &netId)
405 {
406     netId = GetNetForApp();
407     return NETMANAGER_SUCCESS;
408 }
409 } // namespace NetManagerStandard
410 } // namespace OHOS
411