Update Wiki URL
[amule.git] / src / Proxy.h
bloba1f598bbe1096e99a81a36d7d8e214d7054ce49a
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2004-2011 Marcelo Roberto Jimenez ( phoenix@amule.org )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #ifndef __PROXY_H__
27 #define __PROXY_H__
29 #include <wx/wx.h>
31 #include "amuleIPV4Address.h" // For amuleIPV4address
32 #include "StateMachine.h" // For CStateMachine
33 #include "LibSocket.h"
35 /******************************************************************************/
38 * SOCKS4 protocol implementation according to:
39 * - "SOCKS: A protocol for TCP proxy across firewalls":
40 * amule-root/docs/socks4.protocol
42 const unsigned char SOCKS4_VERSION = 0x04;
44 const unsigned char SOCKS4_CMD_CONNECT = 0x01;
45 const unsigned char SOCKS4_CMD_BIND = 0x02;
47 const unsigned char SOCKS4_REPLY_CODE = 0;
48 const unsigned char SOCKS4_REPLY_GRANTED = 90;
49 const unsigned char SOCKS4_REPLY_FAILED = 91;
50 const unsigned char SOCKS4_REPLY_FAILED_NO_IDENTD = 92;
51 const unsigned char SOCKS4_REPLY_FAILED_DIFFERENT_USERIDS = 93;
54 * SOCKS5 protocol implementation according to:
55 * - RFC-1928: SOCKS Protocol Version 5
56 * - RFC-1929: username/password Authentication for SOCKS V5
58 * Also, for the future :) :
59 * - RFC-1961: GSS-API Authentication Method for SOCKS Version 5
60 * - RFC-1508: Generic Security Service Application Program Interface
61 * - RFC-1509: Genecic Security Service API: C-bindings
65 const unsigned char SOCKS5_VERSION = 0x05;
67 const unsigned char SOCKS5_AUTH_METHOD_NO_AUTH_REQUIRED = 0x00;
68 const unsigned char SOCKS5_AUTH_METHOD_GSSAPI = 0x01;
69 const unsigned char SOCKS5_AUTH_METHOD_USERNAME_PASSWORD = 0x02;
70 const unsigned char SOCKS5_AUTH_METHOD_NO_ACCEPTABLE_METHODS = 0xFF;
72 const unsigned char SOCKS5_AUTH_VERSION_USERNAME_PASSWORD = 0x01;
74 const unsigned char SOCKS5_CMD_CONNECT = 0x01;
75 const unsigned char SOCKS5_CMD_BIND = 0x02;
76 const unsigned char SOCKS5_CMD_UDP_ASSOCIATE = 0x03;
78 const unsigned char SOCKS5_RSV = 0x00;
80 const unsigned char SOCKS5_ATYP_IPV4_ADDRESS = 0x01;
81 const unsigned char SOCKS5_ATYP_DOMAINNAME = 0x03;
82 const unsigned char SOCKS5_ATYP_IPV6_ADDRESS = 0x04;
84 const unsigned char SOCKS5_REPLY_SUCCEED = 0x00;
85 const unsigned char SOCKS5_REPLY_GENERAL_SERVER_FAILURE = 0x01;
86 const unsigned char SOCKS5_REPLY_CONNECTION_NOT_ALLOWED = 0x02;
87 const unsigned char SOCKS5_REPLY_NETWORK_UNREACHABLE = 0x03;
88 const unsigned char SOCKS5_REPLY_HOST_UNREACHABLE = 0x04;
89 const unsigned char SOCKS5_REPLY_CONNECTION_REFUSED = 0x05;
90 const unsigned char SOCKS5_REPLY_TTL_EXPIRED = 0x06;
91 const unsigned char SOCKS5_REPLY_COMMAND_NOT_SUPPORTED = 0x07;
92 const unsigned char SOCKS5_REPLY_ATYP_NOT_SUPPORTED = 0x08;
94 //------------------------------------------------------------------------------
95 // CProxyType
96 //------------------------------------------------------------------------------
99 * These constants must match the integer values saved in the configuration file,
100 * DO NOT CHANGE THIS ORDER!!!
102 enum CProxyType {
103 PROXY_NONE = -1,
104 PROXY_SOCKS5,
105 PROXY_SOCKS4,
106 PROXY_HTTP,
107 PROXY_SOCKS4a
111 //------------------------------------------------------------------------------
112 // CProxyData
113 //------------------------------------------------------------------------------
115 * The ProxyData class will hold information about the proxy server to be used.
117 class CProxyData
119 public:
121 * Default constructor.
123 CProxyData();
125 * Constructor.
127 * @param proxyEnable Whether proxy is enabled or not.
128 * @param proxyType The type of the proxy server.
129 * @param proxyHostName The proxy host name or IP address.
130 * @param proxyPort The proxy port number.
131 * @param enablePassword Whether authentication should be performed.
132 * @param userName The user name to authenticate to the server.
133 * @param password The password to authenticate to the server.
135 CProxyData(
136 bool proxyEnable,
137 CProxyType proxyType,
138 const wxString &proxyHostName,
139 unsigned short proxyPort,
140 bool enablePassword,
141 const wxString &userName,
142 const wxString &password
145 * Clears the object contents.
147 void Clear();
149 public:
150 //! Whether proxy is enabled or not.
151 bool m_proxyEnable;
152 //! The type of the proxy server.
153 CProxyType m_proxyType;
154 //! The proxy host name or IP address.
155 wxString m_proxyHostName;
156 //! The proxy port number.
157 unsigned short m_proxyPort;
158 //! Whether authentication should be performed.
159 bool m_enablePassword;
160 //! The user name to authenticate to the server.
161 wxString m_userName;
162 //! The password to authenticate to the server.
163 wxString m_password;
166 //------------------------------------------------------------------------------
167 // CProxyEventHandler
168 //------------------------------------------------------------------------------
170 * Event handler object used during proxy negotiation.
172 class CProxyEventHandler : public wxEvtHandler {
173 public:
175 * Constructor.
177 CProxyEventHandler();
179 private:
181 * Event handler function.
183 void ProxySocketHandler(wxSocketEvent &event);
184 DECLARE_EVENT_TABLE()
187 //------------------------------------------------------------------------------
188 // CProxyStateMachine
189 //------------------------------------------------------------------------------
190 /* This size is just to be a little bit greater than the UDP buffer used in aMule.
191 * Proxy protocol needs much less than this. 1024 would be ok. Other options are
192 * - Default ethernet MTU - Eth-II - IP - UDP: 1,514 - 14 - 20 - 8 = 1472 bytes;
193 * - Default token ring MTU 4,202 - overheads = ??.
194 * It would be really more efficient if the final object was less than
195 * a page (4096 bytes) in size.
197 //const unsigned int PROXY_BUFFER_SIZE = 1024;
198 const unsigned int PROXY_BUFFER_SIZE = 5*1024;
200 enum CProxyCommand {
201 PROXY_CMD_CONNECT,
202 PROXY_CMD_BIND,
203 PROXY_CMD_UDP_ASSOCIATE
206 enum CProxyState {
207 PROXY_STATE_START = 0,
208 PROXY_STATE_END = 1
212 * The ProxyStateMachine class is the ancestor of all proxy classes.
214 * CProxyStateMachine will do all the common work that a proxy class must do
215 * and provide the necessary variables.
217 class CProxyStateMachine : public CStateMachine
219 public:
221 * Constructor.
223 * @param name The name of the state machine. For debug messages only.
224 * @param max_states The maximum number of states that this machine will have.
225 * @param proxyData The necessary proxy information.
226 * @param cmd The type of proxy command to run.
228 CProxyStateMachine(
229 wxString name,
230 const unsigned int max_states,
231 const CProxyData &proxyData,
232 CProxyCommand cmd);
234 * Destructor.
236 virtual ~CProxyStateMachine();
238 * Adds a small string to the state machine name, containing the proxy command.
240 * @param s The original state machine name.
241 * @param cmd The proxy command.
243 static wxString &NewName(wxString &s, CProxyCommand cmd);
245 /* Interface */
246 bool Start(const amuleIPV4Address &peerAddress, CLibSocket *proxyClientSocket);
247 t_sm_state HandleEvent(t_sm_event event);
248 void AddDummyEvent();
249 void ReactivateSocket();
250 char *GetBuffer() { return m_buffer; }
251 amuleIPV4Address &GetProxyBoundAddress(void) const { return *m_proxyBoundAddress; }
252 unsigned char GetLastReply(void) const { return m_lastReply; }
253 bool IsEndState() const { return GetState() == PROXY_STATE_END; }
255 protected:
256 uint32 ProxyWrite(CLibSocket &socket, const void *buffer, wxUint32 nbytes);
257 uint32 ProxyRead(CLibSocket &socket, void *buffer);
258 bool CanReceive() const;
259 bool CanSend() const;
261 // Initialized at constructor
263 const CProxyData &m_proxyData;
264 CProxyCommand m_proxyCommand;
266 // Member variables
268 char m_buffer[PROXY_BUFFER_SIZE];
269 bool m_isLost;
270 bool m_isConnected;
271 bool m_canReceive;
272 bool m_canSend;
273 bool m_ok;
274 unsigned int m_lastRead;
275 int m_lastError;
277 // Will be initialized at Start()
279 amuleIPV4Address *m_peerAddress;
280 CLibSocket *m_proxyClientSocket;
281 amuleIPV4Address *m_proxyBoundAddress;
282 amuleIPV4Address m_proxyBoundAddressIPV4;
283 //wxIPV6address m_proxyBoundAddressIPV6;
285 // Temporary variables
287 unsigned char m_lastReply;
288 unsigned int m_packetLenght;
291 //------------------------------------------------------------------------------
292 // CSocks5StateMachine
293 //------------------------------------------------------------------------------
294 class CSocks5StateMachine;
295 typedef void (CSocks5StateMachine::*Socks5StateProcessor)(bool entry);
296 class CSocks5StateMachine : public CProxyStateMachine
298 private:
299 static const unsigned int SOCKS5_MAX_STATES = 14;
301 enum Socks5State {
302 SOCKS5_STATE_START = PROXY_STATE_START,
303 SOCKS5_STATE_END = PROXY_STATE_END,
304 SOCKS5_STATE_SEND_QUERY_AUTHENTICATION_METHOD,
305 SOCKS5_STATE_RECEIVE_AUTHENTICATION_METHOD,
306 SOCKS5_STATE_PROCESS_AUTHENTICATION_METHOD,
307 SOCKS5_STATE_SEND_AUTHENTICATION_GSSAPI,
308 SOCKS5_STATE_RECEIVE_AUTHENTICATION_GSSAPI,
309 SOCKS5_STATE_PROCESS_AUTHENTICATION_GSSAPI,
310 SOCKS5_STATE_SEND_AUTHENTICATION_USERNAME_PASSWORD,
311 SOCKS5_STATE_RECEIVE_AUTHENTICATION_USERNAME_PASSWORD,
312 SOCKS5_STATE_PROCESS_AUTHENTICATION_USERNAME_PASSWORD,
313 SOCKS5_STATE_SEND_COMMAND_REQUEST,
314 SOCKS5_STATE_RECEIVE_COMMAND_REPLY,
315 SOCKS5_STATE_PROCESS_COMMAND_REPLY
318 public:
319 /* Constructor */
320 CSocks5StateMachine(
321 const CProxyData &proxyData,
322 CProxyCommand proxyCommand);
323 void process_state(t_sm_state state, bool entry);
324 t_sm_state next_state(t_sm_event event);
326 private:
327 /* State Processors */
328 void process_start(bool entry);
329 void process_send_query_authentication_method(bool entry);
330 void process_receive_authentication_method(bool entry);
331 void process_process_authentication_method(bool entry);
332 void process_send_authentication_gssapi(bool entry);
333 void process_receive_authentication_gssapi(bool entry);
334 void process_process_authentication_gssapi(bool entry);
335 void process_send_authentication_username_password(bool entry);
336 void process_receive_authentication_username_password(bool entry);
337 void process_process_authentication_username_password(bool entry);
338 void process_send_command_request(bool entry);
339 void process_receive_command_reply(bool entry);
340 void process_process_command_reply(bool entry);
341 void process_end(bool entry);
342 /* Private Vars */
343 Socks5StateProcessor m_process_state[SOCKS5_MAX_STATES];
344 wxString m_state_name[SOCKS5_MAX_STATES];
347 //------------------------------------------------------------------------------
348 // CSocks4StateMachine
349 //------------------------------------------------------------------------------
350 class CSocks4StateMachine;
351 typedef void (CSocks4StateMachine::*Socks4StateProcessor)(bool entry);
352 class CSocks4StateMachine : public CProxyStateMachine
354 private:
355 static const unsigned int SOCKS4_MAX_STATES = 5;
357 enum Socks4State {
358 SOCKS4_STATE_START = PROXY_STATE_START,
359 SOCKS4_STATE_END = PROXY_STATE_END,
360 SOCKS4_STATE_SEND_COMMAND_REQUEST,
361 SOCKS4_STATE_RECEIVE_COMMAND_REPLY,
362 SOCKS4_STATE_PROCESS_COMMAND_REPLY
365 public:
366 /* Constructor */
367 CSocks4StateMachine(
368 const CProxyData &proxyData,
369 CProxyCommand proxyCommand);
370 void process_state(t_sm_state state, bool entry);
371 t_sm_state next_state(t_sm_event event);
373 private:
374 /* State Processors */
375 void process_start(bool entry);
376 void process_send_command_request(bool entry);
377 void process_receive_command_reply(bool entry);
378 void process_process_command_reply(bool entry);
379 void process_end(bool entry);
380 /* Private Vars */
381 Socks4StateProcessor m_process_state[SOCKS4_MAX_STATES];
382 wxString m_state_name[SOCKS4_MAX_STATES];
385 //------------------------------------------------------------------------------
386 // CHttpStateMachine
387 //------------------------------------------------------------------------------
388 class CHttpStateMachine;
389 typedef void (CHttpStateMachine::*HttpStateProcessor)(bool entry);
390 class CHttpStateMachine : public CProxyStateMachine
392 private:
393 static const unsigned int HTTP_MAX_STATES = 5;
395 enum HttpState {
396 HTTP_STATE_START = PROXY_STATE_START,
397 HTTP_STATE_END = PROXY_STATE_END,
398 HTTP_STATE_SEND_COMMAND_REQUEST,
399 HTTP_STATE_RECEIVE_COMMAND_REPLY,
400 HTTP_STATE_PROCESS_COMMAND_REPLY
403 public:
404 /* Constructor */
405 CHttpStateMachine(
406 const CProxyData &proxyData,
407 CProxyCommand proxyCommand);
408 void process_state(t_sm_state state, bool entry);
409 t_sm_state next_state(t_sm_event event);
411 private:
412 /* State Processors */
413 void process_start(bool entry);
414 void process_send_command_request(bool entry);
415 void process_receive_command_reply(bool entry);
416 void process_process_command_reply(bool entry);
417 void process_end(bool entry);
418 /* Private Vars */
419 HttpStateProcessor m_process_state[HTTP_MAX_STATES];
420 wxString m_state_name[HTTP_MAX_STATES];
423 //------------------------------------------------------------------------------
424 // CProxySocket
425 //------------------------------------------------------------------------------
427 class CDatagramSocketProxy;
429 class CProxySocket : public CLibSocket
431 friend class CProxyEventHandler;
432 public:
433 /* Constructor */
434 CProxySocket(
435 wxSocketFlags flags = wxSOCKET_NONE,
436 const CProxyData *proxyData = NULL,
437 CProxyCommand proxyCommand = PROXY_CMD_CONNECT,
438 CDatagramSocketProxy *udpSocket = NULL);
440 /* Destructor */
441 ~CProxySocket();
443 #ifndef ASIO_SOCKETS
444 /* I know, this is not very good, because SetEventHandler is not
445 * virtual in wxSocketBase, but I need to GetEventHandler in Proxy.cpp,
446 * so...
448 void SetEventHandler(wxEvtHandler &handler, int id = wxID_ANY)
450 m_socketEventHandler = &handler;
451 m_socketEventHandlerId = id;
452 CLibSocket::SetEventHandler(handler, id);
454 wxEvtHandler *GetEventHandler(void) const { return m_socketEventHandler; }
455 int GetEventHandlerId(void) const { return m_socketEventHandlerId; }
456 void SaveEventHandler(void)
458 m_savedSocketEventHandler = m_socketEventHandler;
459 m_savedSocketEventHandlerId = m_socketEventHandlerId;
461 void RestoreEventHandler(void)
463 m_socketEventHandler = m_savedSocketEventHandler;
464 m_socketEventHandlerId = m_savedSocketEventHandlerId;
465 SetEventHandler(*m_socketEventHandler, m_socketEventHandlerId);
467 #endif
468 // Asio mode
469 virtual void OnProxyEvent(int evt);
471 /* Interface */
472 void SetProxyData(const CProxyData *proxyData);
473 bool GetUseProxy() const { return m_useProxy; }
474 char *GetBuffer() { return m_proxyStateMachine->GetBuffer(); }
475 amuleIPV4Address &GetProxyBoundAddress(void) const
476 { return m_proxyStateMachine->GetProxyBoundAddress(); }
477 bool Start(const amuleIPV4Address &peerAddress);
478 bool ProxyIsCapableOf(CProxyCommand proxyCommand) const;
479 bool ProxyNegotiationIsOver() const { return m_proxyStateMachine->IsEndState(); }
480 CDatagramSocketProxy *GetUDPSocket() const { return m_udpSocket; }
482 private:
483 bool m_useProxy;
484 CProxyData m_proxyData;
485 amuleIPV4Address m_proxyAddress;
486 CProxyStateMachine *m_proxyStateMachine;
487 CDatagramSocketProxy *m_udpSocket;
488 wxEvtHandler *m_socketEventHandler;
489 int m_socketEventHandlerId;
490 wxEvtHandler *m_savedSocketEventHandler;
491 int m_savedSocketEventHandlerId;
494 //------------------------------------------------------------------------------
495 // CSocketClientProxy
496 //------------------------------------------------------------------------------
498 class CSocketClientProxy : public CProxySocket
500 public:
501 /* Constructor */
502 CSocketClientProxy(
503 wxSocketFlags flags = wxSOCKET_NONE,
504 const CProxyData *proxyData = NULL);
506 /* Interface */
507 bool Connect(amuleIPV4Address &address, bool wait);
508 uint32 Read(void *buffer, wxUint32 nbytes);
509 uint32 Write(const void *buffer, wxUint32 nbytes);
511 private:
512 wxMutex m_socketLocker;
515 //------------------------------------------------------------------------------
516 // CSocketServerProxy
517 //------------------------------------------------------------------------------
519 class CSocketServerProxy : public CLibSocketServer
521 public:
522 /* Constructor */
523 CSocketServerProxy(
524 amuleIPV4Address &address,
525 wxSocketFlags flags = wxSOCKET_NONE,
526 const CProxyData *proxyData = NULL);
528 private:
529 wxMutex m_socketLocker;
532 //------------------------------------------------------------------------------
533 // CDatagramSocketProxy
534 //------------------------------------------------------------------------------
536 enum UDPOperation {
537 UDP_OPERATION_NONE,
538 UDP_OPERATION_RECV_FROM,
539 UDP_OPERATION_SEND_TO
542 const unsigned int PROXY_UDP_OVERHEAD_IPV4 = 10;
543 const unsigned int PROXY_UDP_OVERHEAD_DOMAIN_NAME = 262;
544 const unsigned int PROXY_UDP_OVERHEAD_IPV6 = 20;
545 const unsigned int PROXY_UDP_MAXIMUM_OVERHEAD = PROXY_UDP_OVERHEAD_DOMAIN_NAME;
547 class CDatagramSocketProxy : public CLibUDPSocket
549 public:
550 /* Constructor */
551 CDatagramSocketProxy(
552 amuleIPV4Address &address,
553 wxSocketFlags flags = wxSOCKET_NONE,
554 const CProxyData *proxyData = NULL);
556 /* Destructor */
557 ~CDatagramSocketProxy();
559 /* Interface */
560 void SetUDPSocketOk() { m_udpSocketOk = true; }
562 /* wxDatagramSocket Interface */
563 virtual uint32 RecvFrom(amuleIPV4Address& addr, void* buf, uint32 nBytes);
564 virtual uint32 SendTo(const amuleIPV4Address& addr, const void* buf, uint32 nBytes);
566 private:
567 bool m_udpSocketOk;
568 CProxySocket m_proxyTCPSocket;
569 enum UDPOperation m_lastUDPOperation;
570 unsigned int m_lastUDPOverhead;
571 wxMutex m_socketLocker;
574 /******************************************************************************/
576 #endif /* __PROXY_H__ */
578 // File_checked_for_headers