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_extra_context.h"
17
18 #include "context_key.h"
19 #include "socket_constant.h"
20 #include "event_manager.h"
21 #include "napi_utils.h"
22 #include "netstack_log.h"
23
24 namespace OHOS::NetStack {
UdpSetExtraOptionsContext(napi_env env,EventManager * manager)25 UdpSetExtraOptionsContext::UdpSetExtraOptionsContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
26
ParseParams(napi_value * params,size_t paramsCount)27 void UdpSetExtraOptionsContext::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_RECEIVE_BUFFER_SIZE)) {
47 options.SetReceiveBufferSize(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_RECEIVE_BUFFER_SIZE));
48 }
49
50 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE)) {
51 options.SetSendBufferSize(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE));
52 }
53
54 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_REUSE_ADDRESS)) {
55 options.SetReuseAddress(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_REUSE_ADDRESS));
56 }
57
58 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SOCKET_TIMEOUT)) {
59 options.SetSocketTimeout(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SOCKET_TIMEOUT));
60 }
61
62 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_BROADCAST)) {
63 options.SetBroadcast(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_BROADCAST));
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 UdpSetExtraOptionsContext::GetSocketFd() const
74 {
75 return (int)(uint64_t)manager_->GetData();
76 }
77
CheckParamsType(napi_value * params,size_t paramsCount)78 bool UdpSetExtraOptionsContext::CheckParamsType(napi_value *params, size_t paramsCount)
79 {
80 if (paramsCount == PARAM_JUST_OPTIONS) {
81 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
82 }
83
84 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
85 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
86 NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
87 }
88 return false;
89 }
90
GetErrorCode() const91 int32_t UdpSetExtraOptionsContext::GetErrorCode() const
92 {
93 if (BaseContext::IsPermissionDenied()) {
94 return PERMISSION_DENIED_CODE;
95 }
96
97 auto err = BaseContext::GetErrorCode();
98 if (err == PARSE_ERROR_CODE) {
99 return PARSE_ERROR_CODE;
100 }
101 return err + SOCKET_ERROR_CODE_BASE;
102 }
103
GetErrorMessage() const104 std::string UdpSetExtraOptionsContext::GetErrorMessage() const
105 {
106 if (BaseContext::IsPermissionDenied()) {
107 return PERMISSION_DENIED_MSG;
108 }
109
110 auto errCode = BaseContext::GetErrorCode();
111 if (errCode == PARSE_ERROR_CODE) {
112 return PARSE_ERROR_MSG;
113 }
114 char err[MAX_ERR_NUM] = {0};
115 (void)strerror_r(errCode, err, MAX_ERR_NUM);
116 return err;
117 }
118 } // namespace OHOS::NetStack
119