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 {
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 address_.SetAddress(addr);
51 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_FAMILY)) {
52 uint32_t family = NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_FAMILY);
53 address_.SetFamilyByJsValue(family);
54 }
55 if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_PORT)) {
56 uint16_t port = static_cast<uint16_t>(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_PORT));
57 address_.SetPort(port);
58 }
59
60 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
61 SetParseOK(SetCallback(params[1]) == napi_ok);
62 return;
63 }
64 SetParseOK(true);
65 }
66
GetSocketFd() const67 int BindContext::GetSocketFd() const
68 {
69 return (int)(uint64_t)manager_->GetData();
70 }
71
CheckParamsType(napi_value * params,size_t paramsCount)72 bool BindContext::CheckParamsType(napi_value *params, size_t paramsCount)
73 {
74 if (paramsCount == PARAM_JUST_OPTIONS) {
75 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
76 }
77
78 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
79 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
80 NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
81 }
82 return false;
83 }
84
GetErrorCode() const85 int32_t BindContext::GetErrorCode() const
86 {
87 if (BaseContext::IsPermissionDenied()) {
88 return PERMISSION_DENIED_CODE;
89 }
90
91 auto err = BaseContext::GetErrorCode();
92 if (err == PARSE_ERROR_CODE) {
93 return PARSE_ERROR_CODE;
94 }
95 return err + SOCKET_ERROR_CODE_BASE;
96 }
97
GetErrorMessage() const98 std::string BindContext::GetErrorMessage() const
99 {
100 if (BaseContext::IsPermissionDenied()) {
101 return PERMISSION_DENIED_MSG;
102 }
103
104 auto errCode = BaseContext::GetErrorCode();
105 if (errCode == PARSE_ERROR_CODE) {
106 return PARSE_ERROR_MSG;
107 }
108 char err[MAX_ERR_NUM] = {0};
109 (void)strerror_r(errCode, err, MAX_ERR_NUM);
110 return err;
111 }
112 } // namespace OHOS::NetStack
113