• 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 
17 #ifndef COMMUNICATION_NETSTACK_SOCKS5_PACKAGE_H
18 #define COMMUNICATION_NETSTACK_SOCKS5_PACKAGE_H
19 
20 
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <map>
24 #include <string>
25 #include <vector>
26 
27 #include "net_address.h"
28 #include "socks5.h"
29 
30 namespace OHOS {
31 namespace NetStack {
32 namespace Socks5 {
33 
34 const uint8_t VERSION = 0x05;
35 const int IPV4_LEN = 4;
36 const int IPV6_LEN = 16;
37 
38 class Socks5Request {
39 public:
40     Socks5Request() = default;
41     virtual ~Socks5Request() = default;
42     virtual std::string Serialize() = 0;
43 };
44 
45 class Socks5Response {
46 public:
47     Socks5Response() = default;
48     virtual ~Socks5Response() = default;
49     virtual bool Deserialize(const void *data, size_t len) = 0;
50 };
51 
52 class Socks5MethodRequest : public Socks5Request {
53 public:
54     uint8_t version_;
55     std::vector<Socks5MethodType> methods_;
56 
57     std::string Serialize();
58 };
59 
60 class Socks5MethodResponse : public Socks5Response {
61 public:
62     uint8_t version_;
63     uint8_t method_;
64 
65     bool Deserialize(const void *data, size_t len);
66 };
67 
68 class Socks5AuthRequest : public Socks5Request {
69 public:
70     uint8_t version_;
71     std::string username_;
72     std::string password_;
73 
74     std::string Serialize();
75 };
76 
77 class Socks5AuthResponse : public Socks5Response {
78 public:
79     uint8_t version_;
80     uint8_t status_;
81 
82     bool Deserialize(const void *data, size_t len);
83 };
84 
85 class Socks5ProxyRequest : public Socks5Request {
86 public:
87     uint8_t version_;
88     Socks5Command cmd_;
89     uint8_t reserved_;
90     Socks5AddrType addrType_;
91     std::string destAddr_;
92     uint16_t destPort_;
93 
94     std::string Serialize();
95 };
96 
97 class Socks5ProxyResponse : public Socks5Response {
98 public:
99     uint8_t version_;
100     uint8_t status_;
101     uint8_t reserved_;
102     Socks5AddrType addrType_;
103     std::string destAddr_;
104     uint16_t destPort_;
105 
106     bool Deserialize(const void *data, size_t len);
107 };
108 
109 class Socks5UdpHeader : public Socks5Request {
110 public:
111     uint16_t reserved_;
112     uint8_t frag_;
113     Socks5AddrType addrType_;
114     std::string destAddr_;
115     uint16_t dstPort_;
116 
117     std::string Serialize();
118     bool Deserialize(const void *data, size_t len);
119 };
120 
121 }  // Socks5
122 }  // NetStack
123 }  // OHOS
124 #endif  //COMMUNICATION_NETSTACK_SOCKS5_PACKAGE_H