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 #include <mutex>
22
23 #include "constant.h"
24 #include "errorcode_convertor.h"
25 #include "napi_utils.h"
26 #include "net_conn_callback_observer.h"
27 #include "net_interface_callback_observer.h"
28 #include "net_conn_client.h"
29 #include "net_handle_interface.h"
30 #include "net_manager_constants.h"
31 #include "netconnection.h"
32 #include "netinterface.h"
33 #include "netmanager_base_common_utils.h"
34 #include "netmanager_base_log.h"
35 #include "securec.h"
36 #include "hi_app_event_report.h"
37
38 namespace OHOS::NetManagerStandard {
39 std::mutex g_predefinedHostMtx;
40
41 namespace {
42 constexpr int32_t NO_PERMISSION_CODE = 1;
43 constexpr int32_t PERMISSION_DENIED_CODE = 13;
44 constexpr int32_t NET_UNREACHABLE_CODE = 101;
45 } // namespace
46
CreateNetHandle(napi_env env,NetHandle * handle)47 napi_value ConnectionExec::CreateNetHandle(napi_env env, NetHandle *handle)
48 {
49 napi_value netHandle = NapiUtils::CreateObject(env);
50 if (NapiUtils::GetValueType(env, netHandle) != napi_object) {
51 return NapiUtils::GetUndefined(env);
52 }
53
54 std::initializer_list<napi_property_descriptor> properties = {
55 DECLARE_NAPI_FUNCTION(NetHandleInterface::FUNCTION_GET_ADDRESSES_BY_NAME,
56 NetHandleInterface::GetAddressesByName),
57 DECLARE_NAPI_FUNCTION(NetHandleInterface::FUNCTION_GET_ADDRESS_BY_NAME,
58 NetHandleInterface::GetAddressByName),
59 DECLARE_NAPI_FUNCTION(NetHandleInterface::FUNCTION_BIND_SOCKET,
60 NetHandleInterface::BindSocket),
61 };
62 NapiUtils::DefineProperties(env, netHandle, properties);
63 NapiUtils::SetUint32Property(env, netHandle, NetHandleInterface::PROPERTY_NET_ID, handle->GetNetId());
64 return netHandle;
65 }
66
CreateNetCapabilities(napi_env env,NetAllCapabilities * capabilities)67 napi_value ConnectionExec::CreateNetCapabilities(napi_env env, NetAllCapabilities *capabilities)
68 {
69 napi_value netCapabilities = NapiUtils::CreateObject(env);
70 if (NapiUtils::GetValueType(env, netCapabilities) != napi_object) {
71 return NapiUtils::GetUndefined(env);
72 }
73
74 NapiUtils::SetUint32Property(env, netCapabilities, KEY_LINK_UP_BAND_WIDTH_KPS, capabilities->linkUpBandwidthKbps_);
75 NapiUtils::SetUint32Property(env, netCapabilities, KEY_LINK_DOWN_BAND_WIDTH_KPS,
76 capabilities->linkDownBandwidthKbps_);
77 if (!capabilities->netCaps_.empty() && capabilities->netCaps_.size() <= MAX_ARRAY_LENGTH) {
78 napi_value networkCap = NapiUtils::CreateArray(env, std::min(capabilities->netCaps_.size(), MAX_ARRAY_LENGTH));
79 auto it = capabilities->netCaps_.begin();
80 for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != capabilities->netCaps_.end(); ++index, ++it) {
81 NapiUtils::SetArrayElement(env, networkCap, index, NapiUtils::CreateUint32(env, *it));
82 }
83 NapiUtils::SetNamedProperty(env, netCapabilities, KEY_NETWORK_CAP, networkCap);
84 }
85 if (!capabilities->bearerTypes_.empty() && capabilities->bearerTypes_.size() <= MAX_ARRAY_LENGTH) {
86 napi_value bearerTypes =
87 NapiUtils::CreateArray(env, std::min(capabilities->bearerTypes_.size(), MAX_ARRAY_LENGTH));
88 auto it = capabilities->bearerTypes_.begin();
89 for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != capabilities->bearerTypes_.end(); ++index, ++it) {
90 NapiUtils::SetArrayElement(env, bearerTypes, index, NapiUtils::CreateUint32(env, *it));
91 }
92 NapiUtils::SetNamedProperty(env, netCapabilities, KEY_BEARER_TYPE, bearerTypes);
93 }
94 return netCapabilities;
95 }
96
CreateConnectionProperties(napi_env env,NetLinkInfo * linkInfo)97 napi_value ConnectionExec::CreateConnectionProperties(napi_env env, NetLinkInfo *linkInfo)
98 {
99 napi_value connectionProperties = NapiUtils::CreateObject(env);
100 if (NapiUtils::GetValueType(env, connectionProperties) != napi_object) {
101 return NapiUtils::GetUndefined(env);
102 }
103 NapiUtils::SetStringPropertyUtf8(env, connectionProperties, KEY_INTERFACE_NAME, linkInfo->ifaceName_);
104 NapiUtils::SetStringPropertyUtf8(env, connectionProperties, KEY_DOMAINS, linkInfo->domain_);
105 NapiUtils::SetUint32Property(env, connectionProperties, KEY_MTU, linkInfo->mtu_);
106 FillLinkAddress(env, connectionProperties, linkInfo);
107 FillRouoteList(env, connectionProperties, linkInfo);
108 FillDns(env, connectionProperties, linkInfo);
109 return connectionProperties;
110 }
111
ExecGetAddressByName(GetAddressByNameContext * context)112 bool ConnectionExec::ExecGetAddressByName(GetAddressByNameContext *context)
113 {
114 return NetHandleExec::ExecGetAddressesByName(context);
115 }
116
GetAddressByNameCallback(GetAddressByNameContext * context)117 napi_value ConnectionExec::GetAddressByNameCallback(GetAddressByNameContext *context)
118 {
119 return NetHandleExec::GetAddressesByNameCallback(context);
120 }
121
ExecGetDefaultNet(GetDefaultNetContext * context)122 bool ConnectionExec::ExecGetDefaultNet(GetDefaultNetContext *context)
123 {
124 auto ret = NetConnClient::GetInstance().GetDefaultNet(context->netHandle_);
125 if (ret != NETMANAGER_SUCCESS) {
126 NETMANAGER_BASE_LOGE("get default net failed %{public}d", ret);
127 context->SetErrorCode(ret);
128 }
129 return ret == NETMANAGER_SUCCESS;
130 }
131
GetDefaultNetCallback(GetDefaultNetContext * context)132 napi_value ConnectionExec::GetDefaultNetCallback(GetDefaultNetContext *context)
133 {
134 return CreateNetHandle(context->GetEnv(), &context->netHandle_);
135 }
136
ExecHasDefaultNet(HasDefaultNetContext * context)137 bool ConnectionExec::ExecHasDefaultNet(HasDefaultNetContext *context)
138 {
139 auto ret = NetConnClient::GetInstance().HasDefaultNet(context->hasDefaultNet_);
140 if (ret != NETMANAGER_SUCCESS && ret != NET_CONN_ERR_NO_DEFAULT_NET) {
141 NETMANAGER_BASE_LOGE("ExecHasDefaultNet ret %{public}d", ret);
142 context->SetErrorCode(ret);
143 return false;
144 }
145 return true;
146 }
147
HasDefaultNetCallback(HasDefaultNetContext * context)148 napi_value ConnectionExec::HasDefaultNetCallback(HasDefaultNetContext *context)
149 {
150 return NapiUtils::GetBoolean(context->GetEnv(), context->hasDefaultNet_);
151 }
152
ExecIsDefaultNetMetered(IsDefaultNetMeteredContext * context)153 bool ConnectionExec::ExecIsDefaultNetMetered(IsDefaultNetMeteredContext *context)
154 {
155 auto ret = NetConnClient::GetInstance().IsDefaultNetMetered(context->isMetered_);
156 if (ret != NETMANAGER_SUCCESS) {
157 NETMANAGER_BASE_LOGE("get net metered status failed %{public}d", ret);
158 context->SetErrorCode(ret);
159 }
160 return ret == NETMANAGER_SUCCESS;
161 }
162
IsDefaultNetMeteredCallback(IsDefaultNetMeteredContext * context)163 napi_value ConnectionExec::IsDefaultNetMeteredCallback(IsDefaultNetMeteredContext *context)
164 {
165 return NapiUtils::GetBoolean(context->GetEnv(), context->isMetered_);
166 }
167
ExecGetNetCapabilities(GetNetCapabilitiesContext * context)168 bool ConnectionExec::ExecGetNetCapabilities(GetNetCapabilitiesContext *context)
169 {
170 if (!context->IsParseOK()) {
171 return false;
172 }
173 auto ret = NetConnClient::GetInstance().GetNetCapabilities(context->netHandle_, context->capabilities_);
174 context->SetErrorCode(ret);
175 return ret == NETMANAGER_SUCCESS;
176 }
177
GetNetCapabilitiesCallback(GetNetCapabilitiesContext * context)178 napi_value ConnectionExec::GetNetCapabilitiesCallback(GetNetCapabilitiesContext *context)
179 {
180 return CreateNetCapabilities(context->GetEnv(), &context->capabilities_);
181 }
182
ExecGetConnectionProperties(GetConnectionPropertiesContext * context)183 bool ConnectionExec::ExecGetConnectionProperties(GetConnectionPropertiesContext *context)
184 {
185 if (!context->IsParseOK()) {
186 return false;
187 }
188 auto ret = NetConnClient::GetInstance().GetConnectionProperties(context->netHandle_, context->linkInfo_);
189 context->SetErrorCode(ret);
190 return ret == NETMANAGER_SUCCESS;
191 }
192
GetConnectionPropertiesCallback(GetConnectionPropertiesContext * context)193 napi_value ConnectionExec::GetConnectionPropertiesCallback(GetConnectionPropertiesContext *context)
194 {
195 return CreateConnectionProperties(context->GetEnv(), &context->linkInfo_);
196 }
197
ExecGetAllNets(GetAllNetsContext * context)198 bool ConnectionExec::ExecGetAllNets(GetAllNetsContext *context)
199 {
200 int32_t ret = NetConnClient::GetInstance().GetAllNets(context->netHandleList_);
201 context->SetErrorCode(ret);
202 return ret == NETMANAGER_SUCCESS;
203 }
204
GetAllNetsCallback(GetAllNetsContext * context)205 napi_value ConnectionExec::GetAllNetsCallback(GetAllNetsContext *context)
206 {
207 napi_value array = NapiUtils::CreateArray(context->GetEnv(), context->netHandleList_.size());
208 uint32_t index = 0;
209 std::for_each(context->netHandleList_.begin(), context->netHandleList_.end(),
210 [array, &index, context](const sptr<NetHandle> &handle) {
211 NapiUtils::SetArrayElement(context->GetEnv(), array, index,
212 CreateNetHandle(context->GetEnv(), handle.GetRefPtr()));
213 ++index;
214 });
215 return array;
216 }
217
ExecEnableAirplaneMode(EnableAirplaneModeContext * context)218 bool ConnectionExec::ExecEnableAirplaneMode(EnableAirplaneModeContext *context)
219 {
220 int32_t res = NetConnClient::GetInstance().SetAirplaneMode(true);
221 if (res != NETMANAGER_SUCCESS) {
222 NETMANAGER_BASE_LOGE("ExecEnableAirplaneMode failed %{public}d", res);
223 context->SetErrorCode(res);
224 }
225 return res == NETMANAGER_SUCCESS;
226 }
227
EnableAirplaneModeCallback(EnableAirplaneModeContext * context)228 napi_value ConnectionExec::EnableAirplaneModeCallback(EnableAirplaneModeContext *context)
229 {
230 return NapiUtils::GetUndefined(context->GetEnv());
231 }
232
ExecDisableAirplaneMode(DisableAirplaneModeContext * context)233 bool ConnectionExec::ExecDisableAirplaneMode(DisableAirplaneModeContext *context)
234 {
235 int32_t res = NetConnClient::GetInstance().SetAirplaneMode(false);
236 if (res != NETMANAGER_SUCCESS) {
237 NETMANAGER_BASE_LOGE("ExecDisableAirplaneMode failed %{public}d", res);
238 context->SetErrorCode(res);
239 }
240 return res == NETMANAGER_SUCCESS;
241 }
242
DisableAirplaneModeCallback(DisableAirplaneModeContext * context)243 napi_value ConnectionExec::DisableAirplaneModeCallback(DisableAirplaneModeContext *context)
244 {
245 return NapiUtils::GetUndefined(context->GetEnv());
246 }
247
ExecReportNetConnected(ReportNetConnectedContext * context)248 bool ConnectionExec::ExecReportNetConnected(ReportNetConnectedContext *context)
249 {
250 if (!context->IsParseOK()) {
251 return false;
252 }
253 int32_t res = NetConnClient::GetInstance().NetDetection(context->netHandle_);
254 if (res != NETMANAGER_SUCCESS) {
255 NETMANAGER_BASE_LOGE("ExecReportNetConnected failed %{public}d", res);
256 context->SetErrorCode(res);
257 }
258 return res == NETMANAGER_SUCCESS;
259 }
260
ReportNetConnectedCallback(ReportNetConnectedContext * context)261 napi_value ConnectionExec::ReportNetConnectedCallback(ReportNetConnectedContext *context)
262 {
263 return NapiUtils::GetUndefined(context->GetEnv());
264 }
265
ExecReportNetDisconnected(ReportNetConnectedContext * context)266 bool ConnectionExec::ExecReportNetDisconnected(ReportNetConnectedContext *context)
267 {
268 if (!context->IsParseOK()) {
269 return false;
270 }
271 int32_t res = NetConnClient::GetInstance().NetDetection(context->netHandle_);
272 if (res != NETMANAGER_SUCCESS) {
273 NETMANAGER_BASE_LOGE("ExecReportNetDisconnected failed %{public}d", res);
274 context->SetErrorCode(res);
275 }
276 return res == NETMANAGER_SUCCESS;
277 }
278
ReportNetDisconnectedCallback(ReportNetConnectedContext * context)279 napi_value ConnectionExec::ReportNetDisconnectedCallback(ReportNetConnectedContext *context)
280 {
281 return NapiUtils::GetUndefined(context->GetEnv());
282 }
283
ExecGetDefaultHttpProxy(GetHttpProxyContext * context)284 bool ConnectionExec::ExecGetDefaultHttpProxy(GetHttpProxyContext *context)
285 {
286 int32_t errorCode = NetConnClient::GetInstance().GetDefaultHttpProxy(context->httpProxy_);
287 if (errorCode != NET_CONN_SUCCESS) {
288 context->SetErrorCode(errorCode);
289 return false;
290 }
291 return true;
292 }
293
GetDefaultHttpProxyCallback(GetHttpProxyContext * context)294 napi_value ConnectionExec::GetDefaultHttpProxyCallback(GetHttpProxyContext *context)
295 {
296 napi_value host = NapiUtils::CreateStringUtf8(context->GetEnv(), context->httpProxy_.GetHost());
297 napi_value port = NapiUtils::CreateInt32(context->GetEnv(), context->httpProxy_.GetPort());
298 auto lists = context->httpProxy_.GetExclusionList();
299 napi_value exclusionList = NapiUtils::CreateArray(context->GetEnv(), lists.size());
300 size_t index = 0;
301 for (auto list : lists) {
302 napi_value jsList = NapiUtils::CreateStringUtf8(context->GetEnv(), list);
303 NapiUtils::SetArrayElement(context->GetEnv(), exclusionList, index++, jsList);
304 }
305 napi_value httpProxy = NapiUtils::CreateObject(context->GetEnv());
306 NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "host", host);
307 NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "port", port);
308 NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "exclusionList", exclusionList);
309 return httpProxy;
310 }
311
ExecGetGlobalHttpProxy(GetHttpProxyContext * context)312 bool ConnectionExec::ExecGetGlobalHttpProxy(GetHttpProxyContext *context)
313 {
314 int32_t errorCode = NetConnClient::GetInstance().GetGlobalHttpProxy(context->httpProxy_);
315 if (errorCode != NET_CONN_SUCCESS) {
316 context->SetErrorCode(errorCode);
317 return false;
318 }
319 return true;
320 }
321
GetGlobalHttpProxyCallback(GetHttpProxyContext * context)322 napi_value ConnectionExec::GetGlobalHttpProxyCallback(GetHttpProxyContext *context)
323 {
324 napi_value host = NapiUtils::CreateStringUtf8(context->GetEnv(), context->httpProxy_.GetHost());
325 napi_value port = NapiUtils::CreateInt32(context->GetEnv(), context->httpProxy_.GetPort());
326 auto lists = context->httpProxy_.GetExclusionList();
327 napi_value exclusionList = NapiUtils::CreateArray(context->GetEnv(), lists.size());
328 size_t index = 0;
329 for (auto list : lists) {
330 napi_value jsList = NapiUtils::CreateStringUtf8(context->GetEnv(), list);
331 NapiUtils::SetArrayElement(context->GetEnv(), exclusionList, index++, jsList);
332 }
333 napi_value httpProxy = NapiUtils::CreateObject(context->GetEnv());
334 NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "host", host);
335 NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "port", port);
336 NapiUtils::SetNamedProperty(context->GetEnv(), httpProxy, "exclusionList", exclusionList);
337 return httpProxy;
338 }
339
ExecSetGlobalHttpProxy(SetGlobalHttpProxyContext * context)340 bool ConnectionExec::ExecSetGlobalHttpProxy(SetGlobalHttpProxyContext *context)
341 {
342 int32_t errorCode = NetConnClient::GetInstance().SetGlobalHttpProxy(context->httpProxy_);
343 if (errorCode != NET_CONN_SUCCESS) {
344 context->SetErrorCode(errorCode);
345 return false;
346 }
347 return true;
348 }
349
SetGlobalHttpProxyCallback(SetGlobalHttpProxyContext * context)350 napi_value ConnectionExec::SetGlobalHttpProxyCallback(SetGlobalHttpProxyContext *context)
351 {
352 return NapiUtils::GetUndefined(context->GetEnv());
353 }
354
ExecSetAppHttpProxy(SetAppHttpProxyContext * context)355 bool ConnectionExec::ExecSetAppHttpProxy(SetAppHttpProxyContext *context)
356 {
357 HiAppEventReport hiAppEventReport("NetworkKit", "ConnectionSetAppHttpProxy");
358 int32_t errorCode = NetConnClient::GetInstance().SetAppHttpProxy(context->httpProxy_);
359 if (errorCode != NET_CONN_SUCCESS) {
360 context->SetErrorCode(errorCode);
361 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, errorCode);
362 return false;
363 }
364 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, NET_CONN_SUCCESS);
365 return true;
366 }
367
SetAppHttpProxyCallback(SetAppHttpProxyContext * context)368 napi_value ConnectionExec::SetAppHttpProxyCallback(SetAppHttpProxyContext *context)
369 {
370 return NapiUtils::GetUndefined(context->GetEnv());
371 }
372
ExecGetAppNet(GetAppNetContext * context)373 bool ConnectionExec::ExecGetAppNet(GetAppNetContext *context)
374 {
375 int32_t netId = 0;
376 int32_t errorCode = NetConnClient::GetInstance().GetAppNet(netId);
377 if (errorCode != NET_CONN_SUCCESS) {
378 NETMANAGER_BASE_LOGE("exec getAppNet failed errorCode: %{public}d", errorCode);
379 context->SetErrorCode(errorCode);
380 return false;
381 }
382 context->netHandle_.SetNetId(netId);
383 return true;
384 }
385
GetAppNetCallback(GetAppNetContext * context)386 napi_value ConnectionExec::GetAppNetCallback(GetAppNetContext *context)
387 {
388 return CreateNetHandle(context->GetEnv(), &context->netHandle_);
389 }
390
ExecSetAppNet(SetAppNetContext * context)391 bool ConnectionExec::ExecSetAppNet(SetAppNetContext *context)
392 {
393 HiAppEventReport hiAppEventReport("NetworkKit", "ConnectionSetAppNet");
394 NETMANAGER_BASE_LOGI("into");
395 int32_t errorCode = NetConnClient::GetInstance().SetAppNet(context->netHandle_.GetNetId());
396 if (errorCode != NET_CONN_SUCCESS) {
397 NETMANAGER_BASE_LOGE("exec setAppNet failed errorCode: %{public}d", errorCode);
398 context->SetErrorCode(errorCode);
399 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, errorCode);
400 return false;
401 }
402 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, NET_CONN_SUCCESS);
403 return true;
404 }
405
SetAppNetCallback(SetAppNetContext * context)406 napi_value ConnectionExec::SetAppNetCallback(SetAppNetContext *context)
407 {
408 return NapiUtils::GetUndefined(context->GetEnv());
409 }
410
ExecGetPacUrl(GetPacUrlContext * context)411 bool ConnectionExec::ExecGetPacUrl(GetPacUrlContext *context)
412 {
413 int32_t errorCode = NetConnClient::GetInstance().GetPacUrl(context->pacUrl_);
414 if (errorCode != NET_CONN_SUCCESS) {
415 NETMANAGER_BASE_LOGE("exec GetPacUrl failed errorCode: %{public}d", errorCode);
416 context->SetErrorCode(errorCode);
417 return false;
418 }
419 return true;
420 }
421
GetPacUrlCallback(GetPacUrlContext * context)422 napi_value ConnectionExec::GetPacUrlCallback(GetPacUrlContext *context)
423 {
424 return NapiUtils::CreateStringUtf8(context->GetEnv(), context->pacUrl_);
425 }
426
ExecSetPacUrl(SetPacUrlContext * context)427 bool ConnectionExec::ExecSetPacUrl(SetPacUrlContext *context)
428 {
429 if (context->pacUrl_.empty()) {
430 NETMANAGER_BASE_LOGE("pac Url is empty!");
431 context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
432 return false;
433 }
434 int32_t errorCode = NetConnClient::GetInstance().SetPacUrl(context->pacUrl_);
435 if (errorCode != NET_CONN_SUCCESS) {
436 NETMANAGER_BASE_LOGE("exec SetPacUrl failed errorCode: %{public}d", errorCode);
437 context->SetErrorCode(errorCode);
438 return false;
439 }
440 return true;
441 }
442
SetPacUrlCallback(SetPacUrlContext * context)443 napi_value ConnectionExec::SetPacUrlCallback(SetPacUrlContext *context)
444 {
445 return NapiUtils::GetUndefined(context->GetEnv());
446 }
447
ExecSetNetExtAttribute(SetNetExtAttributeContext * context)448 bool ConnectionExec::ExecSetNetExtAttribute(SetNetExtAttributeContext *context)
449 {
450 if (!context->IsParseOK()) {
451 return false;
452 }
453 int32_t errorCode = NetConnClient::GetInstance().SetNetExtAttribute(context->netHandle_, context->netExtAttribute_);
454 if (errorCode != NET_CONN_SUCCESS) {
455 NETMANAGER_BASE_LOGE("exec SetNetExtAttribute failed errorCode: %{public}d", errorCode);
456 context->SetErrorCode(errorCode);
457 return false;
458 }
459 return true;
460 }
461
SetNetExtAttributeCallback(SetNetExtAttributeContext * context)462 napi_value ConnectionExec::SetNetExtAttributeCallback(SetNetExtAttributeContext *context)
463 {
464 return NapiUtils::GetUndefined(context->GetEnv());
465 }
466
ExecGetNetExtAttribute(GetNetExtAttributeContext * context)467 bool ConnectionExec::ExecGetNetExtAttribute(GetNetExtAttributeContext *context)
468 {
469 if (!context->IsParseOK()) {
470 return false;
471 }
472 int32_t errorCode = NetConnClient::GetInstance().GetNetExtAttribute(context->netHandle_, context->netExtAttribute_);
473 if (errorCode != NET_CONN_SUCCESS) {
474 NETMANAGER_BASE_LOGE("exec GetNetExtAttribute failed errorCode: %{public}d", errorCode);
475 context->SetErrorCode(errorCode);
476 return false;
477 }
478 return true;
479 }
480
GetNetExtAttributeCallback(GetNetExtAttributeContext * context)481 napi_value ConnectionExec::GetNetExtAttributeCallback(GetNetExtAttributeContext *context)
482 {
483 return NapiUtils::CreateStringUtf8(context->GetEnv(), context->netExtAttribute_);
484 }
485
ExecSetCustomDNSRule(SetCustomDNSRuleContext * context)486 bool ConnectionExec::ExecSetCustomDNSRule(SetCustomDNSRuleContext *context)
487 {
488 if (context == nullptr) {
489 NETMANAGER_BASE_LOGE("context is nullptr");
490 return false;
491 }
492
493 if (!CommonUtils::HasInternetPermission()) {
494 context->SetErrorCode(NETMANAGER_ERR_PERMISSION_DENIED);
495 return false;
496 }
497
498 if (context->host_.empty() || context->ip_.empty()) {
499 context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
500 return false;
501 }
502
503 std::vector<std::string> ip = context->ip_;
504 for (size_t i = 0; i < ip.size(); i++) {
505 if (!CommonUtils::IsValidIPV4(ip[i]) && !CommonUtils::IsValidIPV6(ip[i])) {
506 context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
507 return false;
508 }
509 }
510
511 if (!context->IsParseOK()) {
512 context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
513 return false;
514 }
515
516 std::string host_ips = context->host_ + ",";
517 for (size_t i = 0; i < ip.size(); i++) {
518 host_ips.append(ip[i]);
519 if (i < ip.size() - 1) {
520 host_ips.append(",");
521 }
522 }
523
524 std::lock_guard<std::mutex> lock(g_predefinedHostMtx);
525 NETMANAGER_BASE_LOGI("set host with ip addr");
526 int res = predefined_host_set_hosts(host_ips.c_str());
527 if (res != NETMANAGER_SUCCESS) {
528 NETMANAGER_BASE_LOGE("ExecSetCustomDNSRule failed %{public}d", res);
529 context->SetErrorCode(res);
530 return false;
531 }
532
533 return true;
534 }
535
SetCustomDNSRuleCallback(SetCustomDNSRuleContext * context)536 napi_value ConnectionExec::SetCustomDNSRuleCallback(SetCustomDNSRuleContext *context)
537 {
538 return NapiUtils::GetUndefined(context->GetEnv());
539 }
540
ExecDeleteCustomDNSRule(DeleteCustomDNSRuleContext * context)541 bool ConnectionExec::ExecDeleteCustomDNSRule(DeleteCustomDNSRuleContext *context)
542 {
543 if (context == nullptr) {
544 NETMANAGER_BASE_LOGE("context is nullptr");
545 return false;
546 }
547 if (!CommonUtils::HasInternetPermission()) {
548 context->SetErrorCode(NETMANAGER_ERR_PERMISSION_DENIED);
549 return false;
550 }
551
552 if (context->host_.empty()) {
553 context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
554 return false;
555 }
556
557 if (!context->IsParseOK()) {
558 context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
559 return false;
560 }
561
562 std::lock_guard<std::mutex> lock(g_predefinedHostMtx);
563 NETMANAGER_BASE_LOGI("delete host with ip addr");
564 int res = predefined_host_remove_host(context->host_.c_str());
565 if (res != NETMANAGER_SUCCESS) {
566 NETMANAGER_BASE_LOGE("ExecDeleteCustomDNSRule failed %{public}d", res);
567 context->SetErrorCode(res);
568 return false;
569 }
570 return true;
571 }
572
DeleteCustomDNSRuleCallback(DeleteCustomDNSRuleContext * context)573 napi_value ConnectionExec::DeleteCustomDNSRuleCallback(DeleteCustomDNSRuleContext *context)
574 {
575 return NapiUtils::GetUndefined(context->GetEnv());
576 }
577
ExecDeleteCustomDNSRules(DeleteCustomDNSRulesContext * context)578 bool ConnectionExec::ExecDeleteCustomDNSRules(DeleteCustomDNSRulesContext *context)
579 {
580 if (context == nullptr) {
581 NETMANAGER_BASE_LOGE("context is nullptr");
582 return false;
583 }
584 if (!CommonUtils::HasInternetPermission()) {
585 context->SetErrorCode(NETMANAGER_ERR_PERMISSION_DENIED);
586 return false;
587 }
588
589 if (!context->IsParseOK()) {
590 context->SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
591 return false;
592 }
593
594 std::lock_guard<std::mutex> lock(g_predefinedHostMtx);
595 int res = predefined_host_clear_all_hosts();
596 if (res != NETMANAGER_SUCCESS) {
597 NETMANAGER_BASE_LOGE("ExecDeleteCustomDNSRules failed %{public}d", res);
598 context->SetErrorCode(res);
599 return false;
600 }
601
602 return true;
603 }
604
DeleteCustomDNSRulesCallback(DeleteCustomDNSRulesContext * context)605 napi_value ConnectionExec::DeleteCustomDNSRulesCallback(DeleteCustomDNSRulesContext *context)
606 {
607 return NapiUtils::GetUndefined(context->GetEnv());
608 }
609
ExecSetInterfaceUp(SetInterfaceUpContext * context)610 bool ConnectionExec::ExecSetInterfaceUp(SetInterfaceUpContext *context)
611 {
612 NETMANAGER_BASE_LOGI("ExecSetInterfaceUp");
613 int32_t errorCode = NetConnClient::GetInstance().SetInterfaceUp(context->interface_);
614 if (errorCode != NET_CONN_SUCCESS) {
615 NETMANAGER_BASE_LOGE("exec setInterfaceUp failed errorCode: %{public}d", errorCode);
616 context->SetErrorCode(errorCode);
617 return false;
618 }
619 return true;
620 }
621
SetInterfaceUpCallback(SetInterfaceUpContext * context)622 napi_value ConnectionExec::SetInterfaceUpCallback(SetInterfaceUpContext *context)
623 {
624 return NapiUtils::GetUndefined(context->GetEnv());
625 }
626
ExecSetInterfaceIpAddr(SetInterfaceIpAddrContext * context)627 bool ConnectionExec::ExecSetInterfaceIpAddr(SetInterfaceIpAddrContext *context)
628 {
629 NETMANAGER_BASE_LOGI("ExecSetInterfaceIpAddr");
630 int32_t errorCode = NetConnClient::GetInstance().SetNetInterfaceIpAddress(
631 context->interface_, context->ipAddr_);
632 if (errorCode != NET_CONN_SUCCESS) {
633 NETMANAGER_BASE_LOGE("exec setInterfaceIpAddr failed errorCode: %{public}d", errorCode);
634 context->SetErrorCode(errorCode);
635 return false;
636 }
637 return true;
638 }
639
SetInterfaceIpAddrCallback(SetInterfaceIpAddrContext * context)640 napi_value ConnectionExec::SetInterfaceIpAddrCallback(SetInterfaceIpAddrContext *context)
641 {
642 return NapiUtils::GetUndefined(context->GetEnv());
643 }
644
ExecAddNetworkRoute(AddNetworkRouteContext * context)645 bool ConnectionExec::ExecAddNetworkRoute(AddNetworkRouteContext *context)
646 {
647 NETMANAGER_BASE_LOGI("ExecAddNetworkRoute");
648 std::string destAddress =
649 context->route_.destination_.address_ + "/" + std::to_string(context->route_.destination_.prefixlen_);
650 int32_t errorCode = NetConnClient::GetInstance().AddNetworkRoute(
651 context->netId_, context->route_.iface_, destAddress, context->route_.gateway_.address_);
652 if (errorCode != NET_CONN_SUCCESS) {
653 NETMANAGER_BASE_LOGE("exec addNetworkRoute failed errorCode: %{public}d", errorCode);
654 context->SetErrorCode(errorCode);
655 return false;
656 }
657 return true;
658 }
659
AddNetworkRouteCallback(AddNetworkRouteContext * context)660 napi_value ConnectionExec::AddNetworkRouteCallback(AddNetworkRouteContext *context)
661 {
662 return NapiUtils::GetUndefined(context->GetEnv());
663 }
664
ExecGetNetInterfaceConfiguration(GetNetInterfaceConfigurationContext * context)665 bool ConnectionExec::ExecGetNetInterfaceConfiguration(GetNetInterfaceConfigurationContext *context)
666 {
667 NETMANAGER_BASE_LOGI("ExecAddNetworkRoute");
668 int32_t errorCode = NetConnClient::GetInstance().GetNetInterfaceConfiguration(
669 context->interface_, context->config_);
670 if (errorCode != NET_CONN_SUCCESS) {
671 NETMANAGER_BASE_LOGE("exec getNetInterfaceConfiguration failed errorCode: %{public}d", errorCode);
672 context->SetErrorCode(errorCode);
673 return false;
674 }
675 return true;
676 }
677
GetNetInterfaceConfigurationCallback(GetNetInterfaceConfigurationContext * context)678 napi_value ConnectionExec::GetNetInterfaceConfigurationCallback(GetNetInterfaceConfigurationContext *context)
679 {
680 int32_t errorCode = NetConnClient::GetInstance().GetNetInterfaceConfiguration(
681 context->interface_, context->config_);
682 if (errorCode != NET_CONN_SUCCESS) {
683 NETMANAGER_BASE_LOGE("getNetInterfaceConfiguration callback failed errorCode: %{public}d", errorCode);
684 context->SetErrorCode(errorCode);
685 return NapiUtils::GetUndefined(context->GetEnv());
686 }
687 napi_value ifName = NapiUtils::CreateStringUtf8(context->GetEnv(), context->config_.ifName_);
688 napi_value hwAddr = NapiUtils::CreateStringUtf8(context->GetEnv(), context->config_.hwAddr_);
689 napi_value ipv4Addr = NapiUtils::CreateStringUtf8(context->GetEnv(), context->config_.ipv4Addr_);
690 napi_value prefixLength = NapiUtils::CreateInt32(context->GetEnv(), context->config_.prefixLength_);
691 napi_value flagsList = NapiUtils::CreateArray(context->GetEnv(), context->config_.flags_.size());
692 size_t index = 0;
693 for (auto flag : context->config_.flags_) {
694 napi_value jsList = NapiUtils::CreateStringUtf8(context->GetEnv(), flag);
695 NapiUtils::SetArrayElement(context->GetEnv(), flagsList, index++, jsList);
696 }
697 napi_value interfaceConfig = NapiUtils::CreateObject(context->GetEnv());
698 NapiUtils::SetNamedProperty(context->GetEnv(), interfaceConfig, "interfaceName", ifName);
699 NapiUtils::SetNamedProperty(context->GetEnv(), interfaceConfig, "hwAddress", hwAddr);
700 NapiUtils::SetNamedProperty(context->GetEnv(), interfaceConfig, "ipv4Address", ipv4Addr);
701 NapiUtils::SetNamedProperty(context->GetEnv(), interfaceConfig, "prefixLength", prefixLength);
702 NapiUtils::SetNamedProperty(context->GetEnv(), interfaceConfig, "flags", flagsList);
703 return interfaceConfig;
704 }
705
ExecRegisterNetSupplier(RegisterNetSupplierContext * context)706 bool ConnectionExec::ExecRegisterNetSupplier(RegisterNetSupplierContext *context)
707 {
708 NETMANAGER_BASE_LOGI("ExecRegisterNetSupplier");
709 int32_t errorCode = NetConnClient::GetInstance().RegisterNetSupplier(
710 context->bearerType_, context->ident_, context->netCaps_, context->netSupplierId_);
711 if (errorCode != NET_CONN_SUCCESS) {
712 NETMANAGER_BASE_LOGE("exec registerNetSupplier failed errorCode: %{public}d", errorCode);
713 context->SetErrorCode(errorCode);
714 return false;
715 }
716 return true;
717 }
718
RegisterNetSupplierCallback(RegisterNetSupplierContext * context)719 napi_value ConnectionExec::RegisterNetSupplierCallback(RegisterNetSupplierContext *context)
720 {
721 int32_t errorCode = NetConnClient::GetInstance().RegisterNetSupplier(
722 context->bearerType_, context->ident_, context->netCaps_, context->netSupplierId_);
723 if (errorCode != NET_CONN_SUCCESS) {
724 NETMANAGER_BASE_LOGE("registerNetSupplier callback failed errorCode: %{public}d", errorCode);
725 context->SetErrorCode(errorCode);
726 return NapiUtils::GetUndefined(context->GetEnv());
727 }
728 napi_value netSupplierId = NapiUtils::CreateUint32(context->GetEnv(), context->netSupplierId_);
729 return netSupplierId;
730 }
731
ExecUnregisterNetSupplier(UnregisterNetSupplierContext * context)732 bool ConnectionExec::ExecUnregisterNetSupplier(UnregisterNetSupplierContext *context)
733 {
734 NETMANAGER_BASE_LOGI("ExecUnregisterNetSupplier");
735 int32_t errorCode = NetConnClient::GetInstance().UnregisterNetSupplier(context->netSupplierId_);
736 if (errorCode != NET_CONN_SUCCESS) {
737 NETMANAGER_BASE_LOGE("exec addNetworkRoute failed errorCode: %{public}d", errorCode);
738 context->SetErrorCode(errorCode);
739 return false;
740 }
741 return true;
742 }
743
UnregisterNetSupplierCallback(UnregisterNetSupplierContext * context)744 napi_value ConnectionExec::UnregisterNetSupplierCallback(UnregisterNetSupplierContext *context)
745 {
746 return NapiUtils::GetUndefined(context->GetEnv());
747 }
748
ExecFactoryResetNetwork(FactoryResetNetworkContext * context)749 bool ConnectionExec::ExecFactoryResetNetwork(FactoryResetNetworkContext *context)
750 {
751 NETMANAGER_BASE_LOGI("ExecFactoryResetNetwork into");
752 int32_t errorCode = NetConnClient::GetInstance().FactoryResetNetwork();
753 if (errorCode != NET_CONN_SUCCESS) {
754 NETMANAGER_BASE_LOGE("exec ResetNetwork failed errorCode: %{public}d", errorCode);
755 context->SetErrorCode(errorCode);
756 return false;
757 }
758 return true;
759 }
760
FactoryResetNetworkCallback(FactoryResetNetworkContext * context)761 napi_value ConnectionExec::FactoryResetNetworkCallback(FactoryResetNetworkContext *context)
762 {
763 return NapiUtils::GetUndefined(context->GetEnv());
764 }
765
TransErrorCode(int32_t error)766 int32_t TransErrorCode(int32_t error)
767 {
768 switch (error) {
769 case NO_PERMISSION_CODE:
770 return NETMANAGER_ERR_PERMISSION_DENIED;
771 case PERMISSION_DENIED_CODE:
772 return NETMANAGER_ERR_PERMISSION_DENIED;
773 case NET_UNREACHABLE_CODE:
774 return NETMANAGER_ERR_INTERNAL;
775 default:
776 return NETMANAGER_ERR_OPERATION_FAILED;
777 }
778 }
779
ExecGetAddressesByName(GetAddressByNameContext * context)780 bool ConnectionExec::NetHandleExec::ExecGetAddressesByName(GetAddressByNameContext *context)
781 {
782 if (!context->IsParseOK()) {
783 return false;
784 }
785 uint32_t netid = static_cast<uint32_t>(context->netId_);
786 addrinfo *res = nullptr;
787 queryparam param;
788 param.qp_type = QEURY_TYPE_NORMAL;
789 param.qp_netid = netid;
790 NETMANAGER_BASE_LOGD("getaddrinfo_ext %{public}d %{public}d", netid, param.qp_netid);
791 if (context->host_.empty()) {
792 NETMANAGER_BASE_LOGE("host is empty!");
793 context->SetErrorCode(NETMANAGER_ERR_INVALID_PARAMETER);
794 return false;
795 }
796
797 int status = getaddrinfo_ext(context->host_.c_str(), nullptr, nullptr, &res, ¶m);
798 if (status < 0) {
799 NETMANAGER_BASE_LOGE("getaddrinfo errno %{public}d %{public}s, status: %{public}d", errno, strerror(errno),
800 status);
801 int32_t temp = TransErrorCode(errno);
802 context->SetErrorCode(temp);
803 return false;
804 }
805
806 for (addrinfo *tmp = res; tmp != nullptr; tmp = tmp->ai_next) {
807 std::string host;
808 if (tmp->ai_family == AF_INET) {
809 auto addr = reinterpret_cast<sockaddr_in *>(tmp->ai_addr);
810 char ip[MAX_IPV4_STR_LEN] = {0};
811 inet_ntop(AF_INET, &addr->sin_addr, ip, sizeof(ip));
812 host = ip;
813 } else if (tmp->ai_family == AF_INET6) {
814 auto addr = reinterpret_cast<sockaddr_in6 *>(tmp->ai_addr);
815 char ip[MAX_IPV6_STR_LEN] = {0};
816 inet_ntop(AF_INET6, &addr->sin6_addr, ip, sizeof(ip));
817 host = ip;
818 }
819
820 NetAddress address;
821 SetAddressInfo(host.c_str(), tmp, address);
822
823 context->addresses_.emplace_back(address);
824 }
825 freeaddrinfo(res);
826 return true;
827 }
828
GetAddressesByNameCallback(GetAddressByNameContext * context)829 napi_value ConnectionExec::NetHandleExec::GetAddressesByNameCallback(GetAddressByNameContext *context)
830 {
831 napi_value addresses = NapiUtils::CreateArray(context->GetEnv(), context->addresses_.size());
832 for (uint32_t index = 0; index < context->addresses_.size(); ++index) {
833 napi_value obj = MakeNetAddressJsValue(context->GetEnv(), context->addresses_[index]);
834 NapiUtils::SetArrayElement(context->GetEnv(), addresses, index, obj);
835 }
836 return addresses;
837 }
838
ExecGetAddressByName(GetAddressByNameContext * context)839 bool ConnectionExec::NetHandleExec::ExecGetAddressByName(GetAddressByNameContext *context)
840 {
841 if (!context->IsParseOK()) {
842 return false;
843 }
844 uint32_t netid = static_cast<uint32_t>(context->netId_);
845 addrinfo *res = nullptr;
846 queryparam param;
847 param.qp_type = QEURY_TYPE_NORMAL;
848 param.qp_netid = netid;
849 NETMANAGER_BASE_LOGD("getaddrinfo_ext %{public}d %{public}d", netid, param.qp_netid);
850 if (context->host_.empty()) {
851 NETMANAGER_BASE_LOGE("host is empty!");
852 context->SetErrorCode(NETMANAGER_ERR_INVALID_PARAMETER);
853 return false;
854 }
855
856 int status = getaddrinfo_ext(context->host_.c_str(), nullptr, nullptr, &res, ¶m);
857 if (status < 0) {
858 NETMANAGER_BASE_LOGE("getaddrinfo errno %{public}d %{public}s, status: %{public}d", errno, strerror(errno),
859 status);
860 int32_t temp = TransErrorCode(errno);
861 context->SetErrorCode(temp);
862 return false;
863 }
864
865 if (res != nullptr) {
866 std::string host;
867 if (res->ai_family == AF_INET) {
868 auto addr = reinterpret_cast<sockaddr_in *>(res->ai_addr);
869 char ip[MAX_IPV4_STR_LEN] = {0};
870 inet_ntop(AF_INET, &addr->sin_addr, ip, sizeof(ip));
871 host = ip;
872 } else if (res->ai_family == AF_INET6) {
873 auto addr = reinterpret_cast<sockaddr_in6 *>(res->ai_addr);
874 char ip[MAX_IPV6_STR_LEN] = {0};
875 inet_ntop(AF_INET6, &addr->sin6_addr, ip, sizeof(ip));
876 host = ip;
877 }
878
879 NetAddress address;
880 SetAddressInfo(host.c_str(), res, address);
881
882 context->addresses_.emplace_back(address);
883 }
884 freeaddrinfo(res);
885 return true;
886 }
887
GetAddressByNameCallback(GetAddressByNameContext * context)888 napi_value ConnectionExec::NetHandleExec::GetAddressByNameCallback(GetAddressByNameContext *context)
889 {
890 if (context->addresses_.empty()) {
891 return NapiUtils::GetUndefined(context->GetEnv());
892 }
893 return MakeNetAddressJsValue(context->GetEnv(), context->addresses_[0]);
894 }
895
MakeNetAddressJsValue(napi_env env,const NetAddress & address)896 napi_value ConnectionExec::NetHandleExec::MakeNetAddressJsValue(napi_env env, const NetAddress &address)
897 {
898 napi_value obj = NapiUtils::CreateObject(env);
899 if (NapiUtils::GetValueType(env, obj) != napi_object) {
900 return NapiUtils::GetUndefined(env);
901 }
902
903 NapiUtils::SetStringPropertyUtf8(env, obj, KEY_ADDRESS, address.GetAddress());
904 NapiUtils::SetUint32Property(env, obj, KEY_FAMILY, address.GetJsValueFamily());
905 NapiUtils::SetUint32Property(env, obj, KEY_PORT, address.GetPort());
906 return obj;
907 }
908
ExecBindSocket(BindSocketContext * context)909 bool ConnectionExec::NetHandleExec::ExecBindSocket(BindSocketContext *context)
910 {
911 if (!context->IsParseOK()) {
912 return false;
913 }
914 NetHandle handle(context->netId_);
915 int32_t res = handle.BindSocket(context->socketFd_);
916 if (res != NETMANAGER_SUCCESS) {
917 NETMANAGER_BASE_LOGE("ExecBindSocket failed %{public}d", res);
918 context->SetErrorCode(res);
919 }
920 return res == NETMANAGER_SUCCESS;
921 }
922
BindSocketCallback(BindSocketContext * context)923 napi_value ConnectionExec::NetHandleExec::BindSocketCallback(BindSocketContext *context)
924 {
925 return NapiUtils::GetUndefined(context->GetEnv());
926 }
927
SetAddressInfo(const char * host,addrinfo * info,NetAddress & address)928 void ConnectionExec::NetHandleExec::SetAddressInfo(const char *host, addrinfo *info, NetAddress &address)
929 {
930 address.SetAddress(host);
931 address.SetFamilyBySaFamily(info->ai_addr->sa_family);
932 if (info->ai_addr->sa_family == AF_INET) {
933 auto addr4 = reinterpret_cast<sockaddr_in *>(info->ai_addr);
934 address.SetPort(addr4->sin_port);
935 } else if (info->ai_addr->sa_family == AF_INET6) {
936 auto addr6 = reinterpret_cast<sockaddr_in6 *>(info->ai_addr);
937 address.SetPort(addr6->sin6_port);
938 }
939 }
940
ExecRegister(RegisterContext * context)941 bool ConnectionExec::NetConnectionExec::ExecRegister(RegisterContext *context)
942 {
943 HiAppEventReport hiAppEventReport("NetworkKit", "ConnectionRegister");
944 auto wCallback = context->GetNetConnCallback();
945 sptr<INetConnCallback> callback = wCallback.promote();
946 if (callback == nullptr) {
947 NETMANAGER_BASE_LOGE("ExecRegister getNetConnCallback nullptr");
948 return false;
949 }
950
951 auto conn = context->GetConn();
952 if (conn.hasNetSpecifier_ && conn.hasTimeout_) {
953 sptr<NetSpecifier> specifier = new NetSpecifier(conn.netSpecifier_);
954 int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, callback, conn.timeout_);
955 NETMANAGER_BASE_LOGI("Register result hasNetSpecifier_ and hasTimeout_ %{public}d", ret);
956 context->SetErrorCode(ret);
957 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, ret);
958 return ret == NETMANAGER_SUCCESS;
959 }
960
961 if (conn.hasNetSpecifier_) {
962 sptr<NetSpecifier> specifier = new NetSpecifier(conn.netSpecifier_);
963 int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, callback, 0);
964 NETMANAGER_BASE_LOGD("Register result hasNetSpecifier_ %{public}d", ret);
965 context->SetErrorCode(ret);
966 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, ret);
967 return ret == NETMANAGER_SUCCESS;
968 }
969
970 int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(callback);
971 NETMANAGER_BASE_LOGI("Register result %{public}d", ret);
972 context->SetErrorCode(ret);
973 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, ret);
974 return ret == NETMANAGER_SUCCESS;
975 }
976
RegisterCallback(RegisterContext * context)977 napi_value ConnectionExec::NetConnectionExec::RegisterCallback(RegisterContext *context)
978 {
979 return NapiUtils::GetUndefined(context->GetEnv());
980 }
981
ExecUnregister(UnregisterContext * context)982 bool ConnectionExec::NetConnectionExec::ExecUnregister(UnregisterContext *context)
983 {
984 HiAppEventReport hiAppEventReport("NetworkKit", "ConnectionUnregister");
985 auto wCallback = context->GetNetConnCallback();
986 auto callback = wCallback.promote();
987 if (callback == nullptr) {
988 NETMANAGER_BASE_LOGE("ExecUnregister getNetConnCallback nullptr");
989 return false;
990 }
991
992 int32_t ret = NetConnClient::GetInstance().UnregisterNetConnCallback(callback);
993 if (ret != NETMANAGER_SUCCESS) {
994 NETMANAGER_BASE_LOGD("Unregister result %{public}d", ret);
995 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, ret);
996 context->SetErrorCode(ret);
997 }
998 hiAppEventReport.ReportSdkEvent(RESULT_SUCCESS, ret);
999 return ret == NETMANAGER_SUCCESS;
1000 }
1001
UnregisterCallback(RegisterContext * context)1002 napi_value ConnectionExec::NetConnectionExec::UnregisterCallback(RegisterContext *context)
1003 {
1004 return NapiUtils::GetUndefined(context->GetEnv());
1005 }
1006
ExecIfaceRegister(IfaceRegisterContext * context)1007 bool ConnectionExec::NetInterfaceExec::ExecIfaceRegister(IfaceRegisterContext *context)
1008 {
1009 auto wCallback = context->GetNetInterfaceCallback();
1010 sptr<INetInterfaceStateCallback> callback = wCallback.promote();
1011 if (callback == nullptr) {
1012 NETMANAGER_BASE_LOGE("ExecIfaceRegister getNetInterfaceCallback nullptr");
1013 return false;
1014 }
1015
1016 int32_t ret = NetConnClient::GetInstance().RegisterNetInterfaceCallback(callback);
1017 NETMANAGER_BASE_LOGI("Register result %{public}d", ret);
1018 context->SetErrorCode(ret);
1019 return ret == NETMANAGER_SUCCESS;
1020 }
1021
IfaceRegisterCallback(IfaceRegisterContext * context)1022 napi_value ConnectionExec::NetInterfaceExec::IfaceRegisterCallback(IfaceRegisterContext *context)
1023 {
1024 return NapiUtils::GetUndefined(context->GetEnv());
1025 }
1026
ExecIfaceUnregister(IfaceUnregisterContext * context)1027 bool ConnectionExec::NetInterfaceExec::ExecIfaceUnregister(IfaceUnregisterContext *context)
1028 {
1029 auto wCallback = context->GetNetInterfaceCallback();
1030 auto callback = wCallback.promote();
1031 if (callback == nullptr) {
1032 NETMANAGER_BASE_LOGE("ExecIfaceUnregister getNetInterfaceCallback nullptr");
1033 return false;
1034 }
1035
1036 int32_t ret = NetConnClient::GetInstance().UnregisterNetInterfaceCallback(callback);
1037 if (ret != NETMANAGER_SUCCESS) {
1038 NETMANAGER_BASE_LOGD("Unregister result %{public}d", ret);
1039 context->SetErrorCode(ret);
1040 }
1041 return ret == NETMANAGER_SUCCESS;
1042 }
1043
IfaceUnregisterCallback(IfaceUnregisterContext * context)1044 napi_value ConnectionExec::NetInterfaceExec::IfaceUnregisterCallback(IfaceUnregisterContext *context)
1045 {
1046 return NapiUtils::GetUndefined(context->GetEnv());
1047 }
1048
FillLinkAddress(napi_env env,napi_value connectionProperties,NetLinkInfo * linkInfo)1049 void ConnectionExec::FillLinkAddress(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo)
1050 {
1051 if (!linkInfo->netAddrList_.empty() && linkInfo->netAddrList_.size() <= MAX_ARRAY_LENGTH) {
1052 napi_value linkAddresses =
1053 NapiUtils::CreateArray(env, std::min(linkInfo->netAddrList_.size(), MAX_ARRAY_LENGTH));
1054 auto it = linkInfo->netAddrList_.begin();
1055 for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != linkInfo->netAddrList_.end(); ++index, ++it) {
1056 napi_value netAddr = NapiUtils::CreateObject(env);
1057 NapiUtils::SetStringPropertyUtf8(env, netAddr, KEY_ADDRESS, it->address_);
1058 NapiUtils::SetUint32Property(env, netAddr, KEY_FAMILY, it->family_);
1059 NapiUtils::SetUint32Property(env, netAddr, KEY_PORT, it->port_);
1060
1061 napi_value linkAddr = NapiUtils::CreateObject(env);
1062 NapiUtils::SetNamedProperty(env, linkAddr, KEY_ADDRESS, netAddr);
1063 NapiUtils::SetUint32Property(env, linkAddr, KEY_PREFIX_LENGTH, it->prefixlen_);
1064 NapiUtils::SetArrayElement(env, linkAddresses, index, linkAddr);
1065 }
1066 NapiUtils::SetNamedProperty(env, connectionProperties, KEY_LINK_ADDRESSES, linkAddresses);
1067 }
1068 }
1069
FillRouoteList(napi_env env,napi_value connectionProperties,NetLinkInfo * linkInfo)1070 void ConnectionExec::FillRouoteList(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo)
1071 {
1072 if (!linkInfo->routeList_.empty() && linkInfo->routeList_.size() <= MAX_ARRAY_LENGTH) {
1073 napi_value routes = NapiUtils::CreateArray(env, std::min(linkInfo->routeList_.size(), MAX_ARRAY_LENGTH));
1074 auto it = linkInfo->routeList_.begin();
1075 for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != linkInfo->routeList_.end(); ++index, ++it) {
1076 napi_value route = NapiUtils::CreateObject(env);
1077 NapiUtils::SetStringPropertyUtf8(env, route, KEY_INTERFACE, it->iface_);
1078
1079 napi_value dest = NapiUtils::CreateObject(env);
1080 napi_value netAddr = NapiUtils::CreateObject(env);
1081 NapiUtils::SetStringPropertyUtf8(env, netAddr, KEY_ADDRESS, it->destination_.address_);
1082 NapiUtils::SetUint32Property(env, netAddr, KEY_FAMILY, it->destination_.family_);
1083 NapiUtils::SetUint32Property(env, netAddr, KEY_PORT, it->destination_.port_);
1084 NapiUtils::SetNamedProperty(env, dest, KEY_ADDRESS, netAddr);
1085 NapiUtils::SetUint32Property(env, dest, KEY_PREFIX_LENGTH, it->destination_.prefixlen_);
1086 NapiUtils::SetNamedProperty(env, route, KEY_DESTINATION, dest);
1087
1088 napi_value gateway = NapiUtils::CreateObject(env);
1089 NapiUtils::SetStringPropertyUtf8(env, gateway, KEY_ADDRESS, it->gateway_.address_);
1090 NapiUtils::SetUint32Property(env, gateway, KEY_PREFIX_LENGTH, it->gateway_.prefixlen_);
1091 NapiUtils::SetNamedProperty(env, route, KEY_GATE_WAY, gateway);
1092
1093 NapiUtils::SetBooleanProperty(env, route, KEY_HAS_GET_WAY, it->hasGateway_);
1094 NapiUtils::SetBooleanProperty(env, route, KEY_IS_DEFAULT_ROUE, it->isDefaultRoute_);
1095
1096 NapiUtils::SetArrayElement(env, routes, index, route);
1097 }
1098 NapiUtils::SetNamedProperty(env, connectionProperties, KEY_ROUTES, routes);
1099 }
1100 }
1101
FillDns(napi_env env,napi_value connectionProperties,NetLinkInfo * linkInfo)1102 void ConnectionExec::FillDns(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo)
1103 {
1104 if (!linkInfo->dnsList_.empty() && linkInfo->dnsList_.size() <= MAX_ARRAY_LENGTH) {
1105 napi_value dnsList = NapiUtils::CreateArray(env, std::min(linkInfo->dnsList_.size(), MAX_ARRAY_LENGTH));
1106 auto it = linkInfo->dnsList_.begin();
1107 for (uint32_t index = 0; index < MAX_ARRAY_LENGTH && it != linkInfo->dnsList_.end(); ++index, ++it) {
1108 napi_value netAddr = NapiUtils::CreateObject(env);
1109 NapiUtils::SetStringPropertyUtf8(env, netAddr, KEY_ADDRESS, it->address_);
1110 NapiUtils::SetUint32Property(env, netAddr, KEY_FAMILY, it->family_);
1111 NapiUtils::SetUint32Property(env, netAddr, KEY_PORT, it->port_);
1112 NapiUtils::SetArrayElement(env, dnsList, index, netAddr);
1113 }
1114 NapiUtils::SetNamedProperty(env, connectionProperties, KEY_DNSES, dnsList);
1115 }
1116 }
1117
ExecSetProxyMode(ProxyModeContext * context)1118 bool ConnectionExec::ExecSetProxyMode(ProxyModeContext *context)
1119 {
1120 int32_t errorCode = NetConnClient::GetInstance().SetProxyMode(context->mode_);
1121 if (errorCode != NET_CONN_SUCCESS) {
1122 NETMANAGER_BASE_LOGE("exec SetProxyMode failed errorCode: %{public}d", errorCode);
1123 context->SetErrorCode(errorCode);
1124 return false;
1125 }
1126 return true;
1127 }
1128
ExecGetProxyMode(ProxyModeContext * context)1129 bool ConnectionExec::ExecGetProxyMode(ProxyModeContext *context)
1130 {
1131 int32_t errorCode = NetConnClient::GetInstance().GetProxyMode(context->mode_);
1132 if (errorCode != NET_CONN_SUCCESS) {
1133 NETMANAGER_BASE_LOGE("exec GetProxyMode failed errorCode: %{public}d", errorCode);
1134 context->SetErrorCode(errorCode);
1135 return false;
1136 }
1137 return true;
1138 }
1139
SetProxyModeCallback(ProxyModeContext * context)1140 napi_value ConnectionExec::SetProxyModeCallback(ProxyModeContext *context)
1141 {
1142 return NapiUtils::GetUndefined(context->GetEnv());
1143 }
1144
GetProxyModeCallback(ProxyModeContext * context)1145 napi_value ConnectionExec::GetProxyModeCallback(ProxyModeContext *context)
1146 {
1147 return NapiUtils::CreateInt32(context->GetEnv(), context->mode_);
1148 }
1149
ExecSetPacFileUrl(SetPacFileUrlContext * context)1150 bool ConnectionExec::ExecSetPacFileUrl(SetPacFileUrlContext *context)
1151 {
1152 int32_t errorCode = NetConnClient::GetInstance().SetPacFileUrl(context->pacUrl_);
1153 if (errorCode != NET_CONN_SUCCESS) {
1154 NETMANAGER_BASE_LOGE("exec SetPacFileUrl failed errorCode: %{public}d", errorCode);
1155 context->SetErrorCode(errorCode);
1156 return false;
1157 }
1158 return true;
1159 }
1160
SetPacFileUrlCallback(SetPacFileUrlContext * context)1161 napi_value ConnectionExec::SetPacFileUrlCallback(SetPacFileUrlContext *context)
1162 {
1163 return NapiUtils::GetUndefined(context->GetEnv());
1164 }
1165
ExecGetPacFileUrl(GetPacFileUrlContext * context)1166 bool ConnectionExec::ExecGetPacFileUrl(GetPacFileUrlContext *context)
1167 {
1168 int32_t errorCode = NetConnClient::GetInstance().GetPacFileUrl(context->pacUrl_);
1169 if (errorCode != NET_CONN_SUCCESS) {
1170 NETMANAGER_BASE_LOGE("exec GetPacFileUrl failed errorCode: %{public}d", errorCode);
1171 context->SetErrorCode(errorCode);
1172 return false;
1173 }
1174 return true;
1175 }
1176
GetPacFileUrlCallback(GetPacFileUrlContext * context)1177 napi_value ConnectionExec::GetPacFileUrlCallback(GetPacFileUrlContext *context)
1178 {
1179 return NapiUtils::CreateStringUtf8(context->GetEnv(), context->pacUrl_);
1180 }
1181
ExecFindProxyForUrl(FindPacFileUrlContext * context)1182 bool ConnectionExec::ExecFindProxyForUrl(FindPacFileUrlContext *context)
1183 {
1184 int32_t errorCode = NetConnClient::GetInstance().FindProxyForURL(context->url_, context->proxy_);
1185 if (errorCode != NET_CONN_SUCCESS) {
1186 NETMANAGER_BASE_LOGE("exec ExecFindProxyForUrl failed errorCode: %{public}d", errorCode);
1187 context->SetErrorCode(errorCode);
1188 return false;
1189 }
1190 return true;
1191 }
1192
FindProxyForUrlCallback(FindPacFileUrlContext * context)1193 napi_value ConnectionExec::FindProxyForUrlCallback(FindPacFileUrlContext *context)
1194 {
1195 return NapiUtils::CreateStringUtf8(context->GetEnv(), context->proxy_);
1196 }
1197
1198 } // namespace OHOS::NetManagerStandard
1199