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_membership_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 {
MulticastMembershipContext(napi_env env,const std::shared_ptr<EventManager> & manager)25 MulticastMembershipContext::MulticastMembershipContext(napi_env env, const std::shared_ptr<EventManager> &manager)
26 : BaseContext(env, manager)
27 {
28 }
29
ParseParams(napi_value * params,size_t paramsCount)30 void MulticastMembershipContext::ParseParams(napi_value *params, size_t paramsCount)
31 {
32 if (!CheckParamsType(params, paramsCount)) {
33 return;
34 }
35
36 std::string addr = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[0], KEY_ADDRESS);
37 if (addr.empty()) {
38 NETSTACK_LOGE("invalid address, is empty");
39 }
40 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_FAMILY)) {
41 address_.SetFamilyByJsValue(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_FAMILY));
42 }
43 address_.SetIpAddress(addr);
44
45 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_PORT)) {
46 address_.SetPort(static_cast<uint16_t>(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_PORT)));
47 }
48
49 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
50 SetParseOK(SetCallback(params[PARAM_OPTIONS_AND_CALLBACK - 1]) == napi_ok);
51 return;
52 }
53 SetParseOK(true);
54 }
55
GetSocketFd() const56 int MulticastMembershipContext::GetSocketFd() const
57 {
58 return sharedManager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(sharedManager_->GetData())) : -1;
59 }
60
CheckParamsType(napi_value * params,size_t paramsCount)61 bool MulticastMembershipContext::CheckParamsType(napi_value *params, size_t paramsCount)
62 {
63 if (paramsCount == PARAM_JUST_OPTIONS) {
64 if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object) {
65 NETSTACK_LOGE("first param is not NetAddress");
66 SetNeedThrowException(true);
67 SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
68 return false;
69 }
70 } else if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
71 if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object) {
72 NETSTACK_LOGE("first param is not NetAddress");
73 SetNeedThrowException(true);
74 SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
75 return false;
76 }
77 if (NapiUtils::GetValueType(GetEnv(), params[1]) != napi_function) {
78 NETSTACK_LOGE("second param is not function");
79 return false;
80 }
81 } else {
82 NETSTACK_LOGE("invalid param count");
83 return false;
84 }
85 return true;
86 }
87
SetSocketFd(int sock)88 void MulticastMembershipContext::SetSocketFd(int sock)
89 {
90 sharedManager_->SetData(reinterpret_cast<void *>(sock));
91 }
92
GetErrorCode() const93 int32_t MulticastMembershipContext::GetErrorCode() const
94 {
95 auto err = BaseContext::GetErrorCode();
96 if (err == PARSE_ERROR_CODE) {
97 return PARSE_ERROR_CODE;
98 }
99
100 if (BaseContext::IsPermissionDenied()) {
101 return PERMISSION_DENIED_CODE;
102 }
103
104 #if defined(IOS_PLATFORM)
105 err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
106 #endif
107 return err + SOCKET_ERROR_CODE_BASE;
108 }
109
GetErrorMessage() const110 std::string MulticastMembershipContext::GetErrorMessage() const
111 {
112 if (BaseContext::IsPermissionDenied()) {
113 return PERMISSION_DENIED_MSG;
114 }
115
116 auto errCode = BaseContext::GetErrorCode();
117 if (errCode == PARSE_ERROR_CODE) {
118 return PARSE_ERROR_MSG;
119 }
120 #if defined(IOS_PLATFORM)
121 std::string errMessage;
122 ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
123 return errMessage;
124 #else
125 char err[MAX_ERR_NUM] = {0};
126 (void)strerror_r(errCode, err, MAX_ERR_NUM);
127 return err;
128 #endif
129 }
130 } // namespace OHOS::NetStack::Socket
131