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 <netdb.h>
17
18 #include "net_connection.h"
19 #include "net_conn_client.h"
20 #include "net_connection_adapter.h"
21 #include "net_connection_type.h"
22 #include "net_manager_constants.h"
23 #include "net_mgr_log_wrapper.h"
24
25 using namespace OHOS::NetManagerStandard;
26
27 constexpr int32_t VALID_NETID_START = 100;
28
ErrorCodeTrans(int status)29 static int32_t ErrorCodeTrans(int status)
30 {
31 int32_t ret;
32 switch (status) {
33 case EAI_BADFLAGS:
34 if (errno == EPERM || errno == EACCES) {
35 ret = NETMANAGER_ERR_PERMISSION_DENIED;
36 } else {
37 ret = NETMANAGER_ERR_PARAMETER_ERROR;
38 }
39 break;
40 case EAI_SERVICE:
41 ret = NETMANAGER_ERR_OPERATION_FAILED;
42 break;
43 default:
44 ret = NETMANAGER_ERR_INTERNAL;
45 break;
46 }
47 return ret;
48 }
49
OH_NetConn_GetAddrInfo(char * host,char * serv,struct addrinfo * hint,struct addrinfo ** res,int32_t netId)50 int32_t OH_NetConn_GetAddrInfo(char *host, char *serv, struct addrinfo *hint, struct addrinfo **res, int32_t netId)
51 {
52 int32_t ret = NETMANAGER_SUCCESS;
53 int status = 0;
54 struct queryparam qp_param;
55 if (host == nullptr || res == nullptr) {
56 NETMGR_LOG_E("OH_NetConn_GetAddrInfo received invalid parameters");
57 return NETMANAGER_ERR_PARAMETER_ERROR;
58 }
59
60 if (strlen(host) == 0) {
61 NETMGR_LOG_E("OH_NetConn_GetAddrInfo received invalid host");
62 return NETMANAGER_ERR_PARAMETER_ERROR;
63 }
64
65 if (netId > 0 && netId < VALID_NETID_START) {
66 NETMGR_LOG_E("OH_NetConn_GetAddrInfo received invalid netId");
67 return NETMANAGER_ERR_PARAMETER_ERROR;
68 }
69
70 if (memset_s(&qp_param, sizeof(struct queryparam), 0, sizeof(struct queryparam)) != EOK) {
71 NETMGR_LOG_E("OH_NetConn_GetAddrInfo memset_s failed!");
72 return NETMANAGER_ERR_MEMSET_FAIL;
73 }
74 qp_param.qp_netid = netId;
75 qp_param.qp_type = 0;
76
77 status = getaddrinfo_ext(host, serv, hint, res, &qp_param);
78 if (status < 0) {
79 NETMGR_LOG_E("OH_NetConn_GetAddrInfo fail status:%{public}d", status);
80 ret = ErrorCodeTrans(status);
81 }
82
83 return ret;
84 }
85
OH_NetConn_FreeDnsResult(struct addrinfo * res)86 int32_t OH_NetConn_FreeDnsResult(struct addrinfo *res)
87 {
88 if (res == nullptr) {
89 NETMGR_LOG_E("OH_NetConn_FreeDnsResult received invalid parameters");
90 return NETMANAGER_ERR_PARAMETER_ERROR;
91 }
92
93 freeaddrinfo(res);
94
95 return NETMANAGER_SUCCESS;
96 }
97
OH_NetConn_GetAllNets(NetConn_NetHandleList * netHandleList)98 int32_t OH_NetConn_GetAllNets(NetConn_NetHandleList *netHandleList)
99 {
100 if (netHandleList == nullptr) {
101 NETMGR_LOG_E("OH_NetConn_GetAllNets received invalid parameters");
102 return NETMANAGER_ERR_PARAMETER_ERROR;
103 }
104
105 std::list<OHOS::sptr<NetHandle>> netHandleObjList;
106 int32_t ret = NetConnClient::GetInstance().GetAllNets(netHandleObjList);
107 int32_t retConv = Conv2NetHandleList(netHandleObjList, netHandleList);
108 if (retConv != NETMANAGER_SUCCESS) {
109 return retConv;
110 }
111 return ret;
112 }
113
OH_NetConn_HasDefaultNet(int32_t * hasDefaultNet)114 int32_t OH_NetConn_HasDefaultNet(int32_t *hasDefaultNet)
115 {
116 if (hasDefaultNet == nullptr) {
117 NETMGR_LOG_E("OH_NetConn_HasDefaultNet received invalid parameters");
118 return NETMANAGER_ERR_PARAMETER_ERROR;
119 }
120
121 bool flagBool = false;
122 int32_t ret = NetConnClient::GetInstance().HasDefaultNet(flagBool);
123 *hasDefaultNet = flagBool;
124 return ret;
125 }
126
OH_NetConn_GetDefaultNet(NetConn_NetHandle * netHandle)127 int32_t OH_NetConn_GetDefaultNet(NetConn_NetHandle *netHandle)
128 {
129 if (netHandle == nullptr) {
130 NETMGR_LOG_E("OH_NetConn_GetDefaultNet received invalid parameters");
131 return NETMANAGER_ERR_PARAMETER_ERROR;
132 }
133
134 NetHandle netHandleObj = NetHandle();
135 int32_t ret = NetConnClient::GetInstance().GetDefaultNet(netHandleObj);
136 int32_t retConv = Conv2NetHandle(netHandleObj, netHandle);
137 if (retConv != NETMANAGER_SUCCESS) {
138 return retConv;
139 }
140 return ret;
141 }
142
OH_NetConn_IsDefaultNetMetered(int32_t * isMetered)143 int32_t OH_NetConn_IsDefaultNetMetered(int32_t *isMetered)
144 {
145 if (isMetered == nullptr) {
146 NETMGR_LOG_E("OH_NetConn_IsDefaultNetMetered received invalid parameters");
147 return NETMANAGER_ERR_PARAMETER_ERROR;
148 }
149
150 bool flagBool = false;
151 int32_t ret = NetConnClient::GetInstance().IsDefaultNetMetered(flagBool);
152 *isMetered = flagBool;
153 return ret;
154 }
155
OH_NetConn_GetConnectionProperties(NetConn_NetHandle * netHandle,NetConn_ConnectionProperties * prop)156 int32_t OH_NetConn_GetConnectionProperties(NetConn_NetHandle *netHandle, NetConn_ConnectionProperties *prop)
157 {
158 if (netHandle == nullptr || prop == nullptr) {
159 NETMGR_LOG_E("OH_NetConn_GetConnectionProperties received invalid parameters");
160 return NETMANAGER_ERR_PARAMETER_ERROR;
161 }
162
163 NetHandle netHandleObj = NetHandle();
164 int32_t retConv = Conv2NetHandleObj(netHandle, netHandleObj);
165 if (retConv != NETMANAGER_SUCCESS) {
166 return retConv;
167 }
168 NetLinkInfo infoObj = NetLinkInfo();
169 int32_t ret = NetConnClient::GetInstance().GetConnectionProperties(netHandleObj, infoObj);
170 retConv = Conv2NetLinkInfo(infoObj, prop);
171 if (retConv != NETMANAGER_SUCCESS) {
172 return retConv;
173 }
174 return ret;
175 }
176
OH_NetConn_GetNetCapabilities(NetConn_NetHandle * netHandle,NetConn_NetCapabilities * netAllCapabilities)177 int32_t OH_NetConn_GetNetCapabilities(NetConn_NetHandle *netHandle, NetConn_NetCapabilities *netAllCapabilities)
178 {
179 if (netHandle == nullptr || netAllCapabilities == nullptr) {
180 NETMGR_LOG_E("OH_NetConn_GetNetCapabilities received invalid parameters");
181 return NETMANAGER_ERR_PARAMETER_ERROR;
182 }
183
184 NetHandle netHandleObj = NetHandle();
185 int32_t retConv = Conv2NetHandleObj(netHandle, netHandleObj);
186 if (retConv != NETMANAGER_SUCCESS) {
187 return retConv;
188 }
189 NetAllCapabilities netAllCapsObj = NetAllCapabilities();
190 int32_t ret = NetConnClient::GetInstance().GetNetCapabilities(netHandleObj, netAllCapsObj);
191 retConv = Conv2NetAllCapabilities(netAllCapsObj, netAllCapabilities);
192 if (retConv != NETMANAGER_SUCCESS) {
193 return retConv;
194 }
195 return ret;
196 }
197
OH_NetConn_GetDefaultHttpProxy(NetConn_HttpProxy * httpProxy)198 int32_t OH_NetConn_GetDefaultHttpProxy(NetConn_HttpProxy *httpProxy)
199 {
200 if (httpProxy == nullptr) {
201 NETMGR_LOG_E("OH_NetConn_GetDefaultHttpProxy received invalid parameters");
202 return NETMANAGER_ERR_PARAMETER_ERROR;
203 }
204
205 HttpProxy httpProxyObj = HttpProxy();
206 int32_t ret = NetConnClient::GetInstance().GetDefaultHttpProxy(httpProxyObj);
207 int32_t retConv = Conv2HttpProxy(httpProxyObj, httpProxy);
208 if (retConv != NETMANAGER_SUCCESS) {
209 return retConv;
210 }
211 return ret;
212 }
213
OHOS_NetConn_RegisterDnsResolver(OH_NetConn_CustomDnsResolver resolver)214 int32_t OHOS_NetConn_RegisterDnsResolver(OH_NetConn_CustomDnsResolver resolver)
215 {
216 if (resolver == nullptr) {
217 NETMGR_LOG_E("OHOS_NetConn_RegisterDnsResolver received invalid parameters");
218 return NETMANAGER_ERR_PARAMETER_ERROR;
219 }
220
221 int32_t ret = setdnsresolvehook(resolver);
222 if (ret < 0) {
223 ret = NETMANAGER_ERR_PARAMETER_ERROR;
224 }
225 return ret;
226 }
227
OHOS_NetConn_UnregisterDnsResolver()228 int32_t OHOS_NetConn_UnregisterDnsResolver()
229 {
230 int32_t ret = removednsresolvehook();
231 return ret;
232 }
233
OH_NetConn_RegisterDnsResolver(OH_NetConn_CustomDnsResolver resolver)234 int32_t OH_NetConn_RegisterDnsResolver(OH_NetConn_CustomDnsResolver resolver)
235 {
236 if (resolver == nullptr) {
237 NETMGR_LOG_E("OH_NetConn_RegisterDnsResolver received invalid parameters");
238 return NETMANAGER_ERR_PARAMETER_ERROR;
239 }
240
241 int32_t ret = setdnsresolvehook(resolver);
242 if (ret < 0) {
243 ret = NETMANAGER_ERR_PARAMETER_ERROR;
244 }
245 return ret;
246 }
247
OH_NetConn_UnregisterDnsResolver()248 int32_t OH_NetConn_UnregisterDnsResolver()
249 {
250 int32_t ret = removednsresolvehook();
251 return ret;
252 }
253
OH_NetConn_BindSocket(int32_t socketFd,NetConn_NetHandle * netHandle)254 int32_t OH_NetConn_BindSocket(int32_t socketFd, NetConn_NetHandle *netHandle)
255 {
256 if (netHandle == nullptr) {
257 NETMGR_LOG_E("OH_NetConn_BindSocket netHandle is NULL");
258 return NETMANAGER_ERR_PARAMETER_ERROR;
259 }
260 if (socketFd < 0) {
261 NETMGR_LOG_E("OH_NetConn_BindSocket socketFd is invalid");
262 return NETMANAGER_ERR_PARAMETER_ERROR;
263 }
264 if (netHandle->netId < VALID_NETID_START) {
265 NETMGR_LOG_E("OH_NetConn_BindSocket netId is invalid");
266 return NETMANAGER_ERR_PARAMETER_ERROR;
267 }
268
269 int32_t ret = NetConnClient::GetInstance().BindSocket(socketFd, netHandle->netId);
270 return ret;
271 }
272
RegisterErrorCodeTrans(int32_t err)273 static int32_t RegisterErrorCodeTrans(int32_t err)
274 {
275 switch (err) {
276 case NETMANAGER_SUCCESS: // fall through
277 case NETMANAGER_ERR_PERMISSION_DENIED: // fall through
278 case NETMANAGER_ERR_PARAMETER_ERROR: // fall through
279 case NETMANAGER_ERR_OPERATION_FAILED: // fall through
280 case NET_CONN_ERR_CALLBACK_NOT_FOUND: // fall through
281 case NET_CONN_ERR_SAME_CALLBACK: // fall through
282 case NET_CONN_ERR_NET_OVER_MAX_REQUEST_NUM:
283 return err;
284 default:
285 return NETMANAGER_ERR_INTERNAL;
286 }
287 }
288
OH_NetConn_RegisterNetConnCallback(NetConn_NetSpecifier * specifier,NetConn_NetConnCallback * netConnCallback,uint32_t timeout,uint32_t * callbackId)289 int32_t OH_NetConn_RegisterNetConnCallback(NetConn_NetSpecifier *specifier, NetConn_NetConnCallback *netConnCallback,
290 uint32_t timeout, uint32_t *callbackId)
291 {
292 if (specifier == nullptr) {
293 NETMGR_LOG_E("OH_NetConn_RegisterNetConnCallback specifier is NULL");
294 return NETMANAGER_ERR_PARAMETER_ERROR;
295 }
296
297 if (netConnCallback == nullptr) {
298 NETMGR_LOG_E("OH_NetConn_RegisterNetConnCallback netConnCallback is NULL");
299 return NETMANAGER_ERR_PARAMETER_ERROR;
300 }
301 if (callbackId == nullptr) {
302 NETMGR_LOG_E("OH_NetConn_RegisterNetConnCallback callbackId is NULL");
303 return NETMANAGER_ERR_PARAMETER_ERROR;
304 }
305
306 int32_t ret = NetConnCallbackManager::GetInstance().RegisterNetConnCallback(specifier, netConnCallback, timeout,
307 callbackId);
308 return RegisterErrorCodeTrans(ret);
309 }
310
OH_NetConn_RegisterDefaultNetConnCallback(NetConn_NetConnCallback * netConnCallback,uint32_t * callbackId)311 int32_t OH_NetConn_RegisterDefaultNetConnCallback(NetConn_NetConnCallback *netConnCallback, uint32_t *callbackId)
312 {
313 if (netConnCallback == nullptr) {
314 NETMGR_LOG_E("OH_NetConn_RegisterNetConnCallback netConnCallback is NULL");
315 return NETMANAGER_ERR_PARAMETER_ERROR;
316 }
317 if (callbackId == nullptr) {
318 NETMGR_LOG_E("OH_NetConn_RegisterNetConnCallback callbackId is NULL");
319 return NETMANAGER_ERR_PARAMETER_ERROR;
320 }
321 int32_t ret = NetConnCallbackManager::GetInstance().RegisterNetConnCallback(nullptr, netConnCallback, 0,
322 callbackId);
323 return RegisterErrorCodeTrans(ret);
324 }
325
OH_NetConn_UnregisterNetConnCallback(uint32_t callBackId)326 int32_t OH_NetConn_UnregisterNetConnCallback(uint32_t callBackId)
327 {
328 int32_t ret = NetConnCallbackManager::GetInstance().UnregisterNetConnCallback(callBackId);
329 return RegisterErrorCodeTrans(ret);
330 }
331
OH_NetConn_SetAppHttpProxy(NetConn_HttpProxy * httpProxy)332 int32_t OH_NetConn_SetAppHttpProxy(NetConn_HttpProxy *httpProxy)
333 {
334 if (httpProxy == nullptr) {
335 NETMGR_LOG_E("OH_NetConn_SetAppHttpProxy received invalid parameters");
336 return NETMANAGER_ERR_PARAMETER_ERROR;
337 }
338 HttpProxy httpProxyObj;
339 ConvertNetConn2HttpProxy(*httpProxy, httpProxyObj);
340 int32_t ret = NetConnClient::GetInstance().SetAppHttpProxy(httpProxyObj);
341 return ret;
342 }
343
OH_NetConn_RegisterAppHttpProxyCallback(OH_NetConn_AppHttpProxyChange appHttpProxyChange,uint32_t * callbackId)344 int32_t OH_NetConn_RegisterAppHttpProxyCallback(OH_NetConn_AppHttpProxyChange appHttpProxyChange, uint32_t *callbackId)
345 {
346 if (appHttpProxyChange == nullptr) {
347 NETMGR_LOG_E("OH_NetConn_RegisterAppHttpProxyCallback received invalid parameters");
348 return NETMANAGER_ERR_PARAMETER_ERROR;
349 }
350 if (callbackId == nullptr) {
351 NETMGR_LOG_E("OH_NetConn_RegisterAppHttpProxyCallback received invalid parameters");
352 return NETMANAGER_ERR_PARAMETER_ERROR;
353 }
354 auto opration = [appHttpProxyChange](const HttpProxy& httpProxy) {
355 NetConn_HttpProxy netHttpProxy;
356 int32_t retConv = Conv2HttpProxy(httpProxy, &netHttpProxy);
357 if (retConv != NETMANAGER_SUCCESS) {
358 appHttpProxyChange(nullptr);
359 } else {
360 appHttpProxyChange(&netHttpProxy);
361 }
362 };
363 uint32_t id;
364 NetConnClient::GetInstance().RegisterAppHttpProxyCallback(opration, id);
365 *callbackId = id;
366 return NETMANAGER_SUCCESS;
367 }
368
OH_NetConn_UnregisterAppHttpProxyCallback(uint32_t callbackId)369 void OH_NetConn_UnregisterAppHttpProxyCallback(uint32_t callbackId)
370 {
371 NetConnClient::GetInstance().UnregisterAppHttpProxyCallback(callbackId);
372 }
373