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 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.
10 //
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.
15 //
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
24 //
25 
26 #ifndef REMOTECONNECT_H
27 #define REMOTECONNECT_H
28 
29 
30 #include "ECMuleSocket.h"
31 #include "ECPacket.h"		// Needed for CECPacket
32 
33 #include <wx/event.h>
34 
35 class CECPacketHandlerBase {
36 	public:
~CECPacketHandlerBase()37 		virtual ~CECPacketHandlerBase() { }
38 		virtual void HandlePacket(const CECPacket *) = 0;
39 };
40 
41 class CECLoginPacket : public CECPacket {
42 	public:
43 		CECLoginPacket(const wxString& client, const wxString& version,
44 						bool canZLIB = true, bool canUTF8numbers = true, bool canNotify = false);
45 };
46 
47 class CECAuthPacket : public CECPacket {
48 	public:
49 		CECAuthPacket(const wxString& pass);
50 };
51 
52 //#warning Kry TODO - move to abstract layer.
53 class CRemoteConnect : public CECMuleSocket {
54 private:
55 	// State enums for connection SM ( client side ) in case of async processing
56 	enum {
57 		EC_INIT,         // initial state
58 		EC_CONNECT_SENT, // socket connect request sent
59 		EC_REQ_SENT,     // sent auth request to core, waiting for reply
60 		EC_SALT_RECEIVED,// received salt from core
61 		EC_PASSWD_SENT,  // sent password to core, waiting for OK
62 		EC_OK,           // core replied "ok"
63 		EC_FAIL          // core replied "bad"
64 	} m_ec_state;
65 
66 	// fifo of handlers for on-the-air requests. all EC concept is working in fcfs
67 	// order, so it is ok to assume that order of replies is same as order of requests
68 	std::list<CECPacketHandlerBase *> m_req_fifo;
69 	int m_req_count;
70 	int m_req_fifo_thr;
71 
72 	wxEvtHandler* m_notifier;
73 
74 	wxString m_connectionPassword;
75 	wxString m_server_reply;
76 	wxString m_client;
77 	wxString m_version;
78 
79 	bool m_canZLIB;
80 	bool m_canUTF8numbers;
81 	bool m_canNotify;
82 
83 	void WriteDoneAndQueueEmpty();
84 public:
85 	// The event handler is used for notifying connect/close
86 	CRemoteConnect(wxEvtHandler* evt_handler);
87 
88 	void SetCapabilities(bool canZLIB, bool canUTF8numbers, bool canNotify);
89 
90 	bool ConnectToCore(
91 		const wxString &host, int port,
92 		const wxString& login, const wxString &pass,
93 		const wxString& client, const wxString& version);
94 
GetServerReply()95 	const wxString& GetServerReply() const { return m_server_reply; }
96 
RequestFifoFull()97 	bool RequestFifoFull()
98 	{
99 		return m_req_count > m_req_fifo_thr;
100 	}
101 
102 	virtual void OnConnect(); // To override connection events
103 	virtual void OnLost(); // To override close events
104 
105 	void SendRequest(CECPacketHandlerBase *handler, const CECPacket *request);
106 	void SendPacket(const CECPacket *request);
107 
108 	/********************* EC API ********************/
109 
110 
111 	/* Misc */
112 
113 	// Shuts down aMule
114 	void ShutDown();
115 
116 	// Handles a ED2K link
117 	void Ed2kLink(wxString* link);
118 
119 
120 	/* Kad */
121 
122 	// Connects Kad network
123 	void StartKad();
124 
125 	// Disconnects Kad network
126 	void StopKad();
127 
128 
129 	/* ED2K */
130 
131 	// Connects to ED2K. If ip and port are not 0, connect
132 	// to the specific port. Otherwise, connect to any.
133 	void ConnectED2K(uint32 ip, uint16 port);
134 
135 	// Disconnects from ED2K
136 	void DisconnectED2K();
137 
138 
139 	/* Servers */
140 
141 	// Adds a server
142 	void AddServer(uint32 ip,
143 		       uint16 port);
144 
145 	// Remove specific server
146 	// Returns: Error message or empty string for no error
147 	void RemoveServer(uint32 ip,
148 			  uint16 port);
149 
150 	// Returns ED2K server list
151 	void GetServerList();
152 
153 	// Updates ED2K server from a URL
154 	void UpdateServerList(wxString url);
155 
156 
157 	/* Search */
158 
159 	// Starts new search
160 	void StartSearch();
161 
162 	// Stops current search
163 	void StopSearch();
164 
165 	// Returns search progress in %%
166 	void GetSearchProgress();
167 
168 	// Add 1 or more of found files to download queue
169 	void DownloadSearchResult(uint32* file);
170 
171 
172 	/* Statistics */
173 
174 	// Returns aMule statistics
175 	void GetStatistics();
176 
177 	// Returns aMule connection status
178 	void GetConnectionState();
179 
180 
181 	/* Queue/File handling */
182 
183 	// Returns downloads queue
184 	void GetDlQueue(CMD4Hash* file);
185 
186 	// Returns uploads queue
187 	void GetUpQueue(CMD4Hash* file);
188 
189 	// Returns waiting queue
190 	void GetWtQueue(CMD4Hash* file);
191 
192 	// Swaps A4AF to a file
193 	void SwapA4AFThis(CMD4Hash* file);
194 
195 	// Swaps A4AF to a file (auto)
196 	void SwapA4AFThisAuto(CMD4Hash* file);
197 
198 	// Swaps A4AF to any other files
199 	void SwapA4AFOthers(CMD4Hash* file);
200 
201 	// Pauses download(s)
202 	void Pause(CMD4Hash* file);
203 
204 	// Resumes download(s)
205 	void Resume(CMD4Hash* file);
206 
207 	// Stops download(s)
208 	void Stop(CMD4Hash* file);
209 
210 	// Sets priority for a download
211 	void SetPriority(CMD4Hash* file,
212 			 uint8 priority);
213 
214 	// Deletes a download
215 	void Delete(CMD4Hash* file);
216 
217 	// Sets category for a download
218 	void SetCategory(CMD4Hash* file,
219 			 wxString category);
220 
221 
222 	/* Shared files */
223 
224 	// Returns a list of shared files
225 	void GetSharedFiles();
226 
227 	// Sets priority for 1 or more shared files
228 	void SetSharedPriority(CMD4Hash* file,
229 			       uint8 priority);
230 
231 	// Reloads shared file list
232 	void ReloadSharedFiles();
233 
234 	// Adds a directory to shared file list
235 	void AddDirectoryToSharedFiles(wxString dir);
236 
237 	// Renames a file
238 	void RenameFile(CMD4Hash file,
239 			wxString name);
240 
241 
242 	/* Logging */
243 
244 	// Adds a new debug log line
245 	void AddLogline();
246 
247 	// Adds a new debug log line
248 	void AddDebugLogLine();
249 
250 	// Retrieves the log
251 	void GetLog();
252 
253 	// Returns the last log line.
254 	void GetLastLogLine();
255 
256 	// Retrieves the debug log
257 	void GetDebugLog();
258 
259 	// Retrieves the server info log
260 	void GetServerInfo();
261 
262 	// Clears the log
263 	void ClearLog();
264 
265 	// Clears the debug log
266 	void ClearDebugLog();
267 
268 	// Clears server info log
269 	void ClearServerInfo();
270 
271 
272 	/* Preferences */
273 
274 	// Request for Preferences
275 	void GetPreferences();
276 
277 	// Setting the preferences
278 	void SetPreferencesCategories();
279 	void SetPreferencesGeneral(wxString userNick,
280 				   CMD4Hash userHash);
281 	void SetPreferencesConnections(uint32 LineDownloadCapacity,
282 				       uint32 LineUploadCapacity,
283 				       uint16 MaxDownloadSpeed,
284 				       uint16 MaxUploadSpeed,
285 				       uint16 UploadSlotAllocation,
286 				       uint16 TCPPort,
287 				       uint16 UDPPort,
288 				       bool DisableUDP,
289 				       uint16 MaxSourcesPerFile,
290 				       uint16 MaxConnections,
291 				       bool EnableAutoConnect,
292 				       bool EnableReconnect,
293 				       bool EnableNetworkED2K,
294 				       bool EnableNetworkKademlia);
295 	void SetPreferencesMessageFilter(bool Enabled,
296 					 bool FilterAll,
297 					 bool AllowFromFriends,
298 					 bool FilterFromUnknownClients,
299 					 bool FilterByKeyword,
300 					 wxString Keywords);
301 	void SetPreferencesRemoteCrtl(bool RunOnStartup,
302 				      uint16 Port,
303 				      bool Guest,
304 				      CMD4Hash GuestPasswdHash,
305 				      bool UseGzip,
306 				      uint32 RefreshInterval,
307 				      wxString Template);
308 	void SetPreferencesOnlineSig(bool Enabled);
309 	void SetPreferencesServers(bool RemoveDeadServers,
310 				   uint16 RetriesDeadServers,
311 				   bool AutoUpdate,
312 				   // bool URLList, TODO: Implement this!
313 				   bool AddFromServer,
314 				   bool AddFromClient,
315 				   bool UsePrioritySystem,
316 				   bool SmartLowIDCheck,
317 				   bool SafeServerConnection,
318 				   bool AutoConnectStaticOnly,
319 				   bool ManualHighPriority);
320 	void SetPreferencesFiles(bool ICHEnabled,
321 				 bool AIHCTrust,
322 				 bool NewPaused,
323 				 bool NewDownloadAutoPriority,
324 				 bool PreviewPriority,
325 				 bool NewAutoULPriotiry,
326 				 bool UploadFullChunks,
327 				 bool StartNextPaused,
328 				 bool ResumeSameCategory,
329 				 bool SaveSources,
330 				 bool ExtractMetadata,
331 				 bool AllocateFullChunks,
332 				 bool AllocateFullSize,
333 				 bool CheckFreeSpace,
334 				 uint32 MinFreeSpace);
335 	void SetPreferencesDirectories();
336 	void SetPreferencesStatistics();
337 	void SetPreferencesSecurity(uint8 CanSeeShares,
338 				    uint32 FilePermissions,
339 				    uint32 DirPermissions,
340 				    bool IPFilterEnabled,
341 				    bool IPFilterAutoUpdate,
342 				    wxString IPFilterUpdateURL,
343 				    uint8 IPFilterLevel,
344 				    bool IPFilterFilterLAN,
345 				    bool UseSecIdent);
346 	void SetPreferencesCoreTweaks(uint16 MaxConnectionsPerFive,
347 				      bool Verbose,
348 				      uint32 FileBuffer,
349 				      uint32 ULQueue,
350 				      uint32 SRVKeepAliveTimeout);
351 
352 	// Creates new category
353 	void CreateCategory(uint32 category,
354 			    wxString title,
355 			    wxString folder,
356 			    wxString comment,
357 			    uint32 color,
358 			    uint8 priority);
359 
360 	// Updates existing category
361 	void UpdateCategory(uint32 category,
362 			    wxString title,
363 			    wxString folder,
364 			    wxString comment,
365 			    uint32 color,
366 			    uint8 priority);
367 
368 	// Deletes existing category
369 	void DeleteCategory(uint32 category);
370 
371 	// Retrieves the statistics graphs
372 	void GetStatsGraphs();
373 
374 	// Retrieves the statistics tree
375 	void GetStatsTree();
376 
377 	// Check if connection goes to local machine
378 	bool IsConnectedToLocalHost();
379 
380 private:
381 	virtual const CECPacket *OnPacketReceived(const CECPacket *packet, uint32 trueSize);
382 	bool ProcessAuthPacket(const CECPacket *reply);
383 };
384 
385 DECLARE_LOCAL_EVENT_TYPE(wxEVT_EC_CONNECTION, wxEVT_USER_FIRST + 1000)
386 
387 class wxECSocketEvent : public wxEvent {
388 public:
wxECSocketEvent(int id,bool result,const wxString & reply)389 	wxECSocketEvent(int id, bool result, const wxString& reply) : wxEvent(-1, id)
390 	{
391 		m_value = result;
392 		m_server_reply = reply;
393 	}
Clone(void)394 	wxEvent *Clone(void) const		{ return new wxECSocketEvent(*this); }
GetResult()395 	bool GetResult() const			{ return m_value; }
GetServerReply()396 	const wxString& GetServerReply() const	{ return m_server_reply; }
397 private:
398 	bool m_value;
399 	wxString m_server_reply;
400 };
401 
402 #endif // REMOTECONNECT_H
403 
404 // File_checked_for_headers
405