Esempio n. 1
0
//You can add a friend without a IP to allow the IRC to trade links with lowID users.
bool CFriendList::AddFriend(const uchar* abyUserhash, uint32 dwLastSeen, uint32 dwLastUsedIP, uint16 nLastUsedPort, 
							uint32 dwLastChatted, LPCTSTR pszName, uint32 dwHasHash){
	// client must have an IP (HighID) or a hash
	// TODO: check if this can be switched to a hybridID so clients with *.*.*.0 can be added..
	if (IsLowID(dwLastUsedIP) && dwHasHash==0)
		return false;
	if (SearchFriend(abyUserhash, dwLastUsedIP, nLastUsedPort) != NULL)
		return false;
	CFriend* Record = new CFriend( abyUserhash, dwLastSeen, dwLastUsedIP, nLastUsedPort, dwLastChatted, pszName, dwHasHash );
	m_listFriends.AddTail(Record);
	ShowFriends();
	SaveList();

	// Mighty Knife: log friend activities
	if (thePrefs.GetLogFriendlistActivities ()) {
		#ifdef MIGHTY_TWEAKS
 	    AddLogLine(false, GetResString(IDS_NEWFRIEND)+_T(" '%s', ip %i.%i.%i.%i:%i, ")+GetResString(IDS_CD_UHASH)+_T(" %s"),
									Record->m_strName, (uint8)Record->m_dwLastUsedIP, (uint8)(Record->m_dwLastUsedIP>>8), 
									(uint8)(Record->m_dwLastUsedIP>>16),(uint8)(Record->m_dwLastUsedIP>>24), 
									Record->m_nLastUsedPort, md4str(Record->m_abyUserhash));
		#else
		AddLogLine(false, GetResString(IDS_NEWFRIEND)+_T(" '%s', ")+GetResString(IDS_CD_UHASH)+_T(" %s"),
									Record->m_strName, md4str(Record->m_abyUserhash));
		#endif
	}
Esempio n. 2
0
bool operator==(const CDeadSource& ds1,const CDeadSource& ds2){
	//ASSERT( ((ds1.m_dwID + ds1.m_dwServerIP) ^ isnulmd4(ds1.m_aucHash)) != 0 );
	//ASSERT( ((ds2.m_dwID + ds2.m_dwServerIP) ^ isnulmd4(ds2.m_aucHash)) != 0 );
	return (
		// lowid ed2k and highid kad + ed2k check
		( (ds1.m_dwID != 0 && ds1.m_dwID == ds2.m_dwID) && ((ds1.m_nPort != 0 && ds1.m_nPort == ds2.m_nPort) || (ds1.m_nKadPort != 0 && ds1.m_nKadPort == ds2.m_nKadPort)) && (ds1.m_dwServerIP == ds2.m_dwServerIP || !IsLowID(ds1.m_dwID)) )
		// lowid kad check
		|| ( IsLowID(ds1.m_dwID) && isnulmd4(ds1.m_aucHash) == FALSE && md4cmp(ds1.m_aucHash, ds2.m_aucHash) == 0) );
}
Esempio n. 3
0
//You can add a friend without a IP to allow the IRC to trade links with lowID users.
bool CFriendList::AddFriend(const uchar* abyUserhash, uint32 dwLastSeen, uint32 dwLastUsedIP, uint16 nLastUsedPort, 
							uint32 dwLastChatted, LPCTSTR pszName, uint32 dwHasHash){
	// client must have an IP (HighID) or a hash
	// TODO: check if this can be switched to a hybridID so clients with *.*.*.0 can be added..
	if (IsLowID(dwLastUsedIP) && dwHasHash==0)
		return false;
	if( dwLastUsedIP && IsAlreadyFriend(dwLastUsedIP, nLastUsedPort))
		return false;
	CFriend* Record = new CFriend( abyUserhash, dwLastSeen, dwLastUsedIP, nLastUsedPort, dwLastChatted, pszName, dwHasHash );
	m_listFriends.AddTail(Record);
	ShowFriends();
	SaveList();
	return true;
}
void CGlobalVariable::SetPublicIP(const uint32 dwIP)
{
	if (dwIP != 0){
		ASSERT ( !IsLowID(dwIP));
		ASSERT ( m_pPeerCache );
		if ( GetPublicIP() == 0)
			AddDebugLogLine(DLP_VERYLOW, false, _T("My public IP Address is: %s"),ipstr(dwIP));
		else if (Kademlia::CKademlia::IsConnected() && Kademlia::CKademlia::GetPrefs()->GetIPAddress())
			if(ntohl(Kademlia::CKademlia::GetIPAddress()) != dwIP)
				AddDebugLogLine(DLP_DEFAULT, false,  _T("Public IP Address reported from Kademlia (%s) differs from new found (%s)"),ipstr(ntohl(Kademlia::CKademlia::GetIPAddress())),ipstr(dwIP));
		m_pPeerCache->FoundMyPublicIPAddress(dwIP);	
		
		m_pMyCountry = CGlobalVariable::ip2country->GetCountryFromIP(dwIP);
	}
	else
		AddDebugLogLine(DLP_VERYLOW, false, _T("Deleted public IP"));

	if (dwIP != 0 && dwIP != m_dwPublicIP && CGlobalVariable::serverlist != NULL){
		m_dwPublicIP = dwIP;
		CGlobalVariable::serverlist->CheckForExpiredUDPKeys();
	}
	else
		m_dwPublicIP = dwIP;
}
Esempio n. 5
0
int CmdJoin::process(PeerMgr &mgr ,Peer &peer)
{
	//检查连接状态
	int iret = LS_SUCCEEDED;

	peer.tcp_port_ = tcp_port_;
	peer.udp_port_ = udp_port_;

	//反馈join_r
	CmdJoinR jr(cmd_hdr_,0,mgr.version_);
	jr.ServerId_ = mgr.peerId_;
	jr.start_id_ = mgr.buffer_.start_seg_id_;
	jr.is_server_ = (char)mgr.is_server_;
	if (mgr.chId_ != cmd_hdr_.chId_)
	{
		jr.result_ = 3;//channel Id不匹配
		peer.putQ(jr.serialize());
		
	}
	else
	{

		peer.putQ(jr.serialize());

		//无论是否允许加入都要发生push_peers
		if (mgr.is_server_)
		{
			CmdPushPeers cpprs(mgr.peer_list_);
			peer.putQ(cpprs.serialize(peer.get_next_sequence(),mgr.chId_,mgr.peerId_));
			//推送头
			CmdPushHeader cphdr(mgr.mhdr_);
			peer.putQ(cphdr.serialize(peer.get_next_sequence(),mgr.chId_,mgr.peerId_));
		}
		peer.is_behind_nat_ = IsLowID(peer.peerId_);
		if (mgr.peer_list_.size() < mgr.config_.max_peer_list_size_  && !(mgr.is_server_ && peer.is_behind_nat_)) //允许加入
		{
			////如果接受连接 则加入到本机的列表中。在Peer的open中已经加入
			////PeerPtr p =&peer;

			if(mgr.peer_list_.join(&peer) == LS_SUCCEEDED) //succeed
			{
				//如果接受连接,发送ExBm消息
				CmdExBm exBm;
				exBm.lpData_ = mgr.mhdr_.data_;
				peer.putQ(exBm.serialize(peer.get_next_sequence(),mgr.chId_,mgr.peerId_));
			}
			else
			{
				iret = LS_FAILED;
				//delete &peer;
			}
			
		}//如果不允许连接则发送ask for disconnect消息
		else
		{
			CmdAskForDisconnect cmd;
			peer.putQ(cmd.serialize(peer.get_next_sequence(),mgr.chId_,mgr.peerId_));
		}

	}
	return iret;
}
Esempio n. 6
0
/////////////////////////////////////////////
// CED2KFileLink implementation
/////////////////////////////////////////////
CED2KFileLink::CED2KFileLink(const TCHAR* pszName, const TCHAR* pszSize, const TCHAR* pszHash, 
							 const CStringArray& astrParams, const TCHAR* pszSources)
	: m_size(pszSize)
{
	// Here we have a little problem.. Actually the proper solution would be to decode from UTF8,
	// only if the string does contain escape sequences. But if user pastes a raw UTF8 encoded
	// string (for whatever reason), we would miss to decode that string. On the other side, 
	// always decoding UTF8 can give flaws in case the string is valid for Unicode and UTF8
	// at the same time. However, to avoid the pasting of raw UTF8 strings (which would lead
	// to a greater mess in the network) we always try to decode from UTF8, even if the string
	// did not contain escape sequences.
	m_name = OptUtf8ToStr(URLDecode(pszName));
	m_name.Trim();
	if (m_name.IsEmpty())
		throw GetResString(IDS_ERR_NOTAFILELINK);

	SourcesList = NULL;
	m_hashset = NULL;
	m_bAICHHashValid = false;

	if (_tcslen(pszHash) != 32)
		throw GetResString(IDS_ERR_ILLFORMEDHASH);

	if (_tstoi64(pszSize)>=4294967295)
		throw GetResString(IDS_ERR_TOOLARGEFILE);
	if (_tstoi64(pszSize)<=0)
		throw GetResString(IDS_ERR_NOTAFILELINK);
	
	for (int idx = 0; idx < 16; ++idx) {
		m_hash[idx] = FromHexDigit(*pszHash++)*16;
		m_hash[idx] += FromHexDigit(*pszHash++);
	}

	bool bError = false;
	for (int i = 0; !bError && i < astrParams.GetCount(); i++)
	{
		const CString& strParam = astrParams.GetAt(i);
		ASSERT( !strParam.IsEmpty() );

		CString strTok;
		int iPos = strParam.Find(_T('='));
		if (iPos != -1)
			strTok = strParam.Left(iPos);
		if (strTok == _T("s"))
		{
			CString strURL = strParam.Mid(iPos + 1);
			if (!strURL.IsEmpty())
			{
				TCHAR szScheme[INTERNET_MAX_SCHEME_LENGTH];
				TCHAR szHostName[INTERNET_MAX_HOST_NAME_LENGTH];
				TCHAR szUrlPath[INTERNET_MAX_PATH_LENGTH];
				TCHAR szUserName[INTERNET_MAX_USER_NAME_LENGTH];
				TCHAR szPassword[INTERNET_MAX_PASSWORD_LENGTH];
				TCHAR szExtraInfo[INTERNET_MAX_URL_LENGTH];
				URL_COMPONENTS Url = {0};
				Url.dwStructSize = sizeof(Url);
				Url.lpszScheme = szScheme;
				Url.dwSchemeLength = ARRSIZE(szScheme);
				Url.lpszHostName = szHostName;
				Url.dwHostNameLength = ARRSIZE(szHostName);
				Url.lpszUserName = szUserName;
				Url.dwUserNameLength = ARRSIZE(szUserName);
				Url.lpszPassword = szPassword;
				Url.dwPasswordLength = ARRSIZE(szPassword);
				Url.lpszUrlPath = szUrlPath;
				Url.dwUrlPathLength = ARRSIZE(szUrlPath);
				Url.lpszExtraInfo = szExtraInfo;
				Url.dwExtraInfoLength = ARRSIZE(szExtraInfo);
				if (InternetCrackUrl(strURL, 0, 0, &Url) && Url.dwHostNameLength > 0)
				{
					SUnresolvedHostname* hostname = new SUnresolvedHostname;
					hostname->strURL = strURL;
					hostname->strHostname = szHostName;
					m_HostnameSourcesList.AddTail(hostname);
				}
			}
			else
				ASSERT(0);
		}
		else if (strTok == _T("p"))
		{
			CString strPartHashs = strParam.Tokenize(_T("="), iPos);

			if (m_hashset != NULL){
				ASSERT(0);
				bError = true;
				break;
			}

			m_hashset = new CSafeMemFile(256);
			m_hashset->WriteHash16(m_hash);
			m_hashset->WriteUInt16(0);

			int iPartHashs = 0;
			int iPosPH = 0;
			CString strHash = strPartHashs.Tokenize(_T(":"), iPosPH);
			while (!strHash.IsEmpty())
			{
				uchar aucPartHash[16];
				if (!strmd4(strHash, aucPartHash)){
					bError = true;
					break;
				}
				m_hashset->WriteHash16(aucPartHash);
				iPartHashs++;
				strHash = strPartHashs.Tokenize(_T(":"), iPosPH);
			}
			if (bError)
				break;

			m_hashset->Seek(16, CFile::begin);
			m_hashset->WriteUInt16(iPartHashs);
			m_hashset->Seek(0, CFile::begin);
		}
		else if (strTok == _T("h"))
		{
			CString strHash = strParam.Mid(iPos + 1);
			if (!strHash.IsEmpty())
			{
				if (DecodeBase32(strHash, m_AICHHash.GetRawHash(), CAICHHash::GetHashSize()) == CAICHHash::GetHashSize()){
					m_bAICHHashValid = true;
					ASSERT( m_AICHHash.GetString().CompareNoCase(strHash) == 0 );
				}
				else
					ASSERT( false );
			}
			else
				ASSERT( false );
		}
		else
			ASSERT(0);
	}

	if (bError)
	{
		delete m_hashset;
		m_hashset = NULL;
	}

	if (pszSources)
	{
		TCHAR* pNewString = _tcsdup(pszSources);
		autoFree liberator(pNewString);
		TCHAR* pCh = pNewString;
		TCHAR* pEnd;
		TCHAR* pIP;
		TCHAR* pPort;

		bool bAllowSources;
		TCHAR date[3];
		COleDateTime expirationDate;
		int nYear,nMonth,nDay;

		uint16 nCount = 0;
		uint32 dwID;
		uint16 nPort;
		uint32 dwServerIP = 0; 
		uint16 nServerPort = 0;
		unsigned long ul;

		int nInvalid = 0;

		pCh = _tcsstr( pCh, _T("sources") );
		if( pCh != NULL ) {
			pCh = pCh + 7; // point to char after "sources"
			pEnd = pCh;
			while( *pEnd ) pEnd++; // make pEnd point to the terminating NULL
			bAllowSources=true;
			// if there's an expiration date...
			if( *pCh == _T('@') && (pEnd-pCh) > 7 )
			{
				pCh++; // after '@'
				date[2] = 0; // terminate the two character string
				date[0] = *(pCh++); date[1] = *(pCh++);
				nYear = _tcstol( date, 0, 10 ) + 2000;
				date[0] = *(pCh++); date[1] = *(pCh++);
				nMonth = _tcstol( date, 0, 10 );
				date[0] = *(pCh++); date[1] = *(pCh++);
				nDay = _tcstol( date, 0, 10 );
				bAllowSources = ( expirationDate.SetDate(nYear,nMonth,nDay) == 0 );
				if (bAllowSources) bAllowSources=(COleDateTime::GetCurrentTime() < expirationDate);
			}

			// increment pCh to point to the first "ip:port" and check for sources
			if ( bAllowSources && ++pCh < pEnd ) {
				SourcesList = new CSafeMemFile(256);
				SourcesList->WriteUInt16(nCount); // init to 0, we'll fix this at the end.
				// for each "ip:port" source string until the end
				// limit to prevent overflow (uint16 due to CPartFile::AddClientSources)
				while( *pCh != 0 && nCount < MAXSHORT ) {
					pIP = pCh;
					// find the end of this ip:port string & start of next ip:port string.
					if( (pCh = _tcschr(pCh, _T(','))) != NULL ) {
						*pCh = 0; // terminate current "ip:port"
						pCh++; // point to next "ip:port"
					}
					else
						pCh = pEnd;

					// if port is not present for this ip, go to the next ip.
					if( (pPort = _tcschr(pIP, _T(':'))) == NULL )
					{	nInvalid++;	continue;	}

					*pPort = 0;	// terminate ip string
					pPort++;	// point pPort to port string.

					dwID = inet_addr(CStringA(pIP));
					ul = _tcstoul( pPort, 0, 10 );
					nPort = static_cast<uint16>(ul);

					// skip bad ips / ports
					if (ul > 0xFFFF || ul == 0 )	// port
					{	nInvalid++;	continue;	}
					if( dwID == INADDR_NONE) {	// hostname?
						if (_tcslen(pIP) > 512)
						{	nInvalid++;	continue;	}
						SUnresolvedHostname* hostname = new SUnresolvedHostname;
						hostname->nPort = nPort;
						hostname->strHostname = pIP;
						m_HostnameSourcesList.AddTail(hostname);
						continue;
					}
					//TODO: This will filter out *.*.*.0 clients. Is there a nice way to fix?
					if( IsLowID(dwID) )	// ip
					{	nInvalid++;	continue;	}

					SourcesList->WriteUInt32(dwID);
					SourcesList->WriteUInt16(nPort);
					SourcesList->WriteUInt32(dwServerIP);
					SourcesList->WriteUInt16(nServerPort);
					nCount++;
				}
				SourcesList->SeekToBegin();
				SourcesList->WriteUInt16(nCount);
				SourcesList->SeekToBegin();
				if (nCount==0) {
					delete SourcesList;
					SourcesList=NULL;
				}
			}
		}
	}
}
Esempio n. 7
0
bool CServerSocket::ProcessPacket(char* packet, uint32 size, uint8 opcode) {
    try {
        switch(opcode) {
        case OP_SERVERMESSAGE: {
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                Debug(_T("ServerMsg - OP_ServerMessage\n"));

            CServer* pServer = cur_server ? theApp.serverlist->GetServerByAddress(cur_server->GetAddress(),cur_server->GetPort()) : NULL;
            CSafeMemFile data((const BYTE*)packet, size);
            CString strMessages(data.ReadString(pServer ? pServer->GetUnicodeSupport() : false));

            if (thePrefs.GetDebugServerTCPLevel() > 0) {
                UINT uAddData = data.GetLength() - data.GetPosition();
                if (uAddData > 0) {
                    Debug(_T("*** NOTE: OP_ServerMessage: ***AddData: %u bytes\n"), uAddData);
                    DebugHexDump((uint8*)packet + data.GetPosition(), uAddData);
                }
            }

            // 16.40 servers do not send separate OP_SERVERMESSAGE packets for each line;
            // instead of this they are sending all text lines with one OP_SERVERMESSAGE packet.
            int iPos = 0;
            CString message = strMessages.Tokenize(_T("\r\n"), iPos);
            while (!message.IsEmpty())
            {
                bool bOutputMessage = true;
                if (_tcsnicmp(message, _T("server version"), 14) == 0) {
                    CString strVer = message.Mid(14);
                    strVer.Trim();
                    strVer = strVer.Left(64); // truncate string to avoid misuse by servers in showing ads
                    if (pServer) {
                        pServer->SetVersion(strVer);
                        theApp.emuledlg->serverwnd->serverlistctrl.RefreshServer(pServer);
                        theApp.emuledlg->serverwnd->UpdateMyInfo();
                    }
                    if (thePrefs.GetDebugServerTCPLevel() > 0)
                        Debug(_T("%s\n"), message);
                }
                else if (_tcsncmp(message, _T("ERROR"), 5) == 0) {
                    LogError(LOG_STATUSBAR, _T("%s %s (%s:%u) - %s"),
                             GetResString(IDS_ERROR),
                             pServer ? pServer->GetListName() : GetResString(IDS_PW_SERVER),
                             cur_server ? cur_server->GetAddress() : _T(""),
                             cur_server ? cur_server->GetPort() : 0, message.Mid(5).Trim(_T(" :")));
                    bOutputMessage = false;
                }
                else if (_tcsncmp(message, _T("WARNING"), 7) == 0) {
                    LogWarning(LOG_STATUSBAR, _T("%s %s (%s:%u) - %s"),
                               GetResString(IDS_WARNING),
                               pServer ? pServer->GetListName() : GetResString(IDS_PW_SERVER),
                               cur_server ? cur_server->GetAddress() : _T(""),
                               cur_server ? cur_server->GetPort() : 0, message.Mid(7).Trim(_T(" :")));
                    bOutputMessage = false;
                }

                if (message.Find(_T("[emDynIP: ")) != -1 && message.Find(_T("]")) != -1 && message.Find(_T("[emDynIP: ")) < message.Find(_T("]"))) {
                    CString dynip = message.Mid(message.Find(_T("[emDynIP: ")) + 10, message.Find(_T("]")) - (message.Find(_T("[emDynIP: ")) + 10));
                    dynip.Trim(_T(" "));
                    if (dynip.GetLength() && dynip.GetLength() < 51) {
                        if (pServer) {
                            pServer->SetDynIP(dynip);
                            if (cur_server)
                                cur_server->SetDynIP(dynip);
                            theApp.emuledlg->serverwnd->serverlistctrl.RefreshServer(pServer);
                            theApp.emuledlg->serverwnd->UpdateMyInfo();
                        }
                    }
                }
                if (bOutputMessage)
                    theApp.emuledlg->AddServerMessageLine(message);

                message = strMessages.Tokenize(_T("\r\n"), iPos);
            }
            break;
        }
        case OP_IDCHANGE: {
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                Debug(_T("ServerMsg - OP_IDChange\n"));
            if (size < sizeof(LoginAnswer_Struct)) {
                throw GetResString(IDS_ERR_BADSERVERREPLY);
            }
            LoginAnswer_Struct* la = (LoginAnswer_Struct*)packet;

            // save TCP flags in 'cur_server'
            ASSERT( cur_server );
            if (cur_server) {
                if (size >= sizeof(LoginAnswer_Struct)+4) {
                    DWORD dwFlags = *((uint32*)(packet + sizeof(LoginAnswer_Struct)));
                    if (thePrefs.GetDebugServerTCPLevel() > 0) {
                        CString strInfo;
                        strInfo.AppendFormat(_T("  TCP Flags=0x%08x"), dwFlags);
                        const DWORD dwKnownBits = SRV_TCPFLG_COMPRESSION | SRV_TCPFLG_NEWTAGS | SRV_TCPFLG_UNICODE;
                        if (dwFlags & ~dwKnownBits)
                            strInfo.AppendFormat(_T("  ***UnkBits=0x%08x"), dwFlags & ~dwKnownBits);
                        if (dwFlags & SRV_TCPFLG_COMPRESSION)
                            strInfo.AppendFormat(_T("  Compression=1"));
                        if (dwFlags & SRV_TCPFLG_NEWTAGS)
                            strInfo.AppendFormat(_T("  NewTags=1"));
                        if (dwFlags & SRV_TCPFLG_UNICODE)
                            strInfo.AppendFormat(_T("  Unicode=1"));
                        Debug(_T("%s\n"), strInfo);
                    }
                    cur_server->SetTCPFlags(dwFlags);
                }
                else
                    cur_server->SetTCPFlags(0);

                // copy TCP flags into the server in the server list
                CServer* pServer = theApp.serverlist->GetServerByAddress(cur_server->GetAddress(), cur_server->GetPort());
                if (pServer)
                    pServer->SetTCPFlags(cur_server->GetTCPFlags());
            }

            if (la->clientid == 0) {
                uint8 state = thePrefs.GetSmartIdState();
                if ( state > 0 ) {
                    state++;
                    if( state > 3 )
                        thePrefs.SetSmartIdState(0);
                    else
                        thePrefs.SetSmartIdState(state);
                }
                break;
            }
            if( thePrefs.GetSmartIdCheck() ) {
                if (!IsLowID(la->clientid))
                    thePrefs.SetSmartIdState(1);
                else {
                    uint8 state = thePrefs.GetSmartIdState();
                    if ( state > 0 ) {
                        state++;
                        if( state > 3 )
                            thePrefs.SetSmartIdState(0);
                        else
                            thePrefs.SetSmartIdState(state);
                        break;
                    }
                }
            }

//==>Lowid retry by SlugFiller [cyrex2001]
#ifdef LOWID
            if (thePrefs.GetLowIdRetried())
            {
                if (la->clientid < 16777216 )
                {
                    SetConnectionState(CS_ERROR);
                    AddLogLine(true,GetResString(IDS_LOWIDRETRYING),thePrefs.GetLowIdRetried());
                    thePrefs.SetLowIdRetried();
                    break;
                }
            }
#endif //Lowid retry
//<==Lowid retry [cyrex2001]
            // we need to know our client's HighID when sending our shared files (done indirectly on SetConnectionState)
            serverconnect->clientid = la->clientid;

            if (connectionstate != CS_CONNECTED) {
                SetConnectionState(CS_CONNECTED);
                theApp.OnlineSig();       // Added By Bouc7
            }
            serverconnect->SetClientID(la->clientid);
            AddLogLine(false, GetResString(IDS_NEWCLIENTID), la->clientid);
//==>Reask sourcen after ip change [cyrex2001]
#ifdef RSAIC_MAELLA
            theApp.CheckIDChange();
#endif //Reask sourcen after ip change
//<==Reask sourcen after ip change [cyrex2001]
            theApp.downloadqueue->ResetLocalServerRequests();
            break;
        }
        case OP_SEARCHRESULT: {
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                Debug(_T("ServerMsg - OP_SearchResult\n"));
            CServer* cur_srv = (serverconnect) ? serverconnect->GetCurrentServer() : NULL;
            CServer* pServer = cur_srv ? theApp.serverlist->GetServerByAddress(cur_srv->GetAddress(), cur_srv->GetPort()) : NULL;
            bool bMoreResultsAvailable;
            uint16 uSearchResults = theApp.searchlist->ProcessSearchanswer(packet, size, true/*pServer ? pServer->GetUnicodeSupport() : false*/, cur_srv ? cur_srv->GetIP() : 0, cur_srv ? cur_srv->GetPort() : 0, &bMoreResultsAvailable);
            theApp.emuledlg->searchwnd->LocalSearchEnd(uSearchResults, bMoreResultsAvailable);
            break;
        }
        case OP_FOUNDSOURCES: {
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                Debug(_T("ServerMsg - OP_FoundSources; Sources=%u  %s\n"), (UINT)(uchar)packet[16], DbgGetFileInfo((uchar*)packet));
            ASSERT( cur_server );
            if (cur_server)
            {
                CSafeMemFile sources((BYTE*)packet,size);
                uchar fileid[16];
                sources.ReadHash16(fileid);
                if (CPartFile* file = theApp.downloadqueue->GetFileByID(fileid))
                    file->AddSources(&sources,cur_server->GetIP(), cur_server->GetPort());
            }
            break;
        }
        case OP_SERVERSTATUS: {
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                Debug(_T("ServerMsg - OP_ServerStatus\n"));
            // FIXME some statuspackets have a different size -> why? structur?
            if (size < 8)
                break;//throw "Invalid status packet";
            uint32 cur_user = PeekUInt32(packet);
            uint32 cur_files = PeekUInt32(packet+4);
            CServer* update = cur_server ? theApp.serverlist->GetServerByAddress(cur_server->GetAddress(), cur_server->GetPort()) : NULL;
            if (update) {
                update->SetUserCount(cur_user);
                update->SetFileCount(cur_files);
                theApp.emuledlg->ShowUserCount();
                theApp.emuledlg->serverwnd->serverlistctrl.RefreshServer( update );
                theApp.emuledlg->serverwnd->UpdateMyInfo();
            }
            if (thePrefs.GetDebugServerTCPLevel() > 0) {
                if (size > 8) {
                    Debug(_T("*** NOTE: OP_ServerStatus: ***AddData: %u bytes\n"), size - 8);
                    DebugHexDump((uint8*)packet + 8, size - 8);
                }
            }
            break;
        }
        case OP_SERVERIDENT: {
            // OP_SERVERIDENT - this is sent by the server only if we send a OP_GETSERVERLIST
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                Debug(_T("ServerMsg - OP_ServerIdent\n"));
            if (size<16+4+2+4) {
                if (thePrefs.GetVerbose())
                    DebugLogError(_T("%s"), GetResString(IDS_ERR_KNOWNSERVERINFOREC));
                break;// throw "Invalid server info received";
            }

            CServer* pServer = cur_server ? theApp.serverlist->GetServerByAddress(cur_server->GetAddress(),cur_server->GetPort()) : NULL;
            CString strInfo;
            CSafeMemFile data((BYTE*)packet, size);

            uint8 aucHash[16];
            data.ReadHash16(aucHash);
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                strInfo.AppendFormat(_T("Hash=%s (%s)"), md4str(aucHash), DbgGetHashTypeString(aucHash));
            uint32 nServerIP = data.ReadUInt32();
            uint16 nServerPort = data.ReadUInt16();
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                strInfo.AppendFormat(_T("  IP=%s:%u"), ipstr(nServerIP), nServerPort);
            UINT nTags = data.ReadUInt32();
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                strInfo.AppendFormat(_T("  Tags=%u"), nTags);

            CString strName;
            CString strDescription;
            for (UINT i = 0; i < nTags; i++) {
                CTag tag(&data, pServer ? pServer->GetUnicodeSupport() : false);
                if (tag.GetNameID() == ST_SERVERNAME) {
                    if (tag.IsStr()) {
                        strName = tag.GetStr();
                        if (thePrefs.GetDebugServerTCPLevel() > 0)
                            strInfo.AppendFormat(_T("  Name=%s"), strName);
                    }
                }
                else if (tag.GetNameID() == ST_DESCRIPTION) {
                    if (tag.IsStr()) {
                        strDescription = tag.GetStr();
                        if (thePrefs.GetDebugServerTCPLevel() > 0)
                            strInfo.AppendFormat(_T("  Desc=%s"), strDescription);
                    }
                }
                else if (thePrefs.GetDebugServerTCPLevel() > 0)
                    strInfo.AppendFormat(_T("  ***UnkTag: 0x%02x=%u"), tag.GetNameID(), tag.GetInt());
            }
            if (thePrefs.GetDebugServerTCPLevel() > 0) {
                strInfo += _T('\n');
                Debug(_T("%s"), strInfo);

                UINT uAddData = data.GetLength() - data.GetPosition();
                if (uAddData > 0) {
                    Debug(_T("*** NOTE: OP_ServerIdent: ***AddData: %u bytes\n"), uAddData);
                    DebugHexDump((uint8*)packet + data.GetPosition(), uAddData);
                }
            }

            if (pServer) {
                pServer->SetListName(strName);
                pServer->SetDescription(strDescription);
                if (((uint32*)aucHash)[0] == 0x2A2A2A2A) {
                    const CString& rstrVersion = pServer->GetVersion();
                    if (!rstrVersion.IsEmpty())
                        pServer->SetVersion(_T("eFarm ") + rstrVersion);
                    else
                        pServer->SetVersion(_T("eFarm"));
                }
                theApp.emuledlg->ShowConnectionState();
                theApp.emuledlg->serverwnd->serverlistctrl.RefreshServer(pServer);
                theApp.emuledlg->serverwnd->UpdateMyInfo();
            }
            break;
        }
        // tecxx 1609 2002 - add server's serverlist to own serverlist
        case OP_SERVERLIST: {
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                Debug(_T("ServerMsg - OP_ServerList\n"));
            try {
                CSafeMemFile servers((BYTE*)packet,size);
                UINT count = servers.ReadUInt8();
                // check if packet is valid
                if (1 + count*(4+2) > size)
                    count = 0;
                int addcount = 0;
                while(count)
                {
                    uint32 ip = servers.ReadUInt32();
                    uint16 port = servers.ReadUInt16();
                    CServer* srv = new CServer(port, ipstr(ip));
                    srv->SetListName(srv->GetFullIP());
                    if (!theApp.emuledlg->serverwnd->serverlistctrl.AddServer(srv, true))
                        delete srv;
                    else
                        addcount++;
                    count--;
                }
                if (addcount)
                    AddLogLine(false, GetResString(IDS_NEWSERVERS), addcount);
                if (thePrefs.GetDebugServerTCPLevel() > 0) {
                    UINT uAddData = servers.GetLength() - servers.GetPosition();
                    if (uAddData > 0) {
                        Debug(_T("*** NOTE: OP_ServerList: ***AddData: %u bytes\n"), uAddData);
                        DebugHexDump((uint8*)packet + servers.GetPosition(), uAddData);
                    }
                }
            }
            catch(CFileException* error) {
                if (thePrefs.GetVerbose())
                    DebugLogError(_T("%s"), GetResString(IDS_ERR_BADSERVERLISTRECEIVED));
                error->Delete();
            }
            break;
        }
        case OP_CALLBACKREQUESTED: {
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                Debug(_T("ServerMsg - OP_CallbackRequested\n"));
            if (size == 6)
            {
                uint32 dwIP = PeekUInt32(packet);

                if (theApp.ipfilter->IsFiltered(dwIP)) {
                    theStats.filteredclients++;
                    if (thePrefs.GetLogFilteredIPs())
                        AddDebugLogLine(false, _T("Ignored callback request (IP=%s) - IP filter (%s)"), ipstr(dwIP), theApp.ipfilter->GetLastHit());
                    break;
                }

                if (theApp.clientlist->IsBannedClient(dwIP)) {
                    if (thePrefs.GetLogBannedClients()) {
                        CUpDownClient* pClient = theApp.clientlist->FindClientByIP(dwIP);
                        AddDebugLogLine(false, _T("Ignored callback request from banned client %s; %s"), ipstr(dwIP), pClient->DbgGetClientInfo());
                    }
                    break;
                }

                uint16 nPort = PeekUInt16(packet+4);
                CUpDownClient* client = theApp.clientlist->FindClientByIP(dwIP,nPort);
                if (client)
                    client->TryToConnect();
                else
                {
                    client = new CUpDownClient(0,nPort,dwIP,0,0,true);
                    theApp.clientlist->AddClient(client);
                    client->TryToConnect();
                }
            }
            break;
        }
        case OP_CALLBACK_FAIL: {
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                Debug(_T("ServerMsg - OP_Callback_Fail %s\n"), DbgGetHexDump((uint8*)packet, size));
            break;
        }
        case OP_REJECT: {
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                Debug(_T("ServerMsg - OP_Reject %s\n"), DbgGetHexDump((uint8*)packet, size));
            // this could happen if we send a command with the wrong protocol (e.g. sending a compressed packet to
            // a server which does not support that protocol).
            if (thePrefs.GetVerbose())
                DebugLogError(_T("Server rejected last command"));
            break;
        }
        default:
            if (thePrefs.GetDebugServerTCPLevel() > 0)
                Debug(_T("***NOTE: ServerMsg - Unknown message; opcode=0x%02x  %s\n"), opcode, DbgGetHexDump((uint8*)packet, size));
            ;
        }

        return true;
    }
    catch(CFileException* error)
    {
        if (thePrefs.GetVerbose())
        {
            TCHAR szError[MAX_CFEXP_ERRORMSG];
            error->m_strFileName = _T("server packet");
            error->GetErrorMessage(szError, ARRSIZE(szError));
            DebugLogError(GetResString(IDS_ERR_PACKAGEHANDLING), szError);
        }
        error->Delete();
        ASSERT(0);
        if (opcode==OP_SEARCHRESULT || opcode==OP_FOUNDSOURCES)
            return true;
    }
    catch(CMemoryException* error)
    {
        if (thePrefs.GetVerbose())
            DebugLogError(GetResString(IDS_ERR_PACKAGEHANDLING), _T("CMemoryException"));
        error->Delete();
        ASSERT(0);
        if (opcode==OP_SEARCHRESULT || opcode==OP_FOUNDSOURCES)
            return true;
    }
    catch(CString error)
    {
        if (thePrefs.GetVerbose())
            DebugLogError(GetResString(IDS_ERR_PACKAGEHANDLING), error);
        ASSERT(0);
    }
    catch(...)
    {
        if (thePrefs.GetVerbose())
            DebugLogError(GetResString(IDS_ERR_PACKAGEHANDLING), _T("Unknown exception"));
        ASSERT(0);
    }

    SetConnectionState(CS_DISCONNECTED);
    return false;
}