//this file is part of eMule
//Copyright (C)2002-2006 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 "emule.h"
#include "ServerConnect.h"
#include "Opcodes.h"
#include "UDPSocket.h"
#include "Exceptions.h"
#include "OtherFunctions.h"
#include "Statistics.h"
#include "ServerSocket.h"
#include "ServerList.h"
#include "Server.h"
#include "ListenSocket.h"
#include "SafeFile.h"
#include "Packets.h"
#include "SharedFileList.h"
#include "PeerCacheFinder.h"
#include "emuleDlg.h"
#include "SearchDlg.h"
#include "ServerWnd.h"
#include "TaskbarNotifier.h"
#include "Log.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


// CServerConnect
// MOD: NEW - [NewCoded] -- Loulach -->
/* David: I had some free time so I recoded this class a litle bit, it should be now better 
*                              and the memlake when shuting down while connecting should be also fixed...
*/

void CServerConnect::TryAnotherConnectionrequest()
{
	CServer* next_server;
	while((next_server = used_list->GetNextServer()) != NULL)
	{
		// Barry - Only auto-connect to static server option
		if (thePrefs.GetAutoConnectToStaticServersOnly() && !next_server->IsStaticMember())
			continue;
		if (IsLocalServer(next_server->GetIP(),next_server->GetPort(),true))
			continue;
		break;
	}

	if (next_server == NULL)
	{
		if (connectionattemps.GetCount() == 0)
		{
			if (m_uOutOfServers == 0)
			{
				// 05-Nov-2003: If we have a very short server list, we could put serious load on those few servers
				// if we start the next connection tries without waiting.
				LogWarning(LOG_STATUSBAR, GetResString(IDS_OUTOFSERVERS));
				AddLogLine(false, GetResString(IDS_RECONNECT), CS_RETRYCONNECTTIME);
				m_uOutOfServers = ::GetTickCount();
			}
		}
		return;
	}

	ConnectToServer(next_server, true);
}

void CServerConnect::ConnectToAnyServer(uint32 startAt, bool prioSort, bool isAuto)
{
	//StopConnectionTry();
	//Disconnect();
	connecting = true;
	if(isAuto)
		autoretry = true;
	m_uOutOfServers = 0;
	theApp.emuledlg->ShowConnectionState();

	// Barry - Only auto-connect to static server option
	if (thePrefs.GetAutoConnectToStaticServersOnly() /*&& isAuto*/)
	{
		bool anystatic = false;
		CServer *next_server; 
		used_list->SetServerPosition( startAt );
		while ((next_server = used_list->GetNextServer()) != NULL)
		{
			if (next_server->IsStaticMember())
			{
				anystatic = true;
				break;
			}
		}
		if (!anystatic)
		{
			connecting = false;
			autoretry = false;
			LogError(LOG_STATUSBAR, GetResString(IDS_ERR_NOVALIDSERVERSFOUND));
			return;
		}
	}

	used_list->SetServerPosition(startAt);
	if (thePrefs.GetUseServerPriorities() && prioSort)
		used_list->Sort();

	if (used_list->GetServerCount() == 0){
		connecting = false;
		autoretry = false;
		LogError(LOG_STATUSBAR, GetResString(IDS_ERR_NOVALIDSERVERSFOUND));
		return;
	}
	theApp.listensocket->Process();

	TryAnotherConnectionrequest();
}

void CServerConnect::ConnectToServer(CServer* server, bool multiconnect)
{
	if(!multiconnect){
		autoretry = false;
		if (IsLocalServer(server->GetIP(),server->GetPort(),true))
			return;
		{
			StopConnectionTry();
			Disconnect();
		}
	}

	connecting = true;
	theApp.emuledlg->ShowConnectionState();

	CServerSocket* newsocket = new CServerSocket(this);
	newsocket->Create(0,SOCK_STREAM,FD_READ|FD_WRITE|FD_CLOSE|FD_CONNECT,CT2CA(theApp.GetBindAddress())); // MOD: MOD - [BindToAdapter] <-- Loulach --
	newsocket->ConnectToServer(server);
	connectionattemps.AddTail(newsocket);
}

void CServerConnect::StopConnectionTry()
{
	connecting = false;
	autoretry = false;
	theApp.emuledlg->ShowConnectionState();
	m_uOutOfServers = 0;

	// close all currenty opened sockets except the one which is connected to our current server
	for(POSITION pos = connectionattemps.GetHeadPosition();pos!=NULL;){
		CServerSocket* tmpsock = connectionattemps.GetNext(pos);

		if (tcpsocket == tmpsock)		// don't destroy socket which is connected to server
			continue;
		if (tmpsock->m_bIsDeleting == false)	// don't destroy socket if it is going to destroy itself later on
			DestroySocket(tmpsock);
	}
}

