1 /*
2  * Copyright (c) 2021-2024 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_send_context.h"
17 
18 #include "context_key.h"
19 #include "socket_constant.h"
20 #include "event_manager.h"
21 #include "netstack_log.h"
22 #include "napi_utils.h"
23 
24 namespace OHOS::NetStack::Socket {
TcpSendContext(napi_env env,EventManager * manager)25 TcpSendContext::TcpSendContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
26 
ParseParams(napi_value * params,size_t paramsCount)27 void TcpSendContext::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_ENCODING)) {
47         std::string encoding = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[0], KEY_ENCODING);
48         options.SetEncoding(encoding);
49     }
50     if (!GetData(params[0])) {
51         if (paramsCount == PARAM_OPTIONS_AND_CALLBACK && SetCallback(params[1]) != napi_ok) {
52             NETSTACK_LOGE("failed to set callback");
53         }
54         return;
55     }
56 
57     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
58         SetParseOK(SetCallback(params[1]) == napi_ok);
59         return;
60     }
61     SetParseOK(true);
62 }
63 
GetSocketFd() const64 int TcpSendContext::GetSocketFd() const
65 {
66     return manager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(manager_->GetData())) : -1;
67 }
68 
CheckParamsType(napi_value * params,size_t paramsCount)69 bool TcpSendContext::CheckParamsType(napi_value *params, size_t paramsCount)
70 {
71     if (paramsCount == PARAM_JUST_OPTIONS) {
72         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
73     }
74 
75     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
76         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
77                NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
78     }
79     return false;
80 }
81 
GetData(napi_value udpSendOptions)82 bool TcpSendContext::GetData(napi_value udpSendOptions)
83 {
84     napi_value jsData = NapiUtils::GetNamedProperty(GetEnv(), udpSendOptions, KEY_DATA);
85     if (NapiUtils::GetValueType(GetEnv(), jsData) == napi_string) {
86         std::string data = NapiUtils::GetStringFromValueUtf8(GetEnv(), jsData);
87         if (data.empty()) {
88             NETSTACK_LOGE("string data is empty");
89             return true;
90         }
91         options.SetData(data);
92         return true;
93     }
94 
95     if (NapiUtils::ValueIsArrayBuffer(GetEnv(), jsData)) {
96         size_t length = 0;
97         void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length);
98         if (data == nullptr) {
99             NETSTACK_LOGE("arraybuffer data is empty");
100             return true;
101         }
102         options.SetData(data, length);
103         return true;
104     }
105     return false;
106 }
107 
GetErrorCode() const108 int32_t TcpSendContext::GetErrorCode() const
109 {
110     if (BaseContext::IsPermissionDenied()) {
111         return PERMISSION_DENIED_CODE;
112     }
113 
114     auto err = BaseContext::GetErrorCode();
115     if (err == PARSE_ERROR_CODE) {
116         return PARSE_ERROR_CODE;
117     }
118 #if defined(IOS_PLATFORM)
119     err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
120 #endif
121     return err + SOCKET_ERROR_CODE_BASE;
122 }
123 
GetErrorMessage() const124 std::string TcpSendContext::GetErrorMessage() const
125 {
126     if (BaseContext::IsPermissionDenied()) {
127         return PERMISSION_DENIED_MSG;
128     }
129 
130     auto errCode = BaseContext::GetErrorCode();
131     if (errCode == PARSE_ERROR_CODE) {
132         return PARSE_ERROR_MSG;
133     }
134 #if defined(IOS_PLATFORM)
135     std::string errMessage;
136     ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
137     return errMessage;
138 #else
139     char err[MAX_ERR_NUM] = {0};
140     (void)strerror_r(errCode, err, MAX_ERR_NUM);
141     return err;
142 #endif
143 }
144 } // namespace OHOS::NetStack::Socket
145