1 /*
2 * Copyright (c) 2021-2022 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 "bind_context.h"
17
18 #include "context_key.h"
19 #include "socket_constant.h"
20 #include "event_manager.h"
21 #include "netstack_log.h"
22 #include "napi_utils.h"
23
24 namespace OHOS::NetStack::Socket {
BindContext(napi_env env,EventManager * manager)25 BindContext::BindContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
26
ParseParams(napi_value * params,size_t paramsCount)27 void BindContext::ParseParams(napi_value *params, size_t paramsCount)
28 {
29 bool valid = CheckParamsType(params, paramsCount);
30 if (!valid) {
31 if (paramsCount == PARAM_JUST_CALLBACK) {
32 if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
33 SetCallback(params[0]);
34 }
35 return;
36 }
37 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
38 if (NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function) {
39 SetCallback(params[1]);
40 }
41 return;
42 }
43 return;
44 }
45
46 std::string addr = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[0], KEY_ADDRESS);
47 if (addr.empty()) {
48 NETSTACK_LOGE("address is empty");
49 }
50 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_FAMILY)) {
51 uint32_t family = NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_FAMILY);
52 address_.SetFamilyByJsValue(family);
53 }
54 address_.SetAddress(addr);
55 if (address_.GetAddress().empty()) {
56 return;
57 }
58
59 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_PORT)) {
60 uint16_t port = static_cast<uint16_t>(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_PORT));
61 address_.SetPort(port);
62 }
63
64 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
65 SetParseOK(SetCallback(params[1]) == napi_ok);
66 return;
67 }
68 SetParseOK(true);
69 }
70
GetSocketFd() const71 int BindContext::GetSocketFd() const
72 {
73 return (int)(uint64_t)manager_->GetData();
74 }
75
CheckParamsType(napi_value * params,size_t paramsCount)76 bool BindContext::CheckParamsType(napi_value *params, size_t paramsCount)
77 {
78 if (paramsCount == PARAM_JUST_OPTIONS) {
79 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
80 }
81
82 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
83 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
84 NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
85 }
86 return false;
87 }
88
GetErrorCode() const89 int32_t BindContext::GetErrorCode() const
90 {
91 if (BaseContext::IsPermissionDenied()) {
92 return PERMISSION_DENIED_CODE;
93 }
94
95 auto err = BaseContext::GetErrorCode();
96 if (err == PARSE_ERROR_CODE) {
97 return PARSE_ERROR_CODE;
98 }
99 #if defined(IOS_PLATFORM)
100 err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
101 #endif
102 return err + SOCKET_ERROR_CODE_BASE;
103 }
104
GetErrorMessage() const105 std::string BindContext::GetErrorMessage() const
106 {
107 if (BaseContext::IsPermissionDenied()) {
108 return PERMISSION_DENIED_MSG;
109 }
110
111 auto errCode = BaseContext::GetErrorCode();
112 if (errCode == PARSE_ERROR_CODE) {
113 return PARSE_ERROR_MSG;
114 }
115 #if defined(IOS_PLATFORM)
116 std::string errMessage;
117 ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
118 return errMessage;
119 #else
120 char err[MAX_ERR_NUM] = {0};
121 (void)strerror_r(errCode, err, MAX_ERR_NUM);
122 return err;
123 #endif
124 }
125 } // namespace OHOS::NetStack::Socket
126