void CServerConnect::ConnectionEstablished(CServerSocket* sender)
{

	if (connectionattemps.IsEmpty())
	{
		// we are already connected to another server
		DestroySocket(sender);
		return;
	}
	
	InitLocalIP();
	if (sender->GetConnectionState() == CS_WAITFORLOGIN)
	{
		AddLogLine(false, GetResString(IDS_CONNECTEDTOREQ), sender->cur_server->GetListName(), sender->cur_server->GetFullIP(), sender->cur_server->GetPort());

		CServer* update = theApp.serverlist->GetServerByAddress(sender->cur_server->GetAddress(), sender->cur_server->GetPort());
		if (update){
			update->ResetFailedCount();
			theApp.emuledlg->serverwnd->serverlistctrl.RefreshServer(update);
		}

		// send loginpacket
		CSafeMemFile data(256);
		data.WriteHash16(thePrefs.GetUserHash());
		data.WriteUInt32(GetClientID());
		data.WriteUInt16(thePrefs.GetPort());

		uint32 tagcount = 4;
		data.WriteUInt32(tagcount);

		CTag tagName(CT_NAME,thePrefs.GetUserNick());
		tagName.WriteTagToFile(&data);

		CTag tagVersion(CT_VERSION,EDONKEYVERSION);
		tagVersion.WriteTagToFile(&data);

#ifdef SUPPORT_LARGE_FILES
		CTag tagFlags(CT_SERVER_FLAGS,SRVCAP_ZLIB | SRVCAP_NEWTAGS | SRVCAP_LARGEFILES | SRVCAP_UNICODE);
#else
		CTag tagFlags(CT_SERVER_FLAGS,SRVCAP_ZLIB | SRVCAP_NEWTAGS | SRVCAP_UNICODE);
#endif
		tagFlags.WriteTagToFile(&data);

		// eMule Version (14-Mar-2004: requested by lugdunummaster (need for LowID clients which have no chance 
		// to send an Hello packet to the server during the callback test))
		CTag tagMuleVersion(CT_EMULE_VERSION, 
							//(uCompatibleClientID		<< 24) |
							(CemuleApp::m_nVersionMjr	<< 17) |
							(CemuleApp::m_nVersionMin	<< 10) |
							(CemuleApp::m_nVersionUpd	<<  7) );
		tagMuleVersion.WriteTagToFile(&data);

		Packet* packet = new Packet(&data);
		packet->opcode = OP_LOGINREQUEST;
		if (thePrefs.GetDebugServerTCPLevel() > 0)
			Debug(_T(">>> Sending OP__LoginRequest\n"));
		theStats.AddUpDataOverheadServer(packet->size);
		SendPacket(packet,true,sender);
	}
	else if (sender->GetConnectionState() == CS_CONNECTED)
	{
		theStats.reconnects++;
		theStats.serverConnectTime = GetTickCount();
		Log(LOG_SUCCESS | LOG_STATUSBAR, GetResString(IDS_CONNECTEDTO), sender->cur_server->GetListName());
		// MOD: KLC - [KhaosLugdunumCredits] -- Loulach -->
		// khaos::lugblistfix+ -100 for connect, we should have ~1100 right now.
		sender->cur_server->SetServerCredits(sender->cur_server->GetServerCredits() - 100);
		if(!thePrefs.UseLugdunumCredits() || (!sender->cur_server->ServerBlacklists() && thePrefs.UseLugdunumCredits() == TRUE))
		{
			DebugLog(GetResString(IDS_X_KLF_CON_ACCEPTED), sender->cur_server->GetListName());
			DebugLog(GetResString(IDS_X_KLF_CON_ACCEPTED2), sender->cur_server->GetServerCredits());
		}
		// MOD: KLC END <-- Loulach --
		theApp.emuledlg->ShowConnectionState();
		connecting = false;
		tcpsocket = sender;
			StopConnectionTry();
		POSITION delPos = connectionattemps.Find(sender);
		if(delPos)
			connectionattemps.RemoveAt(delPos);
		theApp.sharedfiles->ClearED2KPublishInfo();
		theApp.sharedfiles->SendListToServer();
		theApp.emuledlg->serverwnd->serverlistctrl.RemoveAllDeadServers();

		// tecxx 1609 2002 - serverlist update
		if (thePrefs.GetAddServersFromServer())
		{
			Packet* packet = new Packet(OP_GETSERVERLIST,0);
			if (thePrefs.GetDebugServerTCPLevel() > 0)
				Debug(_T(">>> Sending OP__GetServerList\n"));
			theStats.AddUpDataOverheadServer(packet->size);
			SendPacket(packet,true,sender);
		}
		CServer* update = theApp.serverlist->GetServerByAddress(sender->cur_server->GetAddress(), sender->cur_server->GetPort());
		theApp.emuledlg->serverwnd->serverlistctrl.RefreshServer(update);
	}
	theApp.emuledlg->ShowConnectionState();
}

