1 /*
2 * Copyright (c) 2023-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 <map>
17
18 #include "net_conn_client.h"
19 #include "net_connection_adapter.h"
20 #include "net_manager_constants.h"
21 #include "net_mgr_log_wrapper.h"
22 #include "securec.h"
23
24 namespace OHOS::NetManagerStandard {
25
26 using BearTypeMap = std::map<NetBearType, NetConn_NetBearerType>;
27 using ReverseBearTypeMap = std::map<NetConn_NetBearerType, NetBearType>;
28 using NetCapMap = std::map<NetCap, NetConn_NetCap>;
29 using ReverseNetCapMap = std::map<NetConn_NetCap, NetCap>;
30
31 static BearTypeMap bearTypeMap = {{BEARER_CELLULAR, NETCONN_BEARER_CELLULAR},
32 {BEARER_WIFI, NETCONN_BEARER_WIFI},
33 {BEARER_BLUETOOTH, NETCONN_BEARER_BLUETOOTH},
34 {BEARER_ETHERNET, NETCONN_BEARER_ETHERNET},
35 {BEARER_VPN, NETCONN_BEARER_VPN}};
36
37 static ReverseBearTypeMap reverseBearTypeMap = {{NETCONN_BEARER_CELLULAR, BEARER_CELLULAR},
38 {NETCONN_BEARER_WIFI, BEARER_WIFI},
39 {NETCONN_BEARER_BLUETOOTH, BEARER_BLUETOOTH},
40 {NETCONN_BEARER_ETHERNET, BEARER_ETHERNET},
41 {NETCONN_BEARER_VPN, BEARER_VPN}};
42
43 static NetCapMap netCapMap = {{NET_CAPABILITY_MMS, NETCONN_NET_CAPABILITY_MMS},
44 {NET_CAPABILITY_SUPL, NETCONN_NET_CAPABILITY_SUPL},
45 {NET_CAPABILITY_DUN, NETCONN_NET_CAPABILITY_DUN},
46 {NET_CAPABILITY_IA, NETCONN_NET_CAPABILITY_IA},
47 {NET_CAPABILITY_XCAP, NETCONN_NET_CAPABILITY_XCAP},
48 {NET_CAPABILITY_NOT_METERED, NETCONN_NET_CAPABILITY_NOT_METERED},
49 {NET_CAPABILITY_INTERNET, NETCONN_NET_CAPABILITY_INTERNET},
50 {NET_CAPABILITY_NOT_VPN, NETCONN_NET_CAPABILITY_NOT_VPN},
51 {NET_CAPABILITY_VALIDATED, NETCONN_NET_CAPABILITY_VALIDATED},
52 {NET_CAPABILITY_PORTAL, NETCONN_NET_CAPABILITY_PORTAL},
53 {NET_CAPABILITY_CHECKING_CONNECTIVITY, NETCONN_NET_CAPABILITY_CHECKING_CONNECTIVITY}};
54
55 static ReverseNetCapMap reverseNetCapMap = {
56 {NETCONN_NET_CAPABILITY_MMS, NET_CAPABILITY_MMS},
57 {NETCONN_NET_CAPABILITY_SUPL, NET_CAPABILITY_SUPL},
58 {NETCONN_NET_CAPABILITY_DUN, NET_CAPABILITY_DUN},
59 {NETCONN_NET_CAPABILITY_IA, NET_CAPABILITY_IA},
60 {NETCONN_NET_CAPABILITY_XCAP, NET_CAPABILITY_XCAP},
61 {NETCONN_NET_CAPABILITY_NOT_METERED, NET_CAPABILITY_NOT_METERED},
62 {NETCONN_NET_CAPABILITY_INTERNET, NET_CAPABILITY_INTERNET},
63 {NETCONN_NET_CAPABILITY_NOT_VPN, NET_CAPABILITY_NOT_VPN},
64 {NETCONN_NET_CAPABILITY_VALIDATED, NET_CAPABILITY_VALIDATED},
65 {NETCONN_NET_CAPABILITY_PORTAL, NET_CAPABILITY_PORTAL},
66 {NETCONN_NET_CAPABILITY_CHECKING_CONNECTIVITY, NET_CAPABILITY_CHECKING_CONNECTIVITY}};
67
Conv2Ch(const std::string s,char * ch)68 static int32_t Conv2Ch(const std::string s, char *ch)
69 {
70 if (s.length() > NETCONN_MAX_STR_LEN - 1) {
71 NETMGR_LOG_E("string out of memory");
72 return NETMANAGER_ERR_INTERNAL;
73 }
74 if (strcpy_s(ch, NETCONN_MAX_STR_LEN, s.c_str()) != EOK) {
75 NETMGR_LOG_E("string copy failed");
76 return NETMANAGER_ERR_INTERNAL;
77 }
78 return NETMANAGER_SUCCESS;
79 }
80
Conv2INetAddr(const INetAddr & netAddrObj,NetConn_NetAddr * netAddr)81 static int32_t Conv2INetAddr(const INetAddr &netAddrObj, NetConn_NetAddr *netAddr)
82 {
83 netAddr->family = netAddrObj.family_;
84 netAddr->prefixlen = netAddrObj.prefixlen_;
85 netAddr->port = netAddrObj.port_;
86
87 int32_t ret = Conv2Ch(netAddrObj.address_, netAddr->address);
88 if (ret != NETMANAGER_SUCCESS) {
89 return ret;
90 }
91 return NETMANAGER_SUCCESS;
92 }
93
Conv2NetHandleList(const std::list<sptr<NetHandle>> & netHandleObjList,NetConn_NetHandleList * netHandleList)94 int32_t Conv2NetHandleList(const std::list<sptr<NetHandle>> &netHandleObjList, NetConn_NetHandleList *netHandleList)
95 {
96 int32_t i = 0;
97 for (const auto &netHandleObj : netHandleObjList) {
98 if (i > NETCONN_MAX_NET_SIZE - 1) {
99 NETMGR_LOG_E("netHandleList out of memory");
100 return NETMANAGER_ERR_INTERNAL;
101 }
102 netHandleList->netHandles[i++].netId = (*netHandleObj).GetNetId();
103 }
104 netHandleList->netHandleListSize = netHandleObjList.size();
105 return NETMANAGER_SUCCESS;
106 }
107
Conv2NetHandle(NetHandle & netHandleObj,NetConn_NetHandle * netHandle)108 int32_t Conv2NetHandle(NetHandle &netHandleObj, NetConn_NetHandle *netHandle)
109 {
110 netHandle->netId = netHandleObj.GetNetId();
111 return NETMANAGER_SUCCESS;
112 }
113
Conv2NetHandleObj(NetConn_NetHandle * netHandle,NetHandle & netHandleObj)114 int32_t Conv2NetHandleObj(NetConn_NetHandle *netHandle, NetHandle &netHandleObj)
115 {
116 netHandleObj.SetNetId(netHandle->netId);
117 return NETMANAGER_SUCCESS;
118 }
119
Conv2HttpProxy(const HttpProxy & httpProxyObj,NetConn_HttpProxy * httpProxy)120 int32_t Conv2HttpProxy(const HttpProxy &httpProxyObj, NetConn_HttpProxy *httpProxy)
121 {
122 int32_t ret = Conv2Ch(httpProxyObj.GetHost(), httpProxy->host);
123 if (ret != NETMANAGER_SUCCESS) {
124 return ret;
125 }
126 httpProxy->port = httpProxyObj.GetPort();
127
128 int32_t i = 0;
129 for (const auto &exclusion : httpProxyObj.GetExclusionList()) {
130 if (i > NETCONN_MAX_EXCLUSION_SIZE - 1) {
131 NETMGR_LOG_E("exclusionList out of memory");
132 return NETMANAGER_ERR_INTERNAL;
133 }
134 ret = Conv2Ch(exclusion, httpProxy->exclusionList[i++]);
135 if (ret != NETMANAGER_SUCCESS) {
136 return ret;
137 }
138 }
139
140 httpProxy->exclusionListSize = static_cast<int32_t>(httpProxyObj.GetExclusionList().size());
141
142 return NETMANAGER_SUCCESS;
143 }
144
ConvertNetConn2HttpProxy(const NetConn_HttpProxy & netConn,HttpProxy & httpProxyObj)145 void ConvertNetConn2HttpProxy(const NetConn_HttpProxy &netConn, HttpProxy &httpProxyObj)
146 {
147 httpProxyObj.SetHost(std::string(netConn.host));
148 httpProxyObj.SetPort(netConn.port);
149 std::list<std::string> exclusionList;
150 for (int32_t i = 0; i < netConn.exclusionListSize; i++) {
151 exclusionList.emplace_back(netConn.exclusionList[i]);
152 }
153 httpProxyObj.SetExclusionList(exclusionList);
154 }
155
Conv2NetLinkInfo(NetLinkInfo & infoObj,NetConn_ConnectionProperties * prop)156 int32_t Conv2NetLinkInfo(NetLinkInfo &infoObj, NetConn_ConnectionProperties *prop)
157 {
158 int32_t ret = Conv2Ch(infoObj.ifaceName_, prop->ifaceName);
159 if (ret != NETMANAGER_SUCCESS) {
160 return ret;
161 }
162 ret = Conv2Ch(infoObj.domain_, prop->domain);
163 if (ret != NETMANAGER_SUCCESS) {
164 return ret;
165 }
166 ret = Conv2Ch(infoObj.tcpBufferSizes_, prop->tcpBufferSizes);
167 if (ret != NETMANAGER_SUCCESS) {
168 return ret;
169 }
170
171 int32_t i = 0;
172 for (const auto &netAddr : infoObj.netAddrList_) {
173 if (i > NETCONN_MAX_ADDR_SIZE - 1) {
174 NETMGR_LOG_E("netAddrList out of memory");
175 return NETMANAGER_ERR_INTERNAL;
176 }
177 ret = Conv2INetAddr(netAddr, &(prop->netAddrList[i++]));
178 if (ret != NETMANAGER_SUCCESS) {
179 return ret;
180 }
181 }
182 prop->netAddrListSize = static_cast<int32_t>(infoObj.netAddrList_.size());
183
184 i = 0;
185 for (const auto &dns : infoObj.dnsList_) {
186 if (i > NETCONN_MAX_ADDR_SIZE - 1) {
187 NETMGR_LOG_E("dnsList out of memory");
188 return NETMANAGER_ERR_INTERNAL;
189 }
190 ret = Conv2INetAddr(dns, &(prop->dnsList[i++]));
191 if (ret != NETMANAGER_SUCCESS) {
192 return ret;
193 }
194 }
195 prop->dnsListSize = static_cast<int32_t>(infoObj.dnsList_.size());
196
197 ret = Conv2HttpProxy(infoObj.httpProxy_, &(prop->httpProxy));
198 if (ret != NETMANAGER_SUCCESS) {
199 return ret;
200 }
201
202 return NETMANAGER_SUCCESS;
203 }
204
Conv2NetAllCapabilities(NetAllCapabilities & netAllCapsObj,NetConn_NetCapabilities * netAllCaps)205 int32_t Conv2NetAllCapabilities(NetAllCapabilities &netAllCapsObj, NetConn_NetCapabilities *netAllCaps)
206 {
207 netAllCaps->linkUpBandwidthKbps = netAllCapsObj.linkUpBandwidthKbps_;
208 netAllCaps->linkDownBandwidthKbps = netAllCapsObj.linkDownBandwidthKbps_;
209
210 int32_t i = 0;
211 for (const auto &netCap : netAllCapsObj.netCaps_) {
212 if (i > NETCONN_MAX_CAP_SIZE - 1) {
213 NETMGR_LOG_E("netCapsList out of memory");
214 return NETMANAGER_ERR_INTERNAL;
215 }
216
217 NetCapMap::iterator iterMap = netCapMap.find(netCap);
218 if (iterMap == netCapMap.end()) {
219 NETMGR_LOG_E("unknown netCapMap key");
220 return NETMANAGER_ERR_INTERNAL;
221 }
222 netAllCaps->netCaps[i++] = iterMap->second;
223 }
224 netAllCaps->netCapsSize = static_cast<int32_t>(netAllCapsObj.netCaps_.size());
225
226 i = 0;
227 for (const auto &bearType : netAllCapsObj.bearerTypes_) {
228 if (i > NETCONN_MAX_BEARER_TYPE_SIZE - 1) {
229 NETMGR_LOG_E("bearerTypes out of memory");
230 return NETMANAGER_ERR_INTERNAL;
231 }
232
233 BearTypeMap::iterator iterMap = bearTypeMap.find(bearType);
234 if (iterMap == bearTypeMap.end()) {
235 NETMGR_LOG_E("unknown bearTypeMap key");
236 return NETMANAGER_ERR_INTERNAL;
237 }
238 netAllCaps->bearerTypes[i++] = iterMap->second;
239 }
240 netAllCaps->bearerTypesSize = static_cast<int32_t>(netAllCapsObj.bearerTypes_.size());
241
242 return NETMANAGER_SUCCESS;
243 }
244
ConvFromNetAllCapabilities(NetAllCapabilities & netAllCapsObj,NetConn_NetCapabilities * netAllCaps)245 int32_t ConvFromNetAllCapabilities(NetAllCapabilities &netAllCapsObj, NetConn_NetCapabilities *netAllCaps)
246 {
247 netAllCapsObj.linkUpBandwidthKbps_ = netAllCaps->linkUpBandwidthKbps;
248 netAllCapsObj.linkDownBandwidthKbps_ = netAllCaps->linkDownBandwidthKbps;
249
250 if (netAllCaps->netCapsSize > NETCONN_MAX_CAP_SIZE) {
251 NETMGR_LOG_E("netCapsList out of memory");
252 return NETMANAGER_ERR_PARAMETER_ERROR;
253 }
254
255 for (int32_t i = 0; i < netAllCaps->netCapsSize; ++i) {
256 auto netCap = netAllCaps->netCaps[i];
257 auto iterMap = reverseNetCapMap.find(netCap);
258 if (iterMap == reverseNetCapMap.end()) {
259 NETMGR_LOG_E("unknown netCapMap key");
260 return NETMANAGER_ERR_PARAMETER_ERROR;
261 }
262 netAllCapsObj.netCaps_.insert(iterMap->second);
263 }
264
265 if (netAllCaps->bearerTypesSize > NETCONN_MAX_BEARER_TYPE_SIZE) {
266 NETMGR_LOG_E("bearerTypes out of memory");
267 return NETMANAGER_ERR_PARAMETER_ERROR;
268 }
269
270 for (int32_t i = 0; i < netAllCaps->bearerTypesSize; ++i) {
271 auto bearType = netAllCaps->bearerTypes[i];
272 auto iterMap = reverseBearTypeMap.find(bearType);
273 if (iterMap == reverseBearTypeMap.end()) {
274 NETMGR_LOG_E("unknown bearTypeMap key");
275 return NETMANAGER_ERR_PARAMETER_ERROR;
276 }
277 netAllCapsObj.bearerTypes_.insert(iterMap->second);
278 }
279
280 return NETMANAGER_SUCCESS;
281 }
282
NetConnCallbackStubAdapter(NetConn_NetConnCallback * callback)283 NetConnCallbackStubAdapter::NetConnCallbackStubAdapter(NetConn_NetConnCallback *callback)
284 {
285 this->callback_.onNetworkAvailable = callback->onNetworkAvailable;
286 this->callback_.onNetCapabilitiesChange = callback->onNetCapabilitiesChange;
287 this->callback_.onConnetionProperties = callback->onConnetionProperties;
288 this->callback_.onNetLost = callback->onNetLost;
289 this->callback_.onNetUnavailable = callback->onNetUnavailable;
290 this->callback_.onNetBlockStatusChange = callback->onNetBlockStatusChange;
291 }
292
NetAvailable(sptr<NetHandle> & netHandle)293 int32_t NetConnCallbackStubAdapter::NetAvailable(sptr<NetHandle> &netHandle)
294 {
295 if (this->callback_.onNetworkAvailable == nullptr || netHandle == nullptr) {
296 return NETMANAGER_SUCCESS;
297 }
298 NetConn_NetHandle netHandleInner;
299 int32_t ret = Conv2NetHandle(*netHandle, &netHandleInner);
300 if (ret != NETMANAGER_SUCCESS) {
301 return ret;
302 }
303
304 this->callback_.onNetworkAvailable(&netHandleInner);
305 return NETMANAGER_SUCCESS;
306 }
307
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)308 int32_t NetConnCallbackStubAdapter::NetCapabilitiesChange(sptr<NetHandle> &netHandle,
309 const sptr<NetAllCapabilities> &netAllCap)
310 {
311 if (this->callback_.onNetCapabilitiesChange == nullptr || netHandle == nullptr || netAllCap == nullptr) {
312 return NETMANAGER_SUCCESS;
313 }
314 NetConn_NetHandle netHandleInner;
315 NetConn_NetCapabilities netAllCapsInner;
316 int32_t ret = Conv2NetHandle(*netHandle, &netHandleInner);
317 if (ret != NETMANAGER_SUCCESS) {
318 return ret;
319 }
320 ret = Conv2NetAllCapabilities(*netAllCap, &netAllCapsInner);
321 if (ret != NETMANAGER_SUCCESS) {
322 return ret;
323 }
324
325 this->callback_.onNetCapabilitiesChange(&netHandleInner, &netAllCapsInner);
326 return NETMANAGER_SUCCESS;
327 }
328
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)329 int32_t NetConnCallbackStubAdapter::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle,
330 const sptr<NetLinkInfo> &info)
331 {
332 if (this->callback_.onConnetionProperties == nullptr || netHandle == nullptr || info == nullptr) {
333 return NETMANAGER_SUCCESS;
334 }
335 NetConn_NetHandle netHandleInner;
336 NetConn_ConnectionProperties netInfoInner;
337 int32_t ret = Conv2NetHandle(*netHandle, &netHandleInner);
338 if (ret != NETMANAGER_SUCCESS) {
339 return ret;
340 }
341 ret = Conv2NetLinkInfo(*info, &netInfoInner);
342 if (ret != NETMANAGER_SUCCESS) {
343 return ret;
344 }
345
346 this->callback_.onConnetionProperties(&netHandleInner, &netInfoInner);
347 return NETMANAGER_SUCCESS;
348 }
349
NetLost(sptr<NetHandle> & netHandle)350 int32_t NetConnCallbackStubAdapter::NetLost(sptr<NetHandle> &netHandle)
351 {
352 if (this->callback_.onNetLost == nullptr || netHandle == nullptr) {
353 return NETMANAGER_SUCCESS;
354 }
355 NetConn_NetHandle netHandleInner;
356 int32_t ret = Conv2NetHandle(*netHandle, &netHandleInner);
357 if (ret != NETMANAGER_SUCCESS) {
358 return ret;
359 }
360
361 this->callback_.onNetLost(&netHandleInner);
362 return NETMANAGER_SUCCESS;
363 }
364
NetUnavailable()365 int32_t NetConnCallbackStubAdapter::NetUnavailable()
366 {
367 if (this->callback_.onNetUnavailable == nullptr) {
368 return NETMANAGER_SUCCESS;
369 }
370 this->callback_.onNetUnavailable();
371 return NETMANAGER_SUCCESS;
372 }
373
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)374 int32_t NetConnCallbackStubAdapter::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
375 {
376 if (this->callback_.onNetBlockStatusChange == nullptr || netHandle == nullptr) {
377 return NETMANAGER_SUCCESS;
378 }
379 NetConn_NetHandle netHandleInner;
380 int32_t ret = Conv2NetHandle(*netHandle, &netHandleInner);
381 if (ret != NETMANAGER_SUCCESS) {
382 return ret;
383 }
384 this->callback_.onNetBlockStatusChange(&netHandleInner, blocked);
385 return NETMANAGER_SUCCESS;
386 }
387
GetInstance()388 NetConnCallbackManager &NetConnCallbackManager::GetInstance()
389 {
390 static NetConnCallbackManager instance;
391 return instance;
392 }
393
RegisterNetConnCallback(NetConn_NetSpecifier * specifier,NetConn_NetConnCallback * netConnCallback,const uint32_t & timeout,uint32_t * callbackId)394 int32_t NetConnCallbackManager::RegisterNetConnCallback(NetConn_NetSpecifier *specifier,
395 NetConn_NetConnCallback *netConnCallback,
396 const uint32_t &timeout, uint32_t *callbackId)
397 {
398 sptr<NetConnCallbackStubAdapter> callback = sptr<NetConnCallbackStubAdapter>::MakeSptr(netConnCallback);
399 sptr<NetSpecifier> specifierInner = new NetSpecifier;
400
401 if (specifier != nullptr) {
402 int32_t ret = ConvFromNetAllCapabilities(specifierInner->netCapabilities_, &specifier->caps);
403 if (ret != NETMANAGER_SUCCESS) {
404 NETMGR_LOG_E("ConvFromNetAllCapabilities failed");
405 return ret;
406 }
407 if (specifier->bearerPrivateIdentifier != nullptr) {
408 specifierInner->ident_ = std::string(specifier->bearerPrivateIdentifier);
409 }
410 ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifierInner, callback, timeout);
411 if (ret != NETMANAGER_SUCCESS) {
412 NETMGR_LOG_E("RegisterNetConnCallback failed");
413 return ret;
414 }
415 } else {
416 int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(callback);
417 if (ret != NETMANAGER_SUCCESS) {
418 NETMGR_LOG_E("RegisterNetConnCallback failed");
419 return ret;
420 }
421 }
422
423 std::lock_guard<std::mutex> lock(this->callbackMapMutex_);
424 *callbackId = this->index_++;
425 this->callbackMap_[*callbackId] = callback;
426 return NETMANAGER_SUCCESS;
427 }
428
UnregisterNetConnCallback(uint32_t callbackId)429 int32_t NetConnCallbackManager::UnregisterNetConnCallback(uint32_t callbackId)
430 {
431 std::lock_guard<std::mutex> lock(this->callbackMapMutex_);
432 auto it = this->callbackMap_.find(callbackId);
433 if (it != this->callbackMap_.end()) {
434 int32_t ret = NetConnClient::GetInstance().UnregisterNetConnCallback(it->second);
435 this->callbackMap_.erase(it);
436 return ret;
437 } else {
438 return NET_CONN_ERR_CALLBACK_NOT_FOUND;
439 }
440 }
441
442 } // namespace OHOS::NetManagerStandard
443