src/examples/cpp03/socks4/socks4.hpp | src/examples/cpp11/socks4/socks4.hpp |
⋮ | ⋮ |
1 | // | 1 | // |
2 | //·socks4.hpp | 2 | //·socks4.hpp |
3 | //·~~~~~~~~~~ | 3 | //·~~~~~~~~~~ |
4 | // | 4 | // |
5 | //·Copyright·(c)·2003-2022·Christopher·M.·Kohlhoff·(chris·at·kohlhoff·dot·com) | 5 | //·Copyright·(c)·2003-2022·Christopher·M.·Kohlhoff·(chris·at·kohlhoff·dot·com) |
6 | // | 6 | // |
7 | //·Distributed·under·the·Boost·Software·License,·Version·1.0.·(See·accompanying | 7 | //·Distributed·under·the·Boost·Software·License,·Version·1.0.·(See·accompanying |
8 | //·file·LICENSE_1_0.txt·or·copy·at·http://www.boost.org/LICENSE_1_0.txt) | 8 | //·file·LICENSE_1_0.txt·or·copy·at·http://www.boost.org/LICENSE_1_0.txt) |
9 | // | 9 | // |
10 | | 10 | |
11 | #ifndef·SOCKS4_HPP | 11 | #ifndef·SOCKS4_HPP |
12 | #define·SOCKS4_HPP | 12 | #define·SOCKS4_HPP |
13 | | 13 | |
| 14 | #include·<array> |
14 | #include·<string> | 15 | #include·<string> |
15 | #include·<asio.hpp> | 16 | #include·<asio/buffer.hpp> |
16 | #include·<boost/array.hpp> | 17 | #include·<asio/ip/tcp.hpp> |
17 | | 18 | |
18 | namespace·socks4·{ | 19 | namespace·socks4·{ |
19 | | 20 | |
20 | const·unsigned·char·version·=·0x04; | 21 | const·unsigned·char·version·=·0x04; |
21 | | 22 | |
22 | class·request | 23 | class·request |
23 | { | 24 | { |
24 | public: | 25 | public: |
25 | ··enum·command_type | 26 | ··enum·command_type |
26 | ··{ | 27 | ··{ |
27 | ····connect·=·0x01, | 28 | ····connect·=·0x01, |
28 | ····bind·=·0x02 | 29 | ····bind·=·0x02 |
29 | ··}; | 30 | ··}; |
30 | | 31 | |
31 | ··request(command_type·cmd,·const·asio::ip::tcp::endpoint&·endpoint, | 32 | ··request(command_type·cmd,·const·asio::ip::tcp::endpoint&·endpoint, |
32 | ······const·std::string&·user_id) | 33 | ······const·std::string&·user_id) |
33 | ····:·version_(version), | 34 | ····:·version_(version), |
34 | ······command_(cmd), | 35 | ······command_(cmd), |
35 | ······user_id_(user_id), | 36 | ······user_id_(user_id), |
36 | ······null_byte_(0) | 37 | ······null_byte_(0) |
37 | ··{ | 38 | ··{ |
38 | ····//·Only·IPv4·is·supported·by·the·SOCKS·4·protocol. | 39 | ····//·Only·IPv4·is·supported·by·the·SOCKS·4·protocol. |
39 | ····if·(endpoint.protocol()·!=·asio::ip::tcp::v4()) | 40 | ····if·(endpoint.protocol()·!=·asio::ip::tcp::v4()) |
40 | ····{ | 41 | ····{ |
41 | ······throw·asio::system_error( | 42 | ······throw·asio::system_error( |
42 | ··········asio::error::address_family_not_supported); | 43 | ··········asio::error::address_family_not_supported); |
43 | ····} | 44 | ····} |
44 | | 45 | |
45 | ····//·Convert·port·number·to·network·byte·order. | 46 | ····//·Convert·port·number·to·network·byte·order. |
46 | ····unsigned·short·port·=·endpoint.port(); | 47 | ····unsigned·short·port·=·endpoint.port(); |
47 | ····port_high_byte_·=·(port·>>·8)·&·0xff; | 48 | ····port_high_byte_·=·(port·>>·8)·&·0xff; |
48 | ····port_low_byte_·=·port·&·0xff; | 49 | ····port_low_byte_·=·port·&·0xff; |
49 | | 50 | |
50 | ····//·Save·IP·address·in·network·byte·order. | 51 | ····//·Save·IP·address·in·network·byte·order. |
51 | ····address_·=·endpoint.address().to_v4().to_bytes(); | 52 | ····address_·=·endpoint.address().to_v4().to_bytes(); |
52 | ··} | 53 | ··} |
53 | | 54 | |
54 | ··boost::array<asio::const_buffer,·7>·buffers()·const | 55 | ··std::array<asio::const_buffer,·7>·buffers()·const |
55 | ··{ | 56 | ··{ |
56 | ····boost::array<asio::const_buffer,·7>·bufs·= | 57 | ····return |
57 | ····{ | 58 | ····{ |
58 | ······{ | 59 | ······{ |
59 | ········asio::buffer(&version_,·1), | 60 | ········asio::buffer(&version_,·1), |
60 | ········asio::buffer(&command_,·1), | 61 | ········asio::buffer(&command_,·1), |
61 | ········asio::buffer(&port_high_byte_,·1), | 62 | ········asio::buffer(&port_high_byte_,·1), |
62 | ········asio::buffer(&port_low_byte_,·1), | 63 | ········asio::buffer(&port_low_byte_,·1), |
63 | ········asio::buffer(address_), | 64 | ········asio::buffer(address_), |
64 | ········asio::buffer(user_id_), | 65 | ········asio::buffer(user_id_), |
65 | ········asio::buffer(&null_byte_,·1) | 66 | ········asio::buffer(&null_byte_,·1) |
66 | ······} | 67 | ······} |
67 | ····}; | 68 | ····}; |
68 | ····return·bufs; | |
69 | ··} | 69 | ··} |
70 | | 70 | |
71 | private: | 71 | private: |
72 | ··unsigned·char·version_; | 72 | ··unsigned·char·version_; |
73 | ··unsigned·char·command_; | 73 | ··unsigned·char·command_; |
74 | ··unsigned·char·port_high_byte_; | 74 | ··unsigned·char·port_high_byte_; |
75 | ··unsigned·char·port_low_byte_; | 75 | ··unsigned·char·port_low_byte_; |
76 | ··asio::ip::address_v4::bytes_type·address_; | 76 | ··asio::ip::address_v4::bytes_type·address_; |
77 | ··std::string·user_id_; | 77 | ··std::string·user_id_; |
78 | ··unsigned·char·null_byte_; | 78 | ··unsigned·char·null_byte_; |
79 | }; | 79 | }; |
80 | | 80 | |
81 | class·reply | 81 | class·reply |
82 | { | 82 | { |
83 | public: | 83 | public: |
84 | ··enum·status_type | 84 | ··enum·status_type |
85 | ··{ | 85 | ··{ |
86 | ····request_granted·=·0x5a, | 86 | ····request_granted·=·0x5a, |
87 | ····request_failed·=·0x5b, | 87 | ····request_failed·=·0x5b, |
88 | ····request_failed_no_identd·=·0x5c, | 88 | ····request_failed_no_identd·=·0x5c, |
89 | ····request_failed_bad_user_id·=·0x5d | 89 | ····request_failed_bad_user_id·=·0x5d |
90 | ··}; | 90 | ··}; |
91 | | 91 | |
92 | ··reply() | 92 | ··reply() |
93 | ····:·null_byte_(0), | 93 | ····:·null_byte_(0), |
94 | ······status_() | 94 | ······status_() |
95 | ··{ | 95 | ··{ |
96 | ··} | 96 | ··} |
97 | | 97 | |
98 | ··boost::array<asio::mutable_buffer,·5>·buffers() | 98 | ··std::array<asio::mutable_buffer,·5>·buffers() |
99 | ··{ | 99 | ··{ |
100 | ····boost::array<asio::mutable_buffer,·5>·bufs·= | 100 | ····return |
101 | ····{ | 101 | ····{ |
102 | ······{ | 102 | ······{ |
103 | ········asio::buffer(&null_byte_,·1), | 103 | ········asio::buffer(&null_byte_,·1), |
104 | ········asio::buffer(&status_,·1), | 104 | ········asio::buffer(&status_,·1), |
105 | ········asio::buffer(&port_high_byte_,·1), | 105 | ········asio::buffer(&port_high_byte_,·1), |
106 | ········asio::buffer(&port_low_byte_,·1), | 106 | ········asio::buffer(&port_low_byte_,·1), |
107 | ········asio::buffer(address_) | 107 | ········asio::buffer(address_) |
108 | ······} | 108 | ······} |
109 | ····}; | 109 | ····}; |
110 | ····return·bufs; | |
111 | ··} | 110 | ··} |
112 | | 111 | |
113 | ··bool·success()·const | 112 | ··bool·success()·const |
114 | ··{ | 113 | ··{ |
115 | ····return·null_byte_·==·0·&&·status_·==·request_granted; | 114 | ····return·null_byte_·==·0·&&·status_·==·request_granted; |
116 | ··} | 115 | ··} |
117 | | 116 | |
118 | ··unsigned·char·status()·const | 117 | ··unsigned·char·status()·const |
119 | ··{ | 118 | ··{ |
120 | ····return·status_; | 119 | ····return·status_; |
121 | ··} | 120 | ··} |
122 | | 121 | |
123 | ··asio::ip::tcp::endpoint·endpoint()·const | 122 | ··asio::ip::tcp::endpoint·endpoint()·const |
124 | ··{ | 123 | ··{ |
125 | ····unsigned·short·port·=·port_high_byte_; | 124 | ····unsigned·short·port·=·port_high_byte_; |
126 | ····port·=·(port·<<·8)·&·0xff00; | 125 | ····port·=·(port·<<·8)·&·0xff00; |
127 | ····port·=·port·|·port_low_byte_; | 126 | ····port·=·port·|·port_low_byte_; |
128 | | 127 | |
129 | ····asio::ip::address_v4·address(address_); | 128 | ····asio::ip::address_v4·address(address_); |
130 | | 129 | |
131 | ····return·asio::ip::tcp::endpoint(address,·port); | 130 | ····return·asio::ip::tcp::endpoint(address,·port); |
132 | ··} | 131 | ··} |
133 | | 132 | |
134 | private: | 133 | private: |
135 | ··unsigned·char·null_byte_; | 134 | ··unsigned·char·null_byte_; |
136 | ··unsigned·char·status_; | 135 | ··unsigned·char·status_; |
137 | ··unsigned·char·port_high_byte_; | 136 | ··unsigned·char·port_high_byte_; |
138 | ··unsigned·char·port_low_byte_; | 137 | ··unsigned·char·port_low_byte_; |
139 | ··asio::ip::address_v4::bytes_type·address_; | 138 | ··asio::ip::address_v4::bytes_type·address_; |
140 | }; | 139 | }; |
141 | | 140 | |
142 | }·//·namespace·socks4 | 141 | }·//·namespace·socks4 |
143 | | 142 | |
144 | #endif·//·SOCKS4_HPP | 143 | #endif·//·SOCKS4_HPP |