Advertisement
smokex

Quazaa OnPing

Sep 27th, 2013
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void CG2Node::OnPing(G2Packet* pPacket)
  2. {
  3.     ASSUME_LOCK(Neighbours.m_pSection);
  4.  
  5.     bool bUdp = false;
  6.     bool bRelay = false;
  7.     //  bool bTestFirewall = false;
  8.     CEndPoint addr;
  9.  
  10.     if(pPacket->m_bCompound)
  11.     {
  12.         char szType[9];
  13.         quint32 nLength = 0, nNext = 0;
  14.  
  15.         while(pPacket->ReadPacket(&szType[0], nLength))
  16.         {
  17.             nNext = pPacket->m_nPosition + nLength;
  18.  
  19.             if(strcmp("UDP", szType) == 0 && nLength >= 6)
  20.             {
  21.                 pPacket->ReadHostAddress(&addr);
  22.                 if(addr.isValid())
  23.                 {
  24.                     bUdp = true;
  25.                 }
  26.             }
  27.             else if(strcmp("RELAY", szType) == 0)
  28.             {
  29.                 bRelay = true;
  30.             }
  31.             else if(strcmp("TFW", szType) == 0)
  32.             {
  33.                 //              bTestFirewall = true;
  34.             }
  35.  
  36.             pPacket->m_nPosition = nNext;
  37.         }
  38.  
  39.     }
  40.  
  41.     if(!bUdp && !bRelay)
  42.     {
  43.         // direct ping
  44.         G2Packet* pPong = G2Packet::New("PO", false);
  45.         SendPacket(pPong, false, true);
  46.         return;
  47.     }
  48.  
  49.     if(bUdp && !bRelay)
  50.     {
  51.         // /PI/UDP
  52.  
  53.         if(Neighbours.IsG2Hub())
  54.         {
  55.             G2Packet* pRelay = G2Packet::New("RELAY");
  56.             pPacket->PrependPacket(pRelay);
  57.  
  58.             int nRelayed = 0, nCount = Neighbours.GetCount();
  59.             QList<int> lToRelayIndex;
  60.  
  61.             for(int i = 0; i < nCount && nRelayed < quazaaSettings.Gnutella2.PingRelayLimit; ++i)
  62.             {
  63.                 int nIndex = qrand() % nCount;
  64.                 if(!lToRelayIndex.contains(nIndex))
  65.                 {
  66.                     CNeighbour* pNode = Neighbours.GetAt(nIndex);
  67.                     if(pNode != this
  68.                        && pNode->m_nProtocol == dpG2
  69.                        && pNode->m_nState == nsConnected
  70.                        && static_cast<CG2Node*>(pNode)->m_nType == G2_LEAF )
  71.                     {
  72.                         pPacket->AddRef();
  73.                         ((CG2Node*)pNode)->SendPacket(pPacket, true, true);
  74.                         nRelayed++;
  75.                     }
  76.                 }
  77.             }
  78.             return;
  79.         }
  80.     }
  81.  
  82.     if(bUdp && bRelay)
  83.     {
  84.         G2Packet* pPong = G2Packet::New("PO", true);
  85.         pPong->WritePacket("RELAY", 0);
  86.         Datagrams.SendPacket(addr, pPong, true);
  87.         pPong->Release();
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement