summaryrefslogtreecommitdiffstats
path: root/libktorrent/torrent/utpex.cpp
blob: 68361e01d917141739558183d84c5b535c30bfeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   [email protected]                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/
#include <net/address.h>
#include <util/functions.h>
#include <util/log.h>
#include "utpex.h"
#include "peer.h"
#include "packetwriter.h"
#include "bdecoder.h"
#include "bencoder.h"
#include "bnode.h"
#include "peermanager.h"


namespace bt
{

	UTPex::UTPex(Peer* peer,Uint32 id) : peer(peer),id(id),last_updated(0)
	{}


	UTPex::~UTPex()
	{}
	
	
	
	void UTPex::handlePexPacket(const Uint8* packet,Uint32 size)
	{
		if (size <= 2 || packet[1] != 1)
			return;
		
		TQByteArray 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)
				{
					TQByteArray 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);
	}
		
	bool UTPex::needsUpdate() const
	{
		return bt::GetCurrentTime() - last_updated >= 60*1000;
	}
	
	void UTPex::update(PeerManager* pman)
	{
		last_updated = bt::GetCurrentTime();
		
		std::map<Uint32,net::Address> added;
		std::map<Uint32,net::Address> npeers;
		
		PeerManager::CItr itr = pman->beginPeerList();
		while (itr != pman->endPeerList())
		{
			const Peer* p = *itr;
			if (p != peer)
			{
				npeers.insert(std::make_pair(p->getID(),p->getAddress()));
				if (peers.count(p->getID()) == 0)
				{
					// new one, add to added
					added.insert(std::make_pair(p->getID(),p->getAddress()));
				}
				else
				{
					// erase from old list, so only the dropped ones are left
					peers.erase(p->getID());
				}
			}
			itr++;
		}
		
		if (!(peers.size() == 0 && added.size() == 0))
		{
			// encode the whole lot
			TQByteArray data;
			BEncoder enc(new BEncoderBufferOutput(data));
			enc.beginDict();
			enc.write(TQString("added"));
			encode(enc,added);
			enc.write(TQString("added.f")); // no idea what this added.f thing means
			enc.write(TQString(""));
			enc.write(TQString("dropped"));
			encode(enc,peers);
			enc.end();
			
			peer->getPacketWriter().sendExtProtMsg(id,data);
		}

		peers = npeers;
	}	
	
	void UTPex::encode(BEncoder & enc,const std::map<Uint32,net::Address> & ps)
	{
		if (ps.size() == 0)
		{
			enc.write(TQString(""));
			return;
		}
		
		Uint8* buf = new Uint8[ps.size() * 6];
		Uint32 size = 0;
		
		std::map<Uint32,net::Address>::const_iterator i = ps.begin();
		while (i != ps.end())
		{
			const net::Address & addr = i->second;
			WriteUint32(buf,size,addr.ip());
			WriteUint16(buf,size + 4,addr.port());
			size += 6;
			i++;
		}
		
		enc.write(buf,size);
		delete [] buf;
	}
}