• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "connection_exec.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21 
22 #include "constant.h"
23 #include "errorcode_convertor.h"
24 #include "napi_utils.h"
25 #include "net_conn_callback_observer.h"
26 #include "net_interface_callback_observer.h"
27 #include "net_conn_client.h"
28 #include "net_handle_interface.h"
29 #include "net_manager_constants.h"
30 #include "netconnection.h"
31 #include "netinterface.h"
32 #include "netmanager_base_common_utils.h"
33 #include "netmanager_base_log.h"
34 #include "securec.h"
35 
36 namespace OHOS::NetManagerStandard {
37 namespace {
38 constexpr int32_t NO_PERMISSION_CODE = 1;
39 constexpr int32_t PERMISSION_DENIED_CODE = 13;
40 constexpr int32_t NET_UNREACHABLE_CODE = 101;
41 } // namespace
42 
CreateNetHandle(napi_env env,NetHandle * handle)43 napi_value ConnectionExec::CreateNetHandle(napi_env env, NetHandle *handle)
44 {
45     napi_value netHandle = NapiUtils::CreateObject(env);
46     if (NapiUtils::GetValueType(env, netHandle) != napi_object) {
47         return NapiUtils::GetUndefined(env);
48     }
49 
50     std::initializer_list<napi_property_descriptor> properties = {
51         DECLARE_NAPI_FUNCTION(NetHandleInterface::FUNCTION_GET_ADDRESSES_BY_NAME,
52                               NetHandleInterface::GetAddressesByName),
53         DECLARE_NAPI_FUNCTION(NetHandleInterface::FUNCTION_GET_ADDRESS_BY_NAME,
54                               NetHandleInterface::GetAddressByName),
55         DECLARE_NAPI_FUNCTION(NetHandleInterface::FUNCTION_BIND_SOCKET,
56                               NetHandleInterface::BindSocket),
57     };
58     NapiUtils::DefineProperties(env, netHandle, properties);
59     NapiUtils::SetUint32Property(env, netHandle, NetHandleInterface::PROPERTY_NET_ID, handle->GetNetId());
60     return netHandle;
61 }
62 
CreateNetCapabilities(napi_env env,NetAllCapabilities * capabilities)63 napi_value ConnectionExec::CreateNetCapabilities(napi_env env, NetAllCapabilities *capabilities)
64 {
65     napi_value netCapabilities = NapiUtils::CreateObject(env);
66     if (NapiUtils::GetValueType(env, netCapabilities) != napi_object) {
67         return NapiUtils::GetUndefined(env);
68     }
69 
70     NapiUtils::SetUint32Property(env, netCapabilities, KEY_LINK_UP_BAND_WIDTH_KPS, capabilities->linkUpBandwidthKbps_);
71     NapiUtils::SetUint32Property(env, netCapabilities, KEY_LINK_DOWN_BAND_WIDTH_KPS,
72                                  capabilities->linkDownBandwidthKbps_);
73     if (!capabilities->netCaps_.empty() && capabilities->netCaps_.size() <= MAX_ARRAY_LENGTH) {
74         napi_value networkCap = NapiUtils::CreateArray(env, std::min(capabilities->netCaps_.size(), MAX_ARRAY_LENGTH));
75         auto it = capabilities->netCaps_.begin();
76         for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != capabilities->netCaps_.end(); ++index, ++it) {
77             NapiUtils::SetArrayElement(env, networkCap, index, NapiUtils::CreateUint32(env, *it));
78         }
79         NapiUtils::SetNamedProperty(env, netCapabilities, KEY_NETWORK_CAP, networkCap);
80     }
81     if (!capabilities->bearerTypes_.empty() && capabilities->bearerTypes_.size() <= MAX_ARRAY_LENGTH) {
82         napi_value bearerTypes =
83             NapiUtils::CreateArray(env, std::min(capabilities->bearerTypes_.size(), MAX_ARRAY_LENGTH));
84         auto it = capabilities->bearerTypes_.begin();
85         for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != capabilities->bearerTypes_.end(); ++index, ++it) {
86             NapiUtils::SetArrayElement(env, bearerTypes, index, NapiUtils::CreateUint32(env, *it));
87         }
88         NapiUtils::SetNamedProperty(env, netCapabilities, KEY_BEARER_TYPE, bearerTypes);
89     }
90     return netCapabilities;
91 }
92 
CreateConnectionProperties(napi_env env,NetLinkInfo * linkInfo)93 napi_value ConnectionExec::CreateConnectionProperties(napi_env env, NetLinkInfo *linkInfo)
94 {
95     napi_value connectionProperties = NapiUtils::CreateObject(env);
96     if (NapiUtils::GetValueType(env, connectionProperties) != napi_object) {
97         return NapiUtils::GetUndefined(env);
98     }
99     NapiUtils::SetStringPropertyUtf8(env, connectionProperties, KEY_INTERFACE_NAME, linkInfo->ifaceName_);
100     NapiUtils::SetStringPropertyUtf8(env, connectionProperties, KEY_DOMAINS, linkInfo->domain_);
101     NapiUtils::SetUint32Property(env, connectionProperties, KEY_MTU, linkInfo->mtu_);
102     FillLinkAddress(env, connectionProperties, linkInfo);
103     FillRouoteList(env, connectionProperties, linkInfo);
104     FillDns(env, connectionProperties, linkInfo);
105     return connectionProperties;
106 }
107 
ExecGetAddressByName(GetAddressByNameContext * context)108 bool ConnectionExec::ExecGetAddressByName(GetAddressByNameContext *context)
109 {
110     return NetHandleExec::ExecGetAddressesByName(context);
111 }
112 
GetAddressByNameCallback(GetAddressByNameContext * context)113 napi_value ConnectionExec::GetAddressByNameCallback(GetAddressByNameContext *context)
114 {
115     return NetHandleExec::GetAddressesByNameCallback(context);
116 }
117 
ExecGetDefaultNet(GetDefaultNetContext * context)118 bool ConnectionExec::ExecGetDefaultNet(GetDefaultNetContext *context)
119 {
120     auto ret = NetConnClient::GetInstance().GetDefaultNet(context->netHandle_);
121     if (ret != NETMANAGER_SUCCESS) {
122         NETMANAGER_BASE_LOGE("get default net failed %{public}d", ret);
123         context->SetErrorCode(ret);
124     }
125     return ret == NETMANAGER_SUCCESS;
126 }
127 
GetDefaultNetCallback(GetDefaultNetContext * context)128 napi_value ConnectionExec::GetDefaultNetCallback(GetDefaultNetContext *context)
129 {
130     return CreateNetHandle(context->GetEnv(), &context->netHandle_);
131 }
132 
ExecHasDefaultNet(HasDefaultNetContext * context)133 bool ConnectionExec::ExecHasDefaultNet(HasDefaultNetContext *context)
134 {
135     auto ret = NetConnClient::GetInstance().HasDefaultNet(context->hasDefaultNet_);
136     if (ret != NETMANAGER_SUCCESS && ret != NET_CONN_ERR_NO_DEFAULT_NET) {
137         NETMANAGER_BASE_LOGE("ExecHasDefaultNet ret %{public}d", ret);
138         context->SetErrorCode(ret);
139         return false;
140     }
141     return true;
142 }
143 
HasDefaultNetCallback(HasDefaultNetContext * context)144 napi_value ConnectionExec::HasDefaultNetCallback(HasDefaultNetContext *context)
145 {
146     return NapiUtils::GetBoolean(context->GetEnv(), context->hasDefaultNet_);
147 }
148 
ExecIsDefaultNetMetered(IsDefaultNetMeteredContext * context)149 bool ConnectionExec::ExecIsDefaultNetMetered(IsDefaultNetMeteredContext *context)
150 {
151     auto ret = NetConnClient::GetInstance().IsDefaultNetMetered(context->isMetered_);
152     if (ret != NETMANAGER_SUCCESS) {
153         NETMANAGER_BASE_LOGE("get net metered status failed %{public}d", ret);
154         context->SetErrorCode(ret);
155     }
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 
ExecSetAppHttpProxy(SetAppHttpProxyContext * context)351 bool ConnectionExec::ExecSetAppHttpProxy(SetAppHttpProxyContext *context)
352 {
353     int32_t errorCode = NetConnClient::GetInstance().SetAppHttpProxy(context->httpProxy_);
354     if (errorCode != NET_CONN_SUCCESS) {
355         context->SetErrorCode(errorCode);
356         return false;
357     }
358     return true;
359 }
360 
SetAppHttpProxyCallback(SetAppHttpProxyContext * context)361 napi_value ConnectionExec::SetAppHttpProxyCallback(SetAppHttpProxyContext *context)
362 {
363     return NapiUtils::GetUndefined(context->GetEnv());
364 }
365 
ExecGetAppNet(GetAppNetContext * context)366 bool ConnectionExec::ExecGetAppNet(GetAppNetContext *context)
367 {
368     int32_t netId = 0;
369     int32_t errorCode = NetConnClient::GetInstance().GetAppNet(netId);
370     if (errorCode != NET_CONN_SUCCESS) {
371         NETMANAGER_BASE_LOGE("exec getAppNet failed errorCode: %{public}d", errorCode);
372         context->SetErrorCode(errorCode);
373         return false;
374     }
375     context->netHandle_.SetNetId(netId);
376     return true;
377 }
378 
GetAppNetCallback(GetAppNetContext * context)379 napi_value ConnectionExec::GetAppNetCallback(GetAppNetContext *context)
380 {
381     return CreateNetHandle(context->GetEnv(), &context->netHandle_);
382 }
383 
ExecSetAppNet(SetAppNetContext * context)384 bool ConnectionExec::ExecSetAppNet(SetAppNetContext *context)
385 {
386     NETMANAGER_BASE_LOGI("into");
387     int32_t errorCode = NetConnClient::GetInstance().SetAppNet(context->netHandle_.GetNetId());
388     if (errorCode != NET_CONN_SUCCESS) {
389         NETMANAGER_BASE_LOGE("exec setAppNet failed errorCode: %{public}d", errorCode);
390         context->SetErrorCode(errorCode);
391         return false;
392     }
393     return true;
394 }
395 
SetAppNetCallback(SetAppNetContext * context)396 napi_value ConnectionExec::SetAppNetCallback(SetAppNetContext *context)
397 {
398     return NapiUtils::GetUndefined(context->GetEnv());
399 }
400 
ExecGetPacUrl(GetPacUrlContext * context)401 bool ConnectionExec::ExecGetPacUrl(GetPacUrlContext *context)
402 {
403     int32_t errorCode = NetConnClient::GetInstance().GetPacUrl(context->pacUrl_);
404     if (errorCode != NET_CONN_SUCCESS) {
405         NETMANAGER_BASE_LOGE("exec GetPacUrl failed errorCode: %{public}d", errorCode);
406         context->SetErrorCode(errorCode);
407         return false;
408     }
409     return true;
410 }
411 
GetPacUrlCallback(GetPacUrlContext * context)412 napi_value ConnectionExec::GetPacUrlCallback(GetPacUrlContext *context)
413 {
414     return NapiUtils::CreateStringUtf8(context->GetEnv(), context->pacUrl_);
415 }
416 
ExecSetPacUrl(SetPacUrlContext * context)417 bool ConnectionExec::ExecSetPacUrl(SetPacUrlContext *context)
418 {
419     if (context->pacUrl_.empty()) {
420         NETMANAGER_BASE_LOGE("pac Url is empty!");
421         context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
422         return false;
423     }
424     int32_t errorCode = NetConnClient::GetInstance().SetPacUrl(context->pacUrl_);
425     if (errorCode != NET_CONN_SUCCESS) {
426         NETMANAGER_BASE_LOGE("exec SetPacUrl failed errorCode: %{public}d", errorCode);
427         context->SetErrorCode(errorCode);
428         return false;
429     }
430     return true;
431 }
432 
SetPacUrlCallback(SetPacUrlContext * context)433 napi_value ConnectionExec::SetPacUrlCallback(SetPacUrlContext *context)
434 {
435     return NapiUtils::GetUndefined(context->GetEnv());
436 }
437 
ExecSetCustomDNSRule(SetCustomDNSRuleContext * context)438 bool ConnectionExec::ExecSetCustomDNSRule(SetCustomDNSRuleContext *context)
439 {
440     if (context == nullptr) {
441         NETMANAGER_BASE_LOGE("context is nullptr");
442         return false;
443     }
444 
445     if (!CommonUtils::HasInternetPermission()) {
446         context->SetErrorCode(NETMANAGER_ERR_PERMISSION_DENIED);
447         return false;
448     }
449 
450     if (context->host_.empty() || context->ip_.empty()) {
451         context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
452         return false;
453     }
454 
455     std::vector<std::string> ip = context->ip_;
456     for (size_t i = 0; i < ip.size(); i++) {
457         if (!CommonUtils::IsValidIPV4(ip[i]) && !CommonUtils::IsValidIPV6(ip[i])) {
458             context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
459             return false;
460         }
461     }
462 
463     if (!context->IsParseOK()) {
464         context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
465         return false;
466     }
467 
468     std::string host_ips = context->host_ + ",";
469     for (size_t i = 0; i < ip.size(); i++) {
470         host_ips.append(ip[i]);
471         if (i < ip.size() - 1) {
472             host_ips.append(",");
473         }
474     }
475 
476     NETMANAGER_BASE_LOGI("set host with ip addr");
477     int res = predefined_host_set_hosts(host_ips.c_str());
478     if (res != NETMANAGER_SUCCESS) {
479         NETMANAGER_BASE_LOGE("ExecSetCustomDNSRule failed %{public}d", res);
480         context->SetErrorCode(res);
481         return false;
482     }
483 
484     return true;
485 }
486 
SetCustomDNSRuleCallback(SetCustomDNSRuleContext * context)487 napi_value ConnectionExec::SetCustomDNSRuleCallback(SetCustomDNSRuleContext *context)
488 {
489     return NapiUtils::GetUndefined(context->GetEnv());
490 }
491 
ExecDeleteCustomDNSRule(DeleteCustomDNSRuleContext * context)492 bool ConnectionExec::ExecDeleteCustomDNSRule(DeleteCustomDNSRuleContext *context)
493 {
494     if (context == nullptr) {
495         NETMANAGER_BASE_LOGE("context is nullptr");
496         return false;
497     }
498     if (!CommonUtils::HasInternetPermission()) {
499         context->SetErrorCode(NETMANAGER_ERR_PERMISSION_DENIED);
500         return false;
501     }
502 
503     if (context->host_.empty()) {
504         context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
505         return false;
506     }
507 
508     if (!context->IsParseOK()) {
509         context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
510         return false;
511     }
512 
513     NETMANAGER_BASE_LOGI("delete host with ip addr");
514     int res = predefined_host_remove_host(context->host_.c_str());
515     if (res != NETMANAGER_SUCCESS) {
516         NETMANAGER_BASE_LOGE("ExecDeleteCustomDNSRule failed %{public}d", res);
517         context->SetErrorCode(res);
518         return false;
519     }
520     return true;
521 }
522 
DeleteCustomDNSRuleCallback(DeleteCustomDNSRuleContext * context)523 napi_value ConnectionExec::DeleteCustomDNSRuleCallback(DeleteCustomDNSRuleContext *context)
524 {
525     return NapiUtils::GetUndefined(context->GetEnv());
526 }
527 
ExecDeleteCustomDNSRules(DeleteCustomDNSRulesContext * context)528 bool ConnectionExec::ExecDeleteCustomDNSRules(DeleteCustomDNSRulesContext *context)
529 {
530     if (context == nullptr) {
531         NETMANAGER_BASE_LOGE("context is nullptr");
532         return false;
533     }
534     if (!CommonUtils::HasInternetPermission()) {
535         context->SetErrorCode(NETMANAGER_ERR_PERMISSION_DENIED);
536         return false;
537     }
538 
539     if (!context->IsParseOK()) {
540         context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
541         return false;
542     }
543 
544     int res = predefined_host_clear_all_hosts();
545     if (res != NETMANAGER_SUCCESS) {
546         NETMANAGER_BASE_LOGE("ExecDeleteCustomDNSRules failed %{public}d", res);
547         context->SetErrorCode(res);
548         return false;
549     }
550 
551     return true;
552 }
553 
DeleteCustomDNSRulesCallback(DeleteCustomDNSRulesContext * context)554 napi_value ConnectionExec::DeleteCustomDNSRulesCallback(DeleteCustomDNSRulesContext *context)
555 {
556     return NapiUtils::GetUndefined(context->GetEnv());
557 }
558 
ExecSetInterfaceUp(SetInterfaceUpContext * context)559 bool ConnectionExec::ExecSetInterfaceUp(SetInterfaceUpContext *context)
560 {
561     NETMANAGER_BASE_LOGI("ExecSetInterfaceUp");
562     int32_t errorCode = NetConnClient::GetInstance().SetInterfaceUp(context->interface_);
563     if (errorCode != NET_CONN_SUCCESS) {
564         NETMANAGER_BASE_LOGE("exec setInterfaceUp failed errorCode: %{public}d", errorCode);
565         context->SetErrorCode(errorCode);
566         return false;
567     }
568     return true;
569 }
570 
SetInterfaceUpCallback(SetInterfaceUpContext * context)571 napi_value ConnectionExec::SetInterfaceUpCallback(SetInterfaceUpContext *context)
572 {
573     return NapiUtils::GetUndefined(context->GetEnv());
574 }
575 
ExecSetInterfaceIpAddr(SetInterfaceIpAddrContext * context)576 bool ConnectionExec::ExecSetInterfaceIpAddr(SetInterfaceIpAddrContext *context)
577 {
578     NETMANAGER_BASE_LOGI("ExecSetInterfaceIpAddr");
579     int32_t errorCode = NetConnClient::GetInstance().SetNetInterfaceIpAddress(
580         context->interface_, context->ipAddr_);
581     if (errorCode != NET_CONN_SUCCESS) {
582         NETMANAGER_BASE_LOGE("exec setInterfaceIpAddr failed errorCode: %{public}d", errorCode);
583         context->SetErrorCode(errorCode);
584         return false;
585     }
586     return true;
587 }
588 
SetInterfaceIpAddrCallback(SetInterfaceIpAddrContext * context)589 napi_value ConnectionExec::SetInterfaceIpAddrCallback(SetInterfaceIpAddrContext *context)
590 {
591     return NapiUtils::GetUndefined(context->GetEnv());
592 }
593 
ExecAddNetworkRoute(AddNetworkRouteContext * context)594 bool ConnectionExec::ExecAddNetworkRoute(AddNetworkRouteContext *context)
595 {
596     NETMANAGER_BASE_LOGI("ExecAddNetworkRoute");
597     std::string destAddress =
598         context->route_.destination_.address_ + "/" + std::to_string(context->route_.destination_.prefixlen_);
599     int32_t errorCode = NetConnClient::GetInstance().AddNetworkRoute(
600         context->netId_, context->route_.iface_, destAddress, context->route_.gateway_.address_);
601     if (errorCode != NET_CONN_SUCCESS) {
602         NETMANAGER_BASE_LOGE("exec addNetworkRoute failed errorCode: %{public}d", errorCode);
603         context->SetErrorCode(errorCode);
604         return false;
605     }
606     return true;
607 }
608 
AddNetworkRouteCallback(AddNetworkRouteContext * context)609 napi_value ConnectionExec::AddNetworkRouteCallback(AddNetworkRouteContext *context)
610 {
611     return NapiUtils::GetUndefined(context->GetEnv());
612 }
613 
ExecGetNetInterfaceConfiguration(GetNetInterfaceConfigurationContext * context)614 bool ConnectionExec::ExecGetNetInterfaceConfiguration(GetNetInterfaceConfigurationContext *context)
615 {
616     NETMANAGER_BASE_LOGI("ExecAddNetworkRoute");
617     int32_t errorCode = NetConnClient::GetInstance().GetNetInterfaceConfiguration(
618         context->interface_, context->config_);
619     if (errorCode != NET_CONN_SUCCESS) {
620         NETMANAGER_BASE_LOGE("exec getNetInterfaceConfiguration failed errorCode: %{public}d", errorCode);
621         context->SetErrorCode(errorCode);
622         return false;
623     }
624     return true;
625 }
626 
GetNetInterfaceConfigurationCallback(GetNetInterfaceConfigurationContext * context)627 napi_value ConnectionExec::GetNetInterfaceConfigurationCallback(GetNetInterfaceConfigurationContext *context)
628 {
629     int32_t errorCode = NetConnClient::GetInstance().GetNetInterfaceConfiguration(
630         context->interface_, context->config_);
631     if (errorCode != NET_CONN_SUCCESS) {
632         NETMANAGER_BASE_LOGE("getNetInterfaceConfiguration callback failed errorCode: %{public}d", errorCode);
633         context->SetErrorCode(errorCode);
634         return NapiUtils::GetUndefined(context->GetEnv());
635     }
636     napi_value ifName = NapiUtils::CreateStringUtf8(context->GetEnv(), context->config_.ifName_);
637     napi_value hwAddr = NapiUtils::CreateStringUtf8(context->GetEnv(), context->config_.hwAddr_);
638     napi_value ipv4Addr = NapiUtils::CreateStringUtf8(context->GetEnv(), context->config_.ipv4Addr_);
639     napi_value prefixLength = NapiUtils::CreateInt32(context->GetEnv(), context->config_.prefixLength_);
640     napi_value flagsList = NapiUtils::CreateArray(context->GetEnv(), context->config_.flags_.size());
641     size_t index = 0;
642     for (auto flag : context->config_.flags_) {
643         napi_value jsList = NapiUtils::CreateStringUtf8(context->GetEnv(), flag);
644         NapiUtils::SetArrayElement(context->GetEnv(), flagsList, index++, jsList);
645     }
646     napi_value interfaceConfig = NapiUtils::CreateObject(context->GetEnv());
647     NapiUtils::SetNamedProperty(context->GetEnv(), interfaceConfig, "interfaceName", ifName);
648     NapiUtils::SetNamedProperty(context->GetEnv(), interfaceConfig, "hwAddress", hwAddr);
649     NapiUtils::SetNamedProperty(context->GetEnv(), interfaceConfig, "ipv4Address", ipv4Addr);
650     NapiUtils::SetNamedProperty(context->GetEnv(), interfaceConfig, "prefixLength", prefixLength);
651     NapiUtils::SetNamedProperty(context->GetEnv(), interfaceConfig, "flags", flagsList);
652     return interfaceConfig;
653 }
654 
ExecRegisterNetSupplier(RegisterNetSupplierContext * context)655 bool ConnectionExec::ExecRegisterNetSupplier(RegisterNetSupplierContext *context)
656 {
657     NETMANAGER_BASE_LOGI("ExecRegisterNetSupplier");
658     int32_t errorCode = NetConnClient::GetInstance().RegisterNetSupplier(
659         context->bearerType_, context->ident_, context->netCaps_, context->netSupplierId_);
660     if (errorCode != NET_CONN_SUCCESS) {
661         NETMANAGER_BASE_LOGE("exec registerNetSupplier failed errorCode: %{public}d", errorCode);
662         context->SetErrorCode(errorCode);
663         return false;
664     }
665     return true;
666 }
667 
RegisterNetSupplierCallback(RegisterNetSupplierContext * context)668 napi_value ConnectionExec::RegisterNetSupplierCallback(RegisterNetSupplierContext *context)
669 {
670     int32_t errorCode = NetConnClient::GetInstance().RegisterNetSupplier(
671         context->bearerType_, context->ident_, context->netCaps_, context->netSupplierId_);
672     if (errorCode != NET_CONN_SUCCESS) {
673         NETMANAGER_BASE_LOGE("registerNetSupplier callback failed errorCode: %{public}d", errorCode);
674         context->SetErrorCode(errorCode);
675         return NapiUtils::GetUndefined(context->GetEnv());
676     }
677     napi_value netSupplierId = NapiUtils::CreateUint32(context->GetEnv(), context->netSupplierId_);
678     return netSupplierId;
679 }
680 
ExecUnregisterNetSupplier(UnregisterNetSupplierContext * context)681 bool ConnectionExec::ExecUnregisterNetSupplier(UnregisterNetSupplierContext *context)
682 {
683     NETMANAGER_BASE_LOGI("ExecUnregisterNetSupplier");
684     int32_t errorCode = NetConnClient::GetInstance().UnregisterNetSupplier(context->netSupplierId_);
685     if (errorCode != NET_CONN_SUCCESS) {
686         NETMANAGER_BASE_LOGE("exec addNetworkRoute failed errorCode: %{public}d", errorCode);
687         context->SetErrorCode(errorCode);
688         return false;
689     }
690     return true;
691 }
692 
UnregisterNetSupplierCallback(UnregisterNetSupplierContext * context)693 napi_value ConnectionExec::UnregisterNetSupplierCallback(UnregisterNetSupplierContext *context)
694 {
695     return NapiUtils::GetUndefined(context->GetEnv());
696 }
697 
ExecFactoryResetNetwork(FactoryResetNetworkContext * context)698 bool ConnectionExec::ExecFactoryResetNetwork(FactoryResetNetworkContext *context)
699 {
700     NETMANAGER_BASE_LOGI("ExecFactoryResetNetwork into");
701     int32_t errorCode = NetConnClient::GetInstance().FactoryResetNetwork();
702     if (errorCode != NET_CONN_SUCCESS) {
703         NETMANAGER_BASE_LOGE("exec ResetNetwork failed errorCode: %{public}d", errorCode);
704         context->SetErrorCode(errorCode);
705         return false;
706     }
707     return true;
708 }
709 
FactoryResetNetworkCallback(FactoryResetNetworkContext * context)710 napi_value ConnectionExec::FactoryResetNetworkCallback(FactoryResetNetworkContext *context)
711 {
712     return NapiUtils::GetUndefined(context->GetEnv());
713 }
714 
TransErrorCode(int32_t error)715 int32_t TransErrorCode(int32_t error)
716 {
717     switch (error) {
718         case NO_PERMISSION_CODE:
719             return NETMANAGER_ERR_PERMISSION_DENIED;
720         case PERMISSION_DENIED_CODE:
721             return NETMANAGER_ERR_PERMISSION_DENIED;
722         case NET_UNREACHABLE_CODE:
723             return NETMANAGER_ERR_INTERNAL;
724         default:
725             return NETMANAGER_ERR_OPERATION_FAILED;
726     }
727 }
728 
ExecGetAddressesByName(GetAddressByNameContext * context)729 bool ConnectionExec::NetHandleExec::ExecGetAddressesByName(GetAddressByNameContext *context)
730 {
731     if (!context->IsParseOK()) {
732         return false;
733     }
734     uint32_t netid = static_cast<uint32_t>(context->netId_);
735     addrinfo *res = nullptr;
736     queryparam param;
737     param.qp_type = QEURY_TYPE_NORMAL;
738     param.qp_netid = netid;
739     NETMANAGER_BASE_LOGD("getaddrinfo_ext %{public}d %{public}d", netid, param.qp_netid);
740     if (context->host_.empty()) {
741         NETMANAGER_BASE_LOGE("host is empty!");
742         context->SetErrorCode(NETMANAGER_ERR_INVALID_PARAMETER);
743         return false;
744     }
745 
746     int status = getaddrinfo_ext(context->host_.c_str(), nullptr, nullptr, &res, &param);
747     if (status < 0) {
748         NETMANAGER_BASE_LOGE("getaddrinfo errno %{public}d %{public}s,  status: %{public}d", errno, strerror(errno),
749                              status);
750         int32_t temp = TransErrorCode(errno);
751         context->SetErrorCode(temp);
752         return false;
753     }
754 
755     for (addrinfo *tmp = res; tmp != nullptr; tmp = tmp->ai_next) {
756         std::string host;
757         if (tmp->ai_family == AF_INET) {
758             auto addr = reinterpret_cast<sockaddr_in *>(tmp->ai_addr);
759             char ip[MAX_IPV4_STR_LEN] = {0};
760             inet_ntop(AF_INET, &addr->sin_addr, ip, sizeof(ip));
761             host = ip;
762         } else if (tmp->ai_family == AF_INET6) {
763             auto addr = reinterpret_cast<sockaddr_in6 *>(tmp->ai_addr);
764             char ip[MAX_IPV6_STR_LEN] = {0};
765             inet_ntop(AF_INET6, &addr->sin6_addr, ip, sizeof(ip));
766             host = ip;
767         }
768 
769         NetAddress address;
770         SetAddressInfo(host.c_str(), tmp, address);
771 
772         context->addresses_.emplace_back(address);
773     }
774     freeaddrinfo(res);
775     return true;
776 }
777 
GetAddressesByNameCallback(GetAddressByNameContext * context)778 napi_value ConnectionExec::NetHandleExec::GetAddressesByNameCallback(GetAddressByNameContext *context)
779 {
780     napi_value addresses = NapiUtils::CreateArray(context->GetEnv(), context->addresses_.size());
781     for (uint32_t index = 0; index < context->addresses_.size(); ++index) {
782         napi_value obj = MakeNetAddressJsValue(context->GetEnv(), context->addresses_[index]);
783         NapiUtils::SetArrayElement(context->GetEnv(), addresses, index, obj);
784     }
785     return addresses;
786 }
787 
ExecGetAddressByName(GetAddressByNameContext * context)788 bool ConnectionExec::NetHandleExec::ExecGetAddressByName(GetAddressByNameContext *context)
789 {
790     if (!context->IsParseOK()) {
791         return false;
792     }
793     uint32_t netid = static_cast<uint32_t>(context->netId_);
794     addrinfo *res = nullptr;
795     queryparam param;
796     param.qp_type = QEURY_TYPE_NORMAL;
797     param.qp_netid = netid;
798     NETMANAGER_BASE_LOGD("getaddrinfo_ext %{public}d %{public}d", netid, param.qp_netid);
799     if (context->host_.empty()) {
800         NETMANAGER_BASE_LOGE("host is empty!");
801         context->SetErrorCode(NETMANAGER_ERR_INVALID_PARAMETER);
802         return false;
803     }
804 
805     int status = getaddrinfo_ext(context->host_.c_str(), nullptr, nullptr, &res, &param);
806     if (status < 0) {
807         NETMANAGER_BASE_LOGE("getaddrinfo errno %{public}d %{public}s,  status: %{public}d", errno, strerror(errno),
808                              status);
809         int32_t temp = TransErrorCode(errno);
810         context->SetErrorCode(temp);
811         return false;
812     }
813 
814     if (res != nullptr) {
815         std::string host;
816         if (res->ai_family == AF_INET) {
817             auto addr = reinterpret_cast<sockaddr_in *>(res->ai_addr);
818             char ip[MAX_IPV4_STR_LEN] = {0};
819             inet_ntop(AF_INET, &addr->sin_addr, ip, sizeof(ip));
820             host = ip;
821         } else if (res->ai_family == AF_INET6) {
822             auto addr = reinterpret_cast<sockaddr_in6 *>(res->ai_addr);
823             char ip[MAX_IPV6_STR_LEN] = {0};
824             inet_ntop(AF_INET6, &addr->sin6_addr, ip, sizeof(ip));
825             host = ip;
826         }
827 
828         NetAddress address;
829         SetAddressInfo(host.c_str(), res, address);
830 
831         context->addresses_.emplace_back(address);
832     }
833     freeaddrinfo(res);
834     return true;
835 }
836 
GetAddressByNameCallback(GetAddressByNameContext * context)837 napi_value ConnectionExec::NetHandleExec::GetAddressByNameCallback(GetAddressByNameContext *context)
838 {
839     if (context->addresses_.empty()) {
840         return NapiUtils::GetUndefined(context->GetEnv());
841     }
842     return MakeNetAddressJsValue(context->GetEnv(), context->addresses_[0]);
843 }
844 
MakeNetAddressJsValue(napi_env env,const NetAddress & address)845 napi_value ConnectionExec::NetHandleExec::MakeNetAddressJsValue(napi_env env, const NetAddress &address)
846 {
847     napi_value obj = NapiUtils::CreateObject(env);
848     if (NapiUtils::GetValueType(env, obj) != napi_object) {
849         return NapiUtils::GetUndefined(env);
850     }
851 
852     NapiUtils::SetStringPropertyUtf8(env, obj, KEY_ADDRESS, address.GetAddress());
853     NapiUtils::SetUint32Property(env, obj, KEY_FAMILY, address.GetJsValueFamily());
854     NapiUtils::SetUint32Property(env, obj, KEY_PORT, address.GetPort());
855     return obj;
856 }
857 
ExecBindSocket(BindSocketContext * context)858 bool ConnectionExec::NetHandleExec::ExecBindSocket(BindSocketContext *context)
859 {
860     if (!context->IsParseOK()) {
861         return false;
862     }
863     NetHandle handle(context->netId_);
864     int32_t res = handle.BindSocket(context->socketFd_);
865     if (res != NETMANAGER_SUCCESS) {
866         NETMANAGER_BASE_LOGE("ExecBindSocket failed %{public}d", res);
867         context->SetErrorCode(res);
868     }
869     return res == NETMANAGER_SUCCESS;
870 }
871 
BindSocketCallback(BindSocketContext * context)872 napi_value ConnectionExec::NetHandleExec::BindSocketCallback(BindSocketContext *context)
873 {
874     return NapiUtils::GetUndefined(context->GetEnv());
875 }
876 
SetAddressInfo(const char * host,addrinfo * info,NetAddress & address)877 void ConnectionExec::NetHandleExec::SetAddressInfo(const char *host, addrinfo *info, NetAddress &address)
878 {
879     address.SetAddress(host);
880     address.SetFamilyBySaFamily(info->ai_addr->sa_family);
881     if (info->ai_addr->sa_family == AF_INET) {
882         auto addr4 = reinterpret_cast<sockaddr_in *>(info->ai_addr);
883         address.SetPort(addr4->sin_port);
884     } else if (info->ai_addr->sa_family == AF_INET6) {
885         auto addr6 = reinterpret_cast<sockaddr_in6 *>(info->ai_addr);
886         address.SetPort(addr6->sin6_port);
887     }
888 }
889 
ExecRegister(RegisterContext * context)890 bool ConnectionExec::NetConnectionExec::ExecRegister(RegisterContext *context)
891 {
892     auto wCallback = context->GetNetConnCallback();
893     sptr<INetConnCallback> callback = wCallback.promote();
894     if (callback == nullptr) {
895         NETMANAGER_BASE_LOGE("ExecRegister getNetConnCallback nullptr");
896         return false;
897     }
898 
899     auto conn = context->GetConn();
900     if (conn.hasNetSpecifier_ && conn.hasTimeout_) {
901         sptr<NetSpecifier> specifier = new NetSpecifier(conn.netSpecifier_);
902         int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, callback, conn.timeout_);
903         NETMANAGER_BASE_LOGI("Register result hasNetSpecifier_ and hasTimeout_ %{public}d", ret);
904         context->SetErrorCode(ret);
905         return ret == NETMANAGER_SUCCESS;
906     }
907 
908     if (conn.hasNetSpecifier_) {
909         sptr<NetSpecifier> specifier = new NetSpecifier(conn.netSpecifier_);
910         int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, callback, 0);
911         NETMANAGER_BASE_LOGI("Register result hasNetSpecifier_ %{public}d", ret);
912         context->SetErrorCode(ret);
913         return ret == NETMANAGER_SUCCESS;
914     }
915 
916     int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(callback);
917     NETMANAGER_BASE_LOGI("Register result %{public}d", ret);
918     context->SetErrorCode(ret);
919     return ret == NETMANAGER_SUCCESS;
920 }
921 
RegisterCallback(RegisterContext * context)922 napi_value ConnectionExec::NetConnectionExec::RegisterCallback(RegisterContext *context)
923 {
924     return NapiUtils::GetUndefined(context->GetEnv());
925 }
926 
ExecUnregister(UnregisterContext * context)927 bool ConnectionExec::NetConnectionExec::ExecUnregister(UnregisterContext *context)
928 {
929     auto wCallback = context->GetNetConnCallback();
930     auto callback = wCallback.promote();
931     if (callback == nullptr) {
932         NETMANAGER_BASE_LOGE("ExecUnregister getNetConnCallback nullptr");
933         return false;
934     }
935 
936     int32_t ret = NetConnClient::GetInstance().UnregisterNetConnCallback(callback);
937     if (ret != NETMANAGER_SUCCESS) {
938         NETMANAGER_BASE_LOGE("Unregister result %{public}d", ret);
939         context->SetErrorCode(ret);
940     }
941     return ret == NETMANAGER_SUCCESS;
942 }
943 
UnregisterCallback(RegisterContext * context)944 napi_value ConnectionExec::NetConnectionExec::UnregisterCallback(RegisterContext *context)
945 {
946     return NapiUtils::GetUndefined(context->GetEnv());
947 }
948 
ExecIfaceRegister(IfaceRegisterContext * context)949 bool ConnectionExec::NetInterfaceExec::ExecIfaceRegister(IfaceRegisterContext *context)
950 {
951     auto wCallback = context->GetNetInterfaceCallback();
952     sptr<INetInterfaceStateCallback> callback = wCallback.promote();
953     if (callback == nullptr) {
954         NETMANAGER_BASE_LOGE("ExecIfaceRegister getNetInterfaceCallback nullptr");
955         return false;
956     }
957 
958     int32_t ret = NetConnClient::GetInstance().RegisterNetInterfaceCallback(callback);
959     NETMANAGER_BASE_LOGI("Register result %{public}d", ret);
960     context->SetErrorCode(ret);
961     return ret == NETMANAGER_SUCCESS;
962 }
963 
IfaceRegisterCallback(IfaceRegisterContext * context)964 napi_value ConnectionExec::NetInterfaceExec::IfaceRegisterCallback(IfaceRegisterContext *context)
965 {
966     return NapiUtils::GetUndefined(context->GetEnv());
967 }
968 
ExecIfaceUnregister(IfaceUnregisterContext * context)969 bool ConnectionExec::NetInterfaceExec::ExecIfaceUnregister(IfaceUnregisterContext *context)
970 {
971     auto wCallback = context->GetNetInterfaceCallback();
972     auto callback = wCallback.promote();
973     if (callback == nullptr) {
974         NETMANAGER_BASE_LOGE("ExecIfaceUnregister getNetInterfaceCallback nullptr");
975         return false;
976     }
977 
978     int32_t ret = NetConnClient::GetInstance().UnregisterNetInterfaceCallback(callback);
979     if (ret != NETMANAGER_SUCCESS) {
980         NETMANAGER_BASE_LOGE("Unregister result %{public}d", ret);
981         context->SetErrorCode(ret);
982     }
983     return ret == NETMANAGER_SUCCESS;
984 }
985 
IfaceUnregisterCallback(IfaceUnregisterContext * context)986 napi_value ConnectionExec::NetInterfaceExec::IfaceUnregisterCallback(IfaceUnregisterContext *context)
987 {
988     return NapiUtils::GetUndefined(context->GetEnv());
989 }
990 
FillLinkAddress(napi_env env,napi_value connectionProperties,NetLinkInfo * linkInfo)991 void ConnectionExec::FillLinkAddress(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo)
992 {
993     if (!linkInfo->netAddrList_.empty() && linkInfo->netAddrList_.size() <= MAX_ARRAY_LENGTH) {
994         napi_value linkAddresses =
995             NapiUtils::CreateArray(env, std::min(linkInfo->netAddrList_.size(), MAX_ARRAY_LENGTH));
996         auto it = linkInfo->netAddrList_.begin();
997         for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != linkInfo->netAddrList_.end(); ++index, ++it) {
998             napi_value netAddr = NapiUtils::CreateObject(env);
999             NapiUtils::SetStringPropertyUtf8(env, netAddr, KEY_ADDRESS, it->address_);
1000             NapiUtils::SetUint32Property(env, netAddr, KEY_FAMILY, it->family_);
1001             NapiUtils::SetUint32Property(env, netAddr, KEY_PORT, it->port_);
1002 
1003             napi_value linkAddr = NapiUtils::CreateObject(env);
1004             NapiUtils::SetNamedProperty(env, linkAddr, KEY_ADDRESS, netAddr);
1005             NapiUtils::SetUint32Property(env, linkAddr, KEY_PREFIX_LENGTH, it->prefixlen_);
1006             NapiUtils::SetArrayElement(env, linkAddresses, index, linkAddr);
1007         }
1008         NapiUtils::SetNamedProperty(env, connectionProperties, KEY_LINK_ADDRESSES, linkAddresses);
1009     }
1010 }
1011 
FillRouoteList(napi_env env,napi_value connectionProperties,NetLinkInfo * linkInfo)1012 void ConnectionExec::FillRouoteList(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo)
1013 {
1014     if (!linkInfo->routeList_.empty() && linkInfo->routeList_.size() <= MAX_ARRAY_LENGTH) {
1015         napi_value routes = NapiUtils::CreateArray(env, std::min(linkInfo->routeList_.size(), MAX_ARRAY_LENGTH));
1016         auto it = linkInfo->routeList_.begin();
1017         for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != linkInfo->routeList_.end(); ++index, ++it) {
1018             napi_value route = NapiUtils::CreateObject(env);
1019             NapiUtils::SetStringPropertyUtf8(env, route, KEY_INTERFACE, it->iface_);
1020 
1021             napi_value dest = NapiUtils::CreateObject(env);
1022             napi_value netAddr = NapiUtils::CreateObject(env);
1023             NapiUtils::SetStringPropertyUtf8(env, netAddr, KEY_ADDRESS, it->destination_.address_);
1024             NapiUtils::SetUint32Property(env, netAddr, KEY_FAMILY, it->destination_.family_);
1025             NapiUtils::SetUint32Property(env, netAddr, KEY_PORT, it->destination_.port_);
1026             NapiUtils::SetNamedProperty(env, dest, KEY_ADDRESS, netAddr);
1027             NapiUtils::SetUint32Property(env, dest, KEY_PREFIX_LENGTH, it->destination_.prefixlen_);
1028             NapiUtils::SetNamedProperty(env, route, KEY_DESTINATION, dest);
1029 
1030             napi_value gateway = NapiUtils::CreateObject(env);
1031             NapiUtils::SetStringPropertyUtf8(env, gateway, KEY_ADDRESS, it->gateway_.address_);
1032             NapiUtils::SetUint32Property(env, gateway, KEY_PREFIX_LENGTH, it->gateway_.prefixlen_);
1033             NapiUtils::SetNamedProperty(env, route, KEY_GATE_WAY, gateway);
1034 
1035             NapiUtils::SetBooleanProperty(env, route, KEY_HAS_GET_WAY, it->hasGateway_);
1036             NapiUtils::SetBooleanProperty(env, route, KEY_IS_DEFAULT_ROUE, it->isDefaultRoute_);
1037 
1038             NapiUtils::SetArrayElement(env, routes, index, route);
1039         }
1040         NapiUtils::SetNamedProperty(env, connectionProperties, KEY_ROUTES, routes);
1041     }
1042 }
1043 
FillDns(napi_env env,napi_value connectionProperties,NetLinkInfo * linkInfo)1044 void ConnectionExec::FillDns(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo)
1045 {
1046     if (!linkInfo->dnsList_.empty() && linkInfo->dnsList_.size() <= MAX_ARRAY_LENGTH) {
1047         napi_value dnsList = NapiUtils::CreateArray(env, std::min(linkInfo->dnsList_.size(), MAX_ARRAY_LENGTH));
1048         auto it = linkInfo->dnsList_.begin();
1049         for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != linkInfo->dnsList_.end(); ++index, ++it) {
1050             napi_value netAddr = NapiUtils::CreateObject(env);
1051             NapiUtils::SetStringPropertyUtf8(env, netAddr, KEY_ADDRESS, it->address_);
1052             NapiUtils::SetUint32Property(env, netAddr, KEY_FAMILY, it->family_);
1053             NapiUtils::SetUint32Property(env, netAddr, KEY_PORT, it->port_);
1054             NapiUtils::SetArrayElement(env, dnsList, index, netAddr);
1055         }
1056         NapiUtils::SetNamedProperty(env, connectionProperties, KEY_DNSES, dnsList);
1057     }
1058 }
1059 } // namespace OHOS::NetManagerStandard
1060