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