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 {
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 options.address.SetAddress(addr);
54 if (NapiUtils::HasNamedProperty(GetEnv(), netAddress, KEY_FAMILY)) {
55 uint32_t family = NapiUtils::GetUint32Property(GetEnv(), netAddress, KEY_FAMILY);
56 options.address.SetFamilyByJsValue(family);
57 }
58 if (NapiUtils::HasNamedProperty(GetEnv(), netAddress, KEY_PORT)) {
59 uint16_t port = static_cast<uint16_t>(NapiUtils::GetUint32Property(GetEnv(), netAddress, KEY_PORT));
60 options.address.SetPort(port);
61 }
62 if (!GetData(params[0])) {
63 return;
64 }
65
66 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
67 SetParseOK(SetCallback(params[1]) == napi_ok);
68 return;
69 }
70 SetParseOK(true);
71 }
72
GetSocketFd() const73 int UdpSendContext::GetSocketFd() const
74 {
75 return (int)(uint64_t)manager_->GetData();
76 }
77
CheckParamsType(napi_value * params,size_t paramsCount)78 bool UdpSendContext::CheckParamsType(napi_value *params, size_t paramsCount)
79 {
80 if (paramsCount == PARAM_JUST_OPTIONS) {
81 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
82 NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS)) ==
83 napi_object;
84 }
85
86 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
87 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
88 NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS)) ==
89 napi_object &&
90 NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
91 }
92 return false;
93 }
94
GetData(napi_value udpSendOptions)95 bool UdpSendContext::GetData(napi_value udpSendOptions)
96 {
97 napi_value jsData = NapiUtils::GetNamedProperty(GetEnv(), udpSendOptions, KEY_DATA);
98 if (NapiUtils::GetValueType(GetEnv(), jsData) == napi_string) {
99 std::string data = NapiUtils::GetStringFromValueUtf8(GetEnv(), jsData);
100 if (data.empty()) {
101 NETSTACK_LOGE("string data is empty");
102 return false;
103 }
104 options.SetData(data);
105 return true;
106 }
107
108 if (NapiUtils::ValueIsArrayBuffer(GetEnv(), jsData)) {
109 size_t length = 0;
110 void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length);
111 if (data == nullptr) {
112 NETSTACK_LOGE("arraybuffer data is empty");
113 return false;
114 }
115 options.SetData(data, length);
116 return true;
117 }
118 return false;
119 }
120
GetErrorCode() const121 int32_t UdpSendContext::GetErrorCode() const
122 {
123 if (BaseContext::IsPermissionDenied()) {
124 return PERMISSION_DENIED_CODE;
125 }
126
127 auto err = BaseContext::GetErrorCode();
128 if (err == PARSE_ERROR_CODE) {
129 return PARSE_ERROR_CODE;
130 }
131 return err + SOCKET_ERROR_CODE_BASE;
132 }
133
GetErrorMessage() const134 std::string UdpSendContext::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 char err[MAX_ERR_NUM] = {0};
145 (void)strerror_r(errCode, err, MAX_ERR_NUM);
146 return err;
147 }
148 } // namespace OHOS::NetStack
149