1 /*
2  * Copyright (c) 2022-2024 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 "tls_bind_context.h"
17 
18 #include <string_view>
19 
20 #include "context_key.h"
21 #include "event_manager.h"
22 #include "netstack_log.h"
23 #include "napi_utils.h"
24 
25 namespace OHOS::NetStack::TlsSocket {
26 static constexpr std::string_view PARSE_ERROR = "address is not type of Socket::NetAddress";
27 
TLSBindContext(napi_env env,EventManager * manager)28 TLSBindContext::TLSBindContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
29 
ParseParams(napi_value * params,size_t paramsCount)30 void TLSBindContext::ParseParams(napi_value *params, size_t paramsCount)
31 {
32     bool valid = CheckParamsType(params, paramsCount);
33     if (!valid) {
34         return;
35     }
36 
37     std::string addr = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[0], KEY_ADDRESS);
38     if (addr.empty()) {
39         NETSTACK_LOGE("address is empty");
40     }
41 
42     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_FAMILY)) {
43         uint32_t family = NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_FAMILY);
44         address_.SetFamilyByJsValue(family);
45     }
46     address_.SetRawAddress(addr);
47     if (address_.GetAddress().empty()) {
48         if (paramsCount == PARAM_OPTIONS_AND_CALLBACK && SetCallback(params[1]) != napi_ok) {
49             NETSTACK_LOGE("failed to set callback");
50         }
51         return;
52     }
53 
54     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_PORT)) {
55         uint16_t port = static_cast<uint16_t>(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_PORT));
56         address_.SetPort(port);
57     }
58 
59     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
60         SetParseOK(SetCallback(params[1]) == napi_ok);
61         return;
62     }
63     SetParseOK(true);
64 }
65 
GetSocketFd() const66 int TLSBindContext::GetSocketFd() const
67 {
68     return manager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(manager_->GetData())) : -1;
69 }
70 
CheckParamsType(napi_value * params,size_t paramsCount)71 bool TLSBindContext::CheckParamsType(napi_value *params, size_t paramsCount)
72 {
73     if (paramsCount == PARAM_JUST_OPTIONS) {
74         if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object) {
75             NETSTACK_LOGE("first param is not NetAddress");
76             SetNeedThrowException(true);
77             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
78             return false;
79         }
80         return true;
81     }
82 
83     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
84         if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object) {
85             NETSTACK_LOGE("first param is not NetAddress");
86             SetNeedThrowException(true);
87             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
88             return false;
89         }
90         if (NapiUtils::GetValueType(GetEnv(), params[1]) != napi_function) {
91             NETSTACK_LOGE("SendContext second param is not function");
92             return false;
93         }
94         return true;
95     }
96     return false;
97 }
98 } // namespace OHOS::NetStack::TlsSocket
99