1 /*
2 * Copyright (c) 2023 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 "multicast_get_loopback_context.h"
17
18 #include "context_key.h"
19 #include "event_manager.h"
20 #include "napi_utils.h"
21 #include "netstack_log.h"
22 #include "socket_constant.h"
23
24 namespace OHOS::NetStack::Socket {
MulticastGetLoopbackContext(napi_env env,const std::shared_ptr<EventManager> & manager)25 MulticastGetLoopbackContext::MulticastGetLoopbackContext(
26 napi_env env, const std::shared_ptr<EventManager> &manager)
27 : BaseContext(env, manager)
28 {
29 }
30
ParseParams(napi_value * params,size_t paramsCount)31 void MulticastGetLoopbackContext::ParseParams(napi_value *params, size_t paramsCount)
32 {
33 if (!CheckParamsType(params, paramsCount)) {
34 return;
35 }
36
37 if (paramsCount != PARAM_NONE) {
38 SetParseOK(SetCallback(params[0]) == napi_ok);
39 return;
40 }
41 SetParseOK(true);
42 }
43
CheckParamsType(napi_value * params,size_t paramsCount)44 bool MulticastGetLoopbackContext::CheckParamsType(napi_value *params, size_t paramsCount)
45 {
46 if (paramsCount == PARAM_NONE) {
47 return true;
48 }
49
50 if (paramsCount == PARAM_JUST_CALLBACK) {
51 if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
52 return true;
53 } else {
54 NETSTACK_LOGE("first param is not callback");
55 SetNeedThrowException(true);
56 SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
57 }
58 }
59 return false;
60 }
61
SetLoopbackMode(bool mode)62 void MulticastGetLoopbackContext::SetLoopbackMode(bool mode)
63 {
64 isLoopback_ = mode;
65 }
66
GetLoopbackMode() const67 bool MulticastGetLoopbackContext::GetLoopbackMode() const
68 {
69 return isLoopback_;
70 }
71
GetSocketFd() const72 int MulticastGetLoopbackContext::GetSocketFd() const
73 {
74 return sharedManager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(sharedManager_->GetData())) : -1;
75 }
76
GetErrorCode() const77 int32_t MulticastGetLoopbackContext::GetErrorCode() const
78 {
79 auto err = BaseContext::GetErrorCode();
80 if (err == PARSE_ERROR_CODE) {
81 return PARSE_ERROR_CODE;
82 }
83
84 if (BaseContext::IsPermissionDenied()) {
85 return PERMISSION_DENIED_CODE;
86 }
87
88 #if defined(IOS_PLATFORM)
89 err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
90 #endif
91 return err + SOCKET_ERROR_CODE_BASE;
92 }
93
GetErrorMessage() const94 std::string MulticastGetLoopbackContext::GetErrorMessage() const
95 {
96 if (BaseContext::IsPermissionDenied()) {
97 return PERMISSION_DENIED_MSG;
98 }
99
100 auto errCode = BaseContext::GetErrorCode();
101 if (errCode == PARSE_ERROR_CODE) {
102 return PARSE_ERROR_MSG;
103 }
104 #if defined(IOS_PLATFORM)
105 std::string errMessage;
106 ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
107 return errMessage;
108 #else
109 char err[MAX_ERR_NUM] = {0};
110 (void)strerror_r(errCode, err, MAX_ERR_NUM);
111 return err;
112 #endif
113 }
114 } // namespace OHOS::NetStack::Socket
115