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