//this file is part of eMule
//Copyright (C)2002 Merkur ( devs@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.
#pragma once
#include "MapKey.h"
#pragma warning(disable:4516) // access-declarations are deprecated; member using-declarations provide a better alternative
#pragma warning(disable:4244) // conversion from 'type1' to 'type2', possible loss of data
#pragma warning(disable:4100) // unreferenced formal parameter
#include <crypto51/rsa.h>
#pragma warning(default:4100) // unreferenced formal parameter
#pragma warning(default:4244) // conversion from 'type1' to 'type2', possible loss of data
#pragma warning(default:4516) // access-declarations are deprecated; member using-declarations provide a better alternative

// NEO: MC - [MetCredits] -- Xanatos -->
class CTag;
class CFileDataIO;
// NEO: MC END <-- Xanatos --
#define	 MAXPUBKEYSIZE		80


#define CRYPT_CIP_REMOTECLIENT	10
#define CRYPT_CIP_LOCALCLIENT	20
#define CRYPT_CIP_NONECLIENT	30

#pragma pack(1)
struct CreditStruct_29a{
	uchar		abyKey[16];
	uint32		nUploadedLo;	// uploaded TO him
	uint32		nDownloadedLo;	// downloaded from him
	uint32		nLastSeen;
	uint32		nUploadedHi;	// upload high 32
	uint32		nDownloadedHi;	// download high 32
	uint16		nReserved3;
};
struct CreditStruct_30d{ // NEO: MC - [MetCredits] <-- Xanatos --
	uchar		abyKey[16];
	uint32		nUploadedLo;	// uploaded TO him
	uint32		nDownloadedLo;	// downloaded from him
	uint32		nLastSeen;
	uint32		nUploadedHi;	// upload high 32
	uint32		nDownloadedHi;	// download high 32
	uint16		nReserved3;
	uint8		nKeySize;
	uchar		abySecureIdent[MAXPUBKEYSIZE];
};
// NEO: MC - [MetCredits] -- Xanatos -->
struct CreditStruct{
	uchar		abyKey[16];
	uint64		nUploaded;	// uploaded TO him
	uint64		nDownloaded;	// downloaded from him
	uint32		nLastSeen;
	uint8		nKeySize;
	uchar		abySecureIdent[MAXPUBKEYSIZE];
	// NEO: SQ - [SaveUploadQueue]
	uint32		nSecuredWaitTime;
	uint32		nUnSecuredWaitTime;
	// NEO: SQ END
	sint64		nDelta; // NEO: NCS - [NeoCreditSystem]
};
// NEO: MC END <-- Xanatos --
#pragma pack()

enum EIdentState{
	IS_NOTAVAILABLE,
	IS_IDNEEDED,
	IS_IDENTIFIED,
	IS_IDFAILED,
	IS_IDBADGUY,
};

class CClientCredits
{
	friend class CClientCreditsList;
public:
	CClientCredits(CFileDataIO* file); // NEO: MC - [MetCredits] <-- Xanatos --
	CClientCredits(CreditStruct_30d* in_credits); // NEO: MC - [MetCredits] <-- Xanatos --
	CClientCredits(const uchar* key);
	~CClientCredits();

