-
Notifications
You must be signed in to change notification settings - Fork 127
/
DeadSourceList.cpp
172 lines (153 loc) · 6 KB
/
DeadSourceList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//this file is part of eMule
//Copyright (C)2002-2008 Merkur ( strEmail.Format("%s@%s", "devteam", "emule-project.net") / http://www.emule-project.net )
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "StdAfx.h"
#include "deadsourcelist.h"
#include "preferences.h"
#include "opcodes.h"
#include "updownclient.h"
#include "partfile.h"
#include "Log.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define CLEANUPTIME MIN2MS(60)
#define BLOCKTIME (::GetTickCount() + (m_bGlobalList ? MIN2MS(15):MIN2MS(45)))
#define BLOCKTIMEFW (::GetTickCount() + (m_bGlobalList ? MIN2MS(30):MIN2MS(45)))
///////////////////////////////////////////////////////////////////////////////////////
//// CDeadSource
CDeadSource::CDeadSource(uint32 dwID, uint16 nPort, uint32 dwServerIP, uint16 nKadPort){
m_dwID = dwID;
m_dwServerIP = dwServerIP;
m_nPort = nPort;
m_nKadPort = nKadPort;
md4clr(m_aucHash);
}
CDeadSource::CDeadSource(const uchar* paucHash){
m_dwID = 0;
m_dwServerIP = 0;
m_nPort = 0;
m_nKadPort = 0;
md4cpy(m_aucHash, paucHash);
}
bool operator==(const CDeadSource& ds1,const CDeadSource& ds2){
//ASSERT( ((ds1.m_dwID + ds1.m_dwServerIP) ^ isnulmd4(ds1.m_aucHash)) != 0 );
//ASSERT( ((ds2.m_dwID + ds2.m_dwServerIP) ^ isnulmd4(ds2.m_aucHash)) != 0 );
return (
// lowid ed2k and highid kad + ed2k check
( (ds1.m_dwID != 0 && ds1.m_dwID == ds2.m_dwID) && ((ds1.m_nPort != 0 && ds1.m_nPort == ds2.m_nPort) || (ds1.m_nKadPort != 0 && ds1.m_nKadPort == ds2.m_nKadPort)) && (ds1.m_dwServerIP == ds2.m_dwServerIP || !IsLowID(ds1.m_dwID)) )
// lowid kad check
|| ( IsLowID(ds1.m_dwID) && isnulmd4(ds1.m_aucHash) == FALSE && md4cmp(ds1.m_aucHash, ds2.m_aucHash) == 0) );
}
CDeadSource& CDeadSource::operator=(const CDeadSource& ds){
m_dwID = ds.m_dwID;
m_dwServerIP = ds.m_dwServerIP;
m_nPort = ds.m_nPort;
m_nKadPort = ds.m_nKadPort;
md4cpy(m_aucHash, ds.m_aucHash);
return *this;
}
///////////////////////////////////////////////////////////////////////////////////////
//// CDeadSourceList
CDeadSourceList::CDeadSourceList()
{
m_dwLastCleanUp = 0;
}
CDeadSourceList::~CDeadSourceList()
{
}
void CDeadSourceList::Init(bool bGlobalList){
m_dwLastCleanUp = ::GetTickCount();
if(bGlobalList){
m_mapDeadSources.InitHashTable(3001);
}
else{
m_mapDeadSources.InitHashTable(503);
}
m_bGlobalList = bGlobalList;
}
bool CDeadSourceList::IsDeadSource(const CUpDownClient* pToCheck) const{
uint32 dwExpTime;
bool bDbgCheck = false;
if(!pToCheck->HasLowID() || pToCheck->GetServerIP() != 0){
if (m_mapDeadSources.Lookup(CDeadSource(pToCheck->GetUserIDHybrid(), pToCheck->GetUserPort(), pToCheck->GetServerIP(), pToCheck->GetKadPort()), dwExpTime)){
if (dwExpTime > ::GetTickCount())
return true;
}
bDbgCheck = true;
}
if (((pToCheck->HasValidBuddyID() || pToCheck->SupportsDirectUDPCallback()) && isnulmd4(pToCheck->GetUserHash()) == FALSE) || (pToCheck->HasLowID() && pToCheck->GetServerIP() == 0) ){
if (m_mapDeadSources.Lookup(CDeadSource(pToCheck->GetUserHash()), dwExpTime)){
if (dwExpTime > ::GetTickCount())
return true;
}
bDbgCheck = true;
}
//ASSERT ( bDbgCheck );
return false;
}
void CDeadSourceList::AddDeadSource(const CUpDownClient* pToAdd){
//if (thePrefs.GetLogFilteredIPs())
// AddDebugLogLine(DLP_VERYLOW, false, _T("Added source to bad source list (%s) - file %s : %s")
// , m_bGlobalList? _T("Global"):_T("Local"), (pToAdd->GetRequestFile() != NULL)? pToAdd->GetRequestFile()->GetFileName() : _T("???"), pToAdd->DbgGetClientInfo() );
if(!pToAdd->HasLowID())
m_mapDeadSources.SetAt(CDeadSource(pToAdd->GetUserIDHybrid(), pToAdd->GetUserPort(), pToAdd->GetServerIP(), pToAdd->GetKadPort()), BLOCKTIME );
else{
bool bDbgCheck = false;
if(pToAdd->GetServerIP() != 0){
bDbgCheck = true;
m_mapDeadSources.SetAt(CDeadSource(pToAdd->GetUserIDHybrid(), pToAdd->GetUserPort(), pToAdd->GetServerIP(), 0), BLOCKTIMEFW);
}
if (pToAdd->HasValidBuddyID() || pToAdd->SupportsDirectUDPCallback()){
bDbgCheck = true;
m_mapDeadSources.SetAt(CDeadSource(pToAdd->GetUserHash()), BLOCKTIMEFW);
}
//ASSERT( bDbgCheck );
}
if (::GetTickCount() - m_dwLastCleanUp > CLEANUPTIME)
CleanUp();
}
void CDeadSourceList::RemoveDeadSource(const CUpDownClient* client)
{
if (!client->HasLowID())
m_mapDeadSources.RemoveKey(CDeadSource(client->GetUserIDHybrid(), client->GetUserPort(), client->GetServerIP(), client->GetKadPort()));
else
{
if (client->GetServerIP() != 0)
m_mapDeadSources.RemoveKey(CDeadSource(client->GetUserIDHybrid(), client->GetUserPort(), client->GetServerIP(), 0));
if (client->HasValidBuddyID() || client->SupportsDirectUDPCallback())
m_mapDeadSources.RemoveKey(CDeadSource(client->GetUserHash()));
}
}
void CDeadSourceList::CleanUp(){
m_dwLastCleanUp = ::GetTickCount();
//if (thePrefs.GetLogFilteredIPs())
// AddDebugLogLine(DLP_VERYLOW, false, _T("Cleaning up DeadSourceList (%s), %i clients on List..."), m_bGlobalList ? _T("Global") : _T("Local"), m_mapDeadSources.GetCount());
POSITION pos = m_mapDeadSources.GetStartPosition();
CDeadSource dsKey;
uint32 dwExpTime;
uint32 dwTick = ::GetTickCount();
while (pos != NULL){
m_mapDeadSources.GetNextAssoc( pos, dsKey, dwExpTime );
if (dwExpTime < dwTick){
m_mapDeadSources.RemoveKey(dsKey);
}
}
//if (thePrefs.GetLogFilteredIPs())
// AddDebugLogLine(DLP_VERYLOW, false, _T("...done, %i clients left on list"), m_mapDeadSources.GetCount());
}