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