1 | /* |
2 | Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file |
3 | |
4 | This file is part of libzmq, the ZeroMQ core engine in C++. |
5 | |
6 | libzmq is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU Lesser General Public License (LGPL) as published |
8 | by the Free Software Foundation; either version 3 of the License, or |
9 | (at your option) any later version. |
10 | |
11 | As a special exception, the Contributors give you permission to link |
12 | this library with independent modules to produce an executable, |
13 | regardless of the license terms of these independent modules, and to |
14 | copy and distribute the resulting executable under terms of your choice, |
15 | provided that you also meet, for each linked independent module, the |
16 | terms and conditions of the license of that module. An independent |
17 | module is a module which is not derived from or based on this library. |
18 | If you modify this library, you must extend this exception to your |
19 | version of the library. |
20 | |
21 | libzmq is distributed in the hope that it will be useful, but WITHOUT |
22 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
24 | License for more details. |
25 | |
26 | You should have received a copy of the GNU Lesser General Public License |
27 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
28 | */ |
29 | |
30 | #ifndef __ZMQ_SOCKS_HPP_INCLUDED__ |
31 | #define __ZMQ_SOCKS_HPP_INCLUDED__ |
32 | |
33 | #include <string> |
34 | #include "fd.hpp" |
35 | #include "stdint.hpp" |
36 | |
37 | namespace zmq |
38 | { |
39 | struct socks_greeting_t |
40 | { |
41 | socks_greeting_t (uint8_t method_); |
42 | socks_greeting_t (const uint8_t *methods_, uint8_t num_methods_); |
43 | |
44 | uint8_t methods[UINT8_MAX]; |
45 | const size_t num_methods; |
46 | }; |
47 | |
48 | class socks_greeting_encoder_t |
49 | { |
50 | public: |
51 | socks_greeting_encoder_t (); |
52 | void encode (const socks_greeting_t &greeting_); |
53 | int output (fd_t fd_); |
54 | bool has_pending_data () const; |
55 | void reset (); |
56 | |
57 | private: |
58 | size_t _bytes_encoded; |
59 | size_t _bytes_written; |
60 | uint8_t _buf[2 + UINT8_MAX]; |
61 | }; |
62 | |
63 | struct socks_choice_t |
64 | { |
65 | socks_choice_t (uint8_t method_); |
66 | |
67 | uint8_t method; |
68 | }; |
69 | |
70 | class socks_choice_decoder_t |
71 | { |
72 | public: |
73 | socks_choice_decoder_t (); |
74 | int input (fd_t fd_); |
75 | bool message_ready () const; |
76 | socks_choice_t decode (); |
77 | void reset (); |
78 | |
79 | private: |
80 | unsigned char _buf[2]; |
81 | size_t _bytes_read; |
82 | }; |
83 | |
84 | |
85 | struct socks_basic_auth_request_t |
86 | { |
87 | socks_basic_auth_request_t (const std::string &username_, |
88 | const std::string &password_); |
89 | |
90 | const std::string username; |
91 | const std::string password; |
92 | }; |
93 | |
94 | class socks_basic_auth_request_encoder_t |
95 | { |
96 | public: |
97 | socks_basic_auth_request_encoder_t (); |
98 | void encode (const socks_basic_auth_request_t &req_); |
99 | int output (fd_t fd_); |
100 | bool has_pending_data () const; |
101 | void reset (); |
102 | |
103 | private: |
104 | size_t _bytes_encoded; |
105 | size_t _bytes_written; |
106 | uint8_t _buf[1 + 1 + UINT8_MAX + 1 + UINT8_MAX]; |
107 | }; |
108 | |
109 | struct socks_auth_response_t |
110 | { |
111 | socks_auth_response_t (uint8_t response_code_); |
112 | uint8_t response_code; |
113 | }; |
114 | |
115 | class socks_auth_response_decoder_t |
116 | { |
117 | public: |
118 | socks_auth_response_decoder_t (); |
119 | int input (fd_t fd_); |
120 | bool message_ready () const; |
121 | socks_auth_response_t decode (); |
122 | void reset (); |
123 | |
124 | private: |
125 | int8_t _buf[2]; |
126 | size_t _bytes_read; |
127 | }; |
128 | |
129 | struct socks_request_t |
130 | { |
131 | socks_request_t (uint8_t command_, std::string hostname_, uint16_t port_); |
132 | |
133 | const uint8_t command; |
134 | const std::string hostname; |
135 | const uint16_t port; |
136 | }; |
137 | |
138 | class socks_request_encoder_t |
139 | { |
140 | public: |
141 | socks_request_encoder_t (); |
142 | void encode (const socks_request_t &req_); |
143 | int output (fd_t fd_); |
144 | bool has_pending_data () const; |
145 | void reset (); |
146 | |
147 | private: |
148 | size_t _bytes_encoded; |
149 | size_t _bytes_written; |
150 | uint8_t _buf[4 + UINT8_MAX + 1 + 2]; |
151 | }; |
152 | |
153 | struct socks_response_t |
154 | { |
155 | socks_response_t (uint8_t response_code_, |
156 | const std::string &address_, |
157 | uint16_t port_); |
158 | uint8_t response_code; |
159 | std::string address; |
160 | uint16_t port; |
161 | }; |
162 | |
163 | class socks_response_decoder_t |
164 | { |
165 | public: |
166 | socks_response_decoder_t (); |
167 | int input (fd_t fd_); |
168 | bool message_ready () const; |
169 | socks_response_t decode (); |
170 | void reset (); |
171 | |
172 | private: |
173 | int8_t _buf[4 + UINT8_MAX + 1 + 2]; |
174 | size_t _bytes_read; |
175 | }; |
176 | } |
177 | |
178 | #endif |
179 | |