Upstream tarball 10068
[amule.git] / src / libs / ec / cpp / RemoteConnect.cpp
bloba0623ee8e38d073c8dd3a594928656194dc0de3f
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2004-2008 Angel Vidal ( kry@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.
20 //
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>
31 #include <wx/intl.h>
33 using std::auto_ptr;
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));
45 #ifdef EC_VERSION_ID
46 CMD4Hash versionhash;
47 wxCHECK2(versionhash.Decode(wxT(EC_VERSION_ID)), /* Do nothing. */);
48 AddTag(CECTag(EC_TAG_VERSION_ID, versionhash));
49 #endif
53 CECAuthPacket::CECAuthPacket(const wxString& pass)
55 CECPacket(EC_OP_AUTH_PASSWD)
57 CMD4Hash passhash;
58 wxCHECK2(passhash.Decode(pass), /* Do nothing. */);
59 AddTag(CECTag(EC_TAG_PASSWD_HASH, passhash));
62 /*!
63 * Connection to remote core
67 CRemoteConnect::CRemoteConnect(wxEvtHandler* evt_handler)
69 CECMuleSocket(evt_handler != 0),
70 m_ec_state(EC_INIT),
71 m_req_fifo(),
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
75 m_req_count(0),
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
78 m_req_fifo_thr(20),
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;
89 m_client = client;
90 m_version = version;
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.");
95 return false;
96 } else {
97 CMD4Hash hash;
98 if (!hash.Decode(m_connectionPassword)) {
99 m_server_reply = _("Invalid password, not a MD5 hash!");
100 return false;
101 } else if (hash.IsEmpty()) {
102 m_server_reply = _("You must specify a non-empty password.");
103 return false;
107 wxIPV4address addr;
109 addr.Hostname(host);
110 addr.Service(port);
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;
128 } else {
129 return false;
132 return true;
135 bool CRemoteConnect::IsConnectedToLocalHost()
137 wxIPV4address addr;
138 return GetPeer(addr) ? addr.IsLocalHost() : false;
141 void CRemoteConnect::WriteDoneAndQueueEmpty()
145 void CRemoteConnect::OnConnect() {
146 if (m_notifier) {
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;
152 } else {
153 // do nothing, calling code will take from here
157 void CRemoteConnect::OnLost() {
158 if (m_notifier) {
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;
168 m_req_count--;
169 packet->DebugPrint(true, trueSize);
170 switch(m_ec_state) {
171 case EC_REQ_SENT:
172 if (ProcessAuthPacket(packet)) {
173 CECAuthPacket passwdPacket(m_connectionPassword);
174 CECSocket::SendPacket(&passwdPacket);
175 m_ec_state = EC_PASSWD_SENT;
177 break;
178 case EC_PASSWD_SENT:
179 ProcessAuthPacket(packet);
180 break;
181 case EC_OK:
182 if ( !m_req_fifo.empty() ) {
183 CECPacketHandlerBase *handler = m_req_fifo.front();
184 m_req_fifo.pop_front();
185 if ( handler ) {
186 handler->HandlePacket(packet);
188 } else {
189 printf("EC error - packet received, but request fifo is empty\n");
191 break;
192 default:
193 break;
196 // no reply by default
197 return next_packet;
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)
206 m_req_count++;
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) {
217 bool result = false;
219 if (!reply) {
220 m_server_reply = _("EC connection failed. Empty reply.");
221 CloseSocket();
222 } else {
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;
229 return true;
230 } else {
231 m_server_reply = _("External Connection: Bad reply, handshake failed. Connection closed.");
232 m_ec_state = EC_FAIL;
233 CloseSocket();
235 } else if ((m_ec_state == EC_PASSWD_SENT) && (reply->GetOpCode() == EC_OP_AUTH_OK)) {
236 m_ec_state = EC_OK;
237 result = true;
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();
241 } else {
242 m_server_reply = _("Succeeded! Connection established.");
244 }else {
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());
250 } else {
251 m_server_reply = _("External Connection: Handshake failed.");
253 CloseSocket();
256 if ( m_notifier ) {
257 wxECSocketEvent event(wxEVT_EC_CONNECTION, result, m_server_reply);
258 m_notifier->AddPendingEvent(event);
260 return result;
263 /******************** EC API ***********************/
265 void CRemoteConnect::StartKad() {
266 CECPacket req(EC_OP_KAD_START);
267 SendPacket(&req);
270 void CRemoteConnect::StopKad() {
271 CECPacket req(EC_OP_KAD_STOP);
272 SendPacket(&req);
275 void CRemoteConnect::ConnectED2K(uint32 ip, uint16 port) {
276 CECPacket req(EC_OP_SERVER_CONNECT);
277 if (ip && port) {
278 req.AddTag(CECTag(EC_TAG_SERVER, EC_IPv4_t(ip, port)));
280 SendPacket(&req);
283 void CRemoteConnect::DisconnectED2K() {
284 CECPacket req(EC_OP_SERVER_DISCONNECT);
285 SendPacket(&req);
288 void CRemoteConnect::RemoveServer(uint32 ip, uint16 port) {
289 CECPacket req(EC_OP_SERVER_REMOVE);
290 if (ip && port) {
291 req.AddTag(CECTag(EC_TAG_SERVER, EC_IPv4_t(ip, port)));
293 SendPacket(&req);
295 // File_checked_for_headers