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,std::shared_ptr<EventManager> & manager)47 SetIfaceConfigContext::SetIfaceConfigContext(napi_env env, std::shared_ptr<EventManager>& manager)
48 : BaseContext(env, manager) {}
49
ParseParams(napi_value * params,size_t paramsCount)50 void SetIfaceConfigContext::ParseParams(napi_value *params, size_t paramsCount)
51 {
52 if (!CheckParamsType(GetEnv(), params, paramsCount)) {
53 SetNeedThrowException(true);
54 SetErrorCode(NETMANAGER_EXT_ERR_PARAMETER_ERROR);
55 return;
56 }
57 iface_ = NapiUtils::GetStringFromValueUtf8(GetEnv(), params[ARG_NUM_0]);
58 config_ = new (std::nothrow) InterfaceConfiguration();
59 if (config_ == nullptr) {
60 SetParseOK(false);
61 return;
62 }
63 config_->mode_ = static_cast<IPSetMode>(NapiUtils::GetInt32Property(GetEnv(), params[ARG_NUM_1], "mode"));
64 std::string ipAddresses = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[ARG_NUM_1], "ipAddr");
65 StaticConfiguration::ExtractNetAddrBySeparator(ipAddresses, config_->ipStatic_.ipAddrList_);
66
67 std::string routeAddresses = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[ARG_NUM_1], "route");
68 StaticConfiguration::ExtractNetAddrBySeparator(routeAddresses, config_->ipStatic_.routeList_);
69
70 std::string gatewayAddresses = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[ARG_NUM_1], "gateway");
71 StaticConfiguration::ExtractNetAddrBySeparator(gatewayAddresses, config_->ipStatic_.gatewayList_);
72
73 std::string maskAddresses = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[ARG_NUM_1], "netMask");
74 StaticConfiguration::ExtractNetAddrBySeparator(maskAddresses, config_->ipStatic_.netMaskList_);
75
76 std::string dnsServers = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[ARG_NUM_1], "dnsServers");
77 StaticConfiguration::ExtractNetAddrBySeparator(dnsServers, config_->ipStatic_.dnsServers_);
78
79 config_->ipStatic_.domain_ = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[ARG_NUM_1], "domain");
80
81 ParseHttpProxy(params);
82 SetParseOK((paramsCount == PARAM_DOUBLE_OPTIONS_AND_CALLBACK) ? (SetCallback(params[ARG_NUM_2]) == napi_ok) : true);
83 }
84
ParseHttpProxy(napi_value * params)85 void SetIfaceConfigContext::ParseHttpProxy(napi_value *params)
86 {
87 if (!NapiUtils::HasNamedProperty(GetEnv(), params[ARG_NUM_1], OBJECT_HTTP_RPPXY)) {
88 NETMANAGER_BASE_LOGE("Do not use HttpProxy.");
89 return;
90 }
91 napi_value value = NapiUtils::GetNamedProperty(GetEnv(), params[ARG_NUM_1], OBJECT_HTTP_RPPXY);
92 if (NapiUtils::GetValueType(GetEnv(), value) != napi_object) {
93 NETMANAGER_BASE_LOGE("httpProxy is not a object");
94 return;
95 }
96
97 std::string host = NapiUtils::GetStringPropertyUtf8(GetEnv(), value, HTTP_RPPXY_HOST);
98 uint16_t port =
99 host.empty() ? 0 : static_cast<uint16_t>(NapiUtils::GetUint32Property(GetEnv(), value, HTTP_RPPXY_PORT));
100 std::list<std::string> exclusionList;
101 if (!host.empty()) {
102 napi_value exclusionsValue = NapiUtils::GetNamedProperty(GetEnv(), value, HTTP_RPPXY_EXCLUSION_LIST);
103 uint32_t size = NapiUtils::GetArrayLength(GetEnv(), exclusionsValue);
104 for (uint32_t i = 0; i < size; ++i) {
105 napi_value element = NapiUtils::GetArrayElement(GetEnv(), exclusionsValue, i);
106 exclusionList.push_back(NapiUtils::GetStringFromValueUtf8(GetEnv(), element));
107 }
108 }
109
110 config_->httpProxy_.SetHost(std::move(host));
111 config_->httpProxy_.SetPort(port);
112 config_->httpProxy_.SetExclusionList(exclusionList);
113 }
114 } // namespace NetManagerStandard
115 } // namespace OHOS
116