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