bool CServerConnect::SendPacket(Packet* packet,bool delpacket, CServerSocket* to){
	if (!to){
		if (tcpsocket){
			tcpsocket->SendPacket(packet,delpacket,true);
		}
		else{
			if (delpacket)
				delete packet;
			return false;
		}
	}
	else{
		to->SendPacket(packet,delpacket,true);
	}
	return true;
}

bool CServerConnect::SendUDPPacket(Packet* packet,CServer* host,bool delpacket){
	if (udpsocket != NULL)
		udpsocket->SendPacket(packet,host);
	if (delpacket)
		delete packet;
	return true;
}

void CServerConnect::ConnectionFailed(CServerSocket* sender){
	if (connectionattemps.IsEmpty() && tcpsocket != sender){
		// just return, cleanup is done by the socket itself
		return;
	}
	//messages
	CServer* update;
	switch (sender->GetConnectionState()){
		case CS_FATALERROR:
			LogError(LOG_STATUSBAR, GetResString(IDS_ERR_FATAL));
			break;
		case CS_DISCONNECTED:
			theApp.sharedfiles->ClearED2KPublishInfo();
			LogError(LOG_STATUSBAR, GetResString(IDS_ERR_LOSTC), sender->cur_server->GetListName(), sender->cur_server->GetFullIP(), sender->cur_server->GetPort());
			break;
		case CS_SERVERDEAD:
			LogError(LOG_STATUSBAR, GetResString(IDS_ERR_DEAD), sender->cur_server->GetListName(), sender->cur_server->GetFullIP(), sender->cur_server->GetPort());
			update = theApp.serverlist->GetServerByAddress(sender->cur_server->GetAddress(), sender->cur_server->GetPort());
			if (update){
				update->AddFailedCount();
				theApp.emuledlg->serverwnd->serverlistctrl.RefreshServer(update);
			}
			break;
		case CS_ERROR:
			break;
		case CS_SERVERFULL:
			LogError(LOG_STATUSBAR, GetResString(IDS_ERR_FULL), sender->cur_server->GetListName(), sender->cur_server->GetFullIP(), sender->cur_server->GetPort());
			break;
		case CS_NOTCONNECTED:
			break; 
	}

	// IMPORTANT: mark this socket not to be deleted in StopConnectionTry(), 
	// because it will delete itself after this function!
	sender->m_bIsDeleting = true;

	switch (sender->GetConnectionState()){
		case CS_FATALERROR:{
			StopConnectionTry();
			if ((thePrefs.Reconnect()) && (autoretry) && (!m_uOutOfServers)){ 
				LogWarning(GetResString(IDS_RECONNECT), CS_RETRYCONNECTTIME);
				m_uOutOfServers = ::GetTickCount();
			}
			break;
		}
		case CS_DISCONNECTED:{
			theApp.sharedfiles->ClearED2KPublishInfo();
			if (tcpsocket)
				tcpsocket->Close();
			tcpsocket = NULL;
			theApp.emuledlg->searchwnd->CancelEd2kSearch();
			// -khaos--+++> Tell our total server duration thinkymajig to update...
			theStats.serverConnectTime = 0;
			theStats.Add2TotalServerDuration();
			// <-----khaos-
			if (thePrefs.Reconnect() && !connecting){
				ConnectToAnyServer(0);		
			}
			if (thePrefs.GetNotifierOnImportantError()) {
				theApp.emuledlg->ShowNotifier(GetResString(IDS_CONNECTIONLOST), TBN_IMPORTANTEVENT);
			}
			break;
		}
		case CS_ERROR:
		case CS_NOTCONNECTED:{
			if (!connecting)
				break;
		}
		case CS_SERVERDEAD:
		case CS_SERVERFULL:{
			if (!connecting)
				break;
			if (!autoretry){
				connecting = false; //StopConnectionTry();
				break;
			}

			POSITION delPos = connectionattemps.Find(sender);
			if(delPos)
				connectionattemps.RemoveAt(delPos);
		
			TryAnotherConnectionrequest();
		}
	}
	theApp.emuledlg->ShowConnectionState();
}

