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_conn_client.h"
27 #include "net_handle_interface.h"
28 #include "net_manager_constants.h"
29 #include "netconnection.h"
30 #include "netmanager_base_common_utils.h"
31 #include "netmanager_base_log.h"
32 #include "securec.h"
33
34 namespace OHOS::NetManagerStandard {
35 namespace {
36 constexpr int32_t NO_PERMISSION_CODE = 1;
37 constexpr int32_t PERMISSION_DENIED_CODE = 13;
38 constexpr int32_t NET_UNREACHABLE_CODE = 101;
39 } // namespace
40
CreateNetHandle(napi_env env,NetHandle * handle)41 napi_value ConnectionExec::CreateNetHandle(napi_env env, NetHandle *handle)
42 {
43 napi_value netHandle = NapiUtils::CreateObject(env);
44 if (NapiUtils::GetValueType(env, netHandle) != napi_object) {
45 return NapiUtils::GetUndefined(env);
46 }
47
48 std::initializer_list<napi_property_descriptor> properties = {
49 DECLARE_NAPI_FUNCTION(NetHandleInterface::FUNCTION_GET_ADDRESSES_BY_NAME,
50 NetHandleInterface::GetAddressesByName),
51 DECLARE_NAPI_FUNCTION(NetHandleInterface::FUNCTION_GET_ADDRESS_BY_NAME,
52 NetHandleInterface::GetAddressByName),
53 DECLARE_NAPI_FUNCTION(NetHandleInterface::FUNCTION_BIND_SOCKET,
54 NetHandleInterface::BindSocket),
55 };
56 NapiUtils::DefineProperties(env, netHandle, properties);
57 NapiUtils::SetUint32Property(env, netHandle, NetHandleInterface::PROPERTY_NET_ID, handle->GetNetId());
58 return netHandle;
59 }
60
CreateNetCapabilities(napi_env env,NetAllCapabilities * capabilities)61 napi_value ConnectionExec::CreateNetCapabilities(napi_env env, NetAllCapabilities *capabilities)
62 {
63 napi_value netCapabilities = NapiUtils::CreateObject(env);
64 if (NapiUtils::GetValueType(env, netCapabilities) != napi_object) {
65 return NapiUtils::GetUndefined(env);
66 }
67
68 NapiUtils::SetUint32Property(env, netCapabilities, KEY_LINK_UP_BAND_WIDTH_KPS, 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 = NapiUtils::CreateArray(env, std::min(capabilities->netCaps_.size(), MAX_ARRAY_LENGTH));
73 auto it = capabilities->netCaps_.begin();
74 for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != capabilities->netCaps_.end(); ++index, ++it) {
75 NapiUtils::SetArrayElement(env, networkCap, index, NapiUtils::CreateUint32(env, *it));
76 }
77 NapiUtils::SetNamedProperty(env, netCapabilities, KEY_NETWORK_CAP, networkCap);
78 }
79 if (!capabilities->bearerTypes_.empty() && capabilities->bearerTypes_.size() <= MAX_ARRAY_LENGTH) {
80 napi_value bearerTypes =
81 NapiUtils::CreateArray(env, std::min(capabilities->bearerTypes_.size(), MAX_ARRAY_LENGTH));
82 auto it = capabilities->bearerTypes_.begin();
83 for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != capabilities->bearerTypes_.end(); ++index, ++it) {
84 NapiUtils::SetArrayElement(env, bearerTypes, index, NapiUtils::CreateUint32(env, *it));
85 }
86 NapiUtils::SetNamedProperty(env, netCapabilities, KEY_BEARER_TYPE, bearerTypes);
87 }
88 return netCapabilities;
89 }
90
CreateConnectionProperties(napi_env env,NetLinkInfo * linkInfo)91 napi_value ConnectionExec::CreateConnectionProperties(napi_env env, NetLinkInfo *linkInfo)
92 {
93 napi_value connectionProperties = NapiUtils::CreateObject(env);
94 if (NapiUtils::GetValueType(env, connectionProperties) != napi_object) {
95 return NapiUtils::GetUndefined(env);
96 }
97 NapiUtils::SetStringPropertyUtf8(env, connectionProperties, KEY_INTERFACE_NAME, linkInfo->ifaceName_);
98 NapiUtils::SetStringPropertyUtf8(env, connectionProperties, KEY_DOMAINS, linkInfo->domain_);
99 NapiUtils::SetUint32Property(env, connectionProperties, KEY_MTU, linkInfo->mtu_);
100 FillLinkAddress(env, connectionProperties, linkInfo);
101 FillRouoteList(env, connectionProperties, linkInfo);
102 FillDns(env, connectionProperties, linkInfo);
103 return connectionProperties;
104 }
105
ExecGetAddressByName(GetAddressByNameContext * context)106 bool ConnectionExec::ExecGetAddressByName(GetAddressByNameContext *context)
107 {
108 return NetHandleExec::ExecGetAddressesByName(context);
109 }
110
GetAddressByNameCallback(GetAddressByNameContext * context)111 napi_value ConnectionExec::GetAddressByNameCallback(GetAddressByNameContext *context)
112 {
113 return NetHandleExec::GetAddressesByNameCallback(context);
114 }
115
ExecGetDefaultNet(GetDefaultNetContext * context)116 bool ConnectionExec::ExecGetDefaultNet(GetDefaultNetContext *context)
117 {
118 auto ret = NetConnClient::GetInstance().GetDefaultNet(context->netHandle_);
119 if (ret != NETMANAGER_SUCCESS) {
120 NETMANAGER_BASE_LOGE("get default net failed %{public}d", ret);
121 context->SetErrorCode(ret);
122 }
123 return ret == NETMANAGER_SUCCESS;
124 }
125
GetDefaultNetCallback(GetDefaultNetContext * context)126 napi_value ConnectionExec::GetDefaultNetCallback(GetDefaultNetContext *context)
127 {
128 return CreateNetHandle(context->GetEnv(), &context->netHandle_);
129 }
130
ExecHasDefaultNet(HasDefaultNetContext * context)131 bool ConnectionExec::ExecHasDefaultNet(HasDefaultNetContext *context)
132 {
133 auto ret = NetConnClient::GetInstance().HasDefaultNet(context->hasDefaultNet_);
134 if (ret != NETMANAGER_SUCCESS && ret != NET_CONN_ERR_NO_DEFAULT_NET) {
135 NETMANAGER_BASE_LOGE("ExecHasDefaultNet ret %{public}d", ret);
136 context->SetErrorCode(ret);
137 return false;
138 }
139 return true;
140 }
141
HasDefaultNetCallback(HasDefaultNetContext * context)142 napi_value ConnectionExec::HasDefaultNetCallback(HasDefaultNetContext *context)
143 {
144 return NapiUtils::GetBoolean(context->GetEnv(), context->hasDefaultNet_);
145 }
146
ExecIsDefaultNetMetered(IsDefaultNetMeteredContext * context)147 bool ConnectionExec::ExecIsDefaultNetMetered(IsDefaultNetMeteredContext *context)
148 {
149 auto ret = NetConnClient::GetInstance().IsDefaultNetMetered(context->isMetered_);
150 if (ret != NETMANAGER_SUCCESS) {
151 NETMANAGER_BASE_LOGE("get net metered status failed %{public}d", ret);
152 context->SetErrorCode(ret);
153 }
154 return ret == NETMANAGER_SUCCESS;
155 }
156
IsDefaultNetMeteredCallback(IsDefaultNetMeteredContext * context)157 napi_value ConnectionExec::IsDefaultNetMeteredCallback(IsDefaultNetMeteredContext *context)
158 {
159 return NapiUtils::GetBoolean(context->GetEnv(), context->isMetered_);
160 }
161
ExecGetNetCapabilities(GetNetCapabilitiesContext * context)162 bool ConnectionExec::ExecGetNetCapabilities(GetNetCapabilitiesContext *context)
163 {
164 if (!context->IsParseOK()) {
165 return false;
166 }
167 auto ret = NetConnClient::GetInstance().GetNetCapabilities(context->netHandle_, context->capabilities_);
168 context->SetErrorCode(ret);
169 return ret == NETMANAGER_SUCCESS;
170 }
171
GetNetCapabilitiesCallback(GetNetCapabilitiesContext * context)172 napi_value ConnectionExec::GetNetCapabilitiesCallback(GetNetCapabilitiesContext *context)
173 {
174 return CreateNetCapabilities(context->GetEnv(), &context->capabilities_);
175 }
176
ExecGetConnectionProperties(GetConnectionPropertiesContext * context)177 bool ConnectionExec::ExecGetConnectionProperties(GetConnectionPropertiesContext *context)
178 {
179 if (!context->IsParseOK()) {
180 return false;
181 }
182 auto ret = NetConnClient::GetInstance().GetConnectionProperties(context->netHandle_, context->linkInfo_);
183 context->SetErrorCode(ret);
184 return ret == NETMANAGER_SUCCESS;
185 }
186
GetConnectionPropertiesCallback(GetConnectionPropertiesContext * context)187 napi_value ConnectionExec::GetConnectionPropertiesCallback(GetConnectionPropertiesContext *context)
188 {
189 return CreateConnectionProperties(context->GetEnv(), &context->linkInfo_);
190 }
191
ExecGetAllNets(GetAllNetsContext * context)192 bool ConnectionExec::ExecGetAllNets(GetAllNetsContext *context)
193 {
194 int32_t ret = NetConnClient::GetInstance().GetAllNets(context->netHandleList_);
195 context->SetErrorCode(ret);
196 return ret == NETMANAGER_SUCCESS;
197 }
198
GetAllNetsCallback(GetAllNetsContext * context)199 napi_value ConnectionExec::GetAllNetsCallback(GetAllNetsContext *context)
200 {
201 napi_value array = NapiUtils::CreateArray(context->GetEnv(), context->netHandleList_.size());
202 uint32_t index = 0;
203 std::for_each(context->netHandleList_.begin(), context->netHandleList_.end(),
204 [array, &index, context](const sptr<NetHandle> &handle) {
205 NapiUtils::SetArrayElement(context->GetEnv(), array, index,
206 CreateNetHandle(context->GetEnv(), handle.GetRefPtr()));
207 ++index;
208 });
209 return array;
210 }
211
ExecEnableAirplaneMode(EnableAirplaneModeContext * context)212 bool ConnectionExec::ExecEnableAirplaneMode(EnableAirplaneModeContext *context)
213 {
214 int32_t res = NetConnClient::GetInstance().SetAirplaneMode(true);
215 if (res != NETMANAGER_SUCCESS) {
216 NETMANAGER_BASE_LOGE("ExecEnableAirplaneMode failed %{public}d", res);
217 context->SetErrorCode(res);
218 }
219 return res == NETMANAGER_SUCCESS;
220 }
221
EnableAirplaneModeCallback(EnableAirplaneModeContext * context)222 napi_value ConnectionExec::EnableAirplaneModeCallback(EnableAirplaneModeContext *context)
223 {
224 return NapiUtils::GetUndefined(context->GetEnv());
225 }
226
ExecDisableAirplaneMode(DisableAirplaneModeContext * context)227 bool ConnectionExec::ExecDisableAirplaneMode(DisableAirplaneModeContext *context)
228 {
229 int32_t res = NetConnClient::GetInstance().SetAirplaneMode(false);
230 if (res != NETMANAGER_SUCCESS) {
231 NETMANAGER_BASE_LOGE("ExecDisableAirplaneMode failed %{public}d", res);
232 context->SetErrorCode(res);
233 }
234 return res == NETMANAGER_SUCCESS;
235 }
236
DisableAirplaneModeCallback(DisableAirplaneModeContext * context)237 napi_value ConnectionExec::DisableAirplaneModeCallback(DisableAirplaneModeContext *context)
238 {
239 return NapiUtils::GetUndefined(context->GetEnv());
240 }
241
ExecReportNetConnected(ReportNetConnectedContext * context)242 bool ConnectionExec::ExecReportNetConnected(ReportNetConnectedContext *context)
243 {
244 if (!context->IsParseOK()) {
245 return false;
246 }
247 int32_t res = NetConnClient::GetInstance().NetDetection(context->netHandle_);
248 if (res != NETMANAGER_SUCCESS) {
249 NETMANAGER_BASE_LOGE("ExecReportNetConnected failed %{public}d", res);
250 context->SetErrorCode(res);
251 }
252 return res == NETMANAGER_SUCCESS;
253 }
254
ReportNetConnectedCallback(ReportNetConnectedContext * context)255 napi_value ConnectionExec::ReportNetConnectedCallback(ReportNetConnectedContext *context)
256 {
257 return NapiUtils::GetUndefined(context->GetEnv());
258 }
259
ExecReportNetDisconnected(ReportNetConnectedContext * context)260 bool ConnectionExec::ExecReportNetDisconnected(ReportNetConnectedContext *context)
261 {
262 if (!context->IsParseOK()) {
263 return false;
264 }
265 int32_t res = NetConnClient::GetInstance().NetDetection(context->netHandle_);
266 if (res != NETMANAGER_SUCCESS) {
267 NETMANAGER_BASE_LOGE("ExecReportNetDisconnected failed %{public}d", res);
268 context->SetErrorCode(res);
269 }
270 return res == NETMANAGER_SUCCESS;
271 }
272
ReportNetDisconnectedCallback(ReportNetConnectedContext * context)273 napi_value ConnectionExec::ReportNetDisconnectedCallback(ReportNetConnectedContext *context)
274 {
275 return NapiUtils::GetUndefined(context->GetEnv());
276 }
277
ExecGetDefaultHttpProxy(GetHttpProxyContext * context)278 bool ConnectionExec::ExecGetDefaultHttpProxy(GetHttpProxyContext *context)
279 {
280 int32_t errorCode = NetConnClient::GetInstance().GetDefaultHttpProxy(context->httpProxy_);
281 if (errorCode != NET_CONN_SUCCESS) {
282 context->SetErrorCode(errorCode);
283 return false;
284 }
285 return true;
286 }
287
GetDefaultHttpProxyCallback(GetHttpProxyContext * context)288 napi_value ConnectionExec::GetDefaultHttpProxyCallback(GetHttpProxyContext *context)
289 {
290 napi_value host = NapiUtils::CreateStringUtf8(context->GetEnv(), context->httpProxy_.GetHost());
291 napi_value port = NapiUtils::CreateInt32(context->GetEnv(), context->httpProxy_.GetPort());
292 auto lists = context->httpProxy_.GetExclusionList();
293 napi_value exclusionList = NapiUtils::CreateArray(context->GetEnv(), lists.size());
294 size_t index = 0;
295 for (auto list : lists) {
296 napi_value jsList = NapiUtils::CreateStringUtf8(context->GetEnv(), list);
297 NapiUtils::SetArrayElement(context->GetEnv(), exclusionList, index++, jsList);
298 }
299 napi_value httpProxy = NapiUtils::CreateObject(context->GetEnv());
300 NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "host", host);
301 NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "port", port);
302 NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "exclusionList", exclusionList);
303 return httpProxy;
304 }
305
ExecGetGlobalHttpProxy(GetHttpProxyContext * context)306 bool ConnectionExec::ExecGetGlobalHttpProxy(GetHttpProxyContext *context)
307 {
308 int32_t errorCode = NetConnClient::GetInstance().GetGlobalHttpProxy(context->httpProxy_);
309 if (errorCode != NET_CONN_SUCCESS) {
310 context->SetErrorCode(errorCode);
311 return false;
312 }
313 return true;
314 }
315
GetGlobalHttpProxyCallback(GetHttpProxyContext * context)316 napi_value ConnectionExec::GetGlobalHttpProxyCallback(GetHttpProxyContext *context)
317 {
318 napi_value host = NapiUtils::CreateStringUtf8(context->GetEnv(), context->httpProxy_.GetHost());
319 napi_value port = NapiUtils::CreateInt32(context->GetEnv(), context->httpProxy_.GetPort());
320 auto lists = context->httpProxy_.GetExclusionList();
321 napi_value exclusionList = NapiUtils::CreateArray(context->GetEnv(), lists.size());
322 size_t index = 0;
323 for (auto list : lists) {
324 napi_value jsList = NapiUtils::CreateStringUtf8(context->GetEnv(), list);
325 NapiUtils::SetArrayElement(context->GetEnv(), exclusionList, index++, jsList);
326 }
327 napi_value httpProxy = NapiUtils::CreateObject(context->GetEnv());
328 NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "host", host);
329 NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "port", port);
330 NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "exclusionList", exclusionList);
331 return httpProxy;
332 }
333
ExecSetGlobalHttpProxy(SetGlobalHttpProxyContext * context)334 bool ConnectionExec::ExecSetGlobalHttpProxy(SetGlobalHttpProxyContext *context)
335 {
336 int32_t errorCode = NetConnClient::GetInstance().SetGlobalHttpProxy(context->httpProxy_);
337 if (errorCode != NET_CONN_SUCCESS) {
338 context->SetErrorCode(errorCode);
339 return false;
340 }
341 return true;
342 }
343
SetGlobalHttpProxyCallback(SetGlobalHttpProxyContext * context)344 napi_value ConnectionExec::SetGlobalHttpProxyCallback(SetGlobalHttpProxyContext *context)
345 {
346 return NapiUtils::GetUndefined(context->GetEnv());
347 }
348
ExecSetAppHttpProxy(SetAppHttpProxyContext * context)349 bool ConnectionExec::ExecSetAppHttpProxy(SetAppHttpProxyContext *context)
350 {
351 int32_t errorCode = NetConnClient::GetInstance().SetAppHttpProxy(context->httpProxy_);
352 if (errorCode != NET_CONN_SUCCESS) {
353 context->SetErrorCode(errorCode);
354 return false;
355 }
356 return true;
357 }
358
SetAppHttpProxyCallback(SetAppHttpProxyContext * context)359 napi_value ConnectionExec::SetAppHttpProxyCallback(SetAppHttpProxyContext *context)
360 {
361 return NapiUtils::GetUndefined(context->GetEnv());
362 }
363
ExecGetAppNet(GetAppNetContext * context)364 bool ConnectionExec::ExecGetAppNet(GetAppNetContext *context)
365 {
366 int32_t netId = 0;
367 int32_t errorCode = NetConnClient::GetInstance().GetAppNet(netId);
368 if (errorCode != NET_CONN_SUCCESS) {
369 NETMANAGER_BASE_LOGE("exec getAppNet failed errorCode: %{public}d", errorCode);
370 context->SetErrorCode(errorCode);
371 return false;
372 }
373 context->netHandle_.SetNetId(netId);
374 return true;
375 }
376
GetAppNetCallback(GetAppNetContext * context)377 napi_value ConnectionExec::GetAppNetCallback(GetAppNetContext *context)
378 {
379 return CreateNetHandle(context->GetEnv(), &context->netHandle_);
380 }
381
ExecSetAppNet(SetAppNetContext * context)382 bool ConnectionExec::ExecSetAppNet(SetAppNetContext *context)
383 {
384 NETMANAGER_BASE_LOGI("into");
385 int32_t errorCode = NetConnClient::GetInstance().SetAppNet(context->netHandle_.GetNetId());
386 if (errorCode != NET_CONN_SUCCESS) {
387 NETMANAGER_BASE_LOGE("exec setAppNet failed errorCode: %{public}d", errorCode);
388 context->SetErrorCode(errorCode);
389 return false;
390 }
391 return true;
392 }
393
SetAppNetCallback(SetAppNetContext * context)394 napi_value ConnectionExec::SetAppNetCallback(SetAppNetContext *context)
395 {
396 return NapiUtils::GetUndefined(context->GetEnv());
397 }
398
ExecSetCustomDNSRule(SetCustomDNSRuleContext * context)399 bool ConnectionExec::ExecSetCustomDNSRule(SetCustomDNSRuleContext *context)
400 {
401 if (context == nullptr) {
402 NETMANAGER_BASE_LOGE("context is nullptr");
403 return false;
404 }
405
406 if (!CommonUtils::HasInternetPermission()) {
407 context->SetErrorCode(NETMANAGER_ERR_PERMISSION_DENIED);
408 return false;
409 }
410
411 if (context->host_.empty() || context->ip_.empty()) {
412 context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
413 return false;
414 }
415
416 std::vector<std::string> ip = context->ip_;
417 for (size_t i = 0; i < ip.size(); i++) {
418 if (!CommonUtils::IsValidIPV4(ip[i]) && !CommonUtils::IsValidIPV6(ip[i])) {
419 context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
420 return false;
421 }
422 }
423
424 if (!context->IsParseOK()) {
425 context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
426 return false;
427 }
428
429 std::string host_ips = context->host_ + ",";
430 for (size_t i = 0; i < ip.size(); i++) {
431 host_ips.append(ip[i]);
432 if (i < ip.size() - 1) {
433 host_ips.append(",");
434 }
435 }
436
437 NETMANAGER_BASE_LOGI("set host with ip addr");
438 int res = predefined_host_set_hosts(host_ips.c_str());
439 if (res != NETMANAGER_SUCCESS) {
440 NETMANAGER_BASE_LOGE("ExecSetCustomDNSRule failed %{public}d", res);
441 context->SetErrorCode(res);
442 return false;
443 }
444
445 return true;
446 }
447
SetCustomDNSRuleCallback(SetCustomDNSRuleContext * context)448 napi_value ConnectionExec::SetCustomDNSRuleCallback(SetCustomDNSRuleContext *context)
449 {
450 return NapiUtils::GetUndefined(context->GetEnv());
451 }
452
ExecDeleteCustomDNSRule(DeleteCustomDNSRuleContext * context)453 bool ConnectionExec::ExecDeleteCustomDNSRule(DeleteCustomDNSRuleContext *context)
454 {
455 if (context == nullptr) {
456 NETMANAGER_BASE_LOGE("context is nullptr");
457 return false;
458 }
459 if (!CommonUtils::HasInternetPermission()) {
460 context->SetErrorCode(NETMANAGER_ERR_PERMISSION_DENIED);
461 return false;
462 }
463
464 if (context->host_.empty()) {
465 context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
466 return false;
467 }
468
469 if (!context->IsParseOK()) {
470 context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
471 return false;
472 }
473
474 NETMANAGER_BASE_LOGI("delete host with ip addr");
475 int res = predefined_host_remove_host(context->host_.c_str());
476 if (res != NETMANAGER_SUCCESS) {
477 NETMANAGER_BASE_LOGE("ExecDeleteCustomDNSRule failed %{public}d", res);
478 context->SetErrorCode(res);
479 return false;
480 }
481 return true;
482 }
483
DeleteCustomDNSRuleCallback(DeleteCustomDNSRuleContext * context)484 napi_value ConnectionExec::DeleteCustomDNSRuleCallback(DeleteCustomDNSRuleContext *context)
485 {
486 return NapiUtils::GetUndefined(context->GetEnv());
487 }
488
ExecDeleteCustomDNSRules(DeleteCustomDNSRulesContext * context)489 bool ConnectionExec::ExecDeleteCustomDNSRules(DeleteCustomDNSRulesContext *context)
490 {
491 if (context == nullptr) {
492 NETMANAGER_BASE_LOGE("context is nullptr");
493 return false;
494 }
495 if (!CommonUtils::HasInternetPermission()) {
496 context->SetErrorCode(NETMANAGER_ERR_PERMISSION_DENIED);
497 return false;
498 }
499
500 if (!context->IsParseOK()) {
501 context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
502 return false;
503 }
504
505 int res = predefined_host_clear_all_hosts();
506 if (res != NETMANAGER_SUCCESS) {
507 NETMANAGER_BASE_LOGE("ExecDeleteCustomDNSRules failed %{public}d", res);
508 context->SetErrorCode(res);
509 return false;
510 }
511
512 return true;
513 }
514
DeleteCustomDNSRulesCallback(DeleteCustomDNSRulesContext * context)515 napi_value ConnectionExec::DeleteCustomDNSRulesCallback(DeleteCustomDNSRulesContext *context)
516 {
517 return NapiUtils::GetUndefined(context->GetEnv());
518 }
519
ExecFactoryResetNetwork(FactoryResetNetworkContext * context)520 bool ConnectionExec::ExecFactoryResetNetwork(FactoryResetNetworkContext *context)
521 {
522 NETMANAGER_BASE_LOGI("ExecFactoryResetNetwork into");
523 int32_t errorCode = NetConnClient::GetInstance().FactoryResetNetwork();
524 if (errorCode != NET_CONN_SUCCESS) {
525 NETMANAGER_BASE_LOGE("exec ResetNetwork failed errorCode: %{public}d", errorCode);
526 context->SetErrorCode(errorCode);
527 return false;
528 }
529 return true;
530 }
531
FactoryResetNetworkCallback(FactoryResetNetworkContext * context)532 napi_value ConnectionExec::FactoryResetNetworkCallback(FactoryResetNetworkContext *context)
533 {
534 return NapiUtils::GetUndefined(context->GetEnv());
535 }
536
TransErrorCode(int32_t error)537 int32_t TransErrorCode(int32_t error)
538 {
539 switch (error) {
540 case NO_PERMISSION_CODE:
541 return NETMANAGER_ERR_PERMISSION_DENIED;
542 case PERMISSION_DENIED_CODE:
543 return NETMANAGER_ERR_PERMISSION_DENIED;
544 case NET_UNREACHABLE_CODE:
545 return NETMANAGER_ERR_INTERNAL;
546 default:
547 return NETMANAGER_ERR_OPERATION_FAILED;
548 }
549 }
550
ExecGetAddressesByName(GetAddressByNameContext * context)551 bool ConnectionExec::NetHandleExec::ExecGetAddressesByName(GetAddressByNameContext *context)
552 {
553 if (!context->IsParseOK()) {
554 return false;
555 }
556 uint32_t netid = static_cast<uint32_t>(context->netId_);
557 addrinfo *res = nullptr;
558 queryparam param;
559 param.qp_type = QEURY_TYPE_NORMAL;
560 param.qp_netid = netid;
561 NETMANAGER_BASE_LOGD("getaddrinfo_ext %{public}d %{public}d", netid, param.qp_netid);
562 if (context->host_.empty()) {
563 NETMANAGER_BASE_LOGE("host is empty!");
564 context->SetErrorCode(NETMANAGER_ERR_INVALID_PARAMETER);
565 return false;
566 }
567
568 int status = getaddrinfo_ext(context->host_.c_str(), nullptr, nullptr, &res, ¶m);
569 if (status < 0) {
570 NETMANAGER_BASE_LOGE("getaddrinfo errno %{public}d %{public}s, status: %{public}d", errno, strerror(errno),
571 status);
572 int32_t temp = TransErrorCode(errno);
573 context->SetErrorCode(temp);
574 return false;
575 }
576
577 for (addrinfo *tmp = res; tmp != nullptr; tmp = tmp->ai_next) {
578 std::string host;
579 if (tmp->ai_family == AF_INET) {
580 auto addr = reinterpret_cast<sockaddr_in *>(tmp->ai_addr);
581 char ip[MAX_IPV4_STR_LEN] = {0};
582 inet_ntop(AF_INET, &addr->sin_addr, ip, sizeof(ip));
583 host = ip;
584 } else if (tmp->ai_family == AF_INET6) {
585 auto addr = reinterpret_cast<sockaddr_in6 *>(tmp->ai_addr);
586 char ip[MAX_IPV6_STR_LEN] = {0};
587 inet_ntop(AF_INET6, &addr->sin6_addr, ip, sizeof(ip));
588 host = ip;
589 }
590
591 NetAddress address;
592 SetAddressInfo(host.c_str(), tmp, address);
593
594 context->addresses_.emplace_back(address);
595 }
596 freeaddrinfo(res);
597 return true;
598 }
599
GetAddressesByNameCallback(GetAddressByNameContext * context)600 napi_value ConnectionExec::NetHandleExec::GetAddressesByNameCallback(GetAddressByNameContext *context)
601 {
602 napi_value addresses = NapiUtils::CreateArray(context->GetEnv(), context->addresses_.size());
603 for (uint32_t index = 0; index < context->addresses_.size(); ++index) {
604 napi_value obj = MakeNetAddressJsValue(context->GetEnv(), context->addresses_[index]);
605 NapiUtils::SetArrayElement(context->GetEnv(), addresses, index, obj);
606 }
607 return addresses;
608 }
609
ExecGetAddressByName(GetAddressByNameContext * context)610 bool ConnectionExec::NetHandleExec::ExecGetAddressByName(GetAddressByNameContext *context)
611 {
612 if (!context->IsParseOK()) {
613 return false;
614 }
615 uint32_t netid = static_cast<uint32_t>(context->netId_);
616 addrinfo *res = nullptr;
617 queryparam param;
618 param.qp_type = QEURY_TYPE_NORMAL;
619 param.qp_netid = netid;
620 NETMANAGER_BASE_LOGD("getaddrinfo_ext %{public}d %{public}d", netid, param.qp_netid);
621 if (context->host_.empty()) {
622 NETMANAGER_BASE_LOGE("host is empty!");
623 context->SetErrorCode(NETMANAGER_ERR_INVALID_PARAMETER);
624 return false;
625 }
626
627 int status = getaddrinfo_ext(context->host_.c_str(), nullptr, nullptr, &res, ¶m);
628 if (status < 0) {
629 NETMANAGER_BASE_LOGE("getaddrinfo errno %{public}d %{public}s, status: %{public}d", errno, strerror(errno),
630 status);
631 int32_t temp = TransErrorCode(errno);
632 context->SetErrorCode(temp);
633 return false;
634 }
635
636 if (res != nullptr) {
637 std::string host;
638 if (res->ai_family == AF_INET) {
639 auto addr = reinterpret_cast<sockaddr_in *>(res->ai_addr);
640 char ip[MAX_IPV4_STR_LEN] = {0};
641 inet_ntop(AF_INET, &addr->sin_addr, ip, sizeof(ip));
642 host = ip;
643 } else if (res->ai_family == AF_INET6) {
644 auto addr = reinterpret_cast<sockaddr_in6 *>(res->ai_addr);
645 char ip[MAX_IPV6_STR_LEN] = {0};
646 inet_ntop(AF_INET6, &addr->sin6_addr, ip, sizeof(ip));
647 host = ip;
648 }
649
650 NetAddress address;
651 SetAddressInfo(host.c_str(), res, address);
652
653 context->addresses_.emplace_back(address);
654 }
655 freeaddrinfo(res);
656 return true;
657 }
658
GetAddressByNameCallback(GetAddressByNameContext * context)659 napi_value ConnectionExec::NetHandleExec::GetAddressByNameCallback(GetAddressByNameContext *context)
660 {
661 if (context->addresses_.empty()) {
662 return NapiUtils::GetUndefined(context->GetEnv());
663 }
664 return MakeNetAddressJsValue(context->GetEnv(), context->addresses_[0]);
665 }
666
MakeNetAddressJsValue(napi_env env,const NetAddress & address)667 napi_value ConnectionExec::NetHandleExec::MakeNetAddressJsValue(napi_env env, const NetAddress &address)
668 {
669 napi_value obj = NapiUtils::CreateObject(env);
670 if (NapiUtils::GetValueType(env, obj) != napi_object) {
671 return NapiUtils::GetUndefined(env);
672 }
673
674 NapiUtils::SetStringPropertyUtf8(env, obj, KEY_ADDRESS, address.GetAddress());
675 NapiUtils::SetUint32Property(env, obj, KEY_FAMILY, address.GetJsValueFamily());
676 NapiUtils::SetUint32Property(env, obj, KEY_PORT, address.GetPort());
677 return obj;
678 }
679
ExecBindSocket(BindSocketContext * context)680 bool ConnectionExec::NetHandleExec::ExecBindSocket(BindSocketContext *context)
681 {
682 if (!context->IsParseOK()) {
683 return false;
684 }
685 NetHandle handle(context->netId_);
686 int32_t res = handle.BindSocket(context->socketFd_);
687 if (res != NETMANAGER_SUCCESS) {
688 NETMANAGER_BASE_LOGE("ExecBindSocket failed %{public}d", res);
689 context->SetErrorCode(res);
690 }
691 return res == NETMANAGER_SUCCESS;
692 }
693
BindSocketCallback(BindSocketContext * context)694 napi_value ConnectionExec::NetHandleExec::BindSocketCallback(BindSocketContext *context)
695 {
696 return NapiUtils::GetUndefined(context->GetEnv());
697 }
698
SetAddressInfo(const char * host,addrinfo * info,NetAddress & address)699 void ConnectionExec::NetHandleExec::SetAddressInfo(const char *host, addrinfo *info, NetAddress &address)
700 {
701 address.SetAddress(host);
702 address.SetFamilyBySaFamily(info->ai_addr->sa_family);
703 if (info->ai_addr->sa_family == AF_INET) {
704 auto addr4 = reinterpret_cast<sockaddr_in *>(info->ai_addr);
705 address.SetPort(addr4->sin_port);
706 } else if (info->ai_addr->sa_family == AF_INET6) {
707 auto addr6 = reinterpret_cast<sockaddr_in6 *>(info->ai_addr);
708 address.SetPort(addr6->sin6_port);
709 }
710 }
711
ExecRegister(RegisterContext * context)712 bool ConnectionExec::NetConnectionExec::ExecRegister(RegisterContext *context)
713 {
714 auto wCallback = context->GetNetConnCallback();
715 sptr<INetConnCallback> callback = wCallback.promote();
716 if (callback == nullptr) {
717 NETMANAGER_BASE_LOGE("ExecRegister getNetConnCallback nullptr");
718 return false;
719 }
720
721 auto conn = context->GetConn();
722 if (conn.hasNetSpecifier_ && conn.hasTimeout_) {
723 sptr<NetSpecifier> specifier = new NetSpecifier(conn.netSpecifier_);
724 int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, callback, conn.timeout_);
725 NETMANAGER_BASE_LOGI("Register result hasNetSpecifier_ and hasTimeout_ %{public}d", ret);
726 context->SetErrorCode(ret);
727 return ret == NETMANAGER_SUCCESS;
728 }
729
730 if (conn.hasNetSpecifier_) {
731 sptr<NetSpecifier> specifier = new NetSpecifier(conn.netSpecifier_);
732 int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, callback, 0);
733 NETMANAGER_BASE_LOGI("Register result hasNetSpecifier_ %{public}d", ret);
734 context->SetErrorCode(ret);
735 return ret == NETMANAGER_SUCCESS;
736 }
737
738 int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(callback);
739 NETMANAGER_BASE_LOGI("Register result %{public}d", ret);
740 context->SetErrorCode(ret);
741 return ret == NETMANAGER_SUCCESS;
742 }
743
RegisterCallback(RegisterContext * context)744 napi_value ConnectionExec::NetConnectionExec::RegisterCallback(RegisterContext *context)
745 {
746 return NapiUtils::GetUndefined(context->GetEnv());
747 }
748
ExecUnregister(UnregisterContext * context)749 bool ConnectionExec::NetConnectionExec::ExecUnregister(UnregisterContext *context)
750 {
751 auto wCallback = context->GetNetConnCallback();
752 auto callback = wCallback.promote();
753 if (callback == nullptr) {
754 NETMANAGER_BASE_LOGE("ExecUnregister getNetConnCallback nullptr");
755 return false;
756 }
757
758 int32_t ret = NetConnClient::GetInstance().UnregisterNetConnCallback(callback);
759 if (ret != NETMANAGER_SUCCESS) {
760 NETMANAGER_BASE_LOGE("Unregister result %{public}d", ret);
761 context->SetErrorCode(ret);
762 }
763 return ret == NETMANAGER_SUCCESS;
764 }
765
UnregisterCallback(RegisterContext * context)766 napi_value ConnectionExec::NetConnectionExec::UnregisterCallback(RegisterContext *context)
767 {
768 return NapiUtils::GetUndefined(context->GetEnv());
769 }
770
FillLinkAddress(napi_env env,napi_value connectionProperties,NetLinkInfo * linkInfo)771 void ConnectionExec::FillLinkAddress(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo)
772 {
773 if (!linkInfo->netAddrList_.empty() && linkInfo->netAddrList_.size() <= MAX_ARRAY_LENGTH) {
774 napi_value linkAddresses =
775 NapiUtils::CreateArray(env, std::min(linkInfo->netAddrList_.size(), MAX_ARRAY_LENGTH));
776 auto it = linkInfo->netAddrList_.begin();
777 for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != linkInfo->netAddrList_.end(); ++index, ++it) {
778 napi_value netAddr = NapiUtils::CreateObject(env);
779 NapiUtils::SetStringPropertyUtf8(env, netAddr, KEY_ADDRESS, it->address_);
780 NapiUtils::SetUint32Property(env, netAddr, KEY_FAMILY, it->family_);
781 NapiUtils::SetUint32Property(env, netAddr, KEY_PORT, it->port_);
782
783 napi_value linkAddr = NapiUtils::CreateObject(env);
784 NapiUtils::SetNamedProperty(env, linkAddr, KEY_ADDRESS, netAddr);
785 NapiUtils::SetUint32Property(env, linkAddr, KEY_PREFIX_LENGTH, it->prefixlen_);
786 NapiUtils::SetArrayElement(env, linkAddresses, index, linkAddr);
787 }
788 NapiUtils::SetNamedProperty(env, connectionProperties, KEY_LINK_ADDRESSES, linkAddresses);
789 }
790 }
791
FillRouoteList(napi_env env,napi_value connectionProperties,NetLinkInfo * linkInfo)792 void ConnectionExec::FillRouoteList(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo)
793 {
794 if (!linkInfo->routeList_.empty() && linkInfo->routeList_.size() <= MAX_ARRAY_LENGTH) {
795 napi_value routes = NapiUtils::CreateArray(env, std::min(linkInfo->routeList_.size(), MAX_ARRAY_LENGTH));
796 auto it = linkInfo->routeList_.begin();
797 for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != linkInfo->routeList_.end(); ++index, ++it) {
798 napi_value route = NapiUtils::CreateObject(env);
799 NapiUtils::SetStringPropertyUtf8(env, route, KEY_INTERFACE, it->iface_);
800
801 napi_value dest = NapiUtils::CreateObject(env);
802 napi_value netAddr = NapiUtils::CreateObject(env);
803 NapiUtils::SetStringPropertyUtf8(env, netAddr, KEY_ADDRESS, it->destination_.address_);
804 NapiUtils::SetUint32Property(env, netAddr, KEY_FAMILY, it->destination_.family_);
805 NapiUtils::SetUint32Property(env, netAddr, KEY_PORT, it->destination_.port_);
806 NapiUtils::SetNamedProperty(env, dest, KEY_ADDRESS, netAddr);
807 NapiUtils::SetUint32Property(env, dest, KEY_PREFIX_LENGTH, it->destination_.prefixlen_);
808 NapiUtils::SetNamedProperty(env, route, KEY_DESTINATION, dest);
809
810 napi_value gateway = NapiUtils::CreateObject(env);
811 NapiUtils::SetStringPropertyUtf8(env, gateway, KEY_ADDRESS, it->gateway_.address_);
812 NapiUtils::SetUint32Property(env, gateway, KEY_PREFIX_LENGTH, it->gateway_.prefixlen_);
813 NapiUtils::SetNamedProperty(env, route, KEY_GATE_WAY, gateway);
814
815 NapiUtils::SetBooleanProperty(env, route, KEY_HAS_GET_WAY, it->hasGateway_);
816 NapiUtils::SetBooleanProperty(env, route, KEY_IS_DEFAULT_ROUE, it->isDefaultRoute_);
817
818 NapiUtils::SetArrayElement(env, routes, index, route);
819 }
820 NapiUtils::SetNamedProperty(env, connectionProperties, KEY_ROUTES, routes);
821 }
822 }
823
FillDns(napi_env env,napi_value connectionProperties,NetLinkInfo * linkInfo)824 void ConnectionExec::FillDns(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo)
825 {
826 if (!linkInfo->dnsList_.empty() && linkInfo->dnsList_.size() <= MAX_ARRAY_LENGTH) {
827 napi_value dnsList = NapiUtils::CreateArray(env, std::min(linkInfo->dnsList_.size(), MAX_ARRAY_LENGTH));
828 auto it = linkInfo->dnsList_.begin();
829 for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != linkInfo->dnsList_.end(); ++index, ++it) {
830 napi_value netAddr = NapiUtils::CreateObject(env);
831 NapiUtils::SetStringPropertyUtf8(env, netAddr, KEY_ADDRESS, it->address_);
832 NapiUtils::SetUint32Property(env, netAddr, KEY_FAMILY, it->family_);
833 NapiUtils::SetUint32Property(env, netAddr, KEY_PORT, it->port_);
834 NapiUtils::SetArrayElement(env, dnsList, index, netAddr);
835 }
836 NapiUtils::SetNamedProperty(env, connectionProperties, KEY_DNSES, dnsList);
837 }
838 }
839 } // namespace OHOS::NetManagerStandard
840