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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277 | //
// This file is part of the aMule Project.
//
// Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
// Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#include "ClientCredits.h" // Interface declarations
#include <cmath>
#include "GetTickCount.h" // Needed for GetTickCount
#include "Logger.h" // Needed for Add(Debug)LogLine
CreditStruct::CreditStruct()
: uploaded(0),
downloaded(0),
nLastSeen(0),
nReserved3(0),
nKeySize(0)
{
memset(abySecureIdent, 0, MAXPUBKEYSIZE);
}
CClientCredits::CClientCredits(CreditStruct* in_credits)
{
m_pCredits = in_credits;
InitalizeIdent();
m_dwUnSecureWaitTime = 0;
m_dwSecureWaitTime = 0;
m_dwWaitTimeIP = 0;
}
CClientCredits::CClientCredits(const CMD4Hash& key)
{
m_pCredits = new CreditStruct();<--- Class 'CClientCredits' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s).<--- Class 'CClientCredits' does not have a operator= which is recommended since it has dynamic memory/resource allocation(s).
m_pCredits->key = key;
InitalizeIdent();
m_dwUnSecureWaitTime = ::GetTickCount();
m_dwSecureWaitTime = ::GetTickCount();
m_dwWaitTimeIP = 0;
}
CClientCredits::~CClientCredits()
{
delete m_pCredits;
}
void CClientCredits::AddDownloaded(uint32 bytes, uint32 dwForIP, bool cryptoavail)
{
switch (GetCurrentIdentState(dwForIP)) {
case IS_IDFAILED:
case IS_IDBADGUY:
case IS_IDNEEDED:
if (cryptoavail) {
return;
}
break;
case IS_NOTAVAILABLE:
case IS_IDENTIFIED:
break;
}
m_pCredits->downloaded += bytes;
}
void CClientCredits::AddUploaded(uint32 bytes, uint32 dwForIP, bool cryptoavail)
{
switch (GetCurrentIdentState(dwForIP)) {
case IS_IDFAILED:
case IS_IDBADGUY:
case IS_IDNEEDED:
if (cryptoavail) {
return;
}
break;
case IS_NOTAVAILABLE:
case IS_IDENTIFIED:
break;
}
m_pCredits->uploaded += bytes;
}
uint64 CClientCredits::GetUploadedTotal() const
{
return m_pCredits->uploaded;
}
uint64 CClientCredits::GetDownloadedTotal() const
{
return m_pCredits->downloaded;
}
float CClientCredits::GetScoreRatio(uint32 dwForIP, bool cryptoavail)
{
// check the client ident status
switch (GetCurrentIdentState(dwForIP)) {
case IS_IDFAILED:
case IS_IDBADGUY:
case IS_IDNEEDED:
if (cryptoavail) {
// bad guy - no credits for you
return 1.0f;
}
break;
case IS_NOTAVAILABLE:
case IS_IDENTIFIED:
break;
}
if (GetDownloadedTotal() < 1000000) {
return 1.0f;
}
float result = 0.0f;
if (!GetUploadedTotal()) {
result = 10.0f;
} else {
result = (GetDownloadedTotal() * 2.0f) / GetUploadedTotal();
}
float result2 = sqrt((GetDownloadedTotal() / 1048576.0) + 2.0f);
if (result > result2) {
result = result2;
}
if (result < 1.0f) {
return 1.0f;
} else if (result > 10.0f) {
return 10.0f;
}
return result;
}
void CClientCredits::SetLastSeen()
{
m_pCredits->nLastSeen = time(NULL);
}
void CClientCredits::InitalizeIdent()
{
if (m_pCredits->nKeySize == 0 ){
memset(m_abyPublicKey,0,80); // for debugging
m_nPublicKeyLen = 0;
m_identState = IS_NOTAVAILABLE;
}
else{
m_nPublicKeyLen = m_pCredits->nKeySize;
memcpy(m_abyPublicKey, m_pCredits->abySecureIdent, m_nPublicKeyLen);
m_identState = IS_IDNEEDED;
}
m_dwCryptRndChallengeFor = 0;
m_dwCryptRndChallengeFrom = 0;
m_dwIdentIP = 0;
}
void CClientCredits::Verified(uint32 dwForIP)
{
m_dwIdentIP = dwForIP;
// client was verified, copy the keyto store him if not done already
if (m_pCredits->nKeySize == 0){
m_pCredits->nKeySize = m_nPublicKeyLen;
memcpy(m_pCredits->abySecureIdent, m_abyPublicKey, m_nPublicKeyLen);
if (GetDownloadedTotal() > 0){
// for security reason, we have to delete all prior credits here
// in order to save this client, set 1 byte
m_pCredits->downloaded = 1;
m_pCredits->uploaded = 1;
AddDebugLogLineN( logCredits, wxT("Credits deleted due to new SecureIdent") );
}
}
m_identState = IS_IDENTIFIED;
}
bool CClientCredits::SetSecureIdent(const uint8_t* pachIdent, uint8 nIdentLen)
{ // verified Public key cannot change, use only if there is not public key yet
if (MAXPUBKEYSIZE < nIdentLen || m_pCredits->nKeySize != 0 ) {
return false;
}
memcpy(m_abyPublicKey,pachIdent, nIdentLen);
m_nPublicKeyLen = nIdentLen;
m_identState = IS_IDNEEDED;
return true;
}
EIdentState CClientCredits::GetCurrentIdentState(uint32 dwForIP) const
{
if (m_identState != IS_IDENTIFIED)
return m_identState;
else{
if (dwForIP == m_dwIdentIP)
return IS_IDENTIFIED;
else
return IS_IDBADGUY;
// mod note: clients which just reconnected after an IP change and have to ident yet will also have this state for 1-2 seconds
// so don't try to spam such clients with "bad guy" messages (besides: spam messages are always bad)
}
}
uint32 CClientCredits::GetSecureWaitStartTime(uint32 dwForIP)
{
if (m_dwUnSecureWaitTime == 0 || m_dwSecureWaitTime == 0)
SetSecWaitStartTime(dwForIP);
if (m_pCredits->nKeySize != 0){ // this client is a SecureHash Client
if (GetCurrentIdentState(dwForIP) == IS_IDENTIFIED){ // good boy
return m_dwSecureWaitTime;
}
else{ // not so good boy
if (dwForIP == m_dwWaitTimeIP){
return m_dwUnSecureWaitTime;
}
else{ // bad boy
// this can also happen if the client has not identified himself yet, but will do later - so maybe he is not a bad boy :) .
m_dwUnSecureWaitTime = ::GetTickCount();
m_dwWaitTimeIP = dwForIP;
return m_dwUnSecureWaitTime;
}
}
}
else{ // not a SecureHash Client - handle it like before for now (no security checks)
return m_dwUnSecureWaitTime;
}
}
void CClientCredits::SetSecWaitStartTime(uint32 dwForIP)
{
m_dwUnSecureWaitTime = ::GetTickCount()-1;
m_dwSecureWaitTime = ::GetTickCount()-1;
m_dwWaitTimeIP = dwForIP;
}
void CClientCredits::ClearWaitStartTime()
{
m_dwUnSecureWaitTime = 0;
m_dwSecureWaitTime = 0;
}
// File_checked_for_headers
|