void CServerConnect::Process()
{
	if(IsConnecting())
		CheckForTimeout();

	if(m_uOutOfServers){
		if(GetTickCount() - m_uOutOfServers >= SEC2MS(CS_RETRYCONNECTTIME)){
			StopConnectionTry();
			if (!IsConnected())
				ConnectToAnyServer();
		}
	}
	else if( autoretry && tcpsocket == NULL && (connectionattemps.GetCount() < (thePrefs.IsSafeServerConnectEnabled() ? 1 : 2)) )
		TryAnotherConnectionrequest();

	// MOD: KLC - [KhaosLugdunumCredits] -- Loulach -->
	if (tcpsocket)
		tcpsocket->cur_server->IncServerCredits();
	// MOD: KLC END <-- Loulach --
}

void CServerConnect::CheckForTimeout()
{ 
	DWORD dwCurTick = ::GetTickCount();
	// If we are using a proxy, increase server connection timeout to default connection timeout
	DWORD dwServerConnectTimeout = thePrefs.GetProxySettings().UseProxy ? CONNECTION_TIMEOUT : CONSERVTIMEOUT;

	for(POSITION pos = connectionattemps.GetHeadPosition();pos!=NULL;){
		CServerSocket* tmpsock = connectionattemps.GetNext(pos);
		if (dwCurTick - tmpsock->m_dwStartTime > dwServerConnectTimeout){
			LogWarning(GetResString(IDS_ERR_CONTIMEOUT), tmpsock->cur_server->GetListName(), tmpsock->cur_server->GetFullIP(), tmpsock->cur_server->GetPort());
			TryAnotherConnectionrequest();
			DestroySocket(tmpsock);
		}
	}
}

bool CServerConnect::Disconnect(){
	if(tcpsocket == NULL)
		return false;
		StopConnectionTry();
	autoretry = false;

	CServer* update = theApp.serverlist->GetServerByAddress( tcpsocket->cur_server->GetAddress(), tcpsocket->cur_server->GetPort() );
	theApp.emuledlg->serverwnd->serverlistctrl.RefreshServer( update);
	DestroySocket(tcpsocket);
	tcpsocket = NULL;

	theApp.sharedfiles->ClearED2KPublishInfo();
	theApp.SetPublicIP(0);

	theApp.emuledlg->ShowConnectionState();
	theApp.emuledlg->AddServerMessageLine(_T(""));
	theApp.emuledlg->AddServerMessageLine(_T(""));
	theApp.emuledlg->AddServerMessageLine(_T(""));
	theApp.emuledlg->AddServerMessageLine(_T(""));
	// -khaos--+++> Tell our total server duration thinkymajig to update...
	theStats.serverConnectTime = 0;
	theStats.Add2TotalServerDuration();
	// <-----khaos-	

	return true;
}

CServerConnect::CServerConnect(CServerList* in_serverlist)
{
	used_list = in_serverlist;
	connecting = false;
	clientid = 0;
	autoretry = false;
	udpsocket = NULL; // MOD: MOD - [BindToAdapter] <-- Loulach --
	tcpsocket = NULL;
	m_uOutOfServers = 0;
	InitLocalIP();
}
// MOD: MOD - [BindToAdapter] -- Loulach -->
bool CServerConnect::CreateUDP(){
	if(udpsocket)
		return true;

	if (thePrefs.GetServerUDPPort() != 0){
	    udpsocket = new CUDPSocket(); // initalize socket for udp packets
		if (!udpsocket->Create()){
			delete udpsocket;
			udpsocket = NULL;
			return false;
		}
	}
	return true;
}

bool CServerConnect::Rebind(){ 
	if(udpsocket) 
		return udpsocket->Rebind(); 
	return false; 
}
// MOD: MOD END <-- Loulach --

CServerConnect::~CServerConnect(){
	// stop all connections
	StopConnectionTry();
	// close connected socket, if any
	Disconnect();
	// close udp socket
	if (udpsocket){
	    udpsocket->Close();
	    delete udpsocket;
    }
}

CServer* CServerConnect::GetCurrentServer(){
#ifdef _TRAX // MOD: TRAX - [TraxEdition] // MOD: MSC - [MultiServerConnection] -- Loulach -->
	if (IsConnected() && tcpsockets.GetHead()->connectionstate == CS_CONNECTED)
		return tcpsockets.GetHead()->cur_server;
#else
	if (IsConnected() && tcpsocket->connectionstate == CS_CONNECTED)
		return tcpsocket->cur_server;
#endif // _TRAX // MOD: TRAX END // MOD: MSC END <-- Loulach --
	return NULL;
}

