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 "ethernet_exec.h"
17
18 #include <cstdint>
19 #include <new>
20 #include <numeric>
21 #include <string>
22
23 #include "ethernet_client.h"
24 #include "napi_utils.h"
25 #include "net_manager_constants.h"
26 #include "netmanager_ext_log.h"
27
28 namespace OHOS {
29 namespace NetManagerStandard {
30 namespace EthernetExec {
31 namespace {
32 constexpr const char *MODE = "mode";
33 constexpr const char *IP_ADDR = "ipAddr";
34 constexpr const char *ROUTE = "route";
35 constexpr const char *NET_MASK = "netMask";
36 constexpr const char *GATEWAY = "gateway";
37 constexpr const char *DNS_SERVERS = "dnsServers";
38 } // namespace
ExecGetIfaceConfig(GetIfaceConfigContext * context)39 bool ExecGetIfaceConfig(GetIfaceConfigContext *context)
40 {
41 int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->GetIfaceConfig(context->iface_, context->config_);
42 if (context->config_ == nullptr || result != NETMANAGER_EXT_SUCCESS) {
43 NETMANAGER_EXT_LOGE("ExecGetIfaceConfig error, errorCode: %{public}d", result);
44 context->SetErrorCode(result);
45 return false;
46 }
47 return true;
48 }
49
GetIfaceConfigCallback(GetIfaceConfigContext * context)50 napi_value GetIfaceConfigCallback(GetIfaceConfigContext *context)
51 {
52 napi_value interfaceConfiguration = NapiUtils::CreateObject(context->GetEnv());
53 NapiUtils::SetInt32Property(context->GetEnv(), interfaceConfiguration, MODE, context->config_->mode_);
54 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, IP_ADDR,
55 context->config_->ipStatic_.ipAddr_.address_);
56 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, ROUTE,
57 context->config_->ipStatic_.route_.address_);
58 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, GATEWAY,
59 context->config_->ipStatic_.gateway_.address_);
60 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, NET_MASK,
61 context->config_->ipStatic_.netMask_.address_);
62 std::string dnsServers = std::accumulate(context->config_->ipStatic_.dnsServers_.begin(),
63 context->config_->ipStatic_.dnsServers_.end(), std::string(),
64 [](const std::string &str_append, INetAddr const &iter) {
65 return str_append + iter.address_ + ",";
66 });
67
68 NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, DNS_SERVERS, dnsServers);
69 return interfaceConfiguration;
70 }
71
ExecSetIfaceConfig(SetIfaceConfigContext * context)72 bool ExecSetIfaceConfig(SetIfaceConfigContext *context)
73 {
74 int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->SetIfaceConfig(context->iface_, context->config_);
75 if (result != NETMANAGER_EXT_SUCCESS) {
76 NETMANAGER_EXT_LOGE("ExecSetIfaceConfig error, errorCode: %{public}d", result);
77 context->SetErrorCode(result);
78 return false;
79 }
80 return true;
81 }
82
SetIfaceConfigCallback(SetIfaceConfigContext * context)83 napi_value SetIfaceConfigCallback(SetIfaceConfigContext *context)
84 {
85 return NapiUtils::GetUndefined(context->GetEnv());
86 }
87
ExecIsIfaceActive(IsIfaceActiveContext * context)88 bool ExecIsIfaceActive(IsIfaceActiveContext *context)
89 {
90 int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->IsIfaceActive(context->iface_, context->ifActivate_);
91 if (result != NETMANAGER_EXT_SUCCESS) {
92 context->SetErrorCode(result);
93 NETMANAGER_EXT_LOGE("ExecIsIfaceActive error, errorCode: %{public}d", result);
94 return false;
95 }
96 return true;
97 }
98
IsIfaceActiveCallback(IsIfaceActiveContext * context)99 napi_value IsIfaceActiveCallback(IsIfaceActiveContext *context)
100 {
101 return NapiUtils::CreateInt32(context->GetEnv(), context->ifActivate_);
102 }
103
ExecGetAllActiveIfaces(GetAllActiveIfacesContext * context)104 bool ExecGetAllActiveIfaces(GetAllActiveIfacesContext *context)
105 {
106 int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->GetAllActiveIfaces(context->ethernetNameList_);
107 if (result != NETMANAGER_EXT_SUCCESS) {
108 context->SetErrorCode(result);
109 NETMANAGER_EXT_LOGE("ExecIsIfaceActive error, errorCode: %{public}d", result);
110 return false;
111 }
112 return true;
113 }
114
GetAllActiveIfacesCallback(GetAllActiveIfacesContext * context)115 napi_value GetAllActiveIfacesCallback(GetAllActiveIfacesContext *context)
116 {
117 napi_value ifaces = NapiUtils::CreateArray(context->GetEnv(), context->ethernetNameList_.size());
118 uint32_t index = 0;
119 for (const auto &iface : context->ethernetNameList_) {
120 napi_value ifaceStr = NapiUtils::CreateStringUtf8(context->GetEnv(), iface);
121 NapiUtils::SetArrayElement(context->GetEnv(), ifaces, index++, ifaceStr);
122 }
123 return ifaces;
124 }
125 } // namespace EthernetExec
126 } // namespace NetManagerStandard
127 } // namespace OHOS
128