• 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 "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_common_utils.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 namespace {
28 constexpr int32_t DNS_MAX_SIZE = 10;
CheckParamsType(napi_env env,napi_value * params,size_t paramsCount)29 bool CheckParamsType(napi_env env, napi_value *params, size_t paramsCount)
30 {
31     if (paramsCount == PARAM_DOUBLE_OPTIONS || paramsCount == PARAM_DOUBLE_OPTIONS_AND_CALLBACK) {
32         if (NapiUtils::GetValueType(env, params[0]) != napi_string ||
33             NapiUtils::GetValueType(env, params[1]) != napi_object) {
34             return false;
35         }
36     } else {
37         // if paramsCount is not 2 or 3, means count error.
38         return false;
39     }
40     return true;
41 }
42 } // namespace
43 
SetIfaceConfigContext(napi_env env,EventManager * manager)44 SetIfaceConfigContext::SetIfaceConfigContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
45 
ParseParams(napi_value * params,size_t paramsCount)46 void SetIfaceConfigContext::ParseParams(napi_value *params, size_t paramsCount)
47 {
48     if (!CheckParamsType(GetEnv(), params, paramsCount)) {
49         SetNeedThrowException(true);
50         SetErrorCode(NETMANAGER_EXT_ERR_PARAMETER_ERROR);
51         return;
52     }
53     iface_ = NapiUtils::GetStringFromValueUtf8(GetEnv(), params[0]);
54     config_ = new (std::nothrow) InterfaceConfiguration();
55     if (config_ == nullptr) {
56         SetParseOK(false);
57         return;
58     }
59     config_->mode_ = static_cast<IPSetMode>(NapiUtils::GetInt32Property(GetEnv(), params[1], "mode"));
60     config_->ipStatic_.ipAddr_.address_ = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[1], "ipAddr");
61     config_->ipStatic_.route_.address_ = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[1], "route");
62     config_->ipStatic_.gateway_.address_ = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[1], "gateway");
63     config_->ipStatic_.netMask_.address_ = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[1], "netMask");
64     config_->ipStatic_.domain_ = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[1], "domain");
65     for (const auto &dns :
66          CommonUtils::Split(NapiUtils::GetStringPropertyUtf8(GetEnv(), params[1], "dnsServers"), ",")) {
67         INetAddr addr;
68         addr.address_ = dns;
69         config_->ipStatic_.dnsServers_.push_back(addr);
70         if (config_->ipStatic_.dnsServers_.size() == DNS_MAX_SIZE) {
71             break;
72         }
73     }
74 
75     if (paramsCount == PARAM_DOUBLE_OPTIONS_AND_CALLBACK) {
76         SetParseOK(SetCallback(params[2]) == napi_ok);
77         return;
78     }
79     SetParseOK(true);
80 }
81 } // namespace NetManagerStandard
82 } // namespace OHOS
83