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