1 /*
2 * Copyright (c) 2021-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 "tcp_extra_context.h"
17
18 #include "context_key.h"
19 #include "socket_constant.h"
20 #include "event_manager.h"
21 #include "napi_utils.h"
22 #include "netstack_log.h"
23
24 namespace OHOS::NetStack::Socket {
TcpSetExtraOptionsContext(napi_env env,EventManager * manager)25 TcpSetExtraOptionsContext::TcpSetExtraOptionsContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
26
ParseContextKey(napi_value * params)27 void TcpSetExtraOptionsContext::ParseContextKey(napi_value *params)
28 {
29 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_RECEIVE_BUFFER_SIZE)) {
30 options_.SetReceiveBufferSize(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_RECEIVE_BUFFER_SIZE));
31 options_.SetRecvBufSizeFlag(true);
32 }
33
34 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE)) {
35 options_.SetSendBufferSize(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE));
36 options_.SetSendBufSizeFlag(true);
37 }
38
39 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_REUSE_ADDRESS)) {
40 options_.SetReuseAddress(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_REUSE_ADDRESS));
41 options_.SetReuseaddrFlag(true);
42 }
43
44 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SOCKET_TIMEOUT)) {
45 options_.SetSocketTimeout(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SOCKET_TIMEOUT));
46 options_.SetTimeoutFlag(true);
47 }
48
49 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_KEEP_ALIVE)) {
50 options_.SetKeepAlive(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_KEEP_ALIVE));
51 options_.SetKeepAliveFlag(true);
52 }
53
54 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_OOB_INLINE)) {
55 options_.SetOOBInline(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_OOB_INLINE));
56 options_.SetOobInlineFlag(true);
57 }
58
59 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_TCP_NO_DELAY)) {
60 options_.SetTCPNoDelay(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_TCP_NO_DELAY));
61 options_.SetTcpNoDelayFlag(true);
62 }
63
64 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SOCKET_LINGER)) {
65 napi_value socketLinger = NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_SOCKET_LINGER);
66 if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object) {
67 if (NapiUtils::HasNamedProperty(GetEnv(), socketLinger, KEY_SOCKET_LINGER_ON)) {
68 options_.socketLinger.SetOn(
69 NapiUtils::GetBooleanProperty(GetEnv(), socketLinger, KEY_SOCKET_LINGER_ON));
70 }
71 if (NapiUtils::HasNamedProperty(GetEnv(), socketLinger, KEY_SOCKET_LINGER_LINGER)) {
72 options_.socketLinger.SetLinger(
73 NapiUtils::GetUint32Property(GetEnv(), socketLinger, KEY_SOCKET_LINGER_LINGER));
74 }
75 }
76 options_.SetLingerFlag(true);
77 }
78 }
79
ParseParams(napi_value * params,size_t paramsCount)80 void TcpSetExtraOptionsContext::ParseParams(napi_value *params, size_t paramsCount)
81 {
82 bool valid = CheckParamsType(params, paramsCount);
83 if (!valid) {
84 if (paramsCount == PARAM_JUST_CALLBACK) {
85 if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
86 SetCallback(params[0]);
87 }
88 return;
89 }
90 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
91 if (NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function) {
92 SetCallback(params[1]);
93 }
94 return;
95 }
96 return;
97 }
98
99 ParseContextKey(params);
100
101 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
102 SetParseOK(SetCallback(params[1]) == napi_ok);
103 return;
104 }
105 SetParseOK(true);
106 }
107
GetSocketFd() const108 int TcpSetExtraOptionsContext::GetSocketFd() const
109 {
110 return manager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(manager_->GetData())) : -1;
111 }
112
CheckParamsType(napi_value * params,size_t paramsCount)113 bool TcpSetExtraOptionsContext::CheckParamsType(napi_value *params, size_t paramsCount)
114 {
115 if (paramsCount == PARAM_JUST_OPTIONS) {
116 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
117 }
118
119 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
120 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
121 NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
122 }
123 return false;
124 }
125
GetErrorCode() const126 int32_t TcpSetExtraOptionsContext::GetErrorCode() const
127 {
128 if (BaseContext::IsPermissionDenied()) {
129 return PERMISSION_DENIED_CODE;
130 }
131
132 auto err = BaseContext::GetErrorCode();
133 if (err == PARSE_ERROR_CODE) {
134 return PARSE_ERROR_CODE;
135 }
136 #if defined(IOS_PLATFORM)
137 err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
138 #endif
139 return err + SOCKET_ERROR_CODE_BASE;
140 }
141
GetErrorMessage() const142 std::string TcpSetExtraOptionsContext::GetErrorMessage() const
143 {
144 if (BaseContext::IsPermissionDenied()) {
145 return PERMISSION_DENIED_MSG;
146 }
147
148 auto errCode = BaseContext::GetErrorCode();
149 if (errCode == PARSE_ERROR_CODE) {
150 return PARSE_ERROR_MSG;
151 }
152 #if defined(IOS_PLATFORM)
153 std::string errMessage;
154 ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
155 return errMessage;
156 #else
157 char err[MAX_ERR_NUM] = {0};
158 (void)strerror_r(errCode, err, MAX_ERR_NUM);
159 return err;
160 #endif
161 }
162 } // namespace OHOS::NetStack::Socket
163