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