Fix a memory leak in our UPnP handler code
[amule.git] / src / amule.h
blobd56eaeb2d245843ca9ef09b704a80af5329660a5
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
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.
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
27 #ifndef AMULE_H
28 #define AMULE_H
31 #include <wx/app.h> // Needed for wxApp
32 #include <wx/intl.h> // Needed for wxLocale
35 #include "Types.h" // Needed for int32, uint16 and uint64
36 #include <map>
37 #ifndef __WINDOWS__
38 #include <signal.h>
39 // #include <wx/unix/execute.h>
40 #endif // __WINDOWS__
42 #ifdef HAVE_CONFIG_H
43 # include "config.h" // Needed for ASIO_SOCKETS
44 #endif
47 class CAbstractFile;
48 class CKnownFile;
49 class ExternalConn;
50 class CamuleDlg;
51 class CPreferences;
52 class CDownloadQueue;
53 class CUploadQueue;
54 class CServerConnect;
55 class CSharedFileList;
56 class CServer;
57 class CFriend;
58 class CMD4Hash;
59 class CServerList;
60 class CListenSocket;
61 class CClientList;
62 class CKnownFileList;
63 class CCanceledFileList;
64 class CSearchList;
65 class CClientCreditsList;
66 class CFriendList;
67 class CClientUDPSocket;
68 class CIPFilter;
69 class UploadBandwidthThrottler;
70 #ifdef ASIO_SOCKETS
71 class CAsioService;
72 #else
73 class wxSocketEvent;
74 #endif
75 #ifdef ENABLE_UPNP
76 class CUPnPControlPoint;
77 class CUPnPPortMapping;
78 #endif
79 class CStatistics;
80 class wxCommandEvent;
81 class wxCloseEvent;
82 class wxFFileOutputStream;
83 class CTimer;
84 class CTimerEvent;
85 class wxSingleInstanceChecker;
86 class CHashingEvent;
87 class CMuleInternalEvent;
88 class CCompletionEvent;
89 class CAllocFinishedEvent;
90 class wxExecuteData;
91 class CLoggingEvent;
94 namespace MuleNotify {
95 class CMuleGUIEvent;
99 using MuleNotify::CMuleGUIEvent;
102 #ifdef AMULE_DAEMON
103 #define AMULE_APP_BASE wxAppConsole
104 #define CORE_TIMER_PERIOD 300
105 #else
106 #define AMULE_APP_BASE wxApp
107 #define CORE_TIMER_PERIOD 100
108 #endif
110 #define CONNECTED_ED2K (1<<0)
111 #define CONNECTED_KAD_NOT (1<<1)
112 #define CONNECTED_KAD_OK (1<<2)
113 #define CONNECTED_KAD_FIREWALLED (1<<3)
116 // Base class common to amule, aamuled and amulegui
117 class CamuleAppCommon
119 private:
120 // Used to detect a previous running instance of aMule
121 wxSingleInstanceChecker* m_singleInstance;
123 bool CheckPassedLink(const wxString &in, wxString &out, int cat);
124 protected:
125 wxString FullMuleVersion;
126 wxString OSDescription;
127 wxString OSType;
128 bool enable_daemon_fork;
129 bool ec_config;
130 bool m_skipConnectionDialog;
131 bool m_geometryEnabled;
132 wxString m_geometryString;
133 wxString m_logFile;
134 wxString m_appName;
135 wxString m_PidFile;
137 bool InitCommon(int argc, wxChar ** argv);
138 void RefreshSingleInstanceChecker();
139 bool CheckMuleDirectory(const wxString& desc, const class CPath& directory, const wxString& alternative, class CPath& outDir);
140 public:
141 wxString m_configFile;
143 CamuleAppCommon();
144 ~CamuleAppCommon();
145 void AddLinksFromFile();
146 // URL functions
147 wxString CreateMagnetLink(const CAbstractFile *f);
148 wxString CreateED2kLink(const CAbstractFile* f, bool add_source = false, bool use_hostname = false, bool add_cryptoptions = false, bool add_AICH = false);
149 // Who am I ?
150 #ifdef AMULE_DAEMON
151 bool IsDaemon() const { return true; }
152 #else
153 bool IsDaemon() const { return false; }
154 #endif
156 #ifdef CLIENT_GUI
157 bool IsRemoteGui() const { return true; }
158 #else
159 bool IsRemoteGui() const { return false; }
160 #endif
162 const wxString& GetMuleAppName() const { return m_appName; }
163 const wxString GetFullMuleVersion() const;
166 class CamuleApp : public AMULE_APP_BASE, public CamuleAppCommon
168 private:
169 enum APPState {
170 APP_STATE_RUNNING = 0,
171 APP_STATE_SHUTTINGDOWN,
172 APP_STATE_STARTING
175 public:
176 CamuleApp();
177 virtual ~CamuleApp();
179 virtual bool OnInit();
180 int OnExit();
181 #if wxUSE_ON_FATAL_EXCEPTION
182 void OnFatalException();
183 #endif
184 bool ReinitializeNetwork(wxString *msg);
186 // derived classes may override those
187 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
189 #ifndef ASIO_SOCKETS
190 // Socket handlers
191 void ListenSocketHandler(wxSocketEvent& event);
192 void UDPSocketHandler(wxSocketEvent& event);
193 #endif
195 virtual int ShowAlert(wxString msg, wxString title, int flags) = 0;
197 // Barry - To find out if app is running or shutting/shut down
198 bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
199 bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
201 // Check ED2K and Kademlia state
202 bool IsFirewalled() const;
203 // Are we connected to at least one network?
204 bool IsConnected() const;
205 // Connection to ED2K
206 bool IsConnectedED2K() const;
208 // What about Kad? Is it running?
209 bool IsKadRunning() const;
210 // Connection to Kad
211 bool IsConnectedKad() const;
212 // Check Kad state (TCP)
213 bool IsFirewalledKad() const;
214 // Check Kad state (UDP)
215 bool IsFirewalledKadUDP() const;
216 // Check Kad state (LAN mode)
217 bool IsKadRunningInLanMode() const;
218 // Kad stats
219 uint32 GetKadUsers() const;
220 uint32 GetKadFiles() const;
221 uint32 GetKadIndexedSources() const;
222 uint32 GetKadIndexedKeywords() const;
223 uint32 GetKadIndexedNotes() const;
224 uint32 GetKadIndexedLoad() const;
225 // True IP of machine
226 uint32 GetKadIPAdress() const;
227 // Buddy status
228 uint8 GetBuddyStatus() const;
229 uint32 GetBuddyIP() const;
230 uint32 GetBuddyPort() const;
232 // Check if we should callback this client
233 bool CanDoCallback(uint32 clientServerIP, uint16 clientServerPort);
235 // Misc functions
236 void OnlineSig(bool zero = false);
237 void Localize_mule();
238 void Trigger_New_version(wxString newMule);
240 // shakraw - new EC code using wxSocketBase
241 ExternalConn* ECServerHandler;
243 // return current (valid) public IP or 0 if unknown
244 // If ignorelocal is true, don't use m_localip
245 uint32 GetPublicIP(bool ignorelocal = false) const;
246 void SetPublicIP(const uint32 dwIP);
248 uint32 GetED2KID() const;
249 uint32 GetID() const;
251 // Other parts of the interface and such
252 CPreferences* glob_prefs;
253 CDownloadQueue* downloadqueue;
254 CUploadQueue* uploadqueue;
255 CServerConnect* serverconnect;
256 CSharedFileList* sharedfiles;
257 CServerList* serverlist;
258 CListenSocket* listensocket;
259 CClientList* clientlist;
260 CKnownFileList* knownfiles;
261 CCanceledFileList* canceledfiles;
262 CSearchList* searchlist;
263 CClientCreditsList* clientcredits;
264 CFriendList* friendlist;
265 CClientUDPSocket* clientudp;
266 CStatistics* m_statistics;
267 CIPFilter* ipfilter;
268 UploadBandwidthThrottler* uploadBandwidthThrottler;
269 #ifdef ASIO_SOCKETS
270 CAsioService* m_AsioService;
271 #endif
272 #ifdef ENABLE_UPNP
273 CUPnPControlPoint* m_upnp;
274 std::vector<CUPnPPortMapping> m_upnpMappings;
275 #endif
276 wxLocale m_locale;
278 void ShutDown();
280 wxString GetLog(bool reset = false);
281 wxString GetServerLog(bool reset = false);
282 wxString GetDebugLog(bool reset = false);
284 bool AddServer(CServer *srv, bool fromUser = false);
285 void AddServerMessageLine(wxString &msg);
286 #ifdef __DEBUG__
287 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
288 #endif
289 void SetOSFiles(const wxString& new_path);
291 const wxString& GetOSType() const { return OSType; }
293 void ShowUserCount();
295 void ShowConnectionState(bool forceUpdate = false);
297 void StartKad();
298 void StopKad();
300 /** Bootstraps kad from the specified IP (must be in hostorder). */
301 void BootstrapKad(uint32 ip, uint16 port);
302 /** Updates the nodes.dat file from the specified url. */
303 void UpdateNotesDat(const wxString& str);
306 void DisconnectED2K();
308 bool CryptoAvailable() const;
310 protected:
312 #ifdef __WXDEBUG__
314 * Handles asserts in a thread-safe manner.
316 virtual void OnAssertFailure(const wxChar* file, int line,
317 const wxChar* func, const wxChar* cond, const wxChar* msg);
318 #endif
320 void OnUDPDnsDone(CMuleInternalEvent& evt);
321 void OnSourceDnsDone(CMuleInternalEvent& evt);
322 void OnServerDnsDone(CMuleInternalEvent& evt);
324 void OnTCPTimer(CTimerEvent& evt);
325 void OnCoreTimer(CTimerEvent& evt);
327 void OnFinishedHashing(CHashingEvent& evt);
328 void OnFinishedAICHHashing(CHashingEvent& evt);
329 void OnFinishedCompletion(CCompletionEvent& evt);
330 void OnFinishedAllocation(CAllocFinishedEvent& evt);
331 void OnFinishedHTTPDownload(CMuleInternalEvent& evt);
332 void OnHashingShutdown(CMuleInternalEvent&);
333 void OnNotifyEvent(CMuleGUIEvent& evt);
335 void SetTimeOnTransfer();
337 APPState m_app_state;
339 wxString m_emulesig_path;
340 wxString m_amulesig_path;
342 uint32 m_dwPublicIP;
344 long webserver_pid;
346 wxString server_msg;
348 CTimer* core_timer;
350 private:
351 virtual void OnUnhandledException();
353 void CheckNewVersion(uint32 result);
355 uint32 m_localip;
359 #ifndef AMULE_DAEMON
362 class CamuleGuiBase {
363 public:
364 CamuleGuiBase();
365 virtual ~CamuleGuiBase();
367 wxString m_FrameTitle;
368 CamuleDlg* amuledlg;
369 int m_FileDetailDialogActive;
371 bool CopyTextToClipboard( wxString strText );
372 void ResetTitle();
374 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
375 virtual int ShowAlert(wxString msg, wxString title, int flags);
377 void AddGuiLogLine(const wxString& line);
378 protected:
380 * This list is used to contain log messages that are to be displayed
381 * on the GUI, when it is currently impossible to do so. This is in order
382 * to allows us to queue messages till after the dialog has been created.
384 std::list<wxString> m_logLines;
388 #ifndef CLIENT_GUI
391 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
394 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
396 int OnExit();
397 bool OnInit();
399 public:
401 virtual int ShowAlert(wxString msg, wxString title, int flags);
403 void ShutDown(wxCloseEvent &evt);
405 wxString GetLog(bool reset = false);
406 wxString GetServerLog(bool reset = false);
407 void AddServerMessageLine(wxString &msg);
408 DECLARE_EVENT_TABLE()
412 DECLARE_APP(CamuleGuiApp)
413 extern CamuleGuiApp *theApp;
416 #else /* !CLIENT_GUI */
419 #include "amule-remote-gui.h"
422 #endif // CLIENT_GUI
425 #define CALL_APP_DATA_LOCK
428 #else /* ! AMULE_DAEMON */
430 // wxWidgets 2.8 requires special code for event handling and sockets.
431 // 2.9 doesn't, so standard event loop and sockets can be used
433 // Windows: aMuled compiles with 2.8 (without the special code),
434 // but works only with 2.9
436 #if !wxCHECK_VERSION(2, 9, 0)
437 // wx 2.8 needs a hand-made event loop in any case
438 #define AMULED28_EVENTLOOP
440 // wx 2.8 also needs extra socket code, unless we have ASIO sockets
442 #ifdef HAVE_CONFIG_H
443 # include "config.h" // defines ASIO_SOCKETS
444 #endif
446 #ifndef ASIO_SOCKETS
447 // MSW: can't run amuled with 2.8 without ASIO sockets, just get it compiled
448 #ifndef __WINDOWS__
449 #define AMULED28_SOCKETS
450 #endif
451 #endif
452 #endif
454 #ifdef AMULED28_SOCKETS
455 #include <wx/socket.h>
457 class CSocketSet;
460 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
462 private:
463 CSocketSet *m_in_set, *m_out_set;
465 wxMutex m_lock;
466 public:
467 CAmuledGSocketFuncTable();
469 void AddSocket(GSocket *socket, GSocketEvent event);
470 void RemoveSocket(GSocket *socket, GSocketEvent event);
471 void RunSelect();
473 virtual bool OnInit();
474 virtual void OnExit();
475 virtual bool CanUseEventLoop();
476 virtual bool Init_Socket(GSocket *socket);
477 virtual void Destroy_Socket(GSocket *socket);
478 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
479 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
480 virtual void Enable_Events(GSocket *socket);
481 virtual void Disable_Events(GSocket *socket);
485 #endif // AMULED28_SOCKETS
487 // AppTrait functionality is required for 2.8 wx sockets
488 // Otherwise it's used to prevent zombie child processes,
489 // which stops working with wx 2.9.5.
490 // So disable it there (no idea if this has a noticeable impact).
492 #if !wxCHECK_VERSION(2, 9, 5) && !defined(__WINDOWS__ )
493 #define AMULED_APPTRAITS
494 #endif
496 #ifdef AMULED_APPTRAITS
498 typedef std::map<int, class wxEndProcessData *> EndProcessDataMap;
500 #include <wx/apptrait.h>
502 class CDaemonAppTraits : public wxConsoleAppTraits
504 private:
505 struct sigaction m_oldSignalChildAction;
506 struct sigaction m_newSignalChildAction;
508 #ifdef AMULED28_SOCKETS
509 CAmuledGSocketFuncTable *m_table;
510 wxMutex m_lock;
511 std::list<wxObject *> m_sched_delete;
512 public:
513 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
514 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
515 virtual void ScheduleForDestroy(wxObject *object);
516 virtual void RemoveFromPendingDelete(wxObject *object);
518 void DeletePending();
519 #else // AMULED28_SOCKETS
520 public:
521 CDaemonAppTraits();
522 #endif // !AMULED28_SOCKETS
524 virtual int WaitForChild(wxExecuteData& execData);
526 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
527 virtual wxStandardPathsBase& GetStandardPaths();
528 #endif
531 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
532 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
534 #endif // AMULED_APPTRAITS
537 class CamuleDaemonApp : public CamuleApp
539 private:
540 #ifdef AMULED28_EVENTLOOP
541 bool m_Exit;
542 #endif
543 #ifdef AMULED28_SOCKETS
544 CAmuledGSocketFuncTable *m_table;
545 #endif
546 bool OnInit();
547 int OnRun();
548 int OnExit();
550 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
552 #ifdef AMULED_APPTRAITS
553 struct sigaction m_oldSignalChildAction;
554 struct sigaction m_newSignalChildAction;
555 public:
556 wxAppTraits *CreateTraits();
557 #endif // AMULED_APPTRAITS
559 public:
561 #ifdef AMULED28_EVENTLOOP
562 CamuleDaemonApp();
564 void ExitMainLoop() { m_Exit = true; }
565 #endif
567 bool CopyTextToClipboard(wxString strText);
569 virtual int ShowAlert(wxString msg, wxString title, int flags);
571 DECLARE_EVENT_TABLE()
574 DECLARE_APP(CamuleDaemonApp)
575 extern CamuleDaemonApp *theApp;
577 #endif /* ! AMULE_DAEMON */
579 #endif // AMULE_H
580 // File_checked_for_headers