2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
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 <wx/stdpaths.h> // Needed for GetDataDir
29 #include "IPFilter.h" // Interface declarations.
30 #include "IPFilterScanner.h" // Interface for flexer
31 #include "Preferences.h" // Needed for thePrefs
32 #include "amule.h" // Needed for theApp
33 #include "Statistics.h" // Needed for theStats
34 #include "HTTPDownload.h" // Needed for CHTTPDownloadThread
35 #include "Logger.h" // Needed for AddDebugLogLineM
36 #include <common/Format.h> // Needed for CFormat
37 #include <common/StringFunctions.h> // Needed for CSimpleTokenizer
38 #include <common/FileFunctions.h> // Needed for UnpackArchive
39 #include <common/TextFile.h> // Needed for CTextFile
40 #include "ThreadScheduler.h" // Needed for CThreadScheduler and CThreadTask
41 #include "ClientList.h" // Needed for CClientList
42 #include "ServerList.h" // Needed for CServerList
43 #include <common/Macros.h> // Needed for DEBUG_ONLY()
44 #include "RangeMap.h" // Needed for CRangeMap
45 #include "ServerConnect.h" // Needed for ConnectToAnyServer()
46 #include "DownloadQueue.h" // Needed for theApp->downloadqueue
49 ////////////////////////////////////////////////////////////
52 BEGIN_DECLARE_EVENT_TYPES()
53 DECLARE_EVENT_TYPE(MULE_EVT_IPFILTER_LOADED
, -1)
54 END_DECLARE_EVENT_TYPES()
56 DEFINE_EVENT_TYPE(MULE_EVT_IPFILTER_LOADED
)
59 class CIPFilterEvent
: public wxEvent
62 CIPFilterEvent(CIPFilter::RangeIPs rangeIPs
, CIPFilter::RangeLengths rangeLengths
, CIPFilter::RangeNames rangeNames
)
63 : wxEvent(-1, MULE_EVT_IPFILTER_LOADED
)
65 // Physically copy the vectors, this will hopefully resize them back to their needed capacity.
66 m_rangeIPs
= rangeIPs
;
67 m_rangeLengths
= rangeLengths
;
68 // This one is usually empty, and should always be swapped, not copied.
69 std::swap(m_rangeNames
, rangeNames
);
72 /** @see wxEvent::Clone */
73 virtual wxEvent
* Clone() const {
74 return new CIPFilterEvent(*this);
77 CIPFilter::RangeIPs m_rangeIPs
;
78 CIPFilter::RangeLengths m_rangeLengths
;
79 CIPFilter::RangeNames m_rangeNames
;
83 typedef void (wxEvtHandler::*MuleIPFilterEventFunction
)(CIPFilterEvent
&);
85 //! Event-handler for completed hashings of new shared files and partfiles.
86 #define EVT_MULE_IPFILTER_LOADED(func) \
87 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_IPFILTER_LOADED, -1, -1, \
88 (wxObjectEventFunction) (wxEventFunction) \
89 wxStaticCastEvent(MuleIPFilterEventFunction, &func), (wxObject*) NULL),
92 ////////////////////////////////////////////////////////////
93 // Thread task for loading the ipfilter.dat files.
96 * This task loads the two ipfilter.dat files, a task that
97 * can take quite a while on a slow system with a large dat-
100 class CIPFilterTask
: public CThreadTask
103 CIPFilterTask(wxEvtHandler
* owner
)
104 : CThreadTask(wxT("Load IPFilter"), wxEmptyString
, ETP_Critical
),
105 m_storeDescriptions(false),
113 AddLogLineN(_("Loading IP filters 'ipfilter.dat' and 'ipfilter_static.dat'."));
114 if ( !LoadFromFile(theApp
->ConfigDir
+ wxT("ipfilter.dat")) &&
115 thePrefs::UseIPFilterSystem() ) {
116 // Load from system wide IP filter file
117 wxStandardPathsBase
&spb(wxStandardPaths::Get());
119 wxString
dataDir(spb
.GetPluginsDir());
120 #elif defined(__WXMAC__)
121 wxString
dataDir(spb
.GetDataDir());
123 wxString
dataDir(spb
.GetDataDir().BeforeLast(wxT('/')) + wxT("/amule"));
125 wxString
systemwideFile(JoinPaths(dataDir
,wxT("ipfilter.dat")));
126 LoadFromFile(systemwideFile
);
130 LoadFromFile(theApp
->ConfigDir
+ wxT("ipfilter_static.dat"));
132 uint8 accessLevel
= thePrefs::GetIPFilterLevel();
133 uint32 size
= m_result
.size();
134 // Reserve a little more so we don't have to resize the vector later.
135 // (Map ranges can exist that have to be stored in several parts.)
136 // Extra memory will be freed in the end.
137 m_rangeIPs
.reserve(size
+ 1000);
138 m_rangeLengths
.reserve(size
+ 1000);
139 if (m_storeDescriptions
) {
140 m_rangeNames
.reserve(size
+ 1000);
142 for (IPMap::iterator it
= m_result
.begin(); it
!= m_result
.end(); ++it
) {
143 if (it
->AccessLevel
< accessLevel
) {
144 // Calculate range "length"
145 // (which is included-end - start and thus length - 1)
148 // 0x8000 - 0xffff 0xfff - 0x07ffffff
149 // that means: remove msb, shift left by 12 bit, add 0xfff
150 // so it can cover 8 consecutive class A nets
151 // larger ranges (or theoretical ranges with uneven ends) have to be split
152 uint32 startIP
= it
.keyStart();
153 uint32 realLength
= it
.keyEnd() - it
.keyStart() + 1;
154 std::string
* descp
= 0;
156 m_rangeIPs
.push_back(startIP
);
157 uint32 curLength
= realLength
;
159 if (realLength
<= 0x8000) {
160 pushLength
= realLength
- 1;
162 if (curLength
>= 0x08000000) {
163 // range to big, limit
164 curLength
= 0x08000000;
167 curLength
&= 0x07FFF000;
169 pushLength
= ((curLength
- 1) >> 12) | 0x8000;
171 m_rangeLengths
.push_back(pushLength
);
173 if (m_storeDescriptions
) {
174 // std::string has no ref counting, so swap it
175 // (it's used so we need half the space than wxString with wide chars)
177 // we split the range so we have to duplicate it
178 m_rangeNames
.push_back(*descp
);
180 // push back empty string and swap
181 m_rangeNames
.push_back(std::string());
182 descp
= & * m_rangeNames
.rbegin();
183 std::swap(*descp
, it
->Description
);
187 realLength
-= curLength
;
188 startIP
+= curLength
;
192 // Numbers are probably different:
193 // - ranges from map that are not blocked because of their level are not added to the table
194 // - some ranges from the map have to be split for the table
195 AddDebugLogLineN(logIPFilter
, CFormat(wxT("Ranges in map: %d blocked ranges in table: %d")) % size
% m_rangeIPs
.size());
197 CIPFilterEvent
evt(m_rangeIPs
, m_rangeLengths
, m_rangeNames
);
198 wxPostEvent(m_owner
, evt
);
202 * This structure is used to contain the range-data in the rangemap.
206 bool operator==( const rangeObject
& other
) const {
207 return AccessLevel
== other
.AccessLevel
;
210 // Since descriptions are only used for debugging messages, there
211 // is no need to keep them in memory when running a non-debug build.
213 //! Contains the user-description of the range.
214 std::string Description
;
217 //! The AccessLevel for this filter.
221 //! The is the type of map used to store the IPs.
222 typedef CRangeMap
<rangeObject
, uint32
> IPMap
;
224 bool m_storeDescriptions
;
226 // the generated filter
227 CIPFilter::RangeIPs m_rangeIPs
;
228 CIPFilter::RangeLengths m_rangeLengths
;
229 CIPFilter::RangeNames m_rangeNames
;
231 wxEvtHandler
* m_owner
;
232 // temporary map for filter generation
238 * @param IPstart The start of the IP-range.
239 * @param IPend The end of the IP-range, must be less than or equal to IPstart.
240 * @param AccessLevel The AccessLevel of this range.
241 * @param Description The assosiated description of this range.
242 * @return true if the range was added, false if it was discarded.
244 * This function inserts the specified range into the IPMap. Invalid
245 * ranges where the AccessLevel is not within the range 0..255, or
246 * where IPEnd < IPstart not inserted.
248 bool AddIPRange(uint32 IPStart
, uint32 IPEnd
, uint16 AccessLevel
, const char* DEBUG_ONLY(Description
))
250 if (AccessLevel
< 256) {
251 if (IPStart
<= IPEnd
) {
253 item
.AccessLevel
= AccessLevel
;
255 if (m_storeDescriptions
) {
256 item
.Description
= Description
;
260 m_result
.insert(IPStart
, IPEnd
, item
);
271 * Loads a IP-list from the specified file, can be text or zip.
273 * @return True if the file was loaded, false otherwise.
275 int LoadFromFile(const wxString
& file
)
277 const CPath path
= CPath(file
);
279 if (!path
.FileExists() || TestDestroy()) {
284 m_storeDescriptions
= theLogger
.IsEnabled(logIPFilter
);
287 const wxChar
* ipfilter_files
[] = {
294 // Try to unpack the file, might be an archive
296 if (UnpackArchive(path
, ipfilter_files
).second
!= EFT_Text
) {
298 CFormat(_("Failed to load ipfilter.dat file '%s', unknown format encountered.")) % file
);
305 if (readFile
.Open(path
.GetRaw())) {
307 yyiprestart(readFile
.fp());
310 uint32 IPAccessLevel
= 0;
311 char * IPDescription
;
312 uint32 time1
= GetTickCountFullRes();
313 while (yyiplex(IPStart
, IPEnd
, IPAccessLevel
, IPDescription
)) {
314 AddIPRange(IPStart
, IPEnd
, IPAccessLevel
, IPDescription
);
317 uint32 time2
= GetTickCountFullRes();
318 AddDebugLogLineN(logIPFilter
, CFormat(wxT("time for lexer: %.3f")) % ((time2
-time1
) / 1000.0));
320 AddLogLineM(true, CFormat(_("Failed to load ipfilter.dat file '%s', could not open file.")) % file
);
324 wxString msg
= CFormat(wxPLURAL("Loaded %u IP-range from '%s'.", "Loaded %u IP-ranges from '%s'.", filtercount
)) % filtercount
% file
;
326 msg
<< wxT(" ") << ( CFormat(wxPLURAL("%u malformed line was discarded.", "%u malformed lines were discarded.", yyip_Bad
)) % yyip_Bad
);
335 ////////////////////////////////////////////////////////////
339 BEGIN_EVENT_TABLE(CIPFilter
, wxEvtHandler
)
340 EVT_MULE_IPFILTER_LOADED(CIPFilter::OnIPFilterEvent
)
346 * This function creates a text-file containing the specified text,
347 * but only if the file does not already exist.
349 static bool CreateDummyFile(const wxString
& filename
, const wxString
& text
)
351 // Create template files
352 if (!wxFileExists(filename
)) {
355 if (file
.Open(filename
, CTextFile::write
)) {
356 file
.WriteLine(text
);
364 CIPFilter::CIPFilter() :
366 m_startKADWhenReady(false),
367 m_connectToAnyServerWhenReady(false)
369 // Setup dummy files for the curious user.
370 const wxString normalDat
= theApp
->ConfigDir
+ wxT("ipfilter.dat");
371 const wxString normalMsg
= wxString()
372 << wxT("# This file is used by aMule to store ipfilter lists downloaded\n")
373 << wxT("# through the auto-update functionality. Do not save ipfilter-\n")
374 << wxT("# ranges here that should not be overwritten by aMule.\n");
376 if (CreateDummyFile(normalDat
, normalMsg
)) {
377 // redownload if user deleted file
378 thePrefs::SetLastHTTPDownloadURL(HTTP_IPFilter
, wxEmptyString
);
381 const wxString staticDat
= theApp
->ConfigDir
+ wxT("ipfilter_static.dat");
382 const wxString staticMsg
= wxString()
383 << wxT("# This file is used to store ipfilter-ranges that should\n")
384 << wxT("# not be overwritten by aMule. If you wish to keep a custom\n")
385 << wxT("# set of ipfilter-ranges that take precedence over ipfilter-\n")
386 << wxT("# ranges aquired through the auto-update functionality, then\n")
387 << wxT("# place them in this file. aMule will not change this file.");
389 CreateDummyFile(staticDat
, staticMsg
);
391 // First load currently available filter, so network connect is possible right after
392 // (in case filter download takes some time).
394 // Check if update should be done only after that.
395 m_updateAfterLoading
= thePrefs::IPFilterAutoLoad() && !thePrefs::IPFilterURL().IsEmpty();
399 void CIPFilter::Reload()
401 // We keep the current filter till the new one has been loaded.
402 CThreadScheduler::AddTask(new CIPFilterTask(this));
406 uint32
CIPFilter::BanCount() const
408 wxMutexLocker
lock(m_mutex
);
410 return m_rangeIPs
.size();
414 bool CIPFilter::IsFiltered(uint32 IPTest
, bool isServer
)
416 if ((!thePrefs::IsFilteringClients() && !isServer
) || (!thePrefs::IsFilteringServers() && isServer
)) {
420 // Somebody connected before we even started the networks.
421 // Filter is not up yet, so block him.
422 AddDebugLogLineN(logIPFilter
, CFormat(wxT("Filtered IP %s because filter isn't ready yet.")) % Uint32toStringIP(IPTest
));
424 theStats::AddFilteredServer();
426 theStats::AddFilteredClient();
430 wxMutexLocker
lock(m_mutex
);
431 // The IP needs to be in host order
432 uint32 ip
= wxUINT32_SWAP_ALWAYS(IPTest
);
434 int imax
= m_rangeIPs
.size() - 1;
437 while (imin
<= imax
) {
438 i
= (imin
+ imax
) / 2;
439 uint32 curIP
= m_rangeIPs
[i
];
441 uint32 curLength
= m_rangeLengths
[i
];
442 if (curLength
>= 0x8000) {
443 curLength
= ((curLength
& 0x7fff) << 12) + 0xfff;
445 if (curIP
+ curLength
>= ip
) {
457 AddDebugLogLineN(logIPFilter
, CFormat(wxT("Filtered IP %s%s")) % Uint32toStringIP(IPTest
)
458 % (i
< (int)m_rangeNames
.size() ? (wxT(" (") + wxString(char2unicode(m_rangeNames
[i
].c_str())) + wxT(")"))
459 : wxString(wxEmptyString
)));
461 theStats::AddFilteredServer();
463 theStats::AddFilteredClient();
471 void CIPFilter::Update(const wxString
& strURL
)
473 if (!strURL
.IsEmpty()) {
476 wxString filename
= theApp
->ConfigDir
+ wxT("ipfilter.download");
477 wxString oldfilename
= theApp
->ConfigDir
+ wxT("ipfilter.dat");
478 CHTTPDownloadThread
*downloader
= new CHTTPDownloadThread(m_URL
, filename
, oldfilename
, HTTP_IPFilter
);
480 downloader
->Create();
486 void CIPFilter::DownloadFinished(uint32 result
)
488 wxString datName
= wxT("ipfilter.dat");
489 if (result
== HTTP_Success
) {
490 // download succeeded. proceed with ipfilter loading
491 wxString newDat
= theApp
->ConfigDir
+ wxT("ipfilter.download");
492 wxString oldDat
= theApp
->ConfigDir
+ datName
;
494 if (wxFileExists(oldDat
) && !wxRemoveFile(oldDat
)) {
495 AddLogLineC(CFormat(_("Failed to remove %s file, aborting update.")) % datName
);
497 } else if (!wxRenameFile(newDat
, oldDat
)) {
498 AddLogLineC(CFormat(_("Failed to rename new %s file, aborting update.")) % datName
);
501 AddLogLineN(CFormat(_("Successfully updated %s")) % datName
);
503 } else if (result
== HTTP_Skipped
) {
504 AddLogLineN(CFormat(_("Skipped download of %s, because requested file is not newer.")) % datName
);
506 AddLogLineC(CFormat(_("Failed to download %s from %s")) % datName
% m_URL
);
509 if (result
== HTTP_Success
) {
510 // Reload both ipfilter files on success
516 void CIPFilter::OnIPFilterEvent(CIPFilterEvent
& evt
)
519 wxMutexLocker
lock(m_mutex
);
520 std::swap(m_rangeIPs
, evt
.m_rangeIPs
);
521 std::swap(m_rangeLengths
, evt
.m_rangeLengths
);
522 std::swap(m_rangeNames
, evt
.m_rangeNames
);
525 if (theApp
->IsOnShutDown()) {
528 AddLogLineN(_("IP filter is ready"));
530 if (thePrefs::IsFilteringClients()) {
531 theApp
->clientlist
->FilterQueues();
533 if (thePrefs::IsFilteringServers()) {
534 theApp
->serverlist
->FilterServers();
536 // Now start networks we didn't start earlier
537 if (m_connectToAnyServerWhenReady
|| m_startKADWhenReady
) {
538 AddLogLineC(_("Connecting"));
540 if (m_connectToAnyServerWhenReady
) {
541 m_connectToAnyServerWhenReady
= false;
542 theApp
->serverconnect
->ConnectToAnyServer();
544 if (m_startKADWhenReady
) {
545 m_startKADWhenReady
= false;
548 theApp
->ShowConnectionState(true); // update connect button
549 if (thePrefs::GetSrcSeedsOn()) {
550 theApp
->downloadqueue
->LoadSourceSeeds();
552 // Trigger filter update if configured
553 if (m_updateAfterLoading
) {
554 m_updateAfterLoading
= false;
555 Update(thePrefs::IPFilterURL());
559 // File_checked_for_headers