示例#1
0
void UTPex::handlePexPacket(const Uint8* packet,Uint32 size)
{
    if (size <= 2 || packet[1] != 1)
        return;

    QByteArray tmp;
    tmp.setRawData((const char*)packet,size);
    BNode* node = 0;
    try
    {
        BDecoder dec(tmp,false,2);
        node = dec.decode();
        if (node && node->getType() == BNode::DICT)
        {
            BDictNode* dict = (BDictNode*)node;

            // ut_pex packet, emit signal to notify PeerManager
            BValueNode* val = dict->getValue("added");
            if (val)
            {
                QByteArray data = val->data().toByteArray();
                peer->emitPex(data);
            }
        }
    }
    catch (...)
    {
        // just ignore invalid packets
        Out(SYS_CON|LOG_DEBUG) << "Invalid extended packet" << endl;
    }
    delete node;
    tmp.resetRawData((const char*)packet,size);
}
示例#2
0
	QByteArray BListNode::getByteArray(Uint32 idx)
	{
		BValueNode* v = getValue(idx);
		if (!v)
			throw bt::Error("Key not found in dict");
		
		if (v->data().getType() != bt::Value::STRING)
			throw bt::Error("Incompatible type");
		
		return v->data().toByteArray();
	}
示例#3
0
	qint64 BListNode::getInt64(Uint32 idx)
	{
		BValueNode* v = getValue(idx);
		if (!v)
			throw bt::Error("Key not found in dict");
		
		if (v->data().getType() != bt::Value::INT64 && v->data().getType() != bt::Value::INT)
			throw bt::Error("Incompatible type");
		
		return v->data().toInt64();
	}
示例#4
0
	QByteArray BDictNode::getByteArray(const QString & key)
	{
		BValueNode* v = getValue(key);
		if (!v)
			throw bt::Error("Key not found in dict");
		
		if (v->data().getType() != bt::Value::STRING)
			throw bt::Error("Incompatible type");
		
		return v->data().toByteArray();
	}
示例#5
0
	qint64 BDictNode::getInt64(const QString & key)
	{
		BValueNode* v = getValue(key);
		if (!v)
			throw bt::Error("Key not found in dict");
		
		if (v->data().getType() != bt::Value::INT64 && v->data().getType() != bt::Value::INT)
			throw bt::Error("Incompatible type");
		
		return v->data().toInt64();
	}
示例#6
0
void HTTPTracker::onScrapeResult(KIO::Job* j)
{
    if (j->error())
    {
        Out(SYS_TRK|LOG_IMPORTANT) << "Scrape failed : " << j->errorString() << endl;
        return;
    }

    KIO::StoredTransferJob* st = (KIO::StoredTransferJob*)j;
    BDecoder dec(st->data(),false,0);
    BNode* n = 0;

    try
    {
        n = dec.decode();
    }
    catch (bt::Error & err)
    {
        Out(SYS_TRK|LOG_IMPORTANT) << "Invalid scrape data " << err.toString() << endl;
        return;
    }

    if (n && n->getType() == BNode::DICT)
    {
        BDictNode* d = (BDictNode*)n;
        d = d->getDict("files");
        if (d)
        {
            d = d->getDict(tor->getInfoHash().toByteArray());
            if (d)
            {
                BValueNode* vn = d->getValue("complete");
                if (vn && vn->data().getType() == Value::INT)
                {
                    seeders = vn->data().toInt();
                }


                vn = d->getValue("incomplete");
                if (vn && vn->data().getType() == Value::INT)
                {
                    leechers = vn->data().toInt();
                }

                Out(SYS_TRK|LOG_DEBUG) << "Scrape : leechers = " << leechers
                                       << ", seeders = " << seeders << endl;
            }
        }
    }

    delete n;
}
示例#7
0
	QString BListNode::getString(Uint32 idx,QTextCodec* tc)
	{
		BValueNode* v = getValue(idx);
		if (!v)
			throw bt::Error("Key not found in dict");
		
		if (v->data().getType() != bt::Value::STRING)
			throw bt::Error("Incompatible type");
		
		if (!tc)
			return v->data().toString();
		else
			return v->data().toString(tc);
	}
示例#8
0
	QString BDictNode::getString(const QString & key,QTextCodec* tc)
	{
		BValueNode* v = getValue(key);
		if (!v)
			throw bt::Error("Key not found in dict");
		
		if (v->data().getType() != bt::Value::STRING)
			throw bt::Error("Incompatible type");
		
		if (!tc)
			return v->data().toString();
		else
			return v->data().toString(tc);
	}
