00001 // 00002 // NeighboursWithED2K.cpp 00003 // 00004 // Copyright (c) Shareaza Development Team, 2002-2005. 00005 // This file is part of SHAREAZA (www.shareaza.com) 00006 // 00007 // Shareaza is free software; you can redistribute it 00008 // and/or modify it under the terms of the GNU General Public License 00009 // as published by the Free Software Foundation; either version 2 of 00010 // the License, or (at your option) any later version. 00011 // 00012 // Shareaza is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with Shareaza; if not, write to the Free Software 00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 // 00021 00022 #include "StdAfx.h" 00023 #include "Shareaza.h" 00024 #include "NeighboursWithED2K.h" 00025 #include "EDNeighbour.h" 00026 #include "EDPacket.h" 00027 #include "Datagrams.h" 00028 #include "Network.h" 00029 #include "ED2K.h" 00030 00031 #ifdef _DEBUG 00032 #undef THIS_FILE 00033 static char THIS_FILE[]=__FILE__; 00034 #define new DEBUG_NEW 00035 #endif 00036 00037 00039 // CNeighboursWithED2K construction 00040 00041 CNeighboursWithED2K::CNeighboursWithED2K() 00042 { 00043 ZeroMemory( m_pEDSources, sizeof(MD4) * 256 ); 00044 ZeroMemory( m_tEDSources, sizeof(DWORD) * 256 ); 00045 } 00046 00047 CNeighboursWithED2K::~CNeighboursWithED2K() 00048 { 00049 } 00050 00052 // CNeighboursWithED2K server lookup 00053 00054 CEDNeighbour* CNeighboursWithED2K::GetDonkeyServer() const 00055 { 00056 for ( POSITION pos = GetIterator() ; pos ; ) 00057 { 00058 CEDNeighbour* pNeighbour = (CEDNeighbour*)GetNext( pos ); 00059 00060 if ( pNeighbour->m_nProtocol == PROTOCOL_ED2K ) 00061 { 00062 if ( pNeighbour->m_nState == nrsConnected && 00063 pNeighbour->m_nClientID != 0 ) 00064 { 00065 return pNeighbour; 00066 } 00067 } 00068 } 00069 00070 return NULL; 00071 } 00072 00074 // CNeighboursWithED2K server lookup 00075 00076 void CNeighboursWithED2K::CloseDonkeys() 00077 { 00078 for ( POSITION pos = GetIterator() ; pos ; ) 00079 { 00080 CEDNeighbour* pNeighbour = (CEDNeighbour*)GetNext( pos ); 00081 00082 if ( pNeighbour->m_nProtocol == PROTOCOL_ED2K ) pNeighbour->Close(); 00083 } 00084 } 00085 00087 // CNeighboursWithED2K advertise a new download 00088 00089 void CNeighboursWithED2K::SendDonkeyDownload(CDownload* pDownload) 00090 { 00091 for ( POSITION pos = GetIterator() ; pos ; ) 00092 { 00093 CEDNeighbour* pNeighbour = (CEDNeighbour*)GetNext( pos ); 00094 00095 if ( pNeighbour->m_nProtocol == PROTOCOL_ED2K ) 00096 { 00097 pNeighbour->SendSharedDownload( pDownload ); 00098 } 00099 } 00100 } 00101 00103 // CNeighboursWithED2K server push 00104 00105 BOOL CNeighboursWithED2K::PushDonkey(DWORD nClientID, IN_ADDR* pServerAddress, WORD nServerPort) 00106 { 00107 if ( ! Network.IsListening() ) return FALSE; 00108 00109 CEDNeighbour* pNeighbour = (CEDNeighbour*)Get( pServerAddress ); 00110 00111 if ( ( pNeighbour != NULL ) && ( pNeighbour->m_nProtocol == PROTOCOL_ED2K ) && 00112 ( ! CEDPacket::IsLowID( pNeighbour->m_nClientID ) ) ) 00113 { 00114 CEDPacket* pPacket = CEDPacket::New( ED2K_C2S_CALLBACKREQUEST ); 00115 pPacket->WriteLongLE( nClientID ); 00116 pNeighbour->Send( pPacket ); 00117 return TRUE; 00118 } 00119 00120 /* 00121 lugdunum requests no more of this 00122 CEDPacket* pPacket = CEDPacket::New( ED2K_C2SG_CALLBACKREQUEST ); 00123 pPacket->WriteLongLE( Network.m_pHost.sin_addr.S_un.S_addr ); 00124 pPacket->WriteShortLE( htons( Network.m_pHost.sin_port ) ); 00125 pPacket->WriteLongLE( nClientID ); 00126 Datagrams.Send( pServerAddress, nServerPort + 4, pPacket ); 00127 return TRUE; 00128 */ 00129 00130 return FALSE; 00131 } 00132 00134 // CNeighboursWithED2K quick source lookup 00135 00136 BOOL CNeighboursWithED2K::FindDonkeySources(MD4* pED2K, IN_ADDR* pServerAddress, WORD nServerPort) 00137 { 00138 if ( ! Network.IsListening() ) return FALSE; 00139 00140 int nHash = (int)pServerAddress->S_un.S_un_b.s_b4 & 255; 00141 DWORD tNow = GetTickCount(); 00142 00143 if ( nHash < 0 ) nHash = 0; 00144 else if ( nHash > 255 ) nHash = 255; 00145 00146 if ( m_pEDSources[ nHash ] == *pED2K ) 00147 { 00148 if ( tNow - m_tEDSources[ nHash ] < 3600000 ) return FALSE; 00149 } 00150 else 00151 { 00152 if ( tNow - m_tEDSources[ nHash ] < 15000 ) return FALSE; 00153 m_pEDSources[ nHash ] = *pED2K; 00154 } 00155 00156 m_tEDSources[ nHash ] = tNow; 00157 00158 CEDPacket* pPacket = CEDPacket::New( ED2K_C2SG_GETSOURCES ); 00159 pPacket->Write( pED2K, sizeof(MD4) ); 00160 Datagrams.Send( pServerAddress, nServerPort + 4, pPacket ); 00161 00162 return TRUE; 00163 }