Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

NeighboursWithRouting.cpp

Go to the documentation of this file.
00001 //
00002 // NeighboursWithRouting.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 "Settings.h"
00025 #include "Statistics.h"
00026 #include "Network.h"
00027 #include "Datagrams.h"
00028 #include "Neighbour.h"
00029 #include "NeighboursWithRouting.h"
00030 #include "QuerySearch.h"
00031 #include "G1Packet.h"
00032 #include "G2Packet.h"
00033 
00034 #ifdef _DEBUG
00035 #undef THIS_FILE
00036 static char THIS_FILE[]=__FILE__;
00037 #define new DEBUG_NEW
00038 #endif
00039 
00040 
00042 // CNeighboursWithRouting construction
00043 
00044 CNeighboursWithRouting::CNeighboursWithRouting()
00045 {
00046 }
00047 
00048 CNeighboursWithRouting::~CNeighboursWithRouting()
00049 {
00050 }
00051 
00053 // CNeighboursWithRouting packet broadcasting
00054 
00055 int CNeighboursWithRouting::Broadcast(CPacket* pPacket, CNeighbour* pExcept, BOOL bGGEP)
00056 {
00057         CSingleLock pLock( &Network.m_pSection, TRUE );
00058         int nCount = 0;
00059 
00060         for ( POSITION pos = GetIterator() ; pos ; )
00061         {
00062                 CNeighbour* pNeighbour = GetNext( pos );
00063 
00064                 if ( pNeighbour != pExcept && pNeighbour->m_nState == nrsConnected )
00065                 {
00066                         if ( pNeighbour->Send( pPacket, FALSE, TRUE ) ) nCount++;
00067                 }
00068         }
00069 
00070         return nCount;
00071 }
00072 
00074 // CNeighboursWithRouting query dispatch
00075 
00076 int CNeighboursWithRouting::RouteQuery(CQuerySearch* pSearch, CPacket* pPacket, CNeighbour* pFrom, BOOL bToHubs)
00077 {
00078         BOOL bHubLoop = FALSE;
00079         int nCount = 0;
00080         POSITION pos;
00081 
00082         CG1Packet* pG1          = ( pPacket->m_nProtocol == PROTOCOL_G1 ) ? (CG1Packet*)pPacket : NULL;
00083         CG2Packet* pG2          = ( pPacket->m_nProtocol == PROTOCOL_G2 ) ? (CG2Packet*)pPacket : NULL;
00084         CG2Packet* pG2Q1        = NULL;
00085         CG2Packet* pG2Q2        = NULL;
00086 
00087         ASSERT( pG1 || pG2 );
00088 
00089         if ( pG2 )
00090         {
00091                 if ( pG2->IsType( "Q2" ) ) pG2Q2 = pG2; else pG2Q1 = pG2;
00092         }
00093 
00094         for ( pos = GetIterator() ; pos ; )
00095         {
00096                 CNeighbour* pNeighbour = (CNeighbour*)GetNext( pos );
00097 
00098                 if ( pNeighbour == pFrom ) continue;
00099                 if ( pNeighbour->m_nState < nrsConnected ) continue;
00100 
00101                 if ( pNeighbour->m_nProtocol == PROTOCOL_G1 )
00102                 {
00103                         if ( bToHubs || pNeighbour->m_nNodeType == ntLeaf )
00104                         {
00105                                 if ( pG1 == NULL )
00106                                 {
00107                                         if ( pG2Q1 != NULL )
00108                                         {
00109                                                 if ( ! pG2Q1->SeekToWrapped() ) break;
00110                                                 pG1 = CG1Packet::New( (GNUTELLAPACKET*)( pG2Q1->m_pBuffer + pG2Q1->m_nPosition ) );
00111                                         }
00112                                         else
00113                                         {
00114                                                 pG1 = pSearch->ToG1Packet();
00115                                         }
00116                                 }
00117 
00118                                 // Dont buffer G2 naturals
00119                                 if ( pNeighbour->SendQuery( pSearch, pG1, pG2Q2 != NULL ) ) nCount++;
00120                                 // if ( pNeighbour->SendQuery( pSearch, pG1, FALSE ) ) nCount++;
00121                         }
00122                 }
00123                 else if ( pNeighbour->m_nProtocol == PROTOCOL_G2 )
00124                 {
00125                         if ( pNeighbour->m_nNodeType == ntLeaf )
00126                         {
00127                                 if ( pG2 == NULL )
00128                                 {
00129                                         //pG2 = pG2Q1 = CG2Packet::New( G2_PACKET_QUERY_WRAP, pG1, Settings.Gnutella1.TranslateTTL );
00130                                         theApp.Message( MSG_ERROR, _T("CNeighboursWithRouting::RouteQuery not relaying wrapped packet to leaf") );
00131                                 }
00132 
00133                                 if ( pNeighbour->SendQuery( pSearch, pG2, FALSE ) ) nCount++;
00134                         }
00135                         else if ( bToHubs )
00136                         {
00137                                 if ( pG2Q2 == NULL )
00138                                 {
00139                                         if ( pG2 == NULL )
00140                                         {
00141                                                 //pG2 = pG2Q1 = CG2Packet::New( G2_PACKET_QUERY_WRAP, pG1, Settings.Gnutella1.TranslateTTL );
00142                                                 theApp.Message( MSG_ERROR, _T("CNeighboursWithRouting::RouteQuery not relaying wrapped packet to hub") );
00143                                         }
00144 
00145                                         if ( pNeighbour->SendQuery( pSearch, pG2, FALSE ) ) nCount++;
00146                                 }
00147                                 else
00148                                 {
00149                                         bHubLoop = TRUE;
00150                                 }
00151                         }
00152                 }
00153         }
00154 
00155         if ( bHubLoop )
00156         {
00157                 if ( pSearch->m_bUDP == FALSE && Datagrams.IsStable() )
00158                 {
00159                         pG2 = pG2->Clone();
00160                         if ( pG2Q2 != pPacket ) pG2Q2->Release();
00161                         pG2Q2 = pG2;
00162 
00163                         BYTE* pPtr = pG2->WriteGetPointer( 5 + 6, 0 );
00164                         *pPtr++ = 0x50;
00165                         *pPtr++ = 6;
00166                         *pPtr++ = 'U';
00167                         *pPtr++ = 'D';
00168                         *pPtr++ = 'P';
00169                         *pPtr++ = Network.m_pHost.sin_addr.S_un.S_un_b.s_b1;
00170                         *pPtr++ = Network.m_pHost.sin_addr.S_un.S_un_b.s_b2;
00171                         *pPtr++ = Network.m_pHost.sin_addr.S_un.S_un_b.s_b3;
00172                         *pPtr++ = Network.m_pHost.sin_addr.S_un.S_un_b.s_b4;
00173                         if ( pPacket->m_bBigEndian )
00174                         {
00175                                 *pPtr++ = (BYTE)( Network.m_pHost.sin_port & 0xFF );
00176                                 *pPtr++ = (BYTE)( ( Network.m_pHost.sin_port >> 8 ) & 0xFF );
00177                         }
00178                         else
00179                         {
00180                                 *pPtr++ = (BYTE)( ( Network.m_pHost.sin_port >> 8 ) & 0xFF );
00181                                 *pPtr++ = (BYTE)( Network.m_pHost.sin_port & 0xFF );
00182                         }
00183                 }
00184 
00185                 for ( pos = GetIterator() ; pos ; )
00186                 {
00187                         CNeighbour* pNeighbour = (CNeighbour*)GetNext( pos );
00188 
00189                         if (    pNeighbour != pFrom &&
00190                                         pNeighbour->m_nState >= nrsConnected &&
00191                                         pNeighbour->m_nProtocol == PROTOCOL_G2 &&
00192                                         pNeighbour->m_nNodeType != ntLeaf )
00193                         {
00194                                 if ( pNeighbour->SendQuery( pSearch, pG2, FALSE ) ) nCount++;
00195                         }
00196                 }
00197         }
00198 
00199         if ( pG1 && pG1 != pPacket ) pG1->Release();
00200         if ( pG2 && pG2 != pPacket ) pG2->Release();
00201 
00202         if ( nCount )
00203         {
00204                 if ( pPacket->m_nProtocol == PROTOCOL_G1 )
00205                         Statistics.Current.Gnutella1.Routed++;
00206                 else if ( pPacket->m_nProtocol == PROTOCOL_G2 )
00207                         Statistics.Current.Gnutella2.Routed++;
00208         }
00209 
00210         return nCount;
00211 }

Generated on Thu Dec 15 10:39:45 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2