示例#9
0
bool HTTPTracker::updateData(const QByteArray & data)
{
//#define DEBUG_PRINT_RESPONSE
#ifdef DEBUG_PRINT_RESPONSE
    Out() << "Data : " << endl;
    Out() << QString(data) << endl;
#endif
    // search for dictionary, there might be random garbage infront of the data
    Uint32 i = 0;
    while (i < data.size())
    {
        if (data[i] == 'd')
            break;
        i++;
    }

    if (i == data.size())
    {
        failures++;
        requestFailed(i18n("Invalid response from tracker"));
        return false;
    }

    BDecoder dec(data,false,i);
    BNode* n = 0;
    try
    {
        n = dec.decode();
    }
    catch (...)
    {
        failures++;
        requestFailed(i18n("Invalid data from tracker"));
        return false;
    }

    if (!n || n->getType() != BNode::DICT)
    {
        failures++;
        requestFailed(i18n("Invalid response from tracker"));
        return false;
    }

    BDictNode* dict = (BDictNode*)n;
    if (dict->getData("failure reason"))
    {
        BValueNode* vn = dict->getValue("failure reason");
        QString msg = vn->data().toString();
        delete n;
        failures++;
        requestFailed(msg);
        return false;
    }

    BValueNode* vn = dict->getValue("interval");

    // if no interval is specified, use 5 minutes
    if (vn)
        interval = vn->data().toInt();
    else
        interval = 5 * 60;

    vn = dict->getValue("incomplete");
    if (vn)
        leechers = vn->data().toInt();

    vn = dict->getValue("complete");
    if (vn)
        seeders = vn->data().toInt();

    BListNode* ln = dict->getList("peers");
    if (!ln)
    {
        // no list, it might however be a compact response
        vn = dict->getValue("peers");
        if (!vn)
        {
            delete n;
            failures++;
            requestFailed(i18n("Invalid response from tracker"));
            return false;
        }

        QByteArray arr = vn->data().toByteArray();
        for (Uint32 i = 0; i < arr.size(); i+=6)
        {
            Uint8 buf[6];
            for (int j = 0; j < 6; j++)
                buf[j] = arr[i + j];

            addPeer(QHostAddress(ReadUint32(buf,0)).toString(),ReadUint16(buf,4));
        }
    }
    else
    {
        for (Uint32 i = 0; i < ln->getNumChildren(); i++)
        {
            BDictNode* dict = dynamic_cast<BDictNode*>(ln->getChild(i));

            if (!dict)
                continue;

            BValueNode* ip_node = dict->getValue("ip");
            BValueNode* port_node = dict->getValue("port");

            if (!ip_node || !port_node)
                continue;

            addPeer(ip_node->data().toString(),port_node->data().toInt());
        }
    }

    delete n;
    return true;
}
示例#10
0
	bool HTTPTracker::updateData(const QByteArray & data)
	{
//#define DEBUG_PRINT_RESPONSE
#ifdef DEBUG_PRINT_RESPONSE
		Out(SYS_TRK | LOG_DEBUG) << "Data : " << endl;
		Out(SYS_TRK | LOG_DEBUG) << QString(data) << endl;
#endif
		// search for dictionary, there might be random garbage infront of the data
		int i = 0;
		while (i < data.size())
		{
			if (data[i] == 'd')
				break;
			i++;
		}

		if (i == data.size())
		{
			failures++;
			failed(i18n("Invalid response from tracker"));
			return false;
		}

		BDecoder dec(data, false, i);
		BNode* n = 0;
		try
		{
			n = dec.decode();
		}
		catch (...)
		{
			failures++;
			failed(i18n("Invalid data from tracker"));
			return false;
		}

		if (!n || n->getType() != BNode::DICT)
		{
			failures++;
			failed(i18n("Invalid response from tracker"));
			return false;
		}

		BDictNode* dict = (BDictNode*)n;
		if (dict->getData("failure reason"))
		{
			BValueNode* vn = dict->getValue("failure reason");
			error = vn->data().toString();
			delete n;
			failures++;
			failed(error);
			return false;
		}

		if (dict->getData("warning message"))
		{
			BValueNode* vn = dict->getValue("warning message");
			warning = vn->data().toString();
		}
		else
			warning.clear();

		BValueNode* vn = dict->getValue("interval");

		// if no interval is specified, use 5 minutes
		if (vn)
			interval = vn->data().toInt();
		else
			interval = 5 * 60;

		vn = dict->getValue("incomplete");
		if (vn)
			leechers = vn->data().toInt();

		vn = dict->getValue("complete");
		if (vn)
			seeders = vn->data().toInt();

		BListNode* ln = dict->getList("peers");
		if (!ln)
		{
			// no list, it might however be a compact response
			vn = dict->getValue("peers");
			if (vn && vn->data().getType() == Value::STRING)
			{
				QByteArray arr = vn->data().toByteArray();
				for (int i = 0;i < arr.size();i += 6)
				{
					Uint8 buf[6];
					for (int j = 0;j < 6;j++)
						buf[j] = arr[i + j];

					Uint32 ip = ReadUint32(buf, 0);
					addPeer(net::Address(ip, ReadUint16(buf, 4)), false);
				}
			}
		}
		else
		{
			for (Uint32 i = 0;i < ln->getNumChildren();i++)
			{
				BDictNode* dict = dynamic_cast<BDictNode*>(ln->getChild(i));

				if (!dict)
					continue;

				BValueNode* ip_node = dict->getValue("ip");
				BValueNode* port_node = dict->getValue("port");

				if (!ip_node || !port_node)
					continue;

				net::Address addr(ip_node->data().toString(), port_node->data().toInt());
				addPeer(addr, false);
			}
		}

		// Check for IPv6 compact peers
		vn = dict->getValue("peers6");
		if (vn && vn->data().getType() == Value::STRING)
		{
			QByteArray arr = vn->data().toByteArray();
			for (int i = 0;i < arr.size();i += 18)
			{
				Q_IPV6ADDR ip;
				memcpy(ip.c, arr.data() + i, 16);
				quint16 port = ReadUint16((const Uint8*)arr.data() + i, 16);

				addPeer(net::Address(ip, port), false);
			}
		}

		delete n;
		return true;
	}