	// NEO: MC - [MetCredits] -- Xanatos -->
	bool	IsEmpty();
	void	WriteToFile(CFileDataIO* file);
	void	ClearTags();
	// NEO: MC END <-- Xanatos --
	const uchar* GetKey() const					{return m_pCredits->abyKey;}
	uchar*	GetSecureIdent()					{return m_abyPublicKey;}
	uint8	GetSecIDKeyLen() const				{return m_nPublicKeyLen;}
	CreditStruct* GetDataStruct() const			{return m_pCredits;}
	void	ClearWaitStartTime();
	// NEO: SQ - [SaveUploadQueue] -- Xanatos -->
	void	SaveQueueWaitTime();
	void	ClearQueueWaitTime();
	// NEO: SQ END <-- Xanatos --
	void	AddDownloaded(uint32 bytes, uint32 dwForIP, bool bHaveFile); // NEO: NCS - [NeoCreditSystem] <-- Xanatos --
	void	AddUploaded(uint32 bytes, uint32 dwForIP, bool bHaveFile); // NEO: NCS - [NeoCreditSystem] <-- Xanatos --
	uint64	GetUploadedTotal() const;
	uint64	GetDownloadedTotal() const;
	// NEO: NCS - [NeoCreditSystem] -- Xanatos -->
	sint64	GetTransferDelta(bool bSession, bool bNeg = false) const; 
	uint32	GetUploadedSession() const;
	uint32	GetDownloadedSession() const; 
	// NEO: NCS END <-- Xanatos --
	float	GetScoreRatio(uint32 dwForIP) const;
	float	GetNeoScoreRatio(bool bHaveFile, uint32 dwForIP) const; // NEO: NCS - [NeoCreditSystem] <-- Xanatos --
#ifdef ARGOS // NEO: NA - [NeoArgos] -- Xanatos -->
	float	GetArgosScoreRatio(uint32 dwForIP) const;
#endif // ARGOS // NEO: NA END <-- Xanatos --
	float	GetRemoteScoreRatio() const; // NEO: RSR - [RemoteScoreRatio] <-- Xanatos --
	uint32	GetLastSeen()					{return m_pCredits->nLastSeen;} // NEO: MC - [MetCredits] <-- Xanatos --
	void	SetLastSeen()					{m_pCredits->nLastSeen = time(NULL);}
	bool	SetSecureIdent(const uchar* pachIdent, uint8 nIdentLen); // Public key cannot change, use only if there is not public key yet
	uint32	m_dwCryptRndChallengeFor;
	uint32	m_dwCryptRndChallengeFrom;
	EIdentState	GetCurrentIdentState(uint32 dwForIP) const; // can be != IdentState
	uint32	GetSecureWaitStartTime(uint32 dwForIP);
	void	SetSecWaitStartTime(uint32 dwForIP);
	// NEO: XCTA - [XmanExtenedCreditTableArragement] -- Xanatos -->
	void	MarkToDelete() {m_bmarktodelete=true;} 
	void	UnMarkToDelete() {m_bmarktodelete=false;}
	bool	GetMarkToDelete() const {return m_bmarktodelete;} 
	uint32	GetLastSeen() const {return m_pCredits->nLastSeen;}
	// NEO: XCTA END  <-- Xanatos --

protected:
	void	Verified(uint32 dwForIP);
	EIdentState IdentState;
private:
	void			InitalizeIdent();
	CreditStruct*	m_pCredits;
	uint32			m_nUploaded; // uploaded TO him // NEO: NCS - [NeoCreditSystem] <-- Xanatos --
	uint32			m_nDownloaded; // downloaded from him // NEO: NCS - [NeoCreditSystem] <-- Xanatos --
	byte			m_abyPublicKey[80];			// even keys which are not verified will be stored here, and - if verified - copied into the struct
	uint8			m_nPublicKeyLen;
	uint32			m_dwIdentIP;
	uint32			m_dwSecureWaitTime;
	uint32			m_dwUnSecureWaitTime;
	uint32			m_dwWaitTimeIP;			   // client IP assigned to the waittime
	CArray<CTag*,CTag*> taglist; // NEO: MC - [MetCredits] <-- Xanatos --
	bool			m_bmarktodelete; // NEO: XCTA - [XmanExtenedCreditTableArragement] <-- Xanatos --
};

class CClientCreditsList
{
public:
	CClientCreditsList();
	~CClientCreditsList();
	
			// return signature size, 0 = Failed | use sigkey param for debug only
	uint8	CreateSignature(CClientCredits* pTarget, uchar* pachOutput, uint8 nMaxSize, uint32 ChallengeIP, uint8 byChaIPKind, CryptoPP::RSASSA_PKCS1v15_SHA_Signer* sigkey = NULL);
	bool	VerifyIdent(CClientCredits* pTarget, const uchar* pachSignature, uint8 nInputSize, uint32 dwForIP, uint8 byChaIPKind);
#ifdef ISPBYPASS // NEO: SCC - [SecureClinetConnection] -- Xanatos -->
	uint8	DecryptData(const uchar* secureData, UINT size, uchar*& plainData);
	uint8	EncryptData(CClientCredits* pTarget, const uchar* plainData, UINT size, uchar*& secureData);
#endif // ISPBYPASS // NEO: SCC END <-- Xanatos --

	CClientCredits* GetCredit(const uchar* key) ;
	void	Process();
	uint8	GetPubKeyLen() const			{return m_nMyPublicKeyLen;}
	byte*	GetPublicKey()					{return m_abyMyPublicKey;}
	bool	CryptoAvailable();
protected:
	bool	LoadList(); // NEO: NB - [NeoBackup] <-- Xanatos --
	void	ClearList(); // NEO: NB - [NeoBackup] <-- Xanatos --
	void	SaveList();
	void	InitalizeCrypting();
	bool	CreateKeyPair();
#ifdef _DEBUG
	bool	Debug_CheckCrypting();
#endif
private:
	CMap<CCKey, const CCKey&, CClientCredits*, CClientCredits*> m_mapClients;
	uint32			m_nLastSaved;
	CryptoPP::RSASSA_PKCS1v15_SHA_Signer*		m_pSignkey;
#ifdef ISPBYPASS // NEO: SCC - [SecureClinetConnection] -- Xanatos -->
	CryptoPP::RSAES_PKCS1v15_Decryptor*			m_pDecryptor;
#endif // ISPBYPASS // NEO: SCC END <-- Xanatos --
	byte			m_abyMyPublicKey[80];
	uint8			m_nMyPublicKeyLen;
};