void CServerConnect::SetClientID(uint32 newid){
	clientid = newid;

	if (!::IsLowID(newid)){
		theApp.SetPublicIP(newid);
		// MOD: RIC - [ReaskOnIDChange] -- Loulach -->
		if(thePrefs.IsCheckIPChange())
			theApp.CheckIDChange(newid);
		// MOD: RIC END <-- Loulach --
	}
	
	theApp.emuledlg->ShowConnectionState();
}

void CServerConnect::DestroySocket(CServerSocket* pSck){
	if (pSck == NULL)
		return;
	// remove socket from list of opened sockets

	POSITION delPos = connectionattemps.Find(pSck);
	if(delPos)
		connectionattemps.RemoveAt(delPos);
	if (pSck->m_SocketData.hSocket != INVALID_SOCKET){ // deadlake PROXYSUPPORT - changed to AsyncSocketEx
		pSck->AsyncSelect(0);
		pSck->Close();
	}

	delete pSck;
}

CServerSocket* CServerConnect::IsLocalServer(uint32 dwIP, uint16 nPort, bool bConnecting){
	if (IsConnected()){
		if(tcpsocket->cur_server->GetIP() == dwIP && tcpsocket->cur_server->GetPort() == nPort)
			return tcpsocket;
	}
	if(bConnecting){
		for(POSITION pos = connectionattemps.GetHeadPosition();pos!=NULL;){
			CServerSocket* cur_server = connectionattemps.GetNext(pos);
			if(cur_server->cur_server->GetIP() == dwIP && cur_server->cur_server->GetPort() == nPort)
				return cur_server;
		}
	}
	return NULL;
}

bool CServerConnect::IsConnectedServer(const CServer* server){
	if (IsConnected()){
		const CServer* cur_srv = tcpsocket->cur_server;
		if( tcpsocket->connectionstate == CS_CONNECTED
			&& cur_srv->GetPort() == server->GetPort()
			&& cur_srv->GetConnPort() == server->GetConnPort() // MOD: SAX - [ServerAUX] <-- Loulach --
			&& _tcsicmp(cur_srv->GetAddress(), server->GetAddress()) == 0)
			return true;
	}
	return false;
}

void CServerConnect::InitLocalIP()
{
	m_nLocalIP = 0;
	// Don't use 'gethostbyname(NULL)'. The winsock DLL may be replaced by a DLL from a third party
	// which is not fully compatible to the original winsock DLL. ppl reported crash with SCORSOCK.DLL
	// when using 'gethostbyname(NULL)'.
	__try{
		char szHost[256];
		if (gethostname(szHost, sizeof szHost) == 0){
			hostent* pHostEnt = gethostbyname(szHost);
			if (pHostEnt != NULL && pHostEnt->h_length == 4 && pHostEnt->h_addr_list[0] != NULL)
				m_nLocalIP = *((uint32*)pHostEnt->h_addr_list[0]);
		}
	}
	__except(EXCEPTION_EXECUTE_HANDLER){
		// at least two ppl reported crashs when using 'gethostbyname' with third party winsock DLLs
		if (thePrefs.GetVerbose())
			DebugLogError(_T("Unknown exception in CServerConnect::InitLocalIP"));
		ASSERT(0);
	}
}

void CServerConnect::KeepConnectionAlive()
{
	DWORD dwServerKeepAliveTimeout = thePrefs.GetServerKeepAliveTimeout();

	if(tcpsocket != NULL){
		CServerSocket* cur_server = tcpsocket;
		if (dwServerKeepAliveTimeout && cur_server->connectionstate == CS_CONNECTED &&
			GetTickCount() - cur_server->GetLastTransmission() >= dwServerKeepAliveTimeout)
		{
			// "Ping" the server if the TCP connection was not used for the specified interval with 
			// an empty publish files packet -> recommended by lugdunummaster himself!
			CSafeMemFile files(4);
			files.WriteUInt32(0); // nr. of files
			Packet* packet = new Packet(&files);
			packet->opcode = OP_OFFERFILES;
			if (thePrefs.GetVerbose())
				AddDebugLogLine(false, _T("Refreshing server connection"));
			if (thePrefs.GetDebugServerTCPLevel() > 0)
				Debug(_T(">>> Sending OP__OfferFiles(KeepAlive) to server\n"));
			theStats.AddUpDataOverheadServer(packet->size);
			cur_server->SendPacket(packet,true);
		}
	}
}

bool CServerConnect::IsLowID()
{
	return ::IsLowID(clientid);
}
// MOD: NEW END <-- Loulach --
