示例#1
0
bool CNetwork::RoutePacket(QUuid& pTargetGUID, G2Packet* pPacket)
{
	CG2Node* pNode = 0;
	CEndPoint pAddr;

	if(m_oRoutingTable.Find(pTargetGUID, &pNode, &pAddr))
	{
		if(pNode)
		{
			pNode->SendPacket(pPacket, true, false);
			systemLog.postLog(LogSeverity::Debug, QString("CNetwork::RoutePacket %1 Packet: %2 routed to neighbour: %3").arg(pTargetGUID.toString()).arg(pPacket->GetType()).arg(pNode->m_oAddress.toString().toAscii().constData()));
			//qDebug() << "CNetwork::RoutePacket " << pTargetGUID.toString() << " Packet: " << pPacket->GetType() << " routed to neighbour: " << pNode->m_oAddress.toString().toAscii().constData();
			return true;
		}
		else if(!pAddr.isNull())
		{
			Datagrams.SendPacket(pAddr, pPacket, true);
			systemLog.postLog(LogSeverity::Debug, QString("CNetwork::RoutePacket %1 Packet: %2 routed to remote node: %3").arg(pTargetGUID.toString()).arg(pPacket->GetType()).arg(pNode->m_oAddress.toString().toAscii().constData()));
			//qDebug() << "CNetwork::RoutePacket " << pTargetGUID.toString() << " Packet: " << pPacket->GetType() << " routed to remote node: " << pNode->m_oAddress.toString().toAscii().constData();
			return true;
		}
		systemLog.postLog(LogSeverity::Debug, QString("CNetwork::RoutePacket - No node and no address!"));
		//qDebug() << "CNetwork::RoutePacket - weird thing, should not happen...";
	}

	systemLog.postLog(LogSeverity::Debug, QString("CNetwork::RoutePacket %1 Packet: %2 DROPED!").arg(pTargetGUID.toString()).arg(pPacket->GetType()));
	//qDebug() << "CNetwork::RoutePacket " << pTargetGUID.toString() << " Packet: " << pPacket->GetType() << " DROPPED!";
	return false;
}
示例#2
0
bool CNetwork::RoutePacket(G2Packet* pPacket, CG2Node* pNbr)
{
	QUuid pGUID;

	if(pPacket->GetTo(pGUID) && pGUID != quazaaSettings.Profile.GUID)   // no i adres != moj adres
	{
		CG2Node* pNode = 0;
		CEndPoint pAddr;

		if(m_oRoutingTable.Find(pGUID, &pNode, &pAddr))
		{
			bool bForwardTCP = false;
			bool bForwardUDP = false;

			if(pNbr)
			{
				if(pNbr->m_nType == G2_LEAF)    // if received from leaf - can forward anywhere
				{
					bForwardTCP = bForwardUDP = true;
				}
				else    // if received from a hub - can be forwarded to leaf
				{
					if(pNode && pNode->m_nType == G2_LEAF)
					{
						bForwardTCP = true;
					}
				}
			}
			else    // received from udp - do not forward via udp
			{
				bForwardTCP = true;
			}

			if(pNode && bForwardTCP)
			{
				pNode->SendPacket(pPacket, true, false);
				return true;
			}
			else if(!pAddr.isNull() && bForwardUDP)
			{
				Datagrams.SendPacket(pAddr, pPacket, true);
				return true;
			}
			// drop
		}
		return true;
	}
	return false;
}
示例#3
0
void CNetwork::DispatchKHL()
{
	QMutexLocker l(&Neighbours.m_pSection);

	if(Neighbours.m_lNodes.isEmpty())
	{
		return;
	}

	G2Packet* pKHL = G2Packet::New("KHL");
	quint32 ts = time(0);
	pKHL->WritePacket("TS", 4)->WriteIntLE(ts);

	for(QList<CG2Node*>::iterator itNode = Neighbours.begin(); itNode != Neighbours.end(); ++itNode)
	{
		CG2Node* pNode = *itNode;
		if(pNode->m_nType == G2_HUB && pNode->m_nState == nsConnected)
		{
			pKHL->WritePacket("NH", 6)->WriteHostAddress(&pNode->m_oAddress);
		}
	}

	quint32 nCount = 0;

	for(; nCount < (quint32)quazaaSettings.Gnutella2.KHLHubCount && HostCache.size() > nCount; nCount++)
	{
		pKHL->WritePacket("CH", 10)->WriteHostAddress(&HostCache.m_lHosts.at(nCount)->m_oAddress);
		pKHL->WriteIntLE(&HostCache.m_lHosts.at(nCount)->m_tTimestamp);
	}


	for(QList<CG2Node*>::iterator itNode = Neighbours.begin(); itNode != Neighbours.end(); ++itNode)
	{
		CG2Node* pNode = *itNode;
		if(pNode->m_nState == nsConnected)
		{
			pNode->SendPacket(pKHL, false, false);
		}
	}
	pKHL->Release();
}
示例#4
0
文件: g2node.cpp 项目: geekt/quazaa
void CG2Node::OnPing(G2Packet* pPacket)
{
	bool bUdp = false;
    bool bRelay = false;
    bool bTestFirewall = false;
    IPv4_ENDPOINT addr;

	if( pPacket->m_bCompound )
    {
		char szType[9];
		quint32 nLength = 0, nNext = 0;

		while( pPacket->ReadPacket(&szType[0], nLength) )
		{
			nNext = pPacket->m_nPosition + nLength;

			if( strcmp("UDP", szType) == 0 && nLength >= 6)
			{
				pPacket->ReadHostAddress(&addr);
				if( addr.ip != 0 && addr.port != 0)
					bUdp = true;
			}
			else if( strcmp("RELAY", szType) == 0 )
			{
				bRelay = true;
			}
			else if( strcmp("TFW", szType) == 0 )
			{
				bTestFirewall = true;
			}

			pPacket->m_nPosition = nNext;
		}

    }

    if( !bUdp && !bRelay )
    {
        // direct ping
		G2Packet* pPong = G2Packet::New("PO", false);
		SendPacket(pPong, false, true);
        return;
    }

    if( bUdp && !bRelay )
    {
        // /PI/UDP

        if( Network.isHub() )
        {
			char* pRelay = pPacket->WriteGetPointer(7, 0);
			*pRelay++ = 0x60;
			*pRelay++ = 0;
			*pRelay++ = 'R'; *pRelay++ = 'E'; *pRelay++ = 'L';
			*pRelay++ = 'A'; *pRelay++ = 'Y';


            QList<CG2Node*> lToRelay;

            for( int i = 0; i < Network.m_lNodes.size(); i++ )
            {
                if( Network.m_lNodes.at(i)->m_nState == nsConnected )
                    lToRelay.append(Network.m_lNodes[i]);
            }

			for( int nCount = 0; nCount < quazaaSettings.Gnutella2.PingRelayLimit && lToRelay.size(); nCount++ )
            {
                int nIndex = qrand() % lToRelay.size();
                pPacket->AddRef();
                CG2Node* pNode = lToRelay.at(nIndex);
				pNode->SendPacket(pPacket, true, true);
                lToRelay.removeAt(nIndex);
            }

            return;
        }
    }

    if( bUdp && bRelay )
    {
		G2Packet* pPong = G2Packet::New("PO", true);
		pPong->WritePacket("RELAY", 0);
        Datagrams.SendPacket(addr, pPong, true);
        pPong->Release();
	}
}