• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "connect_context.h"
20 #include "socket_constant.h"
21 #include "net_address.h"
22 #include "event_manager.h"
23 #include "netstack_log.h"
24 #include "napi_utils.h"
25 #include "socket_exec_common.h"
26 
27 namespace OHOS::NetStack::Socket {
UdpSendContext(napi_env env,const std::shared_ptr<EventManager> & manager)28 UdpSendContext::UdpSendContext(napi_env env, const std::shared_ptr<EventManager> &manager)
29     : BaseContext(env, manager) {}
30 
ParseParams(napi_value * params,size_t paramsCount)31 void UdpSendContext::ParseParams(napi_value *params, size_t paramsCount)
32 {
33     bool valid = CheckParamsType(params, paramsCount);
34     if (!valid) {
35         HandleCallback(params, paramsCount);
36         return;
37     }
38 
39     napi_value netAddress = NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS);
40 
41     std::string addr = NapiUtils::GetStringPropertyUtf8(GetEnv(), netAddress, KEY_ADDRESS);
42     if (NapiUtils::HasNamedProperty(GetEnv(), netAddress, KEY_FAMILY)) {
43         uint32_t family = NapiUtils::GetUint32Property(GetEnv(), netAddress, KEY_FAMILY);
44         options.address.SetFamilyByJsValue(family);
45     }
46     if (!IpMatchFamily(addr, options.address.GetSaFamily())) {
47         return;
48     }
49     options.address.SetRawAddress(addr);
50     if (options.address.GetAddress().empty()) {
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 (NapiUtils::HasNamedProperty(GetEnv(), netAddress, KEY_PORT)) {
58         uint16_t port = static_cast<uint16_t>(NapiUtils::GetUint32Property(GetEnv(), netAddress, KEY_PORT));
59         options.address.SetPort(port);
60     }
61     if (!GetData(params[0])) {
62         if (paramsCount == PARAM_OPTIONS_AND_CALLBACK && SetCallback(params[1]) != napi_ok) {
63             NETSTACK_LOGE("failed to set callback");
64         }
65         return;
66     }
67     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_PROXY)) {
68         NETSTACK_LOGD("handle proxy options");
69         auto opts = std::make_shared<ProxyOptions>();
70         if (opts->ParseOptions(GetEnv(), params[0]) != 0) {
71             NETSTACK_LOGE("parse proxy options failed");
72             return;
73         }
74         if (opts->type_ != ProxyType::NONE) {
75             proxyOptions = opts;
76         }
77     }
78     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
79         SetParseOK(SetCallback(params[1]) == napi_ok);
80         return;
81     }
82     SetParseOK(true);
83 }
84 
GetSocketFd() const85 int UdpSendContext::GetSocketFd() const
86 {
87     return sharedManager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(sharedManager_->GetData())) : -1;
88 }
89 
CheckParamsType(napi_value * params,size_t paramsCount)90 bool UdpSendContext::CheckParamsType(napi_value *params, size_t paramsCount)
91 {
92     if (paramsCount == PARAM_JUST_OPTIONS) {
93         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
94                NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS)) ==
95                    napi_object;
96     }
97 
98     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
99         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
100                NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS)) ==
101                    napi_object &&
102                NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
103     }
104     return false;
105 }
106 
GetData(napi_value udpSendOptions)107 bool UdpSendContext::GetData(napi_value udpSendOptions)
108 {
109     napi_value jsData = NapiUtils::GetNamedProperty(GetEnv(), udpSendOptions, KEY_DATA);
110     if (NapiUtils::GetValueType(GetEnv(), jsData) == napi_string) {
111         std::string data = NapiUtils::GetStringFromValueUtf8(GetEnv(), jsData);
112         if (data.empty()) {
113             NETSTACK_LOGI("string data is empty");
114             return true;
115         }
116         options.SetData(data);
117         return true;
118     }
119 
120     if (NapiUtils::ValueIsArrayBuffer(GetEnv(), jsData)) {
121         size_t length = 0;
122         void *data = NapiUtils::GetInfoFromArrayBufferValue(GetEnv(), jsData, &length);
123         if (data == nullptr) {
124             NETSTACK_LOGI("arraybuffer data is empty");
125             return true;
126         }
127         options.SetData(data, length);
128         return true;
129     }
130     return false;
131 }
132 
HandleCallback(napi_value * params,size_t paramsCount)133 void UdpSendContext::HandleCallback(napi_value *params, size_t paramsCount)
134 {
135     if (paramsCount == PARAM_JUST_CALLBACK) {
136         if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
137             SetCallback(params[0]);
138         }
139         return;
140     }
141     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
142         if (NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function) {
143             SetCallback(params[1]);
144         }
145         return;
146     }
147 }
148 
GetErrorCode() const149 int32_t UdpSendContext::GetErrorCode() const
150 {
151     if (BaseContext::IsPermissionDenied()) {
152         return PERMISSION_DENIED_CODE;
153     }
154 
155     auto err = BaseContext::GetErrorCode();
156     if (err == PARSE_ERROR_CODE) {
157         return PARSE_ERROR_CODE;
158     }
159 #if defined(IOS_PLATFORM)
160     err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
161 #endif
162     return err + SOCKET_ERROR_CODE_BASE;
163 }
164 
GetErrorMessage() const165 std::string UdpSendContext::GetErrorMessage() const
166 {
167     if (BaseContext::IsPermissionDenied()) {
168         return PERMISSION_DENIED_MSG;
169     }
170 
171     auto errCode = BaseContext::GetErrorCode();
172     if (errCode == PARSE_ERROR_CODE) {
173         return PARSE_ERROR_MSG;
174     }
175 
176     if (errCode >= SOCKS5_ERROR_CODE) {
177         return BaseContext::GetErrorMessage();
178     }
179 #if defined(IOS_PLATFORM)
180     std::string errMessage;
181     ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
182     return errMessage;
183 #else
184     char err[MAX_ERR_NUM] = {0};
185     (void)strerror_r(errCode, err, MAX_ERR_NUM);
186     return err;
187 #endif
188 }
189 } // namespace OHOS::NetStack::Socket
190