forked from zeromq/libzmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocks.cpp
346 lines (288 loc) · 8.62 KB
/
socks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* SPDX-License-Identifier: MPL-2.0 */
#include "precompiled.hpp"
#include <sys/types.h>
#include "err.hpp"
#include "socks.hpp"
#include "tcp.hpp"
#include "blob.hpp"
#ifndef ZMQ_HAVE_WINDOWS
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#endif
zmq::socks_greeting_t::socks_greeting_t (uint8_t method_) : num_methods (1)
{
methods[0] = method_;
}
zmq::socks_greeting_t::socks_greeting_t (const uint8_t *methods_,
uint8_t num_methods_) :
num_methods (num_methods_)
{
for (uint8_t i = 0; i < num_methods_; i++)
methods[i] = methods_[i];
}
zmq::socks_greeting_encoder_t::socks_greeting_encoder_t () :
_bytes_encoded (0), _bytes_written (0)
{
}
void zmq::socks_greeting_encoder_t::encode (const socks_greeting_t &greeting_)
{
uint8_t *ptr = _buf;
*ptr++ = 0x05;
*ptr++ = static_cast<uint8_t> (greeting_.num_methods);
for (uint8_t i = 0; i < greeting_.num_methods; i++)
*ptr++ = greeting_.methods[i];
_bytes_encoded = 2 + greeting_.num_methods;
_bytes_written = 0;
}
int zmq::socks_greeting_encoder_t::output (fd_t fd_)
{
const int rc =
tcp_write (fd_, _buf + _bytes_written, _bytes_encoded - _bytes_written);
if (rc > 0)
_bytes_written += static_cast<size_t> (rc);
return rc;
}
bool zmq::socks_greeting_encoder_t::has_pending_data () const
{
return _bytes_written < _bytes_encoded;
}
void zmq::socks_greeting_encoder_t::reset ()
{
_bytes_encoded = _bytes_written = 0;
}
zmq::socks_choice_t::socks_choice_t (unsigned char method_) : method (method_)
{
}
zmq::socks_choice_decoder_t::socks_choice_decoder_t () : _bytes_read (0)
{
}
int zmq::socks_choice_decoder_t::input (fd_t fd_)
{
zmq_assert (_bytes_read < 2);
const int rc = tcp_read (fd_, _buf + _bytes_read, 2 - _bytes_read);
if (rc > 0) {
_bytes_read += static_cast<size_t> (rc);
if (_buf[0] != 0x05)
return -1;
}
return rc;
}
bool zmq::socks_choice_decoder_t::message_ready () const
{
return _bytes_read == 2;
}
zmq::socks_choice_t zmq::socks_choice_decoder_t::decode ()
{
zmq_assert (message_ready ());
return socks_choice_t (_buf[1]);
}
void zmq::socks_choice_decoder_t::reset ()
{
_bytes_read = 0;
}
zmq::socks_basic_auth_request_t::socks_basic_auth_request_t (
const std::string &username_, const std::string &password_) :
username (username_), password (password_)
{
zmq_assert (username_.size () <= UINT8_MAX);
zmq_assert (password_.size () <= UINT8_MAX);
}
zmq::socks_basic_auth_request_encoder_t::socks_basic_auth_request_encoder_t () :
_bytes_encoded (0), _bytes_written (0)
{
}
void zmq::socks_basic_auth_request_encoder_t::encode (
const socks_basic_auth_request_t &req_)
{
unsigned char *ptr = _buf;
*ptr++ = 0x01;
*ptr++ = static_cast<unsigned char> (req_.username.size ());
memcpy (ptr, req_.username.c_str (), req_.username.size ());
ptr += req_.username.size ();
*ptr++ = static_cast<unsigned char> (req_.password.size ());
memcpy (ptr, req_.password.c_str (), req_.password.size ());
ptr += req_.password.size ();
_bytes_encoded = ptr - _buf;
_bytes_written = 0;
}
int zmq::socks_basic_auth_request_encoder_t::output (fd_t fd_)
{
const int rc =
tcp_write (fd_, _buf + _bytes_written, _bytes_encoded - _bytes_written);
if (rc > 0)
_bytes_written += static_cast<size_t> (rc);
return rc;
}
bool zmq::socks_basic_auth_request_encoder_t::has_pending_data () const
{
return _bytes_written < _bytes_encoded;
}
void zmq::socks_basic_auth_request_encoder_t::reset ()
{
_bytes_encoded = _bytes_written = 0;
}
zmq::socks_auth_response_t::socks_auth_response_t (uint8_t response_code_) :
response_code (response_code_)
{
}
zmq::socks_auth_response_decoder_t::socks_auth_response_decoder_t () :
_bytes_read (0)
{
}
int zmq::socks_auth_response_decoder_t::input (fd_t fd_)
{
zmq_assert (_bytes_read < 2);
const int rc = tcp_read (fd_, _buf + _bytes_read, 2 - _bytes_read);
if (rc > 0) {
_bytes_read += static_cast<size_t> (rc);
if (_buf[0] != 0x01)
return -1;
}
return rc;
}
bool zmq::socks_auth_response_decoder_t::message_ready () const
{
return _bytes_read == 2;
}
zmq::socks_auth_response_t zmq::socks_auth_response_decoder_t::decode ()
{
zmq_assert (message_ready ());
return socks_auth_response_t (_buf[1]);
}
void zmq::socks_auth_response_decoder_t::reset ()
{
_bytes_read = 0;
}
zmq::socks_request_t::socks_request_t (uint8_t command_,
std::string hostname_,
uint16_t port_) :
command (command_), hostname (ZMQ_MOVE (hostname_)), port (port_)
{
zmq_assert (hostname.size () <= UINT8_MAX);
}
zmq::socks_request_encoder_t::socks_request_encoder_t () :
_bytes_encoded (0), _bytes_written (0)
{
}
void zmq::socks_request_encoder_t::encode (const socks_request_t &req_)
{
zmq_assert (req_.hostname.size () <= UINT8_MAX);
unsigned char *ptr = _buf;
*ptr++ = 0x05;
*ptr++ = req_.command;
*ptr++ = 0x00;
#if defined ZMQ_HAVE_OPENVMS && defined __ia64 && __INITIAL_POINTER_SIZE == 64
__addrinfo64 hints, *res = NULL;
#else
addrinfo hints, *res = NULL;
#endif
memset (&hints, 0, sizeof hints);
// Suppress potential DNS lookups.
hints.ai_flags = AI_NUMERICHOST;
const int rc = getaddrinfo (req_.hostname.c_str (), NULL, &hints, &res);
if (rc == 0 && res->ai_family == AF_INET) {
const struct sockaddr_in *sockaddr_in =
reinterpret_cast<const struct sockaddr_in *> (res->ai_addr);
*ptr++ = 0x01;
memcpy (ptr, &sockaddr_in->sin_addr, 4);
ptr += 4;
} else if (rc == 0 && res->ai_family == AF_INET6) {
const struct sockaddr_in6 *sockaddr_in6 =
reinterpret_cast<const struct sockaddr_in6 *> (res->ai_addr);
*ptr++ = 0x04;
memcpy (ptr, &sockaddr_in6->sin6_addr, 16);
ptr += 16;
} else {
*ptr++ = 0x03;
*ptr++ = static_cast<unsigned char> (req_.hostname.size ());
memcpy (ptr, req_.hostname.c_str (), req_.hostname.size ());
ptr += req_.hostname.size ();
}
if (rc == 0)
freeaddrinfo (res);
*ptr++ = req_.port / 256;
*ptr++ = req_.port % 256;
_bytes_encoded = ptr - _buf;
_bytes_written = 0;
}
int zmq::socks_request_encoder_t::output (fd_t fd_)
{
const int rc =
tcp_write (fd_, _buf + _bytes_written, _bytes_encoded - _bytes_written);
if (rc > 0)
_bytes_written += static_cast<size_t> (rc);
return rc;
}
bool zmq::socks_request_encoder_t::has_pending_data () const
{
return _bytes_written < _bytes_encoded;
}
void zmq::socks_request_encoder_t::reset ()
{
_bytes_encoded = _bytes_written = 0;
}
zmq::socks_response_t::socks_response_t (uint8_t response_code_,
const std::string &address_,
uint16_t port_) :
response_code (response_code_), address (address_), port (port_)
{
}
zmq::socks_response_decoder_t::socks_response_decoder_t () : _bytes_read (0)
{
}
int zmq::socks_response_decoder_t::input (fd_t fd_)
{
size_t n = 0;
if (_bytes_read < 5)
n = 5 - _bytes_read;
else {
const uint8_t atyp = _buf[3];
zmq_assert (atyp == 0x01 || atyp == 0x03 || atyp == 0x04);
if (atyp == 0x01)
n = 3 + 2;
else if (atyp == 0x03)
n = _buf[4] + 2;
else if (atyp == 0x04)
n = 15 + 2;
}
const int rc = tcp_read (fd_, _buf + _bytes_read, n);
if (rc > 0) {
_bytes_read += static_cast<size_t> (rc);
if (_buf[0] != 0x05)
return -1;
if (_bytes_read >= 2)
if (_buf[1] > 0x08)
return -1;
if (_bytes_read >= 3)
if (_buf[2] != 0x00)
return -1;
if (_bytes_read >= 4) {
const uint8_t atyp = _buf[3];
if (atyp != 0x01 && atyp != 0x03 && atyp != 0x04)
return -1;
}
}
return rc;
}
bool zmq::socks_response_decoder_t::message_ready () const
{
if (_bytes_read < 4)
return false;
const uint8_t atyp = _buf[3];
zmq_assert (atyp == 0x01 || atyp == 0x03 || atyp == 0x04);
if (atyp == 0x01)
return _bytes_read == 10;
if (atyp == 0x03)
return _bytes_read > 4 && _bytes_read == 4 + 1 + _buf[4] + 2u;
return _bytes_read == 22;
}
zmq::socks_response_t zmq::socks_response_decoder_t::decode ()
{
zmq_assert (message_ready ());
return socks_response_t (_buf[1], "", 0);
}
void zmq::socks_response_decoder_t::reset ()
{
_bytes_read = 0;
}