Upstream tarball 10142
[amule.git] / src / updownclient.h
blob2508cad41629ee17d924d6f72552cded51f38e02
1 //
2 // This file is part of the aMule Project.
3 //
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 )
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.
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
26 #ifndef UPDOWNCLIENT_H
27 #define UPDOWNCLIENT_H
29 #include "GetTickCount.h" // Needed for GetTickCount
30 #include "MD4Hash.h"
31 #include <common/StringFunctions.h>
32 #include "NetworkFunctions.h"
33 #include "OtherStructs.h"
34 #include "ClientCredits.h" // Needed for EIdentState
35 #include <ec/cpp/ECID.h> // Needed for CECID
37 #include <map>
40 typedef std::vector<bool> BitVector;
42 class CPartFile;
43 class CClientTCPSocket;
44 class CPacket;
45 class CFriend;
46 class CKnownFile;
47 class CMemFile;
48 class CAICHHash;
51 // uploadstate
52 #define US_UPLOADING 0
53 #define US_ONUPLOADQUEUE 1
54 #define US_WAITCALLBACK 2
55 #define US_CONNECTING 3
56 #define US_PENDING 4
57 #define US_LOWTOLOWIP 5
58 #define US_BANNED 6
59 #define US_ERROR 7
60 #define US_NONE 8
62 // downloadstate
63 enum EDownloadState {
64 DS_DOWNLOADING = 0,
65 DS_ONQUEUE,
66 DS_CONNECTED,
67 DS_CONNECTING,
68 DS_WAITCALLBACK,
69 DS_WAITCALLBACKKAD,
70 DS_REQHASHSET,
71 DS_NONEEDEDPARTS,
72 DS_TOOMANYCONNS,
73 DS_TOOMANYCONNSKAD,
74 DS_LOWTOLOWIP,
75 DS_BANNED,
76 DS_ERROR,
77 DS_NONE,
78 DS_REMOTEQUEUEFULL // not used yet, except in statistics
81 // m_byChatstate
82 enum {
83 MS_NONE = 0,
84 MS_CHATTING,
85 MS_CONNECTING,
86 MS_UNABLETOCONNECT
89 enum ESourceFrom {
90 SF_NONE,
91 SF_LOCAL_SERVER,
92 SF_REMOTE_SERVER,
93 SF_KADEMLIA,
94 SF_SOURCE_EXCHANGE,
95 SF_PASSIVE,
96 SF_LINK,
97 SF_SOURCE_SEEDS,
98 SF_SEARCH_RESULT
101 enum EChatCaptchaState {
102 CA_NONE = 0,
103 CA_CHALLENGESENT,
104 CA_CAPTCHASOLVED,
105 CA_ACCEPTING,
106 CA_CAPTCHARECV,
107 CA_SOLUTIONSENT
110 enum ESecureIdentState {
111 IS_UNAVAILABLE = 0,
112 IS_ALLREQUESTSSEND = 0,
113 IS_SIGNATURENEEDED = 1,
114 IS_KEYANDSIGNEEDED = 2
117 enum EInfoPacketState {
118 IP_NONE = 0,
119 IP_EDONKEYPROTPACK = 1,
120 IP_EMULEPROTPACK = 2,
121 IP_BOTH = 3
124 enum EKadState {
125 KS_NONE,
126 KS_QUEUED_FWCHECK,
127 KS_CONNECTING_FWCHECK,
128 KS_CONNECTED_FWCHECK,
129 KS_QUEUED_BUDDY,
130 KS_INCOMING_BUDDY,
131 KS_CONNECTING_BUDDY,
132 KS_CONNECTED_BUDDY,
133 KS_QUEUED_FWCHECK_UDP,
134 KS_FWCHECK_UDP,
135 KS_CONNECTING_FWCHECK_UDP
138 //! Used to keep track of the state of the client
139 enum ClientState
141 //! New is for clients that have just been created.
142 CS_NEW = 0,
143 //! Listed is for clients that are on the clientlist
144 CS_LISTED,
145 //! Dying signifies clients that have been queued for deletion
146 CS_DYING
149 // This is fixed on ed2k v1, but can be any number on ED2Kv2
150 #define STANDARD_BLOCKS_REQUEST 3
152 class CUpDownClient : public CECID
154 friend class CClientList;
155 friend class CUpDownClientListRem;
156 private:
158 * Please note that only the ClientList is allowed to delete the clients.
159 * To schedule a client for deletion, call the CClientList::AddToDeleteQueue
160 * funtion, which will safely remove dead clients once every second.
162 ~CUpDownClient();
164 public:
165 #ifdef CLIENT_GUI
166 CUpDownClient(class CEC_UpDownClient_Tag *);
167 #else
168 //base
169 CUpDownClient(CClientTCPSocket* sender = 0);
170 CUpDownClient(uint16 in_port, uint32 in_userid, uint32 in_serverup, uint16 in_serverport,CPartFile* in_reqfile, bool ed2kID, bool checkfriend);
171 #endif
173 * This function should be called when the client object is to be deleted.
174 * It'll close the socket of the client and add it to the deletion queue
175 * owned by the CClientList class. However, if the CUpDownClient isn't on
176 * the normal clientlist, it will be deleted immediatly.
178 * The purpose of this is to avoid clients suddenly being removed due to
179 * asyncronous events, such as socket errors, which can result in the
180 * problems, as each CUpDownClient object is often kept in multiple lists,
181 * and instantly removing the client poses the risk of invalidating
182 * currently used iterators and/or creating dangling pointers.
184 * @see CClientList::AddToDeleteQueue
185 * @see CClientList::Process
187 void Safe_Delete();
190 * Specifies if the client has been queued for deletion.
192 * @return True if Safe_Delete has been called, false otherwise.
194 bool HasBeenDeleted() { return m_clientState == CS_DYING; }
196 ClientState GetClientState() { return m_clientState; }
198 bool Disconnected(const wxString& strReason, bool bFromSocket = false);
199 bool TryToConnect(bool bIgnoreMaxCon = false);
200 bool Connect();
201 void ConnectionEstablished();
202 const wxString& GetUserName() const { return m_Username; }
203 //Only use this when you know the real IP or when your clearing it.
204 void SetIP( uint32 val );
205 uint32 GetIP() const { return m_dwUserIP; }
206 bool HasLowID() const { return IsLowID(m_nUserIDHybrid); }
207 wxString GetFullIP() const { return Uint32toStringIP(m_FullUserIP); }
208 uint32 GetConnectIP() const { return m_nConnectIP; }
209 uint32 GetUserIDHybrid() const { return m_nUserIDHybrid; }
210 void SetUserIDHybrid(uint32 val);
211 uint16_t GetUserPort() const { return m_nUserPort; }
212 void SetUserPort(uint16_t port) { m_nUserPort = port; }
213 uint64 GetTransferredDown() const { return m_nTransferredDown; }
214 uint32 GetServerIP() const { return m_dwServerIP; }
215 void SetServerIP(uint32 nIP) { m_dwServerIP = nIP; }
216 uint16 GetServerPort() const { return m_nServerPort; }
217 void SetServerPort(uint16 nPort) { m_nServerPort = nPort; }
218 const CMD4Hash& GetUserHash() const { return m_UserHash; }
219 void SetUserHash(const CMD4Hash& userhash);
220 void ValidateHash() { m_HasValidHash = !m_UserHash.IsEmpty(); }
221 bool HasValidHash() const { return m_HasValidHash; }
222 uint32 GetVersion() const { return m_nClientVersion;}
223 uint8 GetMuleVersion() const { return m_byEmuleVersion;}
224 bool ExtProtocolAvailable() const { return m_bEmuleProtocol;}
225 bool IsEmuleClient() const { return (m_byEmuleVersion > 0);}
226 bool IsBanned() const;
227 const wxString& GetClientFilename() const { return m_clientFilename; }
228 uint16 GetUDPPort() const { return m_nUDPPort; }
229 void SetUDPPort(uint16 nPort) { m_nUDPPort = nPort; }
230 uint8 GetUDPVersion() const { return m_byUDPVer; }
231 uint8 GetExtendedRequestsVersion() const { return m_byExtendedRequestsVer; }
232 bool IsFriend() const { return m_Friend != NULL; }
233 bool IsML() const { return m_bIsML; }
234 bool IsHybrid() const { return m_bIsHybrid; }
235 uint32 GetCompatibleClient() const { return m_byCompatibleClient; }
237 void ClearDownloadBlockRequests();
238 void RequestSharedFileList();
239 void ProcessSharedFileList(const byte* pachPacket, uint32 nSize, wxString& pszDirectory);
241 wxString GetUploadFileInfo();
243 void SetUserName(const wxString& NewName) { m_Username = NewName; }
245 uint8 GetClientSoft() const { return m_clientSoft; }
246 void ReGetClientSoft();
247 bool ProcessHelloAnswer(const byte* pachPacket, uint32 nSize);
248 bool ProcessHelloPacket(const byte* pachPacket, uint32 nSize);
249 void SendHelloAnswer();
250 bool SendHelloPacket();
251 void SendMuleInfoPacket(bool bAnswer, bool OSInfo = false);
252 bool ProcessMuleInfoPacket(const byte* pachPacket, uint32 nSize);
253 void ProcessMuleCommentPacket(const byte* pachPacket, uint32 nSize);
254 bool Compare(const CUpDownClient* tocomp, bool bIgnoreUserhash = false) const;
255 void SetLastSrcReqTime() { m_dwLastSourceRequest = ::GetTickCount(); }
256 void SetLastSrcAnswerTime() { m_dwLastSourceAnswer = ::GetTickCount(); }
257 void SetLastAskedForSources() { m_dwLastAskedForSources = ::GetTickCount(); }
258 uint32 GetLastSrcReqTime() const { return m_dwLastSourceRequest; }
259 uint32 GetLastSrcAnswerTime() const { return m_dwLastSourceAnswer; }
260 uint32 GetLastAskedForSources() const { return m_dwLastAskedForSources; }
261 bool GetFriendSlot() const { return m_bFriendSlot; }
262 void SetFriendSlot(bool bNV) { m_bFriendSlot = bNV; }
263 void SetCommentDirty(bool bDirty = true) { m_bCommentDirty = bDirty; }
264 uint8 GetSourceExchange1Version() const { return m_bySourceExchange1Ver; }
265 bool SupportsSourceExchange2() const { return m_fSupportsSourceEx2; }
267 bool SafeSendPacket(CPacket* packet);
269 void ProcessRequestPartsPacket(const byte* pachPacket, uint32 nSize, bool largeblocks);
270 void ProcessRequestPartsPacketv2(const CMemFile& data);
272 void SendPublicKeyPacket();
273 void SendSignaturePacket();
274 void ProcessPublicKeyPacket(const byte* pachPacket, uint32 nSize);
275 void ProcessSignaturePacket(const byte* pachPacket, uint32 nSize);
276 uint8 GetSecureIdentState();
278 void SendSecIdentStatePacket();
279 void ProcessSecIdentStatePacket(const byte* pachPacket, uint32 nSize);
281 uint8 GetInfoPacketsReceived() const { return m_byInfopacketsReceived; }
282 void InfoPacketsReceived();
284 //upload
285 uint8 GetUploadState() const { return m_nUploadState; }
286 void SetUploadState(uint8 news);
287 uint32 GetTransferredUp() const { return m_nTransferredUp; }
288 uint32 GetSessionUp() const { return m_nTransferredUp - m_nCurSessionUp; }
289 void ResetSessionUp();
290 uint32 GetUploadDatarate() const { return m_nUpDatarate; }
292 #ifndef CLIENT_GUI
293 uint32 GetWaitTime() const { return m_dwUploadTime - GetWaitStartTime(); }
294 uint32 GetUpStartTimeDelay() const { return ::GetTickCount() - m_dwUploadTime; }
295 uint32 GetWaitStartTime() const;
296 #else
297 uint32 m_WaitTime, m_UpStartTimeDelay, m_WaitStartTime;
298 uint32 GetWaitTime() const { return m_WaitTime; }
299 uint32 GetUpStartTimeDelay() const { return m_UpStartTimeDelay; }
300 uint32 GetWaitStartTime() const { return m_WaitStartTime; }
301 #endif
303 bool IsDownloading() const { return (m_nUploadState == US_UPLOADING); }
305 #ifndef CLIENT_GUI
306 uint32 GetScore(
307 bool sysvalue,
308 bool isdownloading = false,
309 bool onlybasevalue = false) const;
310 uint32 GetRating() const { return GetScore(false, IsDownloading(), true); }
311 #else
312 uint32 m_score;
313 uint32 GetScore(
314 bool WXUNUSED(sysvalue),
315 bool WXUNUSED(isdownloading) = false,
316 bool onlybasevalue = false) const
318 return onlybasevalue ? m_rating : m_score;
320 uint16 m_waitingPosition;
321 uint16 GetWaitingPosition() const { return m_waitingPosition; }
322 uint32 m_rating;
323 uint32 GetRating() const { return m_rating; }
324 EIdentState m_identState;
325 #endif
327 void AddReqBlock(Requested_Block_Struct* reqblock);
328 void CreateNextBlockPackage();
329 void SetUpStartTime() { m_dwUploadTime = ::GetTickCount(); }
330 void SetWaitStartTime();
331 void ClearWaitStartTime();
332 void SendHashsetPacket(const CMD4Hash& forfileid);
333 bool SupportMultiPacket() const { return m_bMultiPacket; }
334 bool SupportExtMultiPacket() const { return m_fExtMultiPacket; }
336 void SetUploadFileID(CKnownFile *newreqfile);
339 *Gets the file actually on upload
342 const CKnownFile* GetUploadFile() const { return m_uploadingfile; }
344 void SendOutOfPartReqsAndAddToWaitingQueue();
345 void ProcessExtendedInfo(const CMemFile *data, CKnownFile *tempreqfile);
346 void ProcessFileInfo(const CMemFile* data, const CPartFile* file);
347 void ProcessFileStatus(bool bUdpPacket, const CMemFile* data, const CPartFile* file);
349 const CMD4Hash& GetUploadFileID() const { return m_requpfileid; }
350 void SetUploadFileID(const CMD4Hash& new_id);
351 void ClearUploadFileID() { m_requpfileid.Clear(); m_uploadingfile = NULL;};
352 uint32 SendBlockData();
353 void ClearUploadBlockRequests();
354 #ifndef CLIENT_GUI
355 uint16 GetRankingInfo() const;
356 #else
357 uint16 m_rankingInfo;
358 uint16 GetRankingInfo() const { return m_rankingInfo; }
359 #endif
360 void SendRankingInfo();
361 void SendCommentInfo(CKnownFile *file);
362 bool IsDifferentPartBlock() const;
363 void UnBan();
364 void Ban();
365 bool m_bAddNextConnect; // VQB Fix for LowID slots only on connection
366 uint32 GetAskedCount() const { return m_cAsked; }
367 void AddAskedCount() { m_cAsked++; }
368 void ClearAskedCount() { m_cAsked = 1; } // 1, because it's cleared *after* the first request...
369 void FlushSendBlocks(); // call this when you stop upload,
370 // or the socket might be not able to send
371 void SetLastUpRequest() { m_dwLastUpRequest = ::GetTickCount(); }
372 uint32 GetLastUpRequest() const { return m_dwLastUpRequest; }
373 size_t GetUpPartCount() const { return m_upPartStatus.size(); }
376 //download
377 void SetRequestFile(CPartFile* reqfile);
378 CPartFile* GetRequestFile() const { return m_reqfile; }
380 uint8 GetDownloadState() const { return m_nDownloadState; }
381 void SetDownloadState(uint8 byNewState);
382 uint32 GetLastAskedTime() const { return m_dwLastAskedTime; }
383 void ResetLastAskedTime() { m_dwLastAskedTime = 0; }
385 bool IsPartAvailable(uint16 iPart) const
386 { return ( iPart < m_downPartStatus.size() ) ? m_downPartStatus[iPart] : 0; }
387 bool IsUpPartAvailable(uint16 iPart) const
388 { return ( iPart < m_upPartStatus.size() ) ? m_upPartStatus[iPart] : 0;}
390 const BitVector& GetPartStatus() const { return m_downPartStatus; }
391 const BitVector& GetUpPartStatus() const { return m_upPartStatus; }
392 float GetKBpsDown() const { return kBpsDown; }
393 float CalculateKBpsDown();
394 uint16 GetRemoteQueueRank() const { return m_nRemoteQueueRank; }
395 uint16 GetOldRemoteQueueRank() const { return m_nOldRemoteQueueRank; }
396 void SetRemoteQueueFull(bool flag) { m_bRemoteQueueFull = flag; }
397 bool IsRemoteQueueFull() const { return m_bRemoteQueueFull; }
398 void SetRemoteQueueRank(uint16 nr);
399 bool AskForDownload();
400 void SendStartupLoadReq();
401 void SendFileRequest();
402 void ProcessHashSet(const byte* packet, uint32 size);
403 bool AddRequestForAnotherFile(CPartFile* file);
404 bool DeleteFileRequest(CPartFile* file);
405 void DeleteAllFileRequests();
406 void SendBlockRequests();
407 void ProcessBlockPacket(const byte* packet, uint32 size, bool packed, bool largeblocks);
409 #ifndef CLIENT_GUI
410 uint16 GetAvailablePartCount() const;
411 #else
412 uint16 m_AvailPartCount;
413 uint16 GetAvailablePartCount() const { return m_AvailPartCount; }
414 #endif
416 bool SwapToAnotherFile(bool bIgnoreNoNeeded, bool ignoreSuspensions, bool bRemoveCompletely, CPartFile* toFile = NULL);
417 void UDPReaskACK(uint16 nNewQR);
418 void UDPReaskFNF();
419 void UDPReaskForDownload();
420 bool IsSourceRequestAllowed();
421 uint16 GetUpCompleteSourcesCount() const { return m_nUpCompleteSourcesCount; }
422 void SetUpCompleteSourcesCount(uint16 n) { m_nUpCompleteSourcesCount = n; }
424 //chat
425 uint8 GetChatState() { return m_byChatstate; }
426 void SetChatState(uint8 nNewS) { m_byChatstate = nNewS; }
427 EChatCaptchaState GetChatCaptchaState() const { return (EChatCaptchaState)m_nChatCaptchaState; }
428 void ProcessCaptchaRequest(CMemFile* data);
429 void ProcessCaptchaReqRes(uint8 nStatus);
430 void ProcessChatMessage(wxString message);
431 // message filtering
432 uint8 GetMessagesReceived() const { return m_cMessagesReceived; }
433 void IncMessagesReceived() { m_cMessagesReceived < 255 ? ++m_cMessagesReceived : 255; }
434 uint8 GetMessagesSent() const { return m_cMessagesSent; }
435 void IncMessagesSent() { m_cMessagesSent < 255 ? ++m_cMessagesSent : 255; }
436 bool IsSpammer() const { return m_fIsSpammer; }
437 void SetSpammer(bool bVal);
438 bool IsMessageFiltered(const wxString& message);
440 //File Comment
441 const wxString& GetFileComment() const { return m_strComment; }
442 uint8 GetFileRating() const { return m_iRating; }
444 const wxString& GetSoftStr() const { return m_clientSoftString; }
445 const wxString& GetSoftVerStr() const { return m_clientVerString; }
446 #ifndef CLIENT_GUI
447 const wxString GetServerName() const;
448 #else
449 wxString m_ServerName;
450 const wxString& GetServerName() const { return m_ServerName; }
451 #endif
453 uint16 GetKadPort() const { return m_nKadPort; }
454 void SetKadPort(uint16 nPort) { m_nKadPort = nPort; }
456 // Kry - AICH import
457 void SetReqFileAICHHash(CAICHHash* val);
458 CAICHHash* GetReqFileAICHHash() const {return m_pReqFileAICHHash;}
459 bool IsSupportingAICH() const {return m_fSupportsAICH & 0x01;}
460 void SendAICHRequest(CPartFile* pForFile, uint16 nPart);
461 bool IsAICHReqPending() const {return m_fAICHRequested; }
462 void ProcessAICHAnswer(const byte* packet, uint32 size);
463 void ProcessAICHRequest(const byte* packet, uint32 size);
464 void ProcessAICHFileHash(CMemFile* data, const CPartFile* file);
466 EUtf8Str GetUnicodeSupport() const;
468 // Barry - Process zip file as it arrives, don't need to wait until end of block
469 int unzip(Pending_Block_Struct *block, byte *zipped, uint32 lenZipped, byte **unzipped, uint32 *lenUnzipped, int iRecursion = 0);
470 // Barry - Sets string to show parts downloading, eg NNNYNNNNYYNYN
471 wxString ShowDownloadingParts() const;
472 void UpdateDisplayedInfo(bool force = false);
473 int GetFileListRequested() const { return m_iFileListRequested; }
474 void SetFileListRequested(int iFileListRequested) { m_iFileListRequested = iFileListRequested; }
476 void ResetFileStatusInfo();
478 bool CheckHandshakeFinished() const;
480 bool GetSentCancelTransfer() const { return m_fSentCancelTransfer; }
481 void SetSentCancelTransfer(bool bVal) { m_fSentCancelTransfer = bVal; }
483 wxString GetClientFullInfo();
484 wxString GetClientShortInfo();
486 const wxString& GetClientOSInfo() const { return m_sClientOSInfo; }
488 void ProcessPublicIPAnswer(const byte* pbyData, uint32 uSize);
489 void SendPublicIPRequest();
492 * Sets the current socket of the client.
494 * @param socket The pointer to the new socket, can be NULL.
496 * Please note that this function DOES NOT delete the old socket.
498 void SetSocket(CClientTCPSocket* socket);
501 * Function for accessing the socket owned by a client.
503 * @return The pointer (can be NULL) to the socket used by this client.
505 * Please note that the socket object is quite volatile and can be removed
506 * from one function call to the next, therefore, you should normally use
507 * the safer functions below, which all check if the socket is valid before
508 * deferring it.
510 CClientTCPSocket* GetSocket() const { return m_socket; }
513 * Safe function for checking if the socket is connected.
515 * @return True if the socket exists and is connected, false otherwise.
517 #ifndef CLIENT_GUI
518 bool IsConnected() const;
519 #else
520 bool m_IsConnected;
521 bool IsConnected() const { return m_IsConnected; }
522 #endif
525 * Safe function for sending packets.
527 * @return True if the socket exists and the packet was sent, false otherwise.
529 bool SendPacket(CPacket* packet, bool delpacket = true, bool controlpacket = true);
532 * Safe function for setting the download limit of the socket.
534 * @return Current download speed of the client.
536 float SetDownloadLimit(uint32 reducedownload);
539 * Sends a message to a client
541 * @return True if sent, false if connecting
543 bool SendChatMessage(const wxString& message);
545 bool HasBlocks() const { return !m_BlockRequests_queue.empty(); }
547 /* Source comes from? */
548 ESourceFrom GetSourceFrom() const { return (ESourceFrom)m_nSourceFrom; }
549 void SetSourceFrom(ESourceFrom val) { m_nSourceFrom = val; }
551 /* Kad buddy support */
552 // ID
553 const byte* GetBuddyID() const { return m_achBuddyID; }
554 void SetBuddyID(const byte* m_achTempBuddyID);
555 bool HasValidBuddyID() const { return m_bBuddyIDValid; }
556 /* IP */
557 void SetBuddyIP( uint32 val ) { m_nBuddyIP = val; }
558 uint32 GetBuddyIP() const { return m_nBuddyIP; }
559 /* Port */
560 void SetBuddyPort( uint16 val ) { m_nBuddyPort = val; }
561 uint16 GetBuddyPort() const { return m_nBuddyPort; }
563 //KadIPCheck
564 bool SendBuddyPingPong() { return m_dwLastBuddyPingPongTime < ::GetTickCount(); }
565 bool AllowIncomeingBuddyPingPong() { return m_dwLastBuddyPingPongTime < (::GetTickCount()-(3*60*1000)); }
566 void SetLastBuddyPingPongTime() { m_dwLastBuddyPingPongTime = (::GetTickCount()+(10*60*1000)); }
567 EKadState GetKadState() const { return m_nKadState; }
568 void SetKadState(EKadState nNewS) { m_nKadState = nNewS; }
569 uint8 GetKadVersion() { return m_byKadVersion; }
570 void ProcessFirewallCheckUDPRequest(CMemFile *data);
571 // Kad added by me
572 bool SendBuddyPing();
574 /* Returns the client hash type (SO_EMULE, mldonkey, etc) */
575 int GetHashType() const;
578 * Checks that a client isn't aggressively re-asking for files.
580 * Call this when a file is requested. If the time since the last request is
581 * less than MIN_REQUESTTIME, 3 is added to the m_Aggressiveness variable.
582 * If the time since the last request is >= MIN_REQUESTTIME, the variable is
583 * decremented by 1. The client is banned if the variable reaches 10 or above.
585 * To check if a client is aggressive use the IsClientAggressive() function.
587 * Currently this function is called when the following packets are received:
588 * - OP_STARTUPLOADREQ
589 * - OP_REASKFILEPING
591 void CheckForAggressive();
593 const wxString& GetClientModString() const { return m_strModVersion; }
595 const wxString& GetClientVerString() const { return m_fullClientVerString; }
597 const wxString& GetVersionString() const { return m_clientVersionString; }
599 void UpdateStats();
601 /* Returns a pointer to the credits, only for hash purposes */
602 void* GetCreditsHash() const { return (void*)credits; }
604 uint32 GetLastBlockOffset() const { return m_nLastBlockOffset; }
606 bool GetOSInfoSupport() const { return m_fOsInfoSupport; }
608 bool GetVBTTags() const { return m_fValueBasedTypeTags; }
610 uint16 GetLastPartAsked() const { return m_lastPartAsked; }
612 void SetLastPartAsked(uint16 nPart) { m_lastPartAsked = nPart; }
614 CFriend* GetFriend() const { return m_Friend; }
616 void SetFriend(CFriend* newfriend) { m_Friend = newfriend; }
618 bool IsIdentified() const;
620 bool IsBadGuy() const;
622 bool SUIFailed() const;
624 bool SUINeeded() const;
626 bool SUINotSupported() const;
628 uint64 GetDownloadedTotal() const;
630 uint64 GetUploadedTotal() const;
632 double GetScoreRatio() const;
634 uint32 GetCreationTime() const { return m_nCreationTime; }
636 bool SupportsLargeFiles() const { return m_fSupportsLargeFiles; }
638 EIdentState GetCurrentIdentState() const { return credits ? credits->GetCurrentIdentState(GetIP()) : IS_NOTAVAILABLE; }
640 #ifdef __DEBUG__
641 /* Kry - Debug. See connection_reason definition comment below */
642 void SetConnectionReason(const wxString& reason) { connection_reason = reason; }
643 #endif
645 // Encryption / Obfuscation / ConnectOptions
646 bool SupportsCryptLayer() const { return m_fSupportsCryptLayer; }
647 bool RequestsCryptLayer() const { return SupportsCryptLayer() && m_fRequestsCryptLayer; }
648 bool RequiresCryptLayer() const { return RequestsCryptLayer() && m_fRequiresCryptLayer; }
649 bool SupportsDirectUDPCallback() const { return m_fDirectUDPCallback != 0 && HasValidHash() && GetKadPort() != 0; }
650 uint32_t GetDirectCallbackTimeout() const { return m_dwDirectCallbackTimeout; }
651 bool HasObfuscatedConnectionBeenEstablished() const { return m_hasbeenobfuscatinglately; }
653 void SetCryptLayerSupport(bool bVal) { m_fSupportsCryptLayer = bVal ? 1 : 0; }
654 void SetCryptLayerRequest(bool bVal) { m_fRequestsCryptLayer = bVal ? 1 : 0; }
655 void SetCryptLayerRequires(bool bVal) { m_fRequiresCryptLayer = bVal ? 1 : 0; }
656 void SetDirectUDPCallbackSupport(bool bVal) { m_fDirectUDPCallback = bVal ? 1 : 0; }
657 void SetConnectOptions(uint8_t options, bool encryption = true, bool callback = true); // shortcut, sets crypt, callback, etc from the tagvalue we receive
658 bool ShouldReceiveCryptUDPPackets() const;
660 bool HasDisabledSharedFiles() const { return m_fNoViewSharedFiles; }
662 private:
664 CClientCredits *credits;
665 CFriend *m_Friend;
667 uint64 m_nTransferredUp;
668 sint64 m_nCurQueueSessionPayloadUp;
669 sint64 m_addedPayloadQueueSession;
671 struct TransferredData {
672 uint32 datalen;
673 uint32 timestamp;
676 //////////////////////////////////////////////////////////
677 // Upload data rate computation
679 uint32 m_nUpDatarate;
680 uint32 m_nSumForAvgUpDataRate;
681 std::list<TransferredData> m_AvarageUDR_list;
685 * This struct is used to keep track of CPartFiles which this source shares.
687 struct A4AFStamp {
688 //! Signifies if this sources has needed parts for this file.
689 bool NeededParts;
690 //! This is set when we wish to avoid swapping to this file for a while.
691 uint32 timestamp;
694 //! I typedef in the name of readability!
695 typedef std::map<CPartFile*, A4AFStamp> A4AFList;
696 //! This list contains all PartFiles which this client can be used as a source for.
697 A4AFList m_A4AF_list;
700 * Helper function used by SwapToAnotherFile().
702 * @param it The iterator of the PartFile to be examined.
703 * @param ignorenoneeded Do not check for the status NoNeededParts when checking the file.
704 * @param ignoresuspended Do not check the timestamp when checking the file.
705 * @return True if the file is a viable target, false otherwise.
707 * This function is used to perform checks to see if we should consider
708 * this file a viable target for A4AF swapping. Unless ignoresuspended is
709 * true, it will examine the timestamp of the file and reset it if needed.
711 bool IsValidSwapTarget( A4AFList::iterator it, bool ignorenoneeded = false, bool ignoresuspended = false );
713 CPartFile* m_reqfile;
715 // base
716 void Init();
717 bool ProcessHelloTypePacket(const CMemFile& data);
718 void SendHelloTypePacket(CMemFile* data);
719 void SendFirewallCheckUDPRequest();
720 void ClearHelloProperties(); // eMule 0.42
722 uint32 m_dwUserIP;
723 uint32 m_nConnectIP; // holds the supposed IP or (after we had a connection) the real IP
724 uint32 m_dwServerIP;
725 uint32 m_nUserIDHybrid;
726 uint16_t m_nUserPort;
727 int16 m_nServerPort;
728 uint32 m_nClientVersion;
729 uint32 m_cSendblock;
730 uint8 m_byEmuleVersion;
731 uint8 m_byDataCompVer;
732 bool m_bEmuleProtocol;
733 wxString m_Username;
734 uint32 m_FullUserIP;
735 CMD4Hash m_UserHash;
736 bool m_HasValidHash;
737 uint16 m_nUDPPort;
738 uint8 m_byUDPVer;
739 uint8 m_bySourceExchange1Ver;
740 uint8 m_byAcceptCommentVer;
741 uint8 m_byExtendedRequestsVer;
742 uint8 m_clientSoft;
743 uint32 m_dwLastSourceRequest;
744 uint32 m_dwLastSourceAnswer;
745 uint32 m_dwLastAskedForSources;
746 int m_iFileListRequested;
747 bool m_bFriendSlot;
748 bool m_bCommentDirty;
749 bool m_bIsHybrid;
750 bool m_bIsML;
751 bool m_bSupportsPreview;
752 bool m_bUnicodeSupport;
753 uint16 m_nKadPort;
754 bool m_bMultiPacket;
755 ClientState m_clientState;
756 CClientTCPSocket* m_socket;
757 bool m_fNeedOurPublicIP; // we requested our IP from this client
759 // Kry - Secure User Ident import
760 ESecureIdentState m_SecureIdentState;
761 uint8 m_byInfopacketsReceived; // have we received the edonkeyprot and emuleprot packet already (see InfoPacketsReceived() )
762 uint32 m_dwLastSignatureIP;
763 uint8 m_bySupportSecIdent;
765 uint32 m_byCompatibleClient;
766 std::list<CPacket*> m_WaitingPackets_list;
767 uint32 m_lastRefreshedDLDisplay;
769 //upload
770 void CreateStandartPackets(const unsigned char* data,uint32 togo, Requested_Block_Struct* currentblock);
771 void CreatePackedPackets(const unsigned char* data,uint32 togo, Requested_Block_Struct* currentblock);
773 uint8 m_nUploadState;
774 uint32 m_dwUploadTime;
775 uint32 m_cAsked;
776 uint32 m_dwLastUpRequest;
777 uint32 m_nCurSessionUp;
778 uint16 m_nUpPartCount;
779 CMD4Hash m_requpfileid;
780 uint16 m_nUpCompleteSourcesCount;
782 //! This vector contains the avilability of parts for the file that the user
783 //! is requesting. When changing it, be sure to call CKnownFile::UpdatePartsFrequency
784 //! so that the files know the actual availability of parts.
785 BitVector m_upPartStatus;
786 uint16 m_lastPartAsked;
787 wxString m_strModVersion;
789 std::list<Requested_Block_Struct*> m_BlockRequests_queue;
790 std::list<Requested_Block_Struct*> m_DoneBlocks_list;
792 //download
793 bool m_bRemoteQueueFull;
794 uint8 m_nDownloadState;
795 uint16 m_nPartCount;
796 uint32 m_dwLastAskedTime;
797 wxString m_clientFilename;
798 uint64 m_nTransferredDown;
799 uint32 m_nLastBlockOffset; // Patch for show parts that you download [Cax2]
800 uint16 m_cShowDR;
801 uint32 m_dwLastBlockReceived;
802 uint16 m_nRemoteQueueRank;
803 uint16 m_nOldRemoteQueueRank;
804 bool m_bCompleteSource;
805 bool m_bReaskPending;
806 bool m_bUDPPending;
807 bool m_bHashsetRequested;
809 std::list<Pending_Block_Struct*> m_PendingBlocks_list;
810 std::list<Requested_Block_Struct*> m_DownloadBlocks_list;
812 // download speed calculation
813 float kBpsDown;
814 uint32 msReceivedPrev;
815 uint32 bytesReceivedCycle;
816 // chat
817 wxString m_strComment;
818 uint8 m_byChatstate;
819 uint8 m_nChatCaptchaState;
820 uint8 m_cCaptchasSent;
821 int8 m_iRating;
822 uint8 m_cMessagesReceived; // count of chatmessages he sent to me
823 uint8 m_cMessagesSent; // count of chatmessages I sent to him
824 wxString m_strCaptchaChallenge;
825 wxString m_strCaptchaPendingMsg;
827 unsigned int
828 m_fHashsetRequesting : 1, // we have sent a hashset request to this client
829 m_fNoViewSharedFiles : 1, // client has disabled the 'View Shared Files' feature,
830 // if this flag is not set, we just know that we don't know
831 // for sure if it is enabled
832 m_fSupportsPreview : 1,
833 m_fIsSpammer : 1,
834 m_fSentCancelTransfer: 1, // we have sent an OP_CANCELTRANSFER in the current connection
835 m_fSharedDirectories : 1, // client supports OP_ASKSHAREDIRS opcodes
836 m_fSupportsAICH : 3,
837 m_fAICHRequested : 1,
838 m_fSupportsLargeFiles: 1,
839 m_fSentOutOfPartReqs : 1,
840 m_fExtMultiPacket : 1,
841 m_fRequestsCryptLayer: 1,
842 m_fSupportsCryptLayer: 1,
843 m_fRequiresCryptLayer: 1,
844 m_fSupportsSourceEx2 : 1,
845 m_fSupportsCaptcha : 1,
846 m_fDirectUDPCallback : 1;
848 unsigned int
849 m_fOsInfoSupport : 1,
850 m_fValueBasedTypeTags : 1;
852 /* Razor 1a - Modif by MikaelB */
854 bool m_bHelloAnswerPending;
856 //! This vector contains the avilability of parts for the file we requested
857 //! from this user. When changing it, be sure to call CPartFile::UpdatePartsFrequency
858 //! so that the files know the actual availability of parts.
859 BitVector m_downPartStatus;
861 CAICHHash* m_pReqFileAICHHash;
863 ESourceFrom m_nSourceFrom;
865 /* Kad Stuff */
866 byte m_achBuddyID[16];
867 bool m_bBuddyIDValid;
868 uint32 m_nBuddyIP;
869 uint16 m_nBuddyPort;
871 EKadState m_nKadState;
873 uint8 m_byKadVersion;
874 uint32 m_dwLastBuddyPingPongTime;
875 uint32_t m_dwDirectCallbackTimeout;
877 //! This keeps track of aggressive requests for files.
878 uint16 m_Aggressiveness;
879 //! This tracks the time of the last time since a file was requested
880 uint32 m_LastFileRequest;
882 bool m_OSInfo_sent;
884 wxString m_clientSoftString; /* software name */
885 wxString m_clientVerString; /* version + optional mod name */
886 wxString m_clientVersionString; /* version string */
887 wxString m_fullClientVerString; /* full info string */
888 wxString m_sClientOSInfo;
889 wxString m_pendingMessage;
891 int SecIdentSupRec;
893 CKnownFile* m_uploadingfile;
895 uint8 m_MaxBlockRequests;
897 // needed for stats
898 uint32 m_lastClientSoft;
899 uint32 m_lastClientVersion;
900 wxString m_lastOSInfo;
902 /* For buddies timeout */
903 uint32 m_nCreationTime;
905 /* Calculation of last average speed */
906 uint32 m_lastaverage;
907 uint32 m_last_block_start;
909 /* Save the encryption status for display when disconnected */
910 bool m_hasbeenobfuscatinglately;
912 /* Kry - Debug thing. Clients created just to check their data
913 have this string set to the reason we want to check them.
914 Obviously, once checked, we disconnect them. Take that, sucker.
915 This debug code is just for me I'm afraid. */
916 #ifdef __DEBUG__
917 wxString connection_reason;
918 #endif
922 #define MAKE_CLIENT_VERSION(mjr, min, upd) \
923 ((uint32)(mjr)*100U*10U*100U + (uint32)(min)*100U*10U + (uint32)(upd)*100U)
926 #endif // UPDOWNCLIENT_H
927 // File_checked_for_headers