Ejemplo n.º 1
0
void CChatSessionG2::ParseOutgoingHandshake()
{
	QString sHs = Read(Peek(bytesAvailable()).indexOf("\r\n\r\n") + 4);

	qDebug() << "Chat received:\n" << sHs;

	if( sHs.left(12) == "CHAT/0.2 200" )
	{
		QString sAccept = Parser::GetHeaderValue(sHs, "Accept");
		if( !sAccept.contains("application/x-gnutella2") )
		{
			Send_ChatError("503 Required protocol not accepted");
			CNetworkConnection::Close(true);
			emit systemMessage("Remote host does not support Gnutella2");
			return;
		}

		QString sContentType = Parser::GetHeaderValue(sHs, "Content-Type");
		if( !sContentType.contains("application/x-gnutella2") )
		{
			Send_ChatError("503 Required protocol not provided");
			CNetworkConnection::Close(true);
			emit systemMessage("Remote host does not support Gnutella2");
			return;
		}

		QString sUA = Parser::GetHeaderValue(sHs, "User-Agent");
		if( sUA.indexOf("shareaza", 0, Qt::CaseInsensitive) != -1 )
			m_bShareaza = true;

		emit systemMessage("Exchanging profile information...");
		m_nState = csConnected;
		Send_ChatOK(true);
		SendStartups();
		return;
	}

	qDebug() << m_oAddress.toStringWithPort() << "does not accept chat";

	Close();
}
Ejemplo n.º 2
0
void CG2Node::ParseOutgoingHandshake()
{
    QString sHs = read(peek(bytesAvailable()).indexOf("\r\n\r\n") + 4);

    //qDebug() << "Handshake receive:\n" << sHs;

    QString sAccept = Parser::GetHeaderValue(sHs, "Accept");
    bool bAcceptG2 = sAccept.contains("application/x-gnutella2");

    if( !bAcceptG2 )
    {
        Send_ConnectError("503 Required network not accepted");
        return;
    }

    QString sContentType = Parser::GetHeaderValue(sHs, "Content-Type");
    bool bG2Provided = sContentType.contains("application/x-gnutella2");

    if( !bG2Provided )
    {
        Send_ConnectError("503 Required network not provided");
        return;
    }

    QString sTry = Parser::GetHeaderValue(sHs, "X-Try-Hubs");
    if( bAcceptG2 && bG2Provided && sTry.size() )
    {
        HostCache.AddXTry(sTry);
    }

    if( sHs.left(16) != "GNUTELLA/0.6 200" )
    {
        qDebug() << "Connection rejected: " << sHs.left(sHs.indexOf("\r\n"));
        disconnectFromHost();
        return;
    }

    m_sUserAgent = Parser::GetHeaderValue(sHs, "User-Agent");

    if( m_sUserAgent.isEmpty() )
    {
        Send_ConnectError("503 Anonymous clients are not allowed here");
        return;
    }

	QString sRemoteIP = Parser::GetHeaderValue(sHs, "Remote-IP");
	if( !sRemoteIP.isEmpty() )
		Network.AcquireLocalAddress(sRemoteIP);
	else
	{
		Send_ConnectError("503 Remote-IP header missing");
		return;
	}

    QString sUltra = Parser::GetHeaderValue(sHs, "X-Ultrapeer").toLower();
    //QString sUltraNeeded = Parser::GetHeaderValue(sHs, "X-Ultrapeer-Needed").toLower();

    bool bUltra = (sUltra == "true");
    //bool bUltraNeeded = (sUltraNeeded == "true");

#ifndef _DISABLE_COMPRESSION
    QString sContentEnc = Parser::GetHeaderValue(sHs, "Content-Encoding");
    if( sContentEnc.contains("deflate") )
    {
        if( !EnableInputCompression() )
        {
            qDebug() << "Inflate init error!";
            abort();
            deleteLater();
            return;
        }
    }
#endif

    bool bAcceptDeflate = false;
#ifndef _DISABLE_COMPRESSION
    QString sAcceptEnc = Parser::GetHeaderValue(sHs, "Accept-Encoding");
    if( sAcceptEnc.contains("deflate") && Network.isHub() )
    {
        bAcceptDeflate = true;
    }
#endif

    if( bUltra )
    {
        if( !Network.NeedMore(G2_HUB) )
        {
            Send_ConnectError("503 Maximum hub connections reached");
            return;
        }
        m_nType = G2_HUB;
    }
    else
    {
        if( !Network.NeedMore(G2_LEAF) )
        {
            Send_ConnectError("503 Maximum leaf connections reached");
            return;
        }
        m_nType = G2_LEAF;
    }

    Send_ConnectOK(true, bAcceptDeflate);

#ifndef _DISABLE_COMPRESSION
    if( bAcceptDeflate )
    {
        if( !EnableOutputCompression() )
        {
            qDebug() << "Deflate init error!";
            abort();
            deleteLater();
            return;
        }
    }
#endif

    m_nState = nsConnected;
    emit NodeStateChanged();

    SendStartups();
    m_tLastPacketIn = m_tLastPacketOut = time(0);

}
Ejemplo n.º 3
0
void CG2Node::ParseIncomingHandshake()
{
    qint32 nIndex = peek(bytesAvailable()).indexOf("\r\n\r\n");
    QString sHs = read(nIndex + 4);

    //qDebug() << "Handshake receive:\n" << sHs;

    if( m_sUserAgent.isEmpty() )
        m_sUserAgent = Parser::GetHeaderValue(sHs, "User-Agent");

    if( m_sUserAgent.isEmpty() )
    {
        Send_ConnectError("503 Anonymous clients are not allowed here");
        return;
    }


    if( sHs.startsWith("GNUTELLA CONNECT/0.6") )
    {
        QString sAccept = Parser::GetHeaderValue(sHs, "Accept");
        bool bAcceptG2 = sAccept.contains("application/x-gnutella2");

        if( !bAcceptG2 )
        {
            Send_ConnectError("503 Required network not accepted");
            return;
        }

#ifndef _DISABLE_COMPRESSION
        m_bAcceptDeflate = false;
        QString sAcceptEnc = Parser::GetHeaderValue(sHs, "Accept-Encoding");
        if( sAcceptEnc.contains("deflate") && Network.isHub() )
        {
            m_bAcceptDeflate = true;
        }
#endif

        QString sUltra = Parser::GetHeaderValue(sHs, "X-Ultrapeer").toLower();
        //QString sUltraNeeded = Parser::GetHeaderValue(sHs, "X-Ultrapeer-Needed").toLower();
        if( sUltra.isEmpty() )
        {
            Send_ConnectError("503 No hub mode specified");
            return;
        }

        QString sRemoteIP = Parser::GetHeaderValue(sHs, "Remote-IP");
        if( !sRemoteIP.isEmpty() )
            Network.AcquireLocalAddress(sRemoteIP);
        else
        {
            Send_ConnectError("503 Remote-IP header missing");
            return;
        }

        bool bUltra = (sUltra == "true");

        if( bUltra )
        {
            if( !Network.NeedMore(G2_HUB) )
            {
                Send_ConnectError("503 Maximum hub connections reached");
                return;
            }
            m_nType = G2_HUB;
        }
        else
        {
            if( !Network.NeedMore(G2_LEAF) )
            {
                Send_ConnectError("503 Maximum leaf connections reached");
                return;
            }
            m_nType = G2_LEAF;
        }

        Send_ConnectOK(false, m_bAcceptDeflate);

    }
    else if( sHs.contains(" 200 OK") )
    {
        QString sContentType = Parser::GetHeaderValue(sHs, "Content-Type");
        bool bG2Provided = sContentType.contains("application/x-gnutella2");

        if( !bG2Provided )
        {
            Send_ConnectError("503 Required network not provided");
            return;
        }

#ifndef _DISABLE_COMPRESSION
        QString sContentEnc = Parser::GetHeaderValue(sHs, "Content-Encoding");
        if( sContentEnc.contains("deflate") )
        {
            if( !EnableInputCompression() )
            {
                qDebug() << "Inflate init error!";
                abort();
                deleteLater();
                return;
            }
        }

        if( m_bAcceptDeflate )
        {
            if( !EnableOutputCompression() )
            {
                qDebug() << "Deflate init error!";
                abort();
                deleteLater();
                return;
            }
        }
#endif

        m_nState = nsConnected;
        emit NodeStateChanged();

        SendStartups();
        m_tLastPacketIn = m_tLastPacketOut = time(0);


    }
    else
    {
        qDebug() << "Connection rejected: " << sHs.left(sHs.indexOf("\r\n"));
        m_nState = nsClosing;
        emit NodeStateChanged();
        close();
    }
}