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 "tls_extra_context.h"
17 
18 #include <string_view>
19 
20 #include "context_key.h"
21 #include "event_manager.h"
22 #include "napi_utils.h"
23 #include "netstack_log.h"
24 
25 namespace OHOS::NetStack::TlsSocket {
26 static constexpr std::string_view PARSE_ERROR = "first param is not type of TCPExtraOptions";
27 
TLSSetExtraOptionsContext(napi_env env,EventManager * manager)28 TLSSetExtraOptionsContext::TLSSetExtraOptionsContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
29 
ParseParams(napi_value * params,size_t paramsCount)30 void TLSSetExtraOptionsContext::ParseParams(napi_value *params, size_t paramsCount)
31 {
32     bool valid = CheckParamsType(params, paramsCount);
33     if (!valid) {
34         return;
35     }
36 
37     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_RECEIVE_BUFFER_SIZE)) {
38         options_.SetReceiveBufferSize(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_RECEIVE_BUFFER_SIZE));
39     }
40 
41     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE)) {
42         options_.SetSendBufferSize(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE));
43     }
44 
45     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_REUSE_ADDRESS)) {
46         options_.SetReuseAddress(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_REUSE_ADDRESS));
47     }
48 
49     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SOCKET_TIMEOUT)) {
50         options_.SetSocketTimeout(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SOCKET_TIMEOUT));
51     }
52 
53     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_KEEP_ALIVE)) {
54         options_.SetKeepAlive(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_KEEP_ALIVE));
55     }
56 
57     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_OOB_INLINE)) {
58         options_.SetOOBInline(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_OOB_INLINE));
59     }
60 
61     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_TCP_NO_DELAY)) {
62         options_.SetTCPNoDelay(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_TCP_NO_DELAY));
63     }
64 
65     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SOCKET_LINGER)) {
66         napi_value socketLinger = NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_SOCKET_LINGER);
67         if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object) {
68             if (NapiUtils::HasNamedProperty(GetEnv(), socketLinger, KEY_SOCKET_LINGER_ON)) {
69                 options_.socketLinger.SetOn(
70                     NapiUtils::GetBooleanProperty(GetEnv(), socketLinger, KEY_SOCKET_LINGER_ON));
71             }
72             if (NapiUtils::HasNamedProperty(GetEnv(), socketLinger, KEY_SOCKET_LINGER_LINGER)) {
73                 options_.socketLinger.SetLinger(
74                     NapiUtils::GetUint32Property(GetEnv(), socketLinger, KEY_SOCKET_LINGER_LINGER));
75             }
76         }
77     }
78 
79     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
80         SetParseOK(SetCallback(params[1]) == napi_ok);
81         return;
82     }
83     SetParseOK(true);
84 }
85 
GetSocketFd() const86 int TLSSetExtraOptionsContext::GetSocketFd() const
87 {
88     return manager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(manager_->GetData())) : -1;
89 }
90 
CheckParamsType(napi_value * params,size_t paramsCount)91 bool TLSSetExtraOptionsContext::CheckParamsType(napi_value *params, size_t paramsCount)
92 {
93     if (paramsCount == PARAM_JUST_OPTIONS) {
94         if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object) {
95             NETSTACK_LOGE("first param is not TCPExtraOptions");
96             SetNeedThrowException(true);
97             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
98             return false;
99         }
100         return true;
101     }
102 
103     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
104         if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object) {
105             NETSTACK_LOGE("first param is not TCPExtraOptions");
106             SetNeedThrowException(true);
107             SetError(PARSE_ERROR_CODE, PARSE_ERROR.data());
108             return false;
109         }
110         if (NapiUtils::GetValueType(GetEnv(), params[1]) != napi_function) {
111             NETSTACK_LOGE("SendContext second param is not function");
112             return false;
113         }
114         return true;
115     }
116     return false;
117 }
118 } // namespace OHOS::NetStack::TlsSocket
119