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