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