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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583 | //
// This file is part of the aMule Project.
//
// Copyright (c) 2004-2011 aMule Team ( admin@amule.org / http://www.amule.org )
// Copyright (c) 2004-2011 Marcelo Roberto Jimenez ( phoenix@amule.org )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#ifndef __PROXY_H__
#define __PROXY_H__
#include <wx/wx.h>
#include "amuleIPV4Address.h" // For amuleIPV4address
#include "StateMachine.h" // For CStateMachine
#include "LibSocket.h"
/******************************************************************************/
/*
* SOCKS4 protocol implementation according to:
* - "SOCKS: A protocol for TCP proxy across firewalls":
* amule-root/docs/socks4.protocol
*/
const unsigned char SOCKS4_VERSION = 0x04;
const unsigned char SOCKS4_CMD_CONNECT = 0x01;
const unsigned char SOCKS4_CMD_BIND = 0x02;
const unsigned char SOCKS4_REPLY_CODE = 0;
const unsigned char SOCKS4_REPLY_GRANTED = 90;
const unsigned char SOCKS4_REPLY_FAILED = 91;
const unsigned char SOCKS4_REPLY_FAILED_NO_IDENTD = 92;
const unsigned char SOCKS4_REPLY_FAILED_DIFFERENT_USERIDS = 93;
/*
* SOCKS5 protocol implementation according to:
* - RFC-1928: SOCKS Protocol Version 5
* - RFC-1929: username/password Authentication for SOCKS V5
*
* Also, for the future :) :
* - RFC-1961: GSS-API Authentication Method for SOCKS Version 5
* - RFC-1508: Generic Security Service Application Program Interface
* - RFC-1509: Genecic Security Service API: C-bindings
*
*/
const unsigned char SOCKS5_VERSION = 0x05;
const unsigned char SOCKS5_AUTH_METHOD_NO_AUTH_REQUIRED = 0x00;
const unsigned char SOCKS5_AUTH_METHOD_GSSAPI = 0x01;
const unsigned char SOCKS5_AUTH_METHOD_USERNAME_PASSWORD = 0x02;
const unsigned char SOCKS5_AUTH_METHOD_NO_ACCEPTABLE_METHODS = 0xFF;
const unsigned char SOCKS5_AUTH_VERSION_USERNAME_PASSWORD = 0x01;
const unsigned char SOCKS5_CMD_CONNECT = 0x01;
const unsigned char SOCKS5_CMD_BIND = 0x02;
const unsigned char SOCKS5_CMD_UDP_ASSOCIATE = 0x03;
const unsigned char SOCKS5_RSV = 0x00;
const unsigned char SOCKS5_ATYP_IPV4_ADDRESS = 0x01;
const unsigned char SOCKS5_ATYP_DOMAINNAME = 0x03;
const unsigned char SOCKS5_ATYP_IPV6_ADDRESS = 0x04;
const unsigned char SOCKS5_REPLY_SUCCEED = 0x00;
const unsigned char SOCKS5_REPLY_GENERAL_SERVER_FAILURE = 0x01;
const unsigned char SOCKS5_REPLY_CONNECTION_NOT_ALLOWED = 0x02;
const unsigned char SOCKS5_REPLY_NETWORK_UNREACHABLE = 0x03;
const unsigned char SOCKS5_REPLY_HOST_UNREACHABLE = 0x04;
const unsigned char SOCKS5_REPLY_CONNECTION_REFUSED = 0x05;
const unsigned char SOCKS5_REPLY_TTL_EXPIRED = 0x06;
const unsigned char SOCKS5_REPLY_COMMAND_NOT_SUPPORTED = 0x07;
const unsigned char SOCKS5_REPLY_ATYP_NOT_SUPPORTED = 0x08;
//------------------------------------------------------------------------------
// CProxyType
//------------------------------------------------------------------------------
/*
* These constants must match the integer values saved in the configuration file,
* DO NOT CHANGE THIS ORDER!!!
*/
enum CProxyType {
PROXY_NONE = -1,
PROXY_SOCKS5,
PROXY_SOCKS4,
PROXY_HTTP,
PROXY_SOCKS4a
};
//------------------------------------------------------------------------------
// CProxyData
//------------------------------------------------------------------------------
/**
* The ProxyData class will hold information about the proxy server to be used.
*/
class CProxyData
{
public:
/**
* Default constructor.
*/
CProxyData();
/**
* Constructor.
*
* @param proxyEnable Whether proxy is enabled or not.
* @param proxyType The type of the proxy server.
* @param proxyHostName The proxy host name or IP address.
* @param proxyPort The proxy port number.
* @param enablePassword Whether authentication should be performed.
* @param userName The user name to authenticate to the server.
* @param password The password to authenticate to the server.
*/
CProxyData(
bool proxyEnable,
CProxyType proxyType,
const wxString &proxyHostName,
unsigned short proxyPort,
bool enablePassword,
const wxString &userName,
const wxString &password
);
/**
* Clears the object contents.
*/
void Clear();
public:
//! Whether proxy is enabled or not.
bool m_proxyEnable;
//! The type of the proxy server.
CProxyType m_proxyType;
//! The proxy host name or IP address.
wxString m_proxyHostName;
//! The proxy port number.
unsigned short m_proxyPort;
//! Whether authentication should be performed.
bool m_enablePassword;
//! The user name to authenticate to the server.
wxString m_userName;
//! The password to authenticate to the server.
wxString m_password;
};
#ifndef ASIO_SOCKETS
//------------------------------------------------------------------------------
// CProxyEventHandler
//------------------------------------------------------------------------------
/**
* Event handler object used during proxy negotiation.
*/
class CProxyEventHandler : public wxEvtHandler {
public:
/**
* Constructor.
*/
CProxyEventHandler();
private:
/**
* Event handler function.
*/
void ProxySocketHandler(wxSocketEvent &event);
DECLARE_EVENT_TABLE()
};
#endif /* !ASIO_SOCKETS */
//------------------------------------------------------------------------------
// CProxyStateMachine
//------------------------------------------------------------------------------
/* This size is just to be a little bit greater than the UDP buffer used in aMule.
* Proxy protocol needs much less than this. 1024 would be ok. Other options are
* - Default ethernet MTU - Eth-II - IP - UDP: 1,514 - 14 - 20 - 8 = 1472 bytes;
* - Default token ring MTU 4,202 - overheads = ??.
* It would be really more efficient if the final object was less than
* a page (4096 bytes) in size.
*/
//const unsigned int PROXY_BUFFER_SIZE = 1024;
const unsigned int PROXY_BUFFER_SIZE = 5*1024;
enum CProxyCommand {
PROXY_CMD_CONNECT,
PROXY_CMD_BIND,
PROXY_CMD_UDP_ASSOCIATE
};
enum CProxyState {
PROXY_STATE_START = 0,
PROXY_STATE_END = 1
};
/**
* The ProxyStateMachine class is the ancestor of all proxy classes.
*
* CProxyStateMachine will do all the common work that a proxy class must do
* and provide the necessary variables.
*/
class CProxyStateMachine : public CStateMachine
{
public:
/**
* Constructor.
*
* @param name The name of the state machine. For debug messages only.
* @param max_states The maximum number of states that this machine will have.
* @param proxyData The necessary proxy information.
* @param cmd The type of proxy command to run.
*/
CProxyStateMachine(
wxString name,
const unsigned int max_states,
const CProxyData &proxyData,
CProxyCommand cmd);
/**
* Destructor.
*/
virtual ~CProxyStateMachine();<--- Destructor in derived class
/**
* Adds a small string to the state machine name, containing the proxy command.
*
* @param s The original state machine name.
* @param cmd The proxy command.
*/
static wxString &NewName(wxString &s, CProxyCommand cmd);
/* Interface */
bool Start(const amuleIPV4Address &peerAddress, CLibSocket *proxyClientSocket);
t_sm_state HandleEvent(t_sm_event event);
void AddDummyEvent();
void ReactivateSocket();
char *GetBuffer() { return m_buffer; }
amuleIPV4Address &GetProxyBoundAddress(void) const { return *m_proxyBoundAddress; }
unsigned char GetLastReply(void) const { return m_lastReply; }
bool IsEndState() const { return GetState() == PROXY_STATE_END; }
protected:
uint32 ProxyWrite(CLibSocket &socket, const void *buffer, wxUint32 nbytes);
uint32 ProxyRead(CLibSocket &socket, void *buffer);
bool CanReceive() const;
bool CanSend() const;
//
// Initialized at constructor
//
const CProxyData &m_proxyData;
CProxyCommand m_proxyCommand;
//
// Member variables
//
char m_buffer[PROXY_BUFFER_SIZE];
bool m_isLost;
bool m_isConnected;
bool m_canReceive;
bool m_canSend;
bool m_ok;
unsigned int m_lastRead;
int m_lastError;
//
// Will be initialized at Start()
//
amuleIPV4Address *m_peerAddress;
CLibSocket *m_proxyClientSocket;
amuleIPV4Address *m_proxyBoundAddress;
amuleIPV4Address m_proxyBoundAddressIPV4;
//wxIPV6address m_proxyBoundAddressIPV6;
//
// Temporary variables
//
unsigned char m_lastReply;
unsigned int m_packetLenght;
};
//------------------------------------------------------------------------------
// CSocks5StateMachine
//------------------------------------------------------------------------------
class CSocks5StateMachine;
typedef void (CSocks5StateMachine::*Socks5StateProcessor)(bool entry);
class CSocks5StateMachine : public CProxyStateMachine
{
private:
static const unsigned int SOCKS5_MAX_STATES = 14;
enum Socks5State {
SOCKS5_STATE_START = PROXY_STATE_START,
SOCKS5_STATE_END = PROXY_STATE_END,
SOCKS5_STATE_SEND_QUERY_AUTHENTICATION_METHOD,
SOCKS5_STATE_RECEIVE_AUTHENTICATION_METHOD,
SOCKS5_STATE_PROCESS_AUTHENTICATION_METHOD,
SOCKS5_STATE_SEND_AUTHENTICATION_GSSAPI,
SOCKS5_STATE_RECEIVE_AUTHENTICATION_GSSAPI,
SOCKS5_STATE_PROCESS_AUTHENTICATION_GSSAPI,
SOCKS5_STATE_SEND_AUTHENTICATION_USERNAME_PASSWORD,
SOCKS5_STATE_RECEIVE_AUTHENTICATION_USERNAME_PASSWORD,
SOCKS5_STATE_PROCESS_AUTHENTICATION_USERNAME_PASSWORD,
SOCKS5_STATE_SEND_COMMAND_REQUEST,
SOCKS5_STATE_RECEIVE_COMMAND_REPLY,
SOCKS5_STATE_PROCESS_COMMAND_REPLY
};
public:
/* Constructor */
CSocks5StateMachine(
const CProxyData &proxyData,
CProxyCommand proxyCommand);
void process_state(t_sm_state state, bool entry);<--- Function in derived class
t_sm_state next_state(t_sm_event event);<--- Function in derived class
private:
/* State Processors */
void process_start(bool entry);
void process_send_query_authentication_method(bool entry);
void process_receive_authentication_method(bool entry);
void process_process_authentication_method(bool entry);
void process_send_authentication_gssapi(bool entry);
void process_receive_authentication_gssapi(bool entry);
void process_process_authentication_gssapi(bool entry);
void process_send_authentication_username_password(bool entry);
void process_receive_authentication_username_password(bool entry);
void process_process_authentication_username_password(bool entry);
void process_send_command_request(bool entry);
void process_receive_command_reply(bool entry);
void process_process_command_reply(bool entry);
void process_end(bool entry);
/* Private Vars */
Socks5StateProcessor m_process_state[SOCKS5_MAX_STATES];
wxString m_state_name[SOCKS5_MAX_STATES];
};
//------------------------------------------------------------------------------
// CSocks4StateMachine
//------------------------------------------------------------------------------
class CSocks4StateMachine;
typedef void (CSocks4StateMachine::*Socks4StateProcessor)(bool entry);
class CSocks4StateMachine : public CProxyStateMachine
{
private:
static const unsigned int SOCKS4_MAX_STATES = 5;
enum Socks4State {
SOCKS4_STATE_START = PROXY_STATE_START,
SOCKS4_STATE_END = PROXY_STATE_END,
SOCKS4_STATE_SEND_COMMAND_REQUEST,
SOCKS4_STATE_RECEIVE_COMMAND_REPLY,
SOCKS4_STATE_PROCESS_COMMAND_REPLY
};
public:
/* Constructor */
CSocks4StateMachine(
const CProxyData &proxyData,
CProxyCommand proxyCommand);
void process_state(t_sm_state state, bool entry);<--- Function in derived class
t_sm_state next_state(t_sm_event event);<--- Function in derived class
private:
/* State Processors */
void process_start(bool entry);
void process_send_command_request(bool entry);
void process_receive_command_reply(bool entry);
void process_process_command_reply(bool entry);
void process_end(bool entry);
/* Private Vars */
Socks4StateProcessor m_process_state[SOCKS4_MAX_STATES];
wxString m_state_name[SOCKS4_MAX_STATES];
};
//------------------------------------------------------------------------------
// CHttpStateMachine
//------------------------------------------------------------------------------
class CHttpStateMachine;
typedef void (CHttpStateMachine::*HttpStateProcessor)(bool entry);
class CHttpStateMachine : public CProxyStateMachine
{
private:
static const unsigned int HTTP_MAX_STATES = 5;
enum HttpState {
HTTP_STATE_START = PROXY_STATE_START,
HTTP_STATE_END = PROXY_STATE_END,
HTTP_STATE_SEND_COMMAND_REQUEST,
HTTP_STATE_RECEIVE_COMMAND_REPLY,
HTTP_STATE_PROCESS_COMMAND_REPLY
};
public:
/* Constructor */
CHttpStateMachine(
const CProxyData &proxyData,
CProxyCommand proxyCommand);
void process_state(t_sm_state state, bool entry);<--- Function in derived class
t_sm_state next_state(t_sm_event event);<--- Function in derived class
private:
/* State Processors */
void process_start(bool entry);
void process_send_command_request(bool entry);
void process_receive_command_reply(bool entry);
void process_process_command_reply(bool entry);
void process_end(bool entry);
/* Private Vars */
HttpStateProcessor m_process_state[HTTP_MAX_STATES];
wxString m_state_name[HTTP_MAX_STATES];
};
//------------------------------------------------------------------------------
// CProxySocket
//------------------------------------------------------------------------------
class CDatagramSocketProxy;
class CProxySocket : public CLibSocket
{
friend class CProxyEventHandler;
public:
/* Constructor */
CProxySocket(<--- Class 'CProxySocket' has a constructor with 1 argument that is not explicit. [+]Class 'CProxySocket' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions.
muleSocketFlags flags = MULE_SOCKET_NONE,
const CProxyData *proxyData = NULL,
CProxyCommand proxyCommand = PROXY_CMD_CONNECT,
CDatagramSocketProxy *udpSocket = NULL);
/* Destructor */
~CProxySocket();<--- Destructor in derived class
#ifndef ASIO_SOCKETS
/* I know, this is not very good, because SetEventHandler is not
* virtual in wxSocketBase, but I need to GetEventHandler in Proxy.cpp,
* so...
*/
void SetEventHandler(wxEvtHandler &handler, int id = wxID_ANY)
{
m_socketEventHandler = &handler;
m_socketEventHandlerId = id;
CLibSocket::SetEventHandler(handler, id);
}
wxEvtHandler *GetEventHandler(void) const { return m_socketEventHandler; }
int GetEventHandlerId(void) const { return m_socketEventHandlerId; }
void SaveEventHandler(void)
{
m_savedSocketEventHandler = m_socketEventHandler;
m_savedSocketEventHandlerId = m_socketEventHandlerId;
}
void RestoreEventHandler(void)
{
m_socketEventHandler = m_savedSocketEventHandler;
m_socketEventHandlerId = m_savedSocketEventHandlerId;
SetEventHandler(*m_socketEventHandler, m_socketEventHandlerId);
}
#else
// Asio mode
virtual void OnProxyEvent(int evt);<--- Function in derived class
#endif
/* Interface */
void SetProxyData(const CProxyData *proxyData);
bool GetUseProxy() const { return m_useProxy; }
char *GetBuffer() { return m_proxyStateMachine->GetBuffer(); }
amuleIPV4Address &GetProxyBoundAddress(void) const
{ return m_proxyStateMachine->GetProxyBoundAddress(); }
bool Start(const amuleIPV4Address &peerAddress);
bool ProxyIsCapableOf(CProxyCommand proxyCommand) const;
bool ProxyNegotiationIsOver() const { return m_proxyStateMachine->IsEndState(); }
CDatagramSocketProxy *GetUDPSocket() const { return m_udpSocket; }
private:
bool m_useProxy;
CProxyData m_proxyData;
amuleIPV4Address m_proxyAddress;
CProxyStateMachine *m_proxyStateMachine;
CDatagramSocketProxy *m_udpSocket;
#ifndef ASIO_SOCKETS
wxEvtHandler *m_socketEventHandler;
int m_socketEventHandlerId;
wxEvtHandler *m_savedSocketEventHandler;
int m_savedSocketEventHandlerId;
#endif
};
//------------------------------------------------------------------------------
// CSocketClientProxy
//------------------------------------------------------------------------------
class CSocketClientProxy : public CProxySocket
{
public:
/* Constructor */
CSocketClientProxy(<--- Class 'CSocketClientProxy' has a constructor with 1 argument that is not explicit. [+]Class 'CSocketClientProxy' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions.
muleSocketFlags flags = MULE_SOCKET_NONE,
const CProxyData *proxyData = NULL);
/* Interface */
bool Connect(amuleIPV4Address &address, bool wait);<--- Derived function 'CSocketClientProxy::Connect'
uint32 Read(void *buffer, wxUint32 nbytes);<--- Derived function 'CSocketClientProxy::Read'
uint32 Write(const void *buffer, wxUint32 nbytes);<--- Derived function 'CSocketClientProxy::Write'
private:
wxMutex m_socketLocker;
};
//------------------------------------------------------------------------------
// CSocketServerProxy
//------------------------------------------------------------------------------
class CSocketServerProxy : public CLibSocketServer
{
public:
/* Constructor */
CSocketServerProxy(<--- Class 'CSocketServerProxy' has a constructor with 1 argument that is not explicit. [+]Class 'CSocketServerProxy' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions.
amuleIPV4Address &address,
muleSocketFlags flags = MULE_SOCKET_NONE,
const CProxyData *proxyData = NULL);
private:
wxMutex m_socketLocker;
};
//------------------------------------------------------------------------------
// CDatagramSocketProxy
//------------------------------------------------------------------------------
enum UDPOperation {
UDP_OPERATION_NONE,
UDP_OPERATION_RECV_FROM,
UDP_OPERATION_SEND_TO
};
const unsigned int PROXY_UDP_OVERHEAD_IPV4 = 10;
const unsigned int PROXY_UDP_OVERHEAD_DOMAIN_NAME = 262;
const unsigned int PROXY_UDP_OVERHEAD_IPV6 = 20;
const unsigned int PROXY_UDP_MAXIMUM_OVERHEAD = PROXY_UDP_OVERHEAD_DOMAIN_NAME;
class CDatagramSocketProxy : public CLibUDPSocket
{
public:
/* Constructor */
CDatagramSocketProxy(<--- Class 'CDatagramSocketProxy' has a constructor with 1 argument that is not explicit. [+]Class 'CDatagramSocketProxy' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions.
amuleIPV4Address &address,
muleSocketFlags flags = MULE_SOCKET_NONE,
const CProxyData *proxyData = NULL);
/* Destructor */
~CDatagramSocketProxy();<--- Destructor in derived class
/* Interface */
void SetUDPSocketOk() { m_udpSocketOk = true; }
/* wxDatagramSocket Interface */
virtual uint32 RecvFrom(amuleIPV4Address& addr, void* buf, uint32 nBytes);<--- Function in derived class<--- Function in derived class
virtual uint32 SendTo(const amuleIPV4Address& addr, const void* buf, uint32 nBytes);<--- Function in derived class<--- Function in derived class
private:
bool m_udpSocketOk;
CProxySocket m_proxyTCPSocket;
enum UDPOperation m_lastUDPOperation;
unsigned int m_lastUDPOverhead;
wxMutex m_socketLocker;
};
/******************************************************************************/
#endif /* __PROXY_H__ */
// File_checked_for_headers
|