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 }
32
33 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE)) {
34 options_.SetSendBufferSize(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE));
35 }
36
37 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_REUSE_ADDRESS)) {
38 options_.SetReuseAddress(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_REUSE_ADDRESS));
39 }
40
41 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SOCKET_TIMEOUT)) {
42 options_.SetSocketTimeout(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SOCKET_TIMEOUT));
43 }
44
45 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_KEEP_ALIVE)) {
46 options_.SetKeepAlive(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_KEEP_ALIVE));
47 }
48
49 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_OOB_INLINE)) {
50 options_.SetOOBInline(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_OOB_INLINE));
51 }
52
53 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_TCP_NO_DELAY)) {
54 options_.SetTCPNoDelay(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_TCP_NO_DELAY));
55 }
56
57 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SOCKET_LINGER)) {
58 napi_value socketLinger = NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_SOCKET_LINGER);
59 if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object) {
60 if (NapiUtils::HasNamedProperty(GetEnv(), socketLinger, KEY_SOCKET_LINGER_ON)) {
61 options_.socketLinger.SetOn(
62 NapiUtils::GetBooleanProperty(GetEnv(), socketLinger, KEY_SOCKET_LINGER_ON));
63 }
64 if (NapiUtils::HasNamedProperty(GetEnv(), socketLinger, KEY_SOCKET_LINGER_LINGER)) {
65 options_.socketLinger.SetLinger(
66 NapiUtils::GetUint32Property(GetEnv(), socketLinger, KEY_SOCKET_LINGER_LINGER));
67 }
68 }
69 }
70 }
71
ParseParams(napi_value * params,size_t paramsCount)72 void TcpSetExtraOptionsContext::ParseParams(napi_value *params, size_t paramsCount)
73 {
74 bool valid = CheckParamsType(params, paramsCount);
75 if (!valid) {
76 if (paramsCount == PARAM_JUST_CALLBACK) {
77 if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
78 SetCallback(params[0]);
79 }
80 return;
81 }
82 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
83 if (NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function) {
84 SetCallback(params[1]);
85 }
86 return;
87 }
88 return;
89 }
90
91 ParseContextKey(params);
92
93 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
94 SetParseOK(SetCallback(params[1]) == napi_ok);
95 return;
96 }
97 SetParseOK(true);
98 }
99
GetSocketFd() const100 int TcpSetExtraOptionsContext::GetSocketFd() const
101 {
102 return (int)(uint64_t)manager_->GetData();
103 }
104
CheckParamsType(napi_value * params,size_t paramsCount)105 bool TcpSetExtraOptionsContext::CheckParamsType(napi_value *params, size_t paramsCount)
106 {
107 if (paramsCount == PARAM_JUST_OPTIONS) {
108 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
109 }
110
111 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
112 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
113 NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
114 }
115 return false;
116 }
117
GetErrorCode() const118 int32_t TcpSetExtraOptionsContext::GetErrorCode() const
119 {
120 if (BaseContext::IsPermissionDenied()) {
121 return PERMISSION_DENIED_CODE;
122 }
123
124 auto err = BaseContext::GetErrorCode();
125 if (err == PARSE_ERROR_CODE) {
126 return PARSE_ERROR_CODE;
127 }
128 #if defined(IOS_PLATFORM)
129 err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
130 #endif
131 return err + SOCKET_ERROR_CODE_BASE;
132 }
133
GetErrorMessage() const134 std::string TcpSetExtraOptionsContext::GetErrorMessage() const
135 {
136 if (BaseContext::IsPermissionDenied()) {
137 return PERMISSION_DENIED_MSG;
138 }
139
140 auto errCode = BaseContext::GetErrorCode();
141 if (errCode == PARSE_ERROR_CODE) {
142 return PARSE_ERROR_MSG;
143 }
144 #if defined(IOS_PLATFORM)
145 std::string errMessage;
146 ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
147 return errMessage;
148 #else
149 char err[MAX_ERR_NUM] = {0};
150 (void)strerror_r(errCode, err, MAX_ERR_NUM);
151 return err;
152 #endif
153 }
154 } // namespace OHOS::NetStack::Socket
155