void CDatagrams::onQKR(CEndPoint& addr, G2Packet* pPacket) { if(!Neighbours.isG2Hub()) { return; } CEndPoint oRequestedAddress = addr; CEndPoint oSendingAddress = 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("SNA", szType) == 0 && nLength >= 4) { if(nLength >= 16) { Q_IPV6ADDR ip; pPacket->read(&ip, 16); oSendingAddress.setAddress(ip); } else { quint32 nIp = pPacket->readIntBE<quint32>(); oSendingAddress.setAddress(nIp); } } else if(strcmp("RNA", szType) == 0 && nLength >= 6) { if(nLength >= 18) { pPacket->readHostAddress(&oRequestedAddress, false); } else { pPacket->readHostAddress(&oRequestedAddress); } } pPacket->m_nPosition = nNext; } } if(!oRequestedAddress.port() || oRequestedAddress.isFirewalled()) { return; } G2Packet* pAns = G2Packet::newPacket("QKA", true); quint32 nKey = QueryKeys.create(oRequestedAddress); pAns->writePacket("QK", 4); pAns->writeIntLE<quint32>(nKey); G2Packet* pSNA = G2Packet::newPacket("SNA"); pSNA->writeHostAddress(&oSendingAddress); pAns->writePacket(pSNA); pSNA->release(); sendPacket(oRequestedAddress, pAns, false); pAns->release(); #if LOG_QUERY_HANDLING systemLog.postLog(LogSeverity::Debug, "Node %s asked for a query key (0x%08x) for node %s", qPrintable(addr.toStringWithPort()), nKey, qPrintable(oRequestedAddress.toStringWithPort())); #endif // LOG_QUERY_HANDLING }
void CDatagrams::onQuery(CEndPoint &addr, G2Packet *pPacket) { CQueryPtr pQuery = CQuery::fromPacket(pPacket, &addr); if(pQuery.isNull()) { #if LOG_QUERY_HANDLING qDebug() << "Received malformed query from" << qPrintable(addr.toStringWithPort()); #endif // LOG_QUERY_HANDLING return; } if( !Neighbours.isG2Hub() ) { // Stop receiving queries from others // We are here because we just downgraded to leaf mode // Shareaza should not retry with QK == 0 // TODO: test this #if LOG_QUERY_HANDLING systemLog.postLog(LogSeverity::Debug, "Sending null query key to %s because we're not a hub.", qPrintable(addr.toStringWithPort())); #endif // LOG_QUERY_HANDLING G2Packet* pQKA = G2Packet::newPacket("QKA", true); pQKA->writePacket("QK", 4)->writeIntLE<quint32>(0); if( addr != pQuery->m_oEndpoint ) { pQKA->writePacket("SNA", (pQuery->m_oEndpoint.protocol() == QAbstractSocket::IPv6Protocol ? 18 : 6))->writeHostAddress(&pQuery->m_oEndpoint); } sendPacket(pQuery->m_oEndpoint, pQKA); pQKA->release(); return; } if(!QueryKeys.check(pQuery->m_oEndpoint, pQuery->m_nQueryKey)) { #if LOG_QUERY_HANDLING systemLog.postLog(LogSeverity::Debug, "Issuing query key correction for %s.", qPrintable(addr.toStringWithPort())); #endif // LOG_QUERY_HANDLING G2Packet* pQKA = G2Packet::newPacket("QKA", true); pQKA->writePacket("QK", 4)->writeIntLE<quint32>(QueryKeys.create(pQuery->m_oEndpoint)); if( addr != pQuery->m_oEndpoint ) { pQKA->writePacket("SNA", (pQuery->m_oEndpoint.protocol() == QAbstractSocket::IPv6Protocol ? 18 : 6))->writeHostAddress(&pQuery->m_oEndpoint); } sendPacket(addr, pPacket); pQKA->release(); return; } if( !Network.m_oRoutingTable.add(pQuery->m_oGUID, pQuery->m_oEndpoint) ) { #if LOG_QUERY_HANDLING qDebug() << "Query already processed, ignoring"; #endif // LOG_QUERY_HANDLING G2Packet* pQA = Neighbours.createQueryAck(pQuery->m_oGUID, false, 0, false); sendPacket(pQuery->m_oEndpoint, pQA, true); pQA->release(); return; } #if LOG_QUERY_HANDLING qDebug() << "Processing query from: " << qPrintable(addr.toStringWithPort()); #endif // LOG_QUERY_HANDLING // just in case if( pQuery->m_oEndpoint == Network.m_oAddress ) { systemLog.postLog( LogSeverity::Error, Components::Network, "Q2 received via UDP and return address points to us, changing return address to source %s", qPrintable( addr.toStringWithPort() ) ); G2Packet* pUDP = G2Packet::newPacket("UDP"); pUDP->writeHostAddress(&addr); pUDP->writeIntLE<quint32>(0); pPacket->addOrReplaceChild("UDP", pUDP); } Neighbours.m_pSection.lock(); G2Packet* pQA = Neighbours.createQueryAck(pQuery->m_oGUID); sendPacket(pQuery->m_oEndpoint, pQA, true); pQA->release(); Neighbours.routeQuery(pQuery, pPacket); Neighbours.m_pSection.unlock(); // local search }