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_set_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 {
MulticastSetLoopbackContext(napi_env env,EventManager * manager)25 MulticastSetLoopbackContext::MulticastSetLoopbackContext(napi_env env, EventManager *manager)
26 : BaseContext(env, manager)
27 {
28 }
29
ParseParams(napi_value * params,size_t paramsCount)30 void MulticastSetLoopbackContext::ParseParams(napi_value *params, size_t paramsCount)
31 {
32 if (!CheckParamsType(params, paramsCount)) {
33 return;
34 }
35
36 isLoopback_ = NapiUtils::GetBooleanFromValue(GetEnv(), params[0]);
37
38 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
39 SetParseOK(SetCallback(params[1]) == napi_ok);
40 return;
41 }
42 SetParseOK(true);
43 }
44
CheckParamsType(napi_value * params,size_t paramsCount)45 bool MulticastSetLoopbackContext::CheckParamsType(napi_value *params, size_t paramsCount)
46 {
47 if (paramsCount == PARAM_JUST_OPTIONS) {
48 if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_boolean) {
49 NETSTACK_LOGE("first param is not loopback");
50 SetNeedThrowException(true);
51 SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
52 return false;
53 }
54 } else if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
55 if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_boolean) {
56 NETSTACK_LOGE("first param is not loopback");
57 SetNeedThrowException(true);
58 SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
59 return false;
60 }
61 if (NapiUtils::GetValueType(GetEnv(), params[1]) != napi_function) {
62 NETSTACK_LOGE("second param is not function");
63 return false;
64 }
65 } else {
66 NETSTACK_LOGE("invalid param count");
67 return false;
68 }
69 return true;
70 }
71
SetLoopbackMode(bool mode)72 void MulticastSetLoopbackContext::SetLoopbackMode(bool mode)
73 {
74 isLoopback_ = mode;
75 }
76
GetLoopbackMode() const77 bool MulticastSetLoopbackContext::GetLoopbackMode() const
78 {
79 return isLoopback_;
80 }
81
GetSocketFd() const82 int MulticastSetLoopbackContext::GetSocketFd() const
83 {
84 return manager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(manager_->GetData())) : -1;
85 }
86
GetErrorCode() const87 int32_t MulticastSetLoopbackContext::GetErrorCode() const
88 {
89 auto err = BaseContext::GetErrorCode();
90 if (err == PARSE_ERROR_CODE) {
91 return PARSE_ERROR_CODE;
92 }
93
94 if (BaseContext::IsPermissionDenied()) {
95 return PERMISSION_DENIED_CODE;
96 }
97
98 #if defined(IOS_PLATFORM)
99 err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
100 #endif
101 return err + SOCKET_ERROR_CODE_BASE;
102 }
103
GetErrorMessage() const104 std::string MulticastSetLoopbackContext::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 #if defined(IOS_PLATFORM)
115 std::string errMessage;
116 ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
117 return errMessage;
118 #else
119 char err[MAX_ERR_NUM] = {0};
120 (void)strerror_r(errCode, err, MAX_ERR_NUM);
121 return err;
122 #endif
123 }
124 } // namespace OHOS::NetStack::Socket
125