• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "connection_exec.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "connection_module.h"
22 #include "constant.h"
23 #include "errorcode_convertor.h"
24 #include "net_conn_client.h"
25 #include "net_manager_constants.h"
26 #include "netconnection.h"
27 #include "netmanager_base_common_utils.h"
28 #include "netmanager_base_log.h"
29 #include "napi_utils.h"
30 #include "securec.h"
31 
32 namespace OHOS::NetManagerStandard {
33 namespace {
34 constexpr int32_t NO_PERMISSION_CODE = 1;
35 constexpr int32_t RESOURCE_UNAVALIEBLE_CODE = 11;
36 constexpr int32_t NET_UNREACHABLE_CODE = 101;
37 } // namespace
38 
CreateNetHandle(napi_env env,NetHandle * handle)39 napi_value ConnectionExec::CreateNetHandle(napi_env env, NetHandle *handle)
40 {
41     napi_value netHandle = NapiUtils::CreateObject(env);
42     if (NapiUtils::GetValueType(env, netHandle) != napi_object) {
43         return NapiUtils::GetUndefined(env);
44     }
45 
46     std::initializer_list<napi_property_descriptor> properties = {
47         DECLARE_NAPI_FUNCTION(ConnectionModule::NetHandleInterface::FUNCTION_GET_ADDRESSES_BY_NAME,
48                               ConnectionModule::NetHandleInterface::GetAddressesByName),
49         DECLARE_NAPI_FUNCTION(ConnectionModule::NetHandleInterface::FUNCTION_GET_ADDRESS_BY_NAME,
50                               ConnectionModule::NetHandleInterface::GetAddressByName),
51         DECLARE_NAPI_FUNCTION(ConnectionModule::NetHandleInterface::FUNCTION_BIND_SOCKET,
52                               ConnectionModule::NetHandleInterface::BindSocket),
53     };
54     NapiUtils::DefineProperties(env, netHandle, properties);
55     NapiUtils::SetUint32Property(env, netHandle, ConnectionModule::NetHandleInterface::PROPERTY_NET_ID,
56                                  handle->GetNetId());
57     return netHandle;
58 }
59 
CreateNetCapabilities(napi_env env,NetAllCapabilities * capabilities)60 napi_value ConnectionExec::CreateNetCapabilities(napi_env env, NetAllCapabilities *capabilities)
61 {
62     napi_value netCapabilities = NapiUtils::CreateObject(env);
63     if (NapiUtils::GetValueType(env, netCapabilities) != napi_object) {
64         return NapiUtils::GetUndefined(env);
65     }
66 
67     NapiUtils::SetUint32Property(env, netCapabilities, KEY_LINK_UP_BAND_WIDTH_KPS,
68                                  capabilities->linkUpBandwidthKbps_);
69     NapiUtils::SetUint32Property(env, netCapabilities, KEY_LINK_DOWN_BAND_WIDTH_KPS,
70                                  capabilities->linkDownBandwidthKbps_);
71     if (!capabilities->netCaps_.empty() && capabilities->netCaps_.size() <= MAX_ARRAY_LENGTH) {
72         napi_value networkCap =
73             NapiUtils::CreateArray(env, std::min(capabilities->netCaps_.size(), MAX_ARRAY_LENGTH));
74         auto it = capabilities->netCaps_.begin();
75         for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != capabilities->netCaps_.end(); ++index, ++it) {
76             NapiUtils::SetArrayElement(env, networkCap, index, NapiUtils::CreateUint32(env, *it));
77         }
78         NapiUtils::SetNamedProperty(env, netCapabilities, KEY_NETWORK_CAP, networkCap);
79     }
80     if (!capabilities->bearerTypes_.empty() && capabilities->bearerTypes_.size() <= MAX_ARRAY_LENGTH) {
81         napi_value bearerTypes =
82             NapiUtils::CreateArray(env, std::min(capabilities->bearerTypes_.size(), MAX_ARRAY_LENGTH));
83         auto it = capabilities->bearerTypes_.begin();
84         for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != capabilities->bearerTypes_.end(); ++index, ++it) {
85             NapiUtils::SetArrayElement(env, bearerTypes, index, NapiUtils::CreateUint32(env, *it));
86         }
87         NapiUtils::SetNamedProperty(env, netCapabilities, KEY_BEARER_TYPE, bearerTypes);
88     }
89     return netCapabilities;
90 }
91 
CreateConnectionProperties(napi_env env,NetLinkInfo * linkInfo)92 napi_value ConnectionExec::CreateConnectionProperties(napi_env env, NetLinkInfo *linkInfo)
93 {
94     napi_value connectionProperties = NapiUtils::CreateObject(env);
95     if (NapiUtils::GetValueType(env, connectionProperties) != napi_object) {
96         return NapiUtils::GetUndefined(env);
97     }
98     NapiUtils::SetStringPropertyUtf8(env, connectionProperties, KEY_INTERFACE_NAME, linkInfo->ifaceName_);
99     NapiUtils::SetStringPropertyUtf8(env, connectionProperties, KEY_DOMAINS, linkInfo->domain_);
100     NapiUtils::SetUint32Property(env, connectionProperties, KEY_MTU, linkInfo->mtu_);
101     FillLinkAddress(env, connectionProperties, linkInfo);
102     FillRouoteList(env, connectionProperties, linkInfo);
103     FillDns(env, connectionProperties, linkInfo);
104     return connectionProperties;
105 }
106 
ExecGetAddressByName(GetAddressByNameContext * context)107 bool ConnectionExec::ExecGetAddressByName(GetAddressByNameContext *context)
108 {
109     return NetHandleExec::ExecGetAddressesByName(context);
110 }
111 
GetAddressByNameCallback(GetAddressByNameContext * context)112 napi_value ConnectionExec::GetAddressByNameCallback(GetAddressByNameContext *context)
113 {
114     return NetHandleExec::GetAddressesByNameCallback(context);
115 }
116 
ExecGetDefaultNet(GetDefaultNetContext * context)117 bool ConnectionExec::ExecGetDefaultNet(GetDefaultNetContext *context)
118 {
119     auto ret = NetConnClient::GetInstance().GetDefaultNet(context->netHandle_);
120     if (ret != NETMANAGER_SUCCESS) {
121         NETMANAGER_BASE_LOGE("get default net failed %{public}d", ret);
122         context->SetErrorCode(ret);
123     }
124     return ret == NETMANAGER_SUCCESS;
125 }
126 
GetDefaultNetCallback(GetDefaultNetContext * context)127 napi_value ConnectionExec::GetDefaultNetCallback(GetDefaultNetContext *context)
128 {
129     return CreateNetHandle(context->GetEnv(), &context->netHandle_);
130 }
131 
ExecHasDefaultNet(HasDefaultNetContext * context)132 bool ConnectionExec::ExecHasDefaultNet(HasDefaultNetContext *context)
133 {
134     auto ret = NetConnClient::GetInstance().HasDefaultNet(context->hasDefaultNet_);
135     NETMANAGER_BASE_LOGI("ExecHasDefaultNet ret %{public}d", ret);
136     if (ret != NETMANAGER_SUCCESS && ret != NET_CONN_ERR_NO_DEFAULT_NET) {
137         context->SetErrorCode(ret);
138         return false;
139     }
140     return true;
141 }
142 
HasDefaultNetCallback(HasDefaultNetContext * context)143 napi_value ConnectionExec::HasDefaultNetCallback(HasDefaultNetContext *context)
144 {
145     return NapiUtils::GetBoolean(context->GetEnv(), context->hasDefaultNet_);
146 }
147 
ExecIsDefaultNetMetered(IsDefaultNetMeteredContext * context)148 bool ConnectionExec::ExecIsDefaultNetMetered(IsDefaultNetMeteredContext *context)
149 {
150     auto ret = NetConnClient::GetInstance().IsDefaultNetMetered(context->isMetered_);
151     if (ret != NETMANAGER_SUCCESS) {
152         NETMANAGER_BASE_LOGE("get net metered status failed %{public}d", ret);
153         context->SetErrorCode(ret);
154     }
155     NETMANAGER_BASE_LOGD("exec is default net metered ret %{public}d", ret);
156     return ret == NETMANAGER_SUCCESS;
157 }
158 
IsDefaultNetMeteredCallback(IsDefaultNetMeteredContext * context)159 napi_value ConnectionExec::IsDefaultNetMeteredCallback(IsDefaultNetMeteredContext *context)
160 {
161     return NapiUtils::GetBoolean(context->GetEnv(), context->isMetered_);
162 }
163 
ExecGetNetCapabilities(GetNetCapabilitiesContext * context)164 bool ConnectionExec::ExecGetNetCapabilities(GetNetCapabilitiesContext *context)
165 {
166     if (!context->IsParseOK()) {
167         return false;
168     }
169     auto ret = NetConnClient::GetInstance().GetNetCapabilities(context->netHandle_, context->capabilities_);
170     context->SetErrorCode(ret);
171     return ret == NETMANAGER_SUCCESS;
172 }
173 
GetNetCapabilitiesCallback(GetNetCapabilitiesContext * context)174 napi_value ConnectionExec::GetNetCapabilitiesCallback(GetNetCapabilitiesContext *context)
175 {
176     return CreateNetCapabilities(context->GetEnv(), &context->capabilities_);
177 }
178 
ExecGetConnectionProperties(GetConnectionPropertiesContext * context)179 bool ConnectionExec::ExecGetConnectionProperties(GetConnectionPropertiesContext *context)
180 {
181     if (!context->IsParseOK()) {
182         return false;
183     }
184     auto ret = NetConnClient::GetInstance().GetConnectionProperties(context->netHandle_, context->linkInfo_);
185     context->SetErrorCode(ret);
186     return ret == NETMANAGER_SUCCESS;
187 }
188 
GetConnectionPropertiesCallback(GetConnectionPropertiesContext * context)189 napi_value ConnectionExec::GetConnectionPropertiesCallback(GetConnectionPropertiesContext *context)
190 {
191     return CreateConnectionProperties(context->GetEnv(), &context->linkInfo_);
192 }
193 
ExecGetAllNets(GetAllNetsContext * context)194 bool ConnectionExec::ExecGetAllNets(GetAllNetsContext *context)
195 {
196     int32_t ret = NetConnClient::GetInstance().GetAllNets(context->netHandleList_);
197     context->SetErrorCode(ret);
198     return ret == NETMANAGER_SUCCESS;
199 }
200 
GetAllNetsCallback(GetAllNetsContext * context)201 napi_value ConnectionExec::GetAllNetsCallback(GetAllNetsContext *context)
202 {
203     napi_value array = NapiUtils::CreateArray(context->GetEnv(), context->netHandleList_.size());
204     uint32_t index = 0;
205     std::for_each(context->netHandleList_.begin(), context->netHandleList_.end(),
206                   [array, &index, context](const sptr<NetHandle> &handle) {
207                       NapiUtils::SetArrayElement(context->GetEnv(), array, index,
208                                                  CreateNetHandle(context->GetEnv(), handle.GetRefPtr()));
209                       ++index;
210                   });
211     return array;
212 }
213 
ExecEnableAirplaneMode(EnableAirplaneModeContext * context)214 bool ConnectionExec::ExecEnableAirplaneMode(EnableAirplaneModeContext *context)
215 {
216     int32_t res = NetConnClient::GetInstance().SetAirplaneMode(true);
217     if (res != NETMANAGER_SUCCESS) {
218         NETMANAGER_BASE_LOGE("ExecEnableAirplaneMode failed %{public}d", res);
219         context->SetErrorCode(res);
220     }
221     return res == NETMANAGER_SUCCESS;
222 }
223 
EnableAirplaneModeCallback(EnableAirplaneModeContext * context)224 napi_value ConnectionExec::EnableAirplaneModeCallback(EnableAirplaneModeContext *context)
225 {
226     return NapiUtils::GetUndefined(context->GetEnv());
227 }
228 
ExecDisableAirplaneMode(DisableAirplaneModeContext * context)229 bool ConnectionExec::ExecDisableAirplaneMode(DisableAirplaneModeContext *context)
230 {
231     int32_t res = NetConnClient::GetInstance().SetAirplaneMode(false);
232     if (res != NETMANAGER_SUCCESS) {
233         NETMANAGER_BASE_LOGE("ExecDisableAirplaneMode failed %{public}d", res);
234         context->SetErrorCode(res);
235     }
236     return res == NETMANAGER_SUCCESS;
237 }
238 
DisableAirplaneModeCallback(DisableAirplaneModeContext * context)239 napi_value ConnectionExec::DisableAirplaneModeCallback(DisableAirplaneModeContext *context)
240 {
241     return NapiUtils::GetUndefined(context->GetEnv());
242 }
243 
ExecReportNetConnected(ReportNetConnectedContext * context)244 bool ConnectionExec::ExecReportNetConnected(ReportNetConnectedContext *context)
245 {
246     if (!context->IsParseOK()) {
247         return false;
248     }
249     int32_t res = NetConnClient::GetInstance().NetDetection(context->netHandle_);
250     if (res != NETMANAGER_SUCCESS) {
251         NETMANAGER_BASE_LOGE("ExecReportNetConnected failed %{public}d", res);
252         context->SetErrorCode(res);
253     }
254     return res == NETMANAGER_SUCCESS;
255 }
256 
ReportNetConnectedCallback(ReportNetConnectedContext * context)257 napi_value ConnectionExec::ReportNetConnectedCallback(ReportNetConnectedContext *context)
258 {
259     return NapiUtils::GetUndefined(context->GetEnv());
260 }
261 
ExecReportNetDisconnected(ReportNetConnectedContext * context)262 bool ConnectionExec::ExecReportNetDisconnected(ReportNetConnectedContext *context)
263 {
264     if (!context->IsParseOK()) {
265         return false;
266     }
267     int32_t res = NetConnClient::GetInstance().NetDetection(context->netHandle_);
268     if (res != NETMANAGER_SUCCESS) {
269         NETMANAGER_BASE_LOGE("ExecReportNetDisconnected failed %{public}d", res);
270         context->SetErrorCode(res);
271     }
272     return res == NETMANAGER_SUCCESS;
273 }
274 
ReportNetDisconnectedCallback(ReportNetConnectedContext * context)275 napi_value ConnectionExec::ReportNetDisconnectedCallback(ReportNetConnectedContext *context)
276 {
277     return NapiUtils::GetUndefined(context->GetEnv());
278 }
279 
ExecGetDefaultHttpProxy(GetHttpProxyContext * context)280 bool ConnectionExec::ExecGetDefaultHttpProxy(GetHttpProxyContext *context)
281 {
282     int32_t errorCode = NetConnClient::GetInstance().GetDefaultHttpProxy(context->httpProxy_);
283     if (errorCode != NET_CONN_SUCCESS) {
284         context->SetErrorCode(errorCode);
285         return false;
286     }
287     return true;
288 }
289 
GetDefaultHttpProxyCallback(GetHttpProxyContext * context)290 napi_value ConnectionExec::GetDefaultHttpProxyCallback(GetHttpProxyContext *context)
291 {
292     napi_value host = NapiUtils::CreateStringUtf8(context->GetEnv(), context->httpProxy_.GetHost());
293     napi_value port = NapiUtils::CreateInt32(context->GetEnv(), context->httpProxy_.GetPort());
294     auto lists = context->httpProxy_.GetExclusionList();
295     napi_value exclusionList = NapiUtils::CreateArray(context->GetEnv(), lists.size());
296     size_t index = 0;
297     for (auto list : lists) {
298         napi_value jsList = NapiUtils::CreateStringUtf8(context->GetEnv(), list);
299         NapiUtils::SetArrayElement(context->GetEnv(), exclusionList, index++, jsList);
300     }
301     napi_value httpProxy = NapiUtils::CreateObject(context->GetEnv());
302     NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "host", host);
303     NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "port", port);
304     NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "exclusionList", exclusionList);
305     return httpProxy;
306 }
307 
ExecGetGlobalHttpProxy(GetHttpProxyContext * context)308 bool ConnectionExec::ExecGetGlobalHttpProxy(GetHttpProxyContext *context)
309 {
310     int32_t errorCode = NetConnClient::GetInstance().GetGlobalHttpProxy(context->httpProxy_);
311     if (errorCode != NET_CONN_SUCCESS) {
312         context->SetErrorCode(errorCode);
313         return false;
314     }
315     return true;
316 }
317 
GetGlobalHttpProxyCallback(GetHttpProxyContext * context)318 napi_value ConnectionExec::GetGlobalHttpProxyCallback(GetHttpProxyContext *context)
319 {
320     napi_value host = NapiUtils::CreateStringUtf8(context->GetEnv(), context->httpProxy_.GetHost());
321     napi_value port = NapiUtils::CreateInt32(context->GetEnv(), context->httpProxy_.GetPort());
322     auto lists = context->httpProxy_.GetExclusionList();
323     napi_value exclusionList = NapiUtils::CreateArray(context->GetEnv(), lists.size());
324     size_t index = 0;
325     for (auto list : lists) {
326         napi_value jsList = NapiUtils::CreateStringUtf8(context->GetEnv(), list);
327         NapiUtils::SetArrayElement(context->GetEnv(), exclusionList, index++, jsList);
328     }
329     napi_value httpProxy = NapiUtils::CreateObject(context->GetEnv());
330     NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "host", host);
331     NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "port", port);
332     NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "exclusionList", exclusionList);
333     return httpProxy;
334 }
335 
ExecSetGlobalHttpProxy(SetGlobalHttpProxyContext * context)336 bool ConnectionExec::ExecSetGlobalHttpProxy(SetGlobalHttpProxyContext *context)
337 {
338     int32_t errorCode = NetConnClient::GetInstance().SetGlobalHttpProxy(context->httpProxy_);
339     if (errorCode != NET_CONN_SUCCESS) {
340         context->SetErrorCode(errorCode);
341         return false;
342     }
343     return true;
344 }
345 
SetGlobalHttpProxyCallback(SetGlobalHttpProxyContext * context)346 napi_value ConnectionExec::SetGlobalHttpProxyCallback(SetGlobalHttpProxyContext *context)
347 {
348     return NapiUtils::GetUndefined(context->GetEnv());
349 }
350 
ExecGetAppNet(GetAppNetContext * context)351 bool ConnectionExec::ExecGetAppNet(GetAppNetContext *context)
352 {
353     NETMANAGER_BASE_LOGI("into");
354     int32_t netId = 0;
355     int32_t errorCode = NetConnClient::GetInstance().GetAppNet(netId);
356     if (errorCode != NET_CONN_SUCCESS) {
357         NETMANAGER_BASE_LOGE("exec getAppNet failed errorCode: %{public}d", errorCode);
358         context->SetErrorCode(errorCode);
359         return false;
360     }
361     context->netHandle_.SetNetId(netId);
362     return true;
363 }
364 
GetAppNetCallback(GetAppNetContext * context)365 napi_value ConnectionExec::GetAppNetCallback(GetAppNetContext *context)
366 {
367     NETMANAGER_BASE_LOGI("into");
368     return CreateNetHandle(context->GetEnv(), &context->netHandle_);
369 }
370 
ExecSetAppNet(SetAppNetContext * context)371 bool ConnectionExec::ExecSetAppNet(SetAppNetContext *context)
372 {
373     NETMANAGER_BASE_LOGI("into");
374     int32_t errorCode = NetConnClient::GetInstance().SetAppNet(context->netHandle_.GetNetId());
375     if (errorCode != NET_CONN_SUCCESS) {
376         NETMANAGER_BASE_LOGE("exec setAppNet failed errorCode: %{public}d", errorCode);
377         context->SetErrorCode(errorCode);
378         return false;
379     }
380     return true;
381 }
382 
SetAppNetCallback(SetAppNetContext * context)383 napi_value ConnectionExec::SetAppNetCallback(SetAppNetContext *context)
384 {
385     NETMANAGER_BASE_LOGI("into");
386     return NapiUtils::GetUndefined(context->GetEnv());
387 }
388 
TransErrorCode(int32_t error)389 int32_t TransErrorCode(int32_t error)
390 {
391     switch (error) {
392         case NO_PERMISSION_CODE:
393             return NETMANAGER_ERR_PERMISSION_DENIED;
394         case RESOURCE_UNAVALIEBLE_CODE:
395             return NETMANAGER_ERR_INVALID_PARAMETER;
396         case NET_UNREACHABLE_CODE:
397             return NETMANAGER_ERR_INTERNAL;
398         default:
399             return NETMANAGER_ERR_OPERATION_FAILED;
400     }
401 }
402 
ExecGetAddressesByName(GetAddressByNameContext * context)403 bool ConnectionExec::NetHandleExec::ExecGetAddressesByName(GetAddressByNameContext *context)
404 {
405     if (!context->IsParseOK()) {
406         return false;
407     }
408     addrinfo *res = nullptr;
409     int status = getaddrinfo(context->host_.c_str(), nullptr, nullptr, &res);
410     if (status < 0) {
411         NETMANAGER_BASE_LOGE("getaddrinfo errno %{public}d %{public}s", errno, strerror(errno));
412         int32_t temp = TransErrorCode(errno);
413         context->SetErrorCode(temp);
414         return false;
415     }
416 
417     for (addrinfo *tmp = res; tmp != nullptr; tmp = tmp->ai_next) {
418         std::string host;
419         if (tmp->ai_family == AF_INET) {
420             auto addr = reinterpret_cast<sockaddr_in *>(tmp->ai_addr);
421             char ip[MAX_IPV4_STR_LEN] = {0};
422             inet_ntop(AF_INET, &addr->sin_addr, ip, sizeof(ip));
423             host = ip;
424         } else if (tmp->ai_family == AF_INET6) {
425             auto addr = reinterpret_cast<sockaddr_in6 *>(tmp->ai_addr);
426             char ip[MAX_IPV6_STR_LEN] = {0};
427             inet_ntop(AF_INET6, &addr->sin6_addr, ip, sizeof(ip));
428             host = ip;
429         }
430 
431         NetAddress address;
432         SetAddressInfo(host.c_str(), tmp, address);
433 
434         context->addresses_.emplace_back(address);
435     }
436     freeaddrinfo(res);
437     return true;
438 }
439 
GetAddressesByNameCallback(GetAddressByNameContext * context)440 napi_value ConnectionExec::NetHandleExec::GetAddressesByNameCallback(GetAddressByNameContext *context)
441 {
442     napi_value addresses = NapiUtils::CreateArray(context->GetEnv(), context->addresses_.size());
443     for (uint32_t index = 0; index < context->addresses_.size(); ++index) {
444         napi_value obj = MakeNetAddressJsValue(context->GetEnv(), context->addresses_[index]);
445         NapiUtils::SetArrayElement(context->GetEnv(), addresses, index, obj);
446     }
447     return addresses;
448 }
449 
ExecGetAddressByName(GetAddressByNameContext * context)450 bool ConnectionExec::NetHandleExec::ExecGetAddressByName(GetAddressByNameContext *context)
451 {
452     if (!context->IsParseOK()) {
453         return false;
454     }
455     addrinfo *res = nullptr;
456     int status = getaddrinfo(context->host_.c_str(), nullptr, nullptr, &res);
457     if (status < 0) {
458         NETMANAGER_BASE_LOGE("getaddrinfo errno %{public}d %{public}s", errno, strerror(errno));
459         int32_t temp = TransErrorCode(errno);
460         context->SetErrorCode(temp);
461         return false;
462     }
463 
464     if (res != nullptr) {
465         std::string host;
466         if (res->ai_family == AF_INET) {
467             auto addr = reinterpret_cast<sockaddr_in *>(res->ai_addr);
468             char ip[MAX_IPV4_STR_LEN] = {0};
469             inet_ntop(AF_INET, &addr->sin_addr, ip, sizeof(ip));
470             host = ip;
471         } else if (res->ai_family == AF_INET6) {
472             auto addr = reinterpret_cast<sockaddr_in6 *>(res->ai_addr);
473             char ip[MAX_IPV6_STR_LEN] = {0};
474             inet_ntop(AF_INET6, &addr->sin6_addr, ip, sizeof(ip));
475             host = ip;
476         }
477 
478         NetAddress address;
479         SetAddressInfo(host.c_str(), res, address);
480 
481         context->addresses_.emplace_back(address);
482     }
483     freeaddrinfo(res);
484     return true;
485 }
486 
GetAddressByNameCallback(GetAddressByNameContext * context)487 napi_value ConnectionExec::NetHandleExec::GetAddressByNameCallback(GetAddressByNameContext *context)
488 {
489     if (context->addresses_.empty()) {
490         return NapiUtils::GetUndefined(context->GetEnv());
491     }
492     return MakeNetAddressJsValue(context->GetEnv(), context->addresses_[0]);
493 }
494 
MakeNetAddressJsValue(napi_env env,const NetAddress & address)495 napi_value ConnectionExec::NetHandleExec::MakeNetAddressJsValue(napi_env env, const NetAddress &address)
496 {
497     napi_value obj = NapiUtils::CreateObject(env);
498     if (NapiUtils::GetValueType(env, obj) != napi_object) {
499         return NapiUtils::GetUndefined(env);
500     }
501 
502     NapiUtils::SetStringPropertyUtf8(env, obj, KEY_ADDRESS, address.GetAddress());
503     NapiUtils::SetUint32Property(env, obj, KEY_FAMILY, address.GetJsValueFamily());
504     NapiUtils::SetUint32Property(env, obj, KEY_PORT, address.GetPort());
505     return obj;
506 }
507 
ExecBindSocket(BindSocketContext * context)508 bool ConnectionExec::NetHandleExec::ExecBindSocket(BindSocketContext *context)
509 {
510     if (!context->IsParseOK()) {
511         return false;
512     }
513     NetHandle handle(context->netId_);
514     int32_t res = handle.BindSocket(context->socketFd_);
515     if (res != NETMANAGER_SUCCESS) {
516         NETMANAGER_BASE_LOGE("ExecBindSocket failed %{public}d", res);
517         context->SetErrorCode(res);
518     }
519     return res == NETMANAGER_SUCCESS;
520 }
521 
BindSocketCallback(BindSocketContext * context)522 napi_value ConnectionExec::NetHandleExec::BindSocketCallback(BindSocketContext *context)
523 {
524     return NapiUtils::GetUndefined(context->GetEnv());
525 }
526 
SetAddressInfo(const char * host,addrinfo * info,NetAddress & address)527 void ConnectionExec::NetHandleExec::SetAddressInfo(const char *host, addrinfo *info, NetAddress &address)
528 {
529     address.SetAddress(host);
530     address.SetFamilyBySaFamily(info->ai_addr->sa_family);
531     if (info->ai_addr->sa_family == AF_INET) {
532         auto addr4 = reinterpret_cast<sockaddr_in *>(info->ai_addr);
533         address.SetPort(addr4->sin_port);
534     } else if (info->ai_addr->sa_family == AF_INET6) {
535         auto addr6 = reinterpret_cast<sockaddr_in6 *>(info->ai_addr);
536         address.SetPort(addr6->sin6_port);
537     }
538 }
539 
ExecRegister(RegisterContext * context)540 bool ConnectionExec::NetConnectionExec::ExecRegister(RegisterContext *context)
541 {
542     EventManager *manager = context->GetManager();
543     auto conn = static_cast<NetConnection *>(manager->GetData());
544     sptr<INetConnCallback> callback = conn->GetObserver();
545 
546     if (conn->hasNetSpecifier_ && conn->hasTimeout_) {
547         sptr<NetSpecifier> specifier = new NetSpecifier(conn->netSpecifier_);
548         int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, callback, conn->timeout_);
549         NETMANAGER_BASE_LOGI("Register result hasNetSpecifier_ and hasTimeout_ %{public}d", ret);
550         context->SetErrorCode(ret);
551         return ret == NETMANAGER_SUCCESS;
552     }
553 
554     if (conn->hasNetSpecifier_) {
555         sptr<NetSpecifier> specifier = new NetSpecifier(conn->netSpecifier_);
556         int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, callback, 0);
557         NETMANAGER_BASE_LOGI("Register result hasNetSpecifier_ %{public}d", ret);
558         context->SetErrorCode(ret);
559         return ret == NETMANAGER_SUCCESS;
560     }
561 
562     int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(callback);
563     NETMANAGER_BASE_LOGI("Register result %{public}d", ret);
564     context->SetErrorCode(ret);
565     return ret == NETMANAGER_SUCCESS;
566 }
567 
RegisterCallback(RegisterContext * context)568 napi_value ConnectionExec::NetConnectionExec::RegisterCallback(RegisterContext *context)
569 {
570     return NapiUtils::GetUndefined(context->GetEnv());
571 }
572 
ExecUnregister(UnregisterContext * context)573 bool ConnectionExec::NetConnectionExec::ExecUnregister(UnregisterContext *context)
574 {
575     EventManager *manager = context->GetManager();
576     auto conn = static_cast<NetConnection *>(manager->GetData());
577     sptr<INetConnCallback> callback = conn->GetObserver();
578 
579     int32_t ret = NetConnClient::GetInstance().UnregisterNetConnCallback(callback);
580     NETMANAGER_BASE_LOGI("Unregister result %{public}d", ret);
581     context->SetErrorCode(ret);
582     return ret == NETMANAGER_SUCCESS;
583 }
584 
UnregisterCallback(RegisterContext * context)585 napi_value ConnectionExec::NetConnectionExec::UnregisterCallback(RegisterContext *context)
586 {
587     return NapiUtils::GetUndefined(context->GetEnv());
588 }
589 
FillLinkAddress(napi_env env,napi_value connectionProperties,NetLinkInfo * linkInfo)590 void ConnectionExec::FillLinkAddress(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo)
591 {
592     if (!linkInfo->netAddrList_.empty() && linkInfo->netAddrList_.size() <= MAX_ARRAY_LENGTH) {
593         napi_value linkAddresses =
594             NapiUtils::CreateArray(env, std::min(linkInfo->netAddrList_.size(), MAX_ARRAY_LENGTH));
595         auto it = linkInfo->netAddrList_.begin();
596         for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != linkInfo->netAddrList_.end(); ++index, ++it) {
597             napi_value netAddr = NapiUtils::CreateObject(env);
598             NapiUtils::SetStringPropertyUtf8(env, netAddr, KEY_ADDRESS, it->address_);
599             NapiUtils::SetUint32Property(env, netAddr, KEY_FAMILY, it->family_);
600             NapiUtils::SetUint32Property(env, netAddr, KEY_PORT, it->port_);
601 
602             napi_value linkAddr = NapiUtils::CreateObject(env);
603             NapiUtils::SetNamedProperty(env, linkAddr, KEY_ADDRESS, netAddr);
604             NapiUtils::SetUint32Property(env, linkAddr, KEY_PREFIX_LENGTH, it->prefixlen_);
605             NapiUtils::SetArrayElement(env, linkAddresses, index, linkAddr);
606         }
607         NapiUtils::SetNamedProperty(env, connectionProperties, KEY_LINK_ADDRESSES, linkAddresses);
608     }
609 }
610 
FillRouoteList(napi_env env,napi_value connectionProperties,NetLinkInfo * linkInfo)611 void ConnectionExec::FillRouoteList(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo)
612 {
613     if (!linkInfo->routeList_.empty() && linkInfo->routeList_.size() <= MAX_ARRAY_LENGTH) {
614         napi_value routes = NapiUtils::CreateArray(env, std::min(linkInfo->routeList_.size(), MAX_ARRAY_LENGTH));
615         auto it = linkInfo->routeList_.begin();
616         for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != linkInfo->routeList_.end(); ++index, ++it) {
617             napi_value route = NapiUtils::CreateObject(env);
618             NapiUtils::SetStringPropertyUtf8(env, route, KEY_INTERFACE, it->iface_);
619 
620             napi_value dest = NapiUtils::CreateObject(env);
621             NapiUtils::SetStringPropertyUtf8(env, dest, KEY_ADDRESS, it->destination_.address_);
622             NapiUtils::SetUint32Property(env, dest, KEY_PREFIX_LENGTH, it->destination_.prefixlen_);
623             NapiUtils::SetNamedProperty(env, route, KEY_DESTINATION, dest);
624 
625             napi_value gateway = NapiUtils::CreateObject(env);
626             NapiUtils::SetStringPropertyUtf8(env, gateway, KEY_ADDRESS, it->gateway_.address_);
627             NapiUtils::SetUint32Property(env, gateway, KEY_PREFIX_LENGTH, it->gateway_.prefixlen_);
628             NapiUtils::SetNamedProperty(env, route, KEY_GATE_WAY, gateway);
629 
630             NapiUtils::SetBooleanProperty(env, route, KEY_HAS_GET_WAY, it->hasGateway_);
631             NapiUtils::SetBooleanProperty(env, route, KEY_IS_DEFAULT_ROUE, it->isDefaultRoute_);
632 
633             NapiUtils::SetArrayElement(env, routes, index, route);
634         }
635         NapiUtils::SetNamedProperty(env, connectionProperties, KEY_ROUTES, routes);
636     }
637 }
638 
FillDns(napi_env env,napi_value connectionProperties,NetLinkInfo * linkInfo)639 void ConnectionExec::FillDns(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo)
640 {
641     if (!linkInfo->dnsList_.empty() && linkInfo->dnsList_.size() <= MAX_ARRAY_LENGTH) {
642         napi_value dnsList = NapiUtils::CreateArray(env, std::min(linkInfo->dnsList_.size(), MAX_ARRAY_LENGTH));
643         auto it = linkInfo->dnsList_.begin();
644         for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != linkInfo->dnsList_.end(); ++index, ++it) {
645             napi_value netAddr = NapiUtils::CreateObject(env);
646             NapiUtils::SetStringPropertyUtf8(env, netAddr, KEY_ADDRESS, it->address_);
647             NapiUtils::SetUint32Property(env, netAddr, KEY_FAMILY, it->family_);
648             NapiUtils::SetUint32Property(env, netAddr, KEY_PORT, it->port_);
649             NapiUtils::SetArrayElement(env, dnsList, index, netAddr);
650         }
651         NapiUtils::SetNamedProperty(env, connectionProperties, KEY_DNSES, dnsList);
652     }
653 }
654 } // namespace OHOS::NetManagerStandard
655