• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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,const std::shared_ptr<EventManager> & manager)27 ConnectContext::ConnectContext(napi_env env, const std::shared_ptr<EventManager> &manager)
28     : BaseContext(env, manager) {}
29 
ParseParams(napi_value * params,size_t paramsCount)30 void ConnectContext::ParseParams(napi_value *params, size_t paramsCount)
31 {
32     bool valid = CheckParamsType(params, paramsCount);
33     if (!valid) {
34         HandleCallback(params, paramsCount);
35         return;
36     }
37 
38     napi_value netAddress = NapiUtils::GetNamedProperty(GetEnv(), params[0], KEY_ADDRESS);
39 
40     std::string addr = NapiUtils::GetStringPropertyUtf8(GetEnv(), netAddress, KEY_ADDRESS);
41     if (addr.empty()) {
42         NETSTACK_LOGE("address is empty");
43     }
44 
45     if (NapiUtils::HasNamedProperty(GetEnv(), netAddress, KEY_FAMILY)) {
46         uint32_t family = NapiUtils::GetUint32Property(GetEnv(), netAddress, KEY_FAMILY);
47         options.address.SetFamilyByJsValue(family);
48     }
49     if (!IpMatchFamily(addr, options.address.GetSaFamily())) {
50         return;
51     }
52     options.address.SetRawAddress(addr);
53     if (options.address.GetAddress().empty()) {
54         if (paramsCount == PARAM_OPTIONS_AND_CALLBACK && SetCallback(params[1]) != napi_ok) {
55             NETSTACK_LOGE("failed to set callback");
56         }
57         return;
58     }
59 
60     if (NapiUtils::HasNamedProperty(GetEnv(), netAddress, KEY_PORT)) {
61         uint16_t port = static_cast<uint16_t>(NapiUtils::GetUint32Property(GetEnv(), netAddress, KEY_PORT));
62         options.address.SetPort(port);
63     }
64 
65     uint32_t timeout = NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_TIMEOUT);
66     if (timeout != 0) {
67         options.SetTimeout(timeout);
68     }
69     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_PROXY)) {
70         NETSTACK_LOGD("handle proxy options");
71         auto opts = std::make_shared<ProxyOptions>();
72         if (opts->ParseOptions(GetEnv(), params[0]) != 0) {
73             NETSTACK_LOGE("parse proxy options failed");
74             return;
75         }
76         if (opts->type_ != ProxyType::NONE) {
77             proxyOptions = opts;
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 sharedManager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(sharedManager_->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 
HandleCallback(napi_value * params,size_t paramsCount)109 void ConnectContext::HandleCallback(napi_value *params, size_t paramsCount)
110 {
111     if (paramsCount == PARAM_JUST_CALLBACK) {
112         if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
113             SetCallback(params[0]);
114         }
115         return;
116     }
117     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
118         if (NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function) {
119             SetCallback(params[1]);
120         }
121         return;
122     }
123 }
124 
GetErrorCode() const125 int32_t ConnectContext::GetErrorCode() const
126 {
127     if (BaseContext::IsPermissionDenied()) {
128         return PERMISSION_DENIED_CODE;
129     }
130 
131     auto err = BaseContext::GetErrorCode();
132     if (err == PARSE_ERROR_CODE) {
133         return PARSE_ERROR_CODE;
134     }
135 #if defined(IOS_PLATFORM)
136     err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
137 #endif
138     return err + SOCKET_ERROR_CODE_BASE;
139 }
140 
GetErrorMessage() const141 std::string ConnectContext::GetErrorMessage() const
142 {
143     if (BaseContext::IsPermissionDenied()) {
144         return PERMISSION_DENIED_MSG;
145     }
146 
147     auto errCode = BaseContext::GetErrorCode();
148     if (errCode == PARSE_ERROR_CODE) {
149         return PARSE_ERROR_MSG;
150     }
151 
152     if (errCode >= SOCKS5_ERROR_CODE) {
153         return BaseContext::GetErrorMessage();
154     }
155 #if defined(IOS_PLATFORM)
156     std::string errMessage;
157     ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
158     return errMessage;
159 #else
160     char err[MAX_ERR_NUM] = {0};
161     (void)strerror_r(errCode, err, MAX_ERR_NUM);
162     return err;
163 #endif
164 }
165 } // namespace OHOS::NetStack::Socket
166