2 // This file is part of the aMule Project.
4 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2004-2008 Angel Vidal ( kry@amule.org )
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
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 #include "RemoteConnect.h"
28 #include <common/MD5Sum.h>
29 #include <common/Format.h>
35 DEFINE_LOCAL_EVENT_TYPE(wxEVT_EC_CONNECTION
)
37 CECLoginPacket::CECLoginPacket(const wxString
& client
, const wxString
& version
)
39 CECPacket(EC_OP_AUTH_REQ
)
41 AddTag(CECTag(EC_TAG_CLIENT_NAME
, client
));
42 AddTag(CECTag(EC_TAG_CLIENT_VERSION
, version
));
43 AddTag(CECTag(EC_TAG_PROTOCOL_VERSION
, (uint64
)EC_CURRENT_PROTOCOL_VERSION
));
47 wxCHECK2(versionhash
.Decode(wxT(EC_VERSION_ID
)), /* Do nothing. */);
48 AddTag(CECTag(EC_TAG_VERSION_ID
, versionhash
));
53 CECAuthPacket::CECAuthPacket(const wxString
& pass
)
55 CECPacket(EC_OP_AUTH_PASSWD
)
58 wxCHECK2(passhash
.Decode(pass
), /* Do nothing. */);
59 AddTag(CECTag(EC_TAG_PASSWD_HASH
, passhash
));
63 * Connection to remote core
67 CRemoteConnect::CRemoteConnect(wxEvtHandler
* evt_handler
)
69 CECMuleSocket(evt_handler
!= 0),
72 // Give application some indication about how fast requests are served
73 // When request fifo contain more that certain number of entries, it may
74 // indicate that either core or network is slowing us down
76 // This is not mean to be absolute limit, because we can't drop requests
77 // out of calling context; it is just signal to application to slow down
79 m_notifier(evt_handler
)
83 bool CRemoteConnect::ConnectToCore(const wxString
&host
, int port
,
84 const wxString
&WXUNUSED(login
), const wxString
&pass
,
85 const wxString
& client
, const wxString
& version
)
87 m_connectionPassword
= pass
;
92 // don't even try to connect without a valid password
93 if (m_connectionPassword
.IsEmpty() || m_connectionPassword
== wxT("d41d8cd98f00b204e9800998ecf8427e")) {
94 m_server_reply
= _("You must specify a non-empty password.");
98 if (!hash
.Decode(m_connectionPassword
)) {
99 m_server_reply
= _("Invalid password, not a MD5 hash!");
101 } else if (hash
.IsEmpty()) {
102 m_server_reply
= _("You must specify a non-empty password.");
112 if (ConnectSocket(addr
)) {
113 CECLoginPacket
login_req(m_client
, m_version
);
115 std::auto_ptr
<const CECPacket
> getSalt(SendRecvPacket(&login_req
));
116 m_ec_state
= EC_REQ_SENT
;
118 ProcessAuthPacket(getSalt
.get());
120 CECAuthPacket
passwdPacket(m_connectionPassword
);
122 std::auto_ptr
<const CECPacket
> reply(SendRecvPacket(&passwdPacket
));
123 m_ec_state
= EC_PASSWD_SENT
;
125 return ProcessAuthPacket(reply
.get());
126 } else if (m_notifier
) {
127 m_ec_state
= EC_CONNECT_SENT
;
135 bool CRemoteConnect::IsConnectedToLocalHost()
138 return GetPeer(addr
) ? addr
.IsLocalHost() : false;
141 void CRemoteConnect::WriteDoneAndQueueEmpty()
145 void CRemoteConnect::OnConnect() {
147 wxASSERT(m_ec_state
== EC_CONNECT_SENT
);
148 CECLoginPacket
login_req(m_client
, m_version
);
149 CECSocket::SendPacket(&login_req
);
151 m_ec_state
= EC_REQ_SENT
;
153 // do nothing, calling code will take from here
157 void CRemoteConnect::OnLost() {
159 // Notify app of failure
160 wxECSocketEvent
event(wxEVT_EC_CONNECTION
,false,_("Connection failure"));
161 m_notifier
->AddPendingEvent(event
);
165 const CECPacket
*CRemoteConnect::OnPacketReceived(const CECPacket
*packet
, uint32 trueSize
)
167 CECPacket
*next_packet
= 0;
169 packet
->DebugPrint(true, trueSize
);
172 if (ProcessAuthPacket(packet
)) {
173 CECAuthPacket
passwdPacket(m_connectionPassword
);
174 CECSocket::SendPacket(&passwdPacket
);
175 m_ec_state
= EC_PASSWD_SENT
;
179 ProcessAuthPacket(packet
);
182 if ( !m_req_fifo
.empty() ) {
183 CECPacketHandlerBase
*handler
= m_req_fifo
.front();
184 m_req_fifo
.pop_front();
186 handler
->HandlePacket(packet
);
189 printf("EC error - packet received, but request fifo is empty\n");
196 // no reply by default
201 * Our requests are served by core in FCFS order. And core always replies. So, even
202 * if we're not interested in reply, we preserve place in request fifo.
204 void CRemoteConnect::SendRequest(CECPacketHandlerBase
*handler
, CECPacket
*request
)
207 m_req_fifo
.push_back(handler
);
208 CECSocket::SendPacket(request
);
211 void CRemoteConnect::SendPacket(CECPacket
*request
)
213 SendRequest(0, request
);
216 bool CRemoteConnect::ProcessAuthPacket(const CECPacket
*reply
) {
220 m_server_reply
= _("EC connection failed. Empty reply.");
223 if ((m_ec_state
== EC_REQ_SENT
) && (reply
->GetOpCode() == EC_OP_AUTH_SALT
)) {
224 const CECTag
*passwordSalt
= reply
->GetTagByName(EC_TAG_PASSWD_SALT
);
225 if ( NULL
!= passwordSalt
) {
226 wxString saltHash
= MD5Sum(CFormat(wxT("%lX")) % passwordSalt
->GetInt()).GetHash();
227 m_connectionPassword
= MD5Sum(m_connectionPassword
.Lower() + saltHash
).GetHash();
228 m_ec_state
= EC_SALT_RECEIVED
;
231 m_server_reply
= _("External Connection: Bad reply, handshake failed. Connection closed.");
232 m_ec_state
= EC_FAIL
;
235 } else if ((m_ec_state
== EC_PASSWD_SENT
) && (reply
->GetOpCode() == EC_OP_AUTH_OK
)) {
238 if (reply
->GetTagByName(EC_TAG_SERVER_VERSION
)) {
239 m_server_reply
= _("Succeeded! Connection established to aMule ") +
240 reply
->GetTagByName(EC_TAG_SERVER_VERSION
)->GetStringData();
242 m_server_reply
= _("Succeeded! Connection established.");
245 m_ec_state
= EC_FAIL
;
246 const CECTag
*reason
= reply
->GetTagByName(EC_TAG_STRING
);
247 if (reason
!= NULL
) {
248 m_server_reply
= wxString(_("External Connection: Access denied because: ")) +
249 wxGetTranslation(reason
->GetStringData());
251 m_server_reply
= _("External Connection: Handshake failed.");
257 wxECSocketEvent
event(wxEVT_EC_CONNECTION
, result
, m_server_reply
);
258 m_notifier
->AddPendingEvent(event
);
263 /******************** EC API ***********************/
265 void CRemoteConnect::StartKad() {
266 CECPacket
req(EC_OP_KAD_START
);
270 void CRemoteConnect::StopKad() {
271 CECPacket
req(EC_OP_KAD_STOP
);
275 void CRemoteConnect::ConnectED2K(uint32 ip
, uint16 port
) {
276 CECPacket
req(EC_OP_SERVER_CONNECT
);
278 req
.AddTag(CECTag(EC_TAG_SERVER
, EC_IPv4_t(ip
, port
)));
283 void CRemoteConnect::DisconnectED2K() {
284 CECPacket
req(EC_OP_SERVER_DISCONNECT
);
288 void CRemoteConnect::RemoveServer(uint32 ip
, uint16 port
) {
289 CECPacket
req(EC_OP_SERVER_REMOVE
);
291 req
.AddTag(CECTag(EC_TAG_SERVER
, EC_IPv4_t(ip
, port
)));
295 // File_checked_for_headers