• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 constexpr const char *DOMAIN = "domain";
39 constexpr const char *DEFAULT_SEPARATOR = ",";
40 
AccumulateNetAddress(const std::vector<INetAddr> & netAddrList)41 std::string AccumulateNetAddress(const std::vector<INetAddr> &netAddrList)
42 {
43     return std::accumulate(
44         netAddrList.begin(), netAddrList.end(), std::string(), [](const std::string &addr, const INetAddr &iter) {
45             return addr.empty() ? (addr + iter.address_) : (addr + DEFAULT_SEPARATOR + iter.address_);
46         });
47 }
48 } // namespace
ExecGetIfaceConfig(GetIfaceConfigContext * context)49 bool ExecGetIfaceConfig(GetIfaceConfigContext *context)
50 {
51     int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->GetIfaceConfig(context->iface_, context->config_);
52     if (context->config_ == nullptr || result != NETMANAGER_EXT_SUCCESS) {
53         NETMANAGER_EXT_LOGE("ExecGetIfaceConfig error, errorCode: %{public}d", result);
54         context->SetErrorCode(result);
55         return false;
56     }
57     return true;
58 }
59 
GetIfaceConfigCallback(GetIfaceConfigContext * context)60 napi_value GetIfaceConfigCallback(GetIfaceConfigContext *context)
61 {
62     napi_value interfaceConfiguration = NapiUtils::CreateObject(context->GetEnv());
63     NapiUtils::SetInt32Property(context->GetEnv(), interfaceConfiguration, MODE, context->config_->mode_);
64 
65     std::string ipAddresses = AccumulateNetAddress(context->config_->ipStatic_.ipAddrList_);
66     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, IP_ADDR, ipAddresses);
67 
68     std::string routeAddresses = AccumulateNetAddress(context->config_->ipStatic_.routeList_);
69     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, ROUTE, routeAddresses);
70 
71     std::string gatewayAddresses = AccumulateNetAddress(context->config_->ipStatic_.gatewayList_);
72     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, GATEWAY, gatewayAddresses);
73 
74     std::string maskAddresses = AccumulateNetAddress(context->config_->ipStatic_.netMaskList_);
75     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, NET_MASK, maskAddresses);
76 
77     std::string dnsServers = AccumulateNetAddress(context->config_->ipStatic_.dnsServers_);
78     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, DNS_SERVERS, dnsServers);
79 
80     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), interfaceConfiguration, DOMAIN,
81                                      context->config_->ipStatic_.domain_);
82     return interfaceConfiguration;
83 }
84 
ExecSetIfaceConfig(SetIfaceConfigContext * context)85 bool ExecSetIfaceConfig(SetIfaceConfigContext *context)
86 {
87     int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->SetIfaceConfig(context->iface_, context->config_);
88     if (result != NETMANAGER_EXT_SUCCESS) {
89         NETMANAGER_EXT_LOGE("ExecSetIfaceConfig error, errorCode: %{public}d", result);
90         context->SetErrorCode(result);
91         return false;
92     }
93     return true;
94 }
95 
SetIfaceConfigCallback(SetIfaceConfigContext * context)96 napi_value SetIfaceConfigCallback(SetIfaceConfigContext *context)
97 {
98     return NapiUtils::GetUndefined(context->GetEnv());
99 }
100 
ExecIsIfaceActive(IsIfaceActiveContext * context)101 bool ExecIsIfaceActive(IsIfaceActiveContext *context)
102 {
103     int32_t result =
104         DelayedSingleton<EthernetClient>::GetInstance()->IsIfaceActive(context->iface_, context->ifActivate_);
105     if (result != NETMANAGER_EXT_SUCCESS) {
106         context->SetErrorCode(result);
107         NETMANAGER_EXT_LOGE("ExecIsIfaceActive error, errorCode: %{public}d", result);
108         return false;
109     }
110     return true;
111 }
112 
IsIfaceActiveCallback(IsIfaceActiveContext * context)113 napi_value IsIfaceActiveCallback(IsIfaceActiveContext *context)
114 {
115     return NapiUtils::CreateInt32(context->GetEnv(), context->ifActivate_);
116 }
117 
ExecGetAllActiveIfaces(GetAllActiveIfacesContext * context)118 bool ExecGetAllActiveIfaces(GetAllActiveIfacesContext *context)
119 {
120     int32_t result = DelayedSingleton<EthernetClient>::GetInstance()->GetAllActiveIfaces(context->ethernetNameList_);
121     if (result != NETMANAGER_EXT_SUCCESS) {
122         context->SetErrorCode(result);
123         NETMANAGER_EXT_LOGE("ExecIsIfaceActive error, errorCode: %{public}d", result);
124         return false;
125     }
126     return true;
127 }
128 
GetAllActiveIfacesCallback(GetAllActiveIfacesContext * context)129 napi_value GetAllActiveIfacesCallback(GetAllActiveIfacesContext *context)
130 {
131     napi_value ifaces = NapiUtils::CreateArray(context->GetEnv(), context->ethernetNameList_.size());
132     uint32_t index = 0;
133     for (const auto &iface : context->ethernetNameList_) {
134         napi_value ifaceStr = NapiUtils::CreateStringUtf8(context->GetEnv(), iface);
135         NapiUtils::SetArrayElement(context->GetEnv(), ifaces, index++, ifaceStr);
136     }
137     return ifaces;
138 }
139 } // namespace EthernetExec
140 } // namespace NetManagerStandard
141 } // namespace OHOS
142