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


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58