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