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