1 /*
2 * Copyright (C) 2022-2023 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 "set_iface_config_context.h"
17
18 #include <new>
19
20 #include "constant.h"
21 #include "napi_utils.h"
22 #include "net_manager_constants.h"
23 #include "netmanager_base_log.h"
24
25 namespace OHOS {
26 namespace NetManagerStandard {
27 namespace {
28 constexpr const char *OBJECT_HTTP_RPPXY = "httpProxy";
29 constexpr const char *HTTP_RPPXY_HOST = "host";
30 constexpr const char *HTTP_RPPXY_PORT = "port";
31 constexpr const char *HTTP_RPPXY_EXCLUSION_LIST = "exclusionList";
CheckParamsType(napi_env env,napi_value * params,size_t paramsCount)32 bool CheckParamsType(napi_env env, napi_value *params, size_t paramsCount)
33 {
34 if (paramsCount == PARAM_DOUBLE_OPTIONS || paramsCount == PARAM_DOUBLE_OPTIONS_AND_CALLBACK) {
35 if (NapiUtils::GetValueType(env, params[ARG_NUM_0]) != napi_string ||
36 NapiUtils::GetValueType(env, params[ARG_NUM_1]) != napi_object) {
37 return false;
38 }
39 } else {
40 // if paramsCount is not 2 or 3, means count error.
41 return false;
42 }
43 return true;
44 }
45 } // namespace
46
SetIfaceConfigContext(napi_env env,EventManager * manager)47 SetIfaceConfigContext::SetIfaceConfigContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
48
ParseParams(napi_value * params,size_t paramsCount)49 void SetIfaceConfigContext::ParseParams(napi_value *params, size_t paramsCount)
50 {
51 if (!CheckParamsType(GetEnv(), params, paramsCount)) {
52 SetNeedThrowException(true);
53 SetErrorCode(NETMANAGER_EXT_ERR_PARAMETER_ERROR);
54 return;
55 }
56 iface_ = NapiUtils::GetStringFromValueUtf8(GetEnv(), params[ARG_NUM_0]);
57 config_ = new (std::nothrow) InterfaceConfiguration();
58 if (config_ == nullptr) {
59 SetParseOK(false);
60 return;
61 }
62 config_->mode_ = static_cast<IPSetMode>(NapiUtils::GetInt32Property(GetEnv(), params[ARG_NUM_1], "mode"));
63 std::string ipAddresses = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[ARG_NUM_1], "ipAddr");
64 StaticConfiguration::ExtractNetAddrBySeparator(ipAddresses, config_->ipStatic_.ipAddrList_);
65
66 std::string routeAddresses = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[ARG_NUM_1], "route");
67 StaticConfiguration::ExtractNetAddrBySeparator(routeAddresses, config_->ipStatic_.routeList_);
68
69 std::string gatewayAddresses = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[ARG_NUM_1], "gateway");
70 StaticConfiguration::ExtractNetAddrBySeparator(gatewayAddresses, config_->ipStatic_.gatewayList_);
71
72 std::string maskAddresses = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[ARG_NUM_1], "netMask");
73 StaticConfiguration::ExtractNetAddrBySeparator(maskAddresses, config_->ipStatic_.netMaskList_);
74
75 std::string dnsServers = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[ARG_NUM_1], "dnsServers");
76 StaticConfiguration::ExtractNetAddrBySeparator(dnsServers, config_->ipStatic_.dnsServers_);
77
78 config_->ipStatic_.domain_ = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[ARG_NUM_1], "domain");
79
80 ParseHttpProxy(params);
81 SetParseOK((paramsCount == PARAM_DOUBLE_OPTIONS_AND_CALLBACK) ? (SetCallback(params[ARG_NUM_2]) == napi_ok) : true);
82 }
83
ParseHttpProxy(napi_value * params)84 void SetIfaceConfigContext::ParseHttpProxy(napi_value *params)
85 {
86 if (!NapiUtils::HasNamedProperty(GetEnv(), params[ARG_NUM_1], OBJECT_HTTP_RPPXY)) {
87 NETMANAGER_BASE_LOGE("Do not use HttpProxy.");
88 return;
89 }
90 napi_value value = NapiUtils::GetNamedProperty(GetEnv(), params[ARG_NUM_1], OBJECT_HTTP_RPPXY);
91 if (NapiUtils::GetValueType(GetEnv(), value) != napi_object) {
92 NETMANAGER_BASE_LOGE("httpProxy is not a object");
93 return;
94 }
95
96 std::string host = NapiUtils::GetStringPropertyUtf8(GetEnv(), value, HTTP_RPPXY_HOST);
97 uint16_t port =
98 host.empty() ? 0 : static_cast<uint16_t>(NapiUtils::GetUint32Property(GetEnv(), value, HTTP_RPPXY_PORT));
99 std::list<std::string> exclusionList;
100 if (!host.empty()) {
101 napi_value exclusionsValue = NapiUtils::GetNamedProperty(GetEnv(), value, HTTP_RPPXY_EXCLUSION_LIST);
102 uint32_t size = NapiUtils::GetArrayLength(GetEnv(), exclusionsValue);
103 for (uint32_t i = 0; i < size; ++i) {
104 napi_value element = NapiUtils::GetArrayElement(GetEnv(), exclusionsValue, i);
105 exclusionList.push_back(NapiUtils::GetStringFromValueUtf8(GetEnv(), element));
106 }
107 }
108
109 config_->httpProxy_.SetHost(std::move(host));
110 config_->httpProxy_.SetPort(port);
111 config_->httpProxy_.SetExclusionList(exclusionList);
112 }
113 } // namespace NetManagerStandard
114 } // namespace OHOS
115