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 | //
// 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 Angel Vidal ( kry@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
//
#include "RemoteConnect.h"
#include <common/SmartPtr.h> // Needed for CSmartPtr
#include <common/MD5Sum.h>
#include <common/Format.h>
#include "../../../amuleIPV4Address.h"
#include <wx/intl.h>
DEFINE_LOCAL_EVENT_TYPE(wxEVT_EC_CONNECTION)
CECLoginPacket::CECLoginPacket(const wxString& client, const wxString& version,
bool canZLIB, bool canUTF8numbers, bool canNotify)
:
CECPacket(EC_OP_AUTH_REQ)
{
AddTag(CECTag(EC_TAG_CLIENT_NAME, client));
AddTag(CECTag(EC_TAG_CLIENT_VERSION, version));
AddTag(CECTag(EC_TAG_PROTOCOL_VERSION, (uint64)EC_CURRENT_PROTOCOL_VERSION));
#ifdef EC_VERSION_ID
CMD4Hash versionhash;
wxCHECK2(versionhash.Decode(wxT(EC_VERSION_ID)), /* Do nothing. */);<--- syntax error
AddTag(CECTag(EC_TAG_VERSION_ID, versionhash));
#endif
// Send capabilities:
// support ZLIB compression
if (canZLIB) AddTag(CECEmptyTag(EC_TAG_CAN_ZLIB));
// support encoding of integers as UTF-8
if (canUTF8numbers) AddTag(CECEmptyTag(EC_TAG_CAN_UTF8_NUMBERS));
// client accepts push messages
if (canNotify) AddTag(CECEmptyTag(EC_TAG_CAN_NOTIFY));
}
CECAuthPacket::CECAuthPacket(const wxString& pass)
:
CECPacket(EC_OP_AUTH_PASSWD)
{
CMD4Hash passhash;
wxCHECK2(passhash.Decode(pass), /* Do nothing. */);<--- syntax error
AddTag(CECTag(EC_TAG_PASSWD_HASH, passhash));
}
/*!
* Connection to remote core
*
*/
CRemoteConnect::CRemoteConnect(wxEvtHandler* evt_handler)
:
CECMuleSocket(evt_handler != 0),
m_ec_state(EC_INIT),
m_req_fifo(),
// Give application some indication about how fast requests are served
// When request fifo contain more that certain number of entries, it may
// indicate that either core or network is slowing us down
m_req_count(0),
// This is not mean to be absolute limit, because we can't drop requests
// out of calling context; it is just signal to application to slow down
m_req_fifo_thr(20),
m_notifier(evt_handler),
m_canZLIB(false),
m_canUTF8numbers(false),
m_canNotify(false)
{
}
void CRemoteConnect::SetCapabilities(bool canZLIB, bool canUTF8numbers, bool canNotify)
{
m_canZLIB = canZLIB;
if (canZLIB) {
m_my_flags |= EC_FLAG_ZLIB;
}
m_canUTF8numbers = canUTF8numbers;
if (canUTF8numbers) {
m_my_flags |= EC_FLAG_UTF8_NUMBERS;
}
m_canNotify = canNotify;
}
bool CRemoteConnect::ConnectToCore(const wxString &host, int port,
const wxString &WXUNUSED(login), const wxString &pass,
const wxString& client, const wxString& version)
{
m_connectionPassword = pass;
m_client = client;
m_version = version;
// don't even try to connect without a valid password
if (m_connectionPassword.IsEmpty() || m_connectionPassword == wxT("d41d8cd98f00b204e9800998ecf8427e")) {
m_server_reply = _("You must specify a non-empty password.");
return false;
} else {
CMD4Hash hash;
if (!hash.Decode(m_connectionPassword)) {
m_server_reply = _("Invalid password, not a MD5 hash!");
return false;
} else if (hash.IsEmpty()) {
m_server_reply = _("You must specify a non-empty password.");
return false;
}
}
amuleIPV4Address addr;
addr.Hostname(host);
addr.Service(port);
if (ConnectSocket(addr)) {
// We get here only in case of synchronous connect.
// Otherwise we continue in OnConnect.
CECLoginPacket login_req(m_client, m_version, m_canZLIB, m_canUTF8numbers, m_canNotify);
CSmartPtr<const CECPacket> getSalt(SendRecvPacket(&login_req));
m_ec_state = EC_REQ_SENT;
ProcessAuthPacket(getSalt.get());
CECAuthPacket passwdPacket(m_connectionPassword);
CSmartPtr<const CECPacket> reply(SendRecvPacket(&passwdPacket));
m_ec_state = EC_PASSWD_SENT;
return ProcessAuthPacket(reply.get());
} else if (m_notifier) {
m_ec_state = EC_CONNECT_SENT;
} else {
return false;
}
return true;
}
bool CRemoteConnect::IsConnectedToLocalHost()
{
amuleIPV4Address addr;
return addr.Hostname(GetPeer()) ? addr.IsLocalHost() : false;
}
void CRemoteConnect::WriteDoneAndQueueEmpty()
{
}
void CRemoteConnect::OnConnect() {
if (m_notifier) {
wxASSERT(m_ec_state == EC_CONNECT_SENT);
CECLoginPacket login_req(m_client, m_version, m_canZLIB, m_canUTF8numbers, m_canNotify);
CECSocket::SendPacket(&login_req);
m_ec_state = EC_REQ_SENT;
} else {
// do nothing, calling code will take from here
}
}
void CRemoteConnect::OnLost() {
if (m_notifier) {
// Notify app of failure
wxECSocketEvent event(wxEVT_EC_CONNECTION,false,_("Connection failure"));
m_notifier->AddPendingEvent(event);
}
}
const CECPacket *CRemoteConnect::OnPacketReceived(const CECPacket *packet, uint32 trueSize)
{
CECPacket *next_packet = 0;
m_req_count--;
packet->DebugPrint(true, trueSize);
switch(m_ec_state) {
case EC_REQ_SENT:
if (ProcessAuthPacket(packet)) {
CECAuthPacket passwdPacket(m_connectionPassword);
CECSocket::SendPacket(&passwdPacket);
m_ec_state = EC_PASSWD_SENT;
}
break;
case EC_PASSWD_SENT:
ProcessAuthPacket(packet);
break;
case EC_OK:
if ( !m_req_fifo.empty() ) {
CECPacketHandlerBase *handler = m_req_fifo.front();
m_req_fifo.pop_front();
if ( handler ) {
handler->HandlePacket(packet);
}
} else {
printf("EC error - packet received, but request fifo is empty\n");
}
break;
default:
break;
}
// no reply by default
return next_packet;
}
/*
* Our requests are served by core in FCFS order. And core always replies. So, even
* if we're not interested in reply, we preserve place in request fifo.
*/
void CRemoteConnect::SendRequest(CECPacketHandlerBase *handler, const CECPacket *request)
{
m_req_count++;
m_req_fifo.push_back(handler);
CECSocket::SendPacket(request);
}
void CRemoteConnect::SendPacket(const CECPacket *request)
{
SendRequest(0, request);
}
bool CRemoteConnect::ProcessAuthPacket(const CECPacket *reply) {
bool result = false;
if (!reply) {
m_server_reply = _("EC connection failed. Empty reply.");
CloseSocket();
} else {
if ((m_ec_state == EC_REQ_SENT) && (reply->GetOpCode() == EC_OP_AUTH_SALT)) {
const CECTag *passwordSalt = reply->GetTagByName(EC_TAG_PASSWD_SALT);
if ( NULL != passwordSalt) {
wxString saltHash = MD5Sum(CFormat(wxT("%lX")) % passwordSalt->GetInt()).GetHash();
m_connectionPassword = MD5Sum(m_connectionPassword.Lower() + saltHash).GetHash();
m_ec_state = EC_SALT_RECEIVED;
return true;
} else {
m_server_reply = _("External Connection: Bad reply, handshake failed. Connection closed.");
m_ec_state = EC_FAIL;
CloseSocket();
}
} else if ((m_ec_state == EC_PASSWD_SENT) && (reply->GetOpCode() == EC_OP_AUTH_OK)) {
m_ec_state = EC_OK;
result = true;
if (reply->GetTagByName(EC_TAG_SERVER_VERSION)) {
m_server_reply = _("Succeeded! Connection established to aMule ") +
reply->GetTagByName(EC_TAG_SERVER_VERSION)->GetStringData();
} else {
m_server_reply = _("Succeeded! Connection established.");
}
}else {
m_ec_state = EC_FAIL;
const CECTag *reason = reply->GetTagByName(EC_TAG_STRING);
if (reason != NULL) {
m_server_reply = wxString(_("External Connection: Access denied because: ")) +
wxGetTranslation(reason->GetStringData());
} else {
m_server_reply = _("External Connection: Handshake failed.");
}
CloseSocket();
}
}
if ( m_notifier ) {
wxECSocketEvent event(wxEVT_EC_CONNECTION, result, m_server_reply);
m_notifier->AddPendingEvent(event);
}
return result;
}
/******************** EC API ***********************/
void CRemoteConnect::StartKad() {
CECPacket req(EC_OP_KAD_START);
SendPacket(&req);
}
void CRemoteConnect::StopKad() {
CECPacket req(EC_OP_KAD_STOP);
SendPacket(&req);
}
void CRemoteConnect::ConnectED2K(uint32 ip, uint16 port) {
CECPacket req(EC_OP_SERVER_CONNECT);
if (ip && port) {
req.AddTag(CECTag(EC_TAG_SERVER, EC_IPv4_t(ip, port)));
}
SendPacket(&req);
}
void CRemoteConnect::DisconnectED2K() {
CECPacket req(EC_OP_SERVER_DISCONNECT);
SendPacket(&req);
}
void CRemoteConnect::RemoveServer(uint32 ip, uint16 port) {
CECPacket req(EC_OP_SERVER_REMOVE);
if (ip && port) {
req.AddTag(CECTag(EC_TAG_SERVER, EC_IPv4_t(ip, port)));
}
SendPacket(&req);
}
// File_checked_for_headers
|