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_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::Socket {
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         options.SetRecvBufSizeFlag(true);
49     }
50 
51     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE)) {
52         options.SetSendBufferSize(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SEND_BUFFER_SIZE));
53         options.SetSendBufSizeFlag(true);
54     }
55 
56     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_REUSE_ADDRESS)) {
57         auto reuseAddr = NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_REUSE_ADDRESS);
58         options.SetReuseAddress(reuseAddr);
59         options.SetReuseaddrFlag(true);
60         auto manager = this->GetManager();
61         if (manager != nullptr) {
62             manager->SetReuseAddr(reuseAddr);
63         }
64     }
65 
66     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_SOCKET_TIMEOUT)) {
67         options.SetSocketTimeout(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_SOCKET_TIMEOUT));
68         options.SetTimeoutFlag(true);
69     }
70 
71     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_BROADCAST)) {
72         options.SetBroadcast(NapiUtils::GetBooleanProperty(GetEnv(), params[0], KEY_BROADCAST));
73         options.SetBroadcastFlag(true);
74     }
75 
76     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
77         SetParseOK(SetCallback(params[1]) == napi_ok);
78         return;
79     }
80     SetParseOK(true);
81 }
82 
GetSocketFd() const83 int UdpSetExtraOptionsContext::GetSocketFd() const
84 {
85     return manager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(manager_->GetData())) : -1;
86 }
87 
CheckParamsType(napi_value * params,size_t paramsCount)88 bool UdpSetExtraOptionsContext::CheckParamsType(napi_value *params, size_t paramsCount)
89 {
90     if (paramsCount == PARAM_JUST_OPTIONS) {
91         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
92     }
93 
94     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
95         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
96                NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
97     }
98     return false;
99 }
100 
GetErrorCode() const101 int32_t UdpSetExtraOptionsContext::GetErrorCode() const
102 {
103     if (BaseContext::IsPermissionDenied()) {
104         return PERMISSION_DENIED_CODE;
105     }
106 
107     auto err = BaseContext::GetErrorCode();
108     if (err == PARSE_ERROR_CODE) {
109         return PARSE_ERROR_CODE;
110     }
111 #if defined(IOS_PLATFORM)
112     err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
113 #endif
114     return err + SOCKET_ERROR_CODE_BASE;
115 }
116 
GetErrorMessage() const117 std::string UdpSetExtraOptionsContext::GetErrorMessage() const
118 {
119     if (BaseContext::IsPermissionDenied()) {
120         return PERMISSION_DENIED_MSG;
121     }
122 
123     auto errCode = BaseContext::GetErrorCode();
124     if (errCode == PARSE_ERROR_CODE) {
125         return PARSE_ERROR_MSG;
126     }
127 #if defined(IOS_PLATFORM)
128     std::string errMessage;
129     ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
130     return errMessage;
131 #else
132     char err[MAX_ERR_NUM] = {0};
133     (void)strerror_r(errCode, err, MAX_ERR_NUM);
134     return err;
135 #endif
136 }
137 } // namespace OHOS::NetStack::Socket
138