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 __SOCKS_CONNECTER_HPP_INCLUDED__
31#define __SOCKS_CONNECTER_HPP_INCLUDED__
32
33#include "fd.hpp"
34#include "stream_connecter_base.hpp"
35#include "stdint.hpp"
36#include "socks.hpp"
37
38namespace zmq
39{
40class io_thread_t;
41class session_base_t;
42struct address_t;
43
44class socks_connecter_t : public stream_connecter_base_t
45{
46 public:
47 // If 'delayed_start' is true connecter first waits for a while,
48 // then starts connection process.
49 socks_connecter_t (zmq::io_thread_t *io_thread_,
50 zmq::session_base_t *session_,
51 const options_t &options_,
52 address_t *addr_,
53 address_t *proxy_addr_,
54 bool delayed_start_);
55 ~socks_connecter_t ();
56
57 void set_auth_method_basic (const std::string &username,
58 const std::string &password);
59 void set_auth_method_none ();
60
61
62 private:
63 enum
64 {
65 unplugged,
66 waiting_for_reconnect_time,
67 waiting_for_proxy_connection,
68 sending_greeting,
69 waiting_for_choice,
70 sending_basic_auth_request,
71 waiting_for_auth_response,
72 sending_request,
73 waiting_for_response
74 };
75
76 // Method ID
77 enum
78 {
79 socks_no_auth_required = 0x00,
80 socks_basic_auth = 0x02,
81 socks_no_acceptable_method = 0xff
82 };
83
84 // Handlers for I/O events.
85 virtual void in_event ();
86 virtual void out_event ();
87
88 // Internal function to start the actual connection establishment.
89 void start_connecting ();
90
91 int process_server_response (const socks_choice_t &response_);
92 int process_server_response (const socks_response_t &response_);
93 int process_server_response (const socks_auth_response_t &response_);
94
95 int parse_address (const std::string &address_,
96 std::string &hostname_,
97 uint16_t &port_);
98
99 int connect_to_proxy ();
100
101 void error ();
102
103 // Open TCP connecting socket. Returns -1 in case of error,
104 // 0 if connect was successful immediately. Returns -1 with
105 // EAGAIN errno if async connect was launched.
106 int open ();
107
108 // Get the file descriptor of newly created connection. Returns
109 // retired_fd if the connection was unsuccessful.
110 zmq::fd_t check_proxy_connection ();
111
112 socks_greeting_encoder_t _greeting_encoder;
113 socks_choice_decoder_t _choice_decoder;
114 socks_basic_auth_request_encoder_t _basic_auth_request_encoder;
115 socks_auth_response_decoder_t _auth_response_decoder;
116 socks_request_encoder_t _request_encoder;
117 socks_response_decoder_t _response_decoder;
118
119 // SOCKS address; owned by this connecter.
120 address_t *_proxy_addr;
121
122 // User defined authentication method
123 int _auth_method;
124
125 // Credentials for basic authentication
126 std::string _auth_username;
127 std::string _auth_password;
128
129 int _status;
130
131 ZMQ_NON_COPYABLE_NOR_MOVABLE (socks_connecter_t)
132};
133}
134
135#endif
136