• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "socks5_passwd_method.h"
17 
18 #include "netstack_log.h"
19 #include "socks5_utils.h"
20 
21 namespace OHOS {
22 namespace NetStack {
23 namespace Socks5 {
RequestAuth(std::int32_t socketId,const std::string & username,const std::string & password,const Socks5ProxyAddress & proxy)24 bool Socks5PasswdMethod::RequestAuth(std::int32_t socketId, const std::string &username,
25     const std::string &password, const Socks5ProxyAddress &proxy)
26 {
27     if (username.empty() || password.empty()) {
28         GetSocks5Instance()->UpdateErrorInfo(Socks5Status::SOCKS5_USER_PASS_INVALID);
29         NETSTACK_LOGE("socks5 username or password is empty, socket is %{public}d", socketId);
30         return false;
31     }
32 
33     Socks5AuthRequest request{};
34     request.version_ = SOCKS5_SUBVERSION;
35     request.username_ = username;
36     request.password_ = password;
37 
38     const socklen_t addrLen{Socks5Utils::GetAddressLen(proxy.netAddress_)};
39     const std::pair<sockaddr *, socklen_t> addrInfo{proxy.addr_, addrLen};
40     Socks5AuthResponse response{};
41     if (!Socks5Utils::RequestProxyServer(GetSocks5Instance(), socketId, addrInfo, &request, &response)) {
42         NETSTACK_LOGE("RequestProxy failed, socket is %{public}d", socketId);
43         return false;
44     }
45     if (response.status_ != static_cast<uint8_t>(Socks5Status::SUCCESS)) {
46         GetSocks5Instance()->UpdateErrorInfo(Socks5Status::SOCKS5_USER_PASS_INVALID);
47         NETSTACK_LOGE("socks5 fail to request auth, socket is %{public}d", socketId);
48         return false;
49     }
50     return true;
51 }
52 
53 } // Socks5
54 } // NetStack
55 } // OHOS
56