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 #ifndef COMMUNICATION_NETSTACK_SOCKS5_H 17 #define COMMUNICATION_NETSTACK_SOCKS5_H 18 19 #include <sys/types.h> 20 #include <sys/socket.h> 21 #include <map> 22 #include <string> 23 #include <vector> 24 25 #include "net_address.h" 26 27 namespace OHOS { 28 namespace NetStack { 29 namespace Socks5 { 30 enum class Socks5Command : uint8_t { 31 TCP_CONNECTION = 0x01, 32 TCP_BIND = 0x02, 33 UDP_ASSOCIATE = 0x03 34 }; 35 36 enum class Socks5MethodType : uint8_t { 37 NO_AUTH = 0x00, 38 GSSAPI = 0x01, 39 PASSWORD = 0x02, 40 NO_METHODS = 0xFF 41 }; 42 43 enum class Socks5AddrType : uint8_t { 44 IPV4 = 0x01, 45 DOMAIN_NAME = 0x03, 46 IPV6 = 0x04 47 }; 48 49 enum class Socks5AuthState : uint32_t { 50 INIT = 0x00, 51 FAIL = 0x01, 52 SUCCESS = 0x02 53 }; 54 55 enum class Socks5Status : uint8_t { 56 SUCCESS = 0x00, 57 FAIL = 0x01, 58 CONNECTION_NOT_ALLOWED = 0x02, 59 NETWORK_UNREACHABLE = 0x03, 60 HOST_UNREACHABLE = 0x04, 61 CONNECTION_REFUSED_BY_HOST = 0x05, 62 TTL_EXPIRED = 0x06, 63 COMMAND_NOT_SUPPORTED = 0x07, 64 ADDRESS_TYPE_NOT_SUPPORTED = 0x08, 65 66 SOCKS5_NOT_ACTIVE = 0xA1, 67 SOCKS5_METHOD_ERROR, 68 SOCKS5_MAKE_SOCKET_ERROR, 69 70 // throw error 71 SOCKS5_OTHER_ERROR = 205, 72 SOCKS5_FAIL_TO_CONNECT_PROXY, 73 SOCKS5_USER_PASS_INVALID, 74 SOCKS5_FAIL_TO_CONNECT_REMOTE, 75 SOCKS5_METHOD_NEGO_ERROR, 76 SOCKS5_FAIL_TO_SEND_MSG, 77 SOCKS5_FAIL_TO_RECV_MSG, 78 SOCKS5_SERIALIZE_ERROR, 79 SOCKS5_DESERIALIZE_ERROR, 80 81 OTHER_STATUS 82 }; 83 84 static constexpr const char *SOCKS5_TCP_KEEP_ALIVE_THREAD_NAME = "OS_NET_SOCKS5_TCP_KEEP_ALIVE"; 85 static constexpr uint8_t SOCKS5_VERSION{5U}; 86 static constexpr uint8_t SOCKS5_SUBVERSION(1U); 87 static constexpr int32_t SOCKS5_INVALID_SOCKET_FD{-1}; 88 static const std::vector<Socks5MethodType> SOCKS5_METHODS{Socks5MethodType::NO_AUTH, Socks5MethodType::PASSWORD}; 89 static const std::map<Socks5Status, std::string> g_errStatusMap = { 90 {Socks5Status::SUCCESS, "Success"}, 91 {Socks5Status::FAIL, "Socks5 general socks server failure"}, 92 {Socks5Status::CONNECTION_NOT_ALLOWED, "Socks5 connection not allowed by ruleset"}, 93 {Socks5Status::NETWORK_UNREACHABLE, "Socks5 network unreachable"}, 94 {Socks5Status::HOST_UNREACHABLE, "Socks5 host unreachable"}, 95 {Socks5Status::CONNECTION_REFUSED_BY_HOST, "Socks5 connection refused by host"}, 96 {Socks5Status::TTL_EXPIRED, "Socks5 ttl expired"}, 97 {Socks5Status::COMMAND_NOT_SUPPORTED, "Socks5 command not supported"}, 98 {Socks5Status::ADDRESS_TYPE_NOT_SUPPORTED, "Socks5 address type not supported"}, 99 {Socks5Status::SOCKS5_NOT_ACTIVE, "Socks5 is not active"}, 100 {Socks5Status::SOCKS5_METHOD_ERROR, "Socks5 method request error"}, 101 {Socks5Status::SOCKS5_MAKE_SOCKET_ERROR, "Socks5 make tcp socket error"}, 102 {Socks5Status::SOCKS5_FAIL_TO_SEND_MSG, "Socks5 fail to send message"}, 103 {Socks5Status::SOCKS5_FAIL_TO_RECV_MSG, "Socks5 fail to recv message"}, 104 {Socks5Status::SOCKS5_SERIALIZE_ERROR, "Socks5 serialize error"}, 105 {Socks5Status::SOCKS5_DESERIALIZE_ERROR, "Socks5 deserialize error"}, 106 {Socks5Status::SOCKS5_OTHER_ERROR, "Socks5 proxy error occured"}, 107 {Socks5Status::SOCKS5_FAIL_TO_CONNECT_PROXY, "Socks5 failed to connect proxy server"}, 108 {Socks5Status::SOCKS5_USER_PASS_INVALID, "Socks5 username or password invalid"}, 109 {Socks5Status::SOCKS5_FAIL_TO_CONNECT_REMOTE, "Socks5 failed to connect remote server"}, 110 {Socks5Status::SOCKS5_METHOD_NEGO_ERROR, "Socks5 failed to negotiate auth method"}, 111 {Socks5Status::OTHER_STATUS, "Socks5 unassigned status"} 112 }; 113 114 using Socks5Buffer = std::string; 115 class Socks5ProxyAddress { 116 public: 117 Socket::NetAddress netAddress_{}; 118 sockaddr_in addrV4_{}; 119 sockaddr_in6 addrV6_{}; 120 sockaddr* addr_{nullptr}; 121 }; 122 123 class Socks5Option { 124 public: 125 Socks5ProxyAddress proxyAddress_{}; 126 std::string username_{}; 127 std::string password_{}; 128 }; 129 } // Socks5 130 } // NetStack 131 } // OHOS 132 #endif // COMMUNICATION_NETSTACK_SOCKS5_H 133