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.
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 IPFILTER_H
27 #define IPFILTER_H
28 
29 #include <wx/event.h>	// Needed for wxEvent
30 
31 #include "Types.h"	// Needed for uint8, uint16 and uint32
32 
33 class CIPFilterEvent;
34 
35 /**
36  * This class represents a list of IPs that should not be accepted
37  * as valid connection destinations nor sources. It provides an
38  * interface to query whether or not a specific IP is filtered.
39  *
40  * Currently this class can handle IPRange files in the Peer-Guardian
41  * format and the AntiP2P format, read from either text files or text
42  * files compressed with the zip compression format.
43  *
44  * This class is thread-safe.
45  */
46 class CIPFilter : public wxEvtHandler
47 {
48 public:
49 	/**
50 	 * Constructor.
51 	 */
52 	CIPFilter();
53 
54 	/**
55 	 * Checks if a IP is filtered with the current list and AccessLevel.
56 	 *
57 	 * @param IP2test The IP-Address to test for.
58 	 * @param isServer Whether this IP belongs to a server or a client. Needed for statistical purposes only.
59 	 * @return True if it is filtered, false otherwise.
60 	 *
61 	 * Note: IP2Test must be in anti-host order (BE on LE platform, LE on BE platform).
62 	 */
63 	bool	IsFiltered( uint32 IP2test, bool isServer = false );
64 
65 	/**
66 	 * Returns the number of banned ranges.
67 	 */
68 	uint32	BanCount() const;
69 
70 	/**
71 	 * Reloads the ipfilter files, discarding the current list of ranges.
72 	 */
73 	void	Reload();
74 
75 	/**
76 	 * Starts a download of the ipfilter-list at the specified URL.
77 	 *
78 	 * @param A valid URL.
79 	 *
80 	 * Once the file has been downloaded, the ipfilter.dat file
81 	 * will be replaced with the new file and Reload will be called.
82 	 */
83 	void	Update(const wxString& strURL);
84 
85 	/**
86 	 * This function is called when a download is completed.
87 	 */
88 	void	DownloadFinished(uint32 result);
89 
90 	/**
91 	 * True once initial startup has finished (stays true while reloading later).
92 	 */
IsReady()93 	bool	IsReady() const { return m_ready; }
94 
95 	/**
96 	 * These functions are called to tell the filter to start networks once it
97 	 * has finished loading.
98 	 */
StartKADWhenReady()99 	void	StartKADWhenReady() { m_startKADWhenReady = true; }
ConnectToAnyServerWhenReady()100 	void	ConnectToAnyServerWhenReady() { m_connectToAnyServerWhenReady = true; }
101 
102 private:
103 	/** Handles the result of loading the dat-files. */
104 	void	OnIPFilterEvent(CIPFilterEvent&);
105 
106 	//! The URL from which the IP filter was downloaded
107 	wxString m_URL;
108 
109 	// The IP ranges
110 	typedef std::vector<uint32> RangeIPs;
111 	RangeIPs m_rangeIPs;
112 	typedef std::vector<uint16> RangeLengths;
113 	RangeLengths m_rangeLengths;
114 	// Name for each range. This usually stays empty for memory reasons,
115 	// except if IP-Filter debugging is active.
116 	typedef std::vector<std::string> RangeNames;
117 	RangeNames m_rangeNames;
118 
119 	//! Mutex used to ensure thread-safety of this class
120 	mutable wxMutex	m_mutex;
121 
122 	// false if loading (on startup only)
123 	bool m_ready;
124 	// flags to start networks after loading
125 	bool m_startKADWhenReady;
126 	bool m_connectToAnyServerWhenReady;
127 	// should update be performed after filter is loaded ?
128 	bool m_updateAfterLoading;
129 
130 	friend class CIPFilterEvent;
131 	friend class CIPFilterTask;
132 
133 	DECLARE_EVENT_TABLE()
134 };
135 
136 #endif
137 // File_checked_for_headers
138