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