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