1 /*
2 * Copyright (c) 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 <map>
17
18 #include "net_connection_adapter.h"
19 #include "net_manager_constants.h"
20 #include "net_mgr_log_wrapper.h"
21 #include "securec.h"
22
23 namespace OHOS::NetManagerStandard {
24
25 using BearTypeMap = std::map<NetBearType, NetConn_NetBearerType>;
26 using NetCapMap = std::map<NetCap, NetConn_NetCap>;
27
28 static BearTypeMap bearTypeMap = {{BEARER_CELLULAR, NETCONN_BEARER_CELLULAR},
29 {BEARER_WIFI, NETCONN_BEARER_WIFI},
30 {BEARER_ETHERNET, NETCONN_BEARER_ETHERNET}};
31
32 static NetCapMap netCapMap = {{NET_CAPABILITY_MMS, NETCONN_NET_CAPABILITY_MMS},
33 {NET_CAPABILITY_NOT_METERED, NETCONN_NET_CAPABILITY_NOT_METERED},
34 {NET_CAPABILITY_INTERNET, NETCONN_NET_CAPABILITY_INTERNET},
35 {NET_CAPABILITY_NOT_VPN, NETCONN_NET_CAPABILITY_NOT_VPN},
36 {NET_CAPABILITY_VALIDATED, NETCONN_NET_CAPABILITY_VALIDATED}};
37
Conv2Ch(const std::string s,char * ch)38 static int32_t Conv2Ch(const std::string s, char *ch)
39 {
40 if (s.length() > NETCONN_MAX_STR_LEN - 1) {
41 NETMGR_LOG_E("string out of memory");
42 return NETMANAGER_ERR_INTERNAL;
43 }
44 if (strcpy_s(ch, s.length() + 1, s.c_str()) != 0) {
45 NETMGR_LOG_E("string copy failed");
46 return NETMANAGER_ERR_INTERNAL;
47 }
48 return NETMANAGER_SUCCESS;
49 }
50
Conv2INetAddr(const INetAddr & netAddrObj,NetConn_NetAddr * netAddr)51 static int32_t Conv2INetAddr(const INetAddr &netAddrObj, NetConn_NetAddr *netAddr)
52 {
53 netAddr->family = netAddrObj.family_;
54 netAddr->prefixlen = netAddrObj.prefixlen_;
55 netAddr->port = netAddrObj.port_;
56
57 int32_t ret = Conv2Ch(netAddrObj.address_, netAddr->address);
58 if (ret != NETMANAGER_SUCCESS) {
59 return ret;
60 }
61 return NETMANAGER_SUCCESS;
62 }
63
Conv2NetHandleList(const std::list<sptr<NetHandle>> & netHandleObjList,NetConn_NetHandleList * netHandleList)64 int32_t Conv2NetHandleList(const std::list<sptr<NetHandle>> &netHandleObjList, NetConn_NetHandleList *netHandleList)
65 {
66 int32_t i = 0;
67 for (const auto& netHandleObj : netHandleObjList) {
68 if (i > NETCONN_MAX_NET_SIZE - 1) {
69 NETMGR_LOG_E("netHandleList out of memory");
70 return NETMANAGER_ERR_INTERNAL;
71 }
72 netHandleList->netHandles[i++].netId = (*netHandleObj).GetNetId();
73 }
74 netHandleList->netHandleListSize = netHandleObjList.size();
75 return NETMANAGER_SUCCESS;
76 }
77
Conv2NetHandle(NetHandle & netHandleObj,NetConn_NetHandle * netHandle)78 int32_t Conv2NetHandle(NetHandle &netHandleObj, NetConn_NetHandle *netHandle)
79 {
80 netHandle->netId = netHandleObj.GetNetId();
81 return NETMANAGER_SUCCESS;
82 }
83
Conv2NetHandleObj(NetConn_NetHandle * netHandle,NetHandle & netHandleObj)84 int32_t Conv2NetHandleObj(NetConn_NetHandle *netHandle, NetHandle &netHandleObj)
85 {
86 netHandleObj.SetNetId(netHandle->netId);
87 return NETMANAGER_SUCCESS;
88 }
89
Conv2HttpProxy(HttpProxy & httpProxyObj,NetConn_HttpProxy * httpProxy)90 int32_t Conv2HttpProxy(HttpProxy &httpProxyObj, NetConn_HttpProxy *httpProxy)
91 {
92 int32_t ret = Conv2Ch(httpProxyObj.GetHost(), httpProxy->host);
93 if (ret != NETMANAGER_SUCCESS) {
94 return ret;
95 }
96 httpProxy->port = httpProxyObj.GetPort();
97
98 int32_t i = 0;
99 for (const auto& exclusion : httpProxyObj.GetExclusionList()) {
100 if (i > NETCONN_MAX_EXCLUSION_SIZE - 1) {
101 NETMGR_LOG_E("exclusionList out of memory");
102 return NETMANAGER_ERR_INTERNAL;
103 }
104 ret = Conv2Ch(exclusion, httpProxy->exclusionList[i++]);
105 if (ret != NETMANAGER_SUCCESS) {
106 return ret;
107 }
108 }
109
110 httpProxy->exclusionListSize = httpProxyObj.GetExclusionList().size();
111
112 return NETMANAGER_SUCCESS;
113 }
114
115
Conv2NetLinkInfo(NetLinkInfo & infoObj,NetConn_ConnectionProperties * prop)116 int32_t Conv2NetLinkInfo(NetLinkInfo &infoObj, NetConn_ConnectionProperties *prop)
117 {
118 int32_t ret = Conv2Ch(infoObj.ifaceName_, prop->ifaceName);
119 if (ret != NETMANAGER_SUCCESS) {
120 return ret;
121 }
122 ret = Conv2Ch(infoObj.domain_, prop->domain);
123 if (ret != NETMANAGER_SUCCESS) {
124 return ret;
125 }
126 ret = Conv2Ch(infoObj.tcpBufferSizes_, prop->tcpBufferSizes);
127 if (ret != NETMANAGER_SUCCESS) {
128 return ret;
129 }
130
131 int32_t i = 0;
132 for (const auto& netAddr : infoObj.netAddrList_) {
133 if (i > NETCONN_MAX_ADDR_SIZE - 1) {
134 NETMGR_LOG_E("netAddrList out of memory");
135 return NETMANAGER_ERR_INTERNAL;
136 }
137 ret = Conv2INetAddr(netAddr, &(prop->netAddrList[i++]));
138 if (ret != NETMANAGER_SUCCESS) {
139 return ret;
140 }
141 }
142 prop->netAddrListSize = infoObj.netAddrList_.size();
143
144 i = 0;
145 for (const auto& dns : infoObj.dnsList_) {
146 if (i > NETCONN_MAX_ADDR_SIZE - 1) {
147 NETMGR_LOG_E("dnsList out of memory");
148 return NETMANAGER_ERR_INTERNAL;
149 }
150 ret = Conv2INetAddr(dns, &(prop->dnsList[i++]));
151 if (ret != NETMANAGER_SUCCESS) {
152 return ret;
153 }
154 }
155 prop->dnsListSize = infoObj.dnsList_.size();
156
157 ret = Conv2HttpProxy(infoObj.httpProxy_, &(prop->httpProxy));
158 if (ret != NETMANAGER_SUCCESS) {
159 return ret;
160 }
161
162 return NETMANAGER_SUCCESS;
163 }
164
Conv2NetAllCapabilities(NetAllCapabilities & netAllCapsObj,NetConn_NetCapabilities * netAllCaps)165 int32_t Conv2NetAllCapabilities(NetAllCapabilities &netAllCapsObj, NetConn_NetCapabilities *netAllCaps)
166 {
167 netAllCaps->linkUpBandwidthKbps = netAllCapsObj.linkUpBandwidthKbps_;
168 netAllCaps->linkDownBandwidthKbps = netAllCapsObj.linkDownBandwidthKbps_;
169
170 int32_t i = 0;
171 for (const auto& netCap : netAllCapsObj.netCaps_) {
172 if (i > NETCONN_MAX_CAP_SIZE - 1) {
173 NETMGR_LOG_E("netCapsList out of memory");
174 return NETMANAGER_ERR_INTERNAL;
175 }
176
177 NetCapMap::iterator iterMap = netCapMap.find(netCap);
178 if (iterMap == netCapMap.end()) {
179 NETMGR_LOG_E("unknown netCapMap key");
180 return NETMANAGER_ERR_INTERNAL;
181 }
182 netAllCaps->netCaps[i++] = iterMap->second;
183 }
184 netAllCaps->netCapsSize = netAllCapsObj.netCaps_.size();
185
186 i = 0;
187 for (const auto& bearType : netAllCapsObj.bearerTypes_) {
188 if (i > NETCONN_MAX_BEARER_TYPE_SIZE - 1) {
189 NETMGR_LOG_E("bearerTypes out of memory");
190 return NETMANAGER_ERR_INTERNAL;
191 }
192
193 BearTypeMap::iterator iterMap = bearTypeMap.find(bearType);
194 if (iterMap == bearTypeMap.end()) {
195 NETMGR_LOG_E("unknown bearTypeMap key");
196 return NETMANAGER_ERR_INTERNAL;
197 }
198 netAllCaps->bearerTypes[i++] = iterMap->second;
199 }
200 netAllCaps->bearerTypesSize = netAllCapsObj.bearerTypes_.size();
201
202 return NETMANAGER_SUCCESS;
203 }
204
205 } // namespace OHOS::NetManagerStandard