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