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 "udp_send_context.h"
17
18 #include "context_key.h"
19 #include "socket_constant.h"
20 #include "net_address.h"
21 #include "event_manager.h"
22 #include "netstack_log.h"
23 #include "napi_utils.h"
24 #include "socket_exec_common.h"
25
26 namespace OHOS::NetStack::Socket {
UdpSendContext(napi_env env,EventManager * manager)27 UdpSendContext::UdpSendContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
28
ParseParams(napi_value * params,size_t paramsCount)29 void UdpSendContext::ParseParams(napi_value *params, size_t paramsCount)
30 {
31 bool valid = CheckParamsType(params, paramsCount);
32 if (!valid) {
33 if (paramsCount == PARAM_JUST_CALLBACK) {
34 if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
35 SetCallback(params[0]);
36 }
37 return;
38 }
39 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
40 if (NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function) {
41 SetCallback(params[1]);
42 }
43 return;
44 }
45 return;
46 }
47
48 napi_value netAddress = NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS);
49
50 std::string addr = NapiUtils::GetStringPropertyUtf8(GetEnv(), netAddress, KEY_ADDRESS);
51 if (NapiUtils::HasNamedProperty(GetEnv(), netAddress, KEY_FAMILY)) {
52 uint32_t family = NapiUtils::GetUint32Property(GetEnv(), netAddress, KEY_FAMILY);
53 options.address.SetFamilyByJsValue(family);
54 }
55 if (!IpMatchFamily(addr, options.address.GetSaFamily())) {
56 return;
57 }
58 options.address.SetRawAddress(addr);
59 if (options.address.GetAddress().empty()) {
60 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK && SetCallback(params[1]) != napi_ok) {
61 NETSTACK_LOGE("failed to set callback");
62 }
63 return;
64 }
65
66 if (NapiUtils::HasNamedProperty(GetEnv(), netAddress, KEY_PORT)) {
67 uint16_t port = static_cast<uint16_t>(NapiUtils::GetUint32Property(GetEnv(), netAddress, KEY_PORT));
68 options.address.SetPort(port);
69 }
70 if (!GetData(params[0])) {
71 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK && SetCallback(params[1]) != napi_ok) {
72 NETSTACK_LOGE("failed to set callback");
73 }
74 return;
75 }
76
77 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
78 SetParseOK(SetCallback(params[1]) == napi_ok);
79 return;
80 }
81 SetParseOK(true);
82 }
83
GetSocketFd() const84 int UdpSendContext::GetSocketFd() const
85 {
86 return manager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(manager_->GetData())) : -1;
87 }
88
CheckParamsType(napi_value * params,size_t paramsCount)89 bool UdpSendContext::CheckParamsType(napi_value *params, size_t paramsCount)
90 {
91 if (paramsCount == PARAM_JUST_OPTIONS) {
92 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
93 NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS)) ==
94 napi_object;
95 }
96
97 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
98 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
99 NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS)) ==
100 napi_object &&
101 NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
102 }
103 return false;
104 }
105
GetData(napi_value udpSendOptions)106 bool UdpSendContext::GetData(napi_value udpSendOptions)
107 {
108 napi_value jsData = NapiUtils::GetNamedProperty(GetEnv(), udpSendOptions, KEY_DATA);
109 if (NapiUtils::GetValueType(GetEnv(), jsData) == napi_string) {
110 std::string data = NapiUtils::GetStringFromValueUtf8(GetEnv(), jsData);
111 if (data.empty()) {
112 NETSTACK_LOGI("string data is empty");
113 return true;
114 }
115 options.SetData(data);
116 return true;
117 }
118
119 if (NapiUtils::ValueIsArrayBuffer(GetEnv(), jsData)) {
120 size_t length = 0;
121 void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length);
122 if (data == nullptr) {
123 NETSTACK_LOGI("arraybuffer data is empty");
124 return true;
125 }
126 options.SetData(data, length);
127 return true;
128 }
129 return false;
130 }
131
GetErrorCode() const132 int32_t UdpSendContext::GetErrorCode() const
133 {
134 if (BaseContext::IsPermissionDenied()) {
135 return PERMISSION_DENIED_CODE;
136 }
137
138 auto err = BaseContext::GetErrorCode();
139 if (err == PARSE_ERROR_CODE) {
140 return PARSE_ERROR_CODE;
141 }
142 #if defined(IOS_PLATFORM)
143 err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
144 #endif
145 return err + SOCKET_ERROR_CODE_BASE;
146 }
147
GetErrorMessage() const148 std::string UdpSendContext::GetErrorMessage() const
149 {
150 if (BaseContext::IsPermissionDenied()) {
151 return PERMISSION_DENIED_MSG;
152 }
153
154 auto errCode = BaseContext::GetErrorCode();
155 if (errCode == PARSE_ERROR_CODE) {
156 return PARSE_ERROR_MSG;
157 }
158 #if defined(IOS_PLATFORM)
159 std::string errMessage;
160 ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
161 return errMessage;
162 #else
163 char err[MAX_ERR_NUM] = {0};
164 (void)strerror_r(errCode, err, MAX_ERR_NUM);
165 return err;
166 #endif
167 }
168 } // namespace OHOS::NetStack::Socket
169