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