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