summaryrefslogtreecommitdiffstats
path: root/libktorrent/torrent/packetreader.cpp
blob: b17becc5f0a3b80471f96405e56c0efc5fc00434 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@gmail.com                                               *
 *                                                                         *
 *   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.             *
 ***************************************************************************/
 
//#define LOG_PACKET
#ifdef LOG_PACKET
#include <sys/types.h>
#include <unistd.h>
#endif

#include <util/log.h>
#include <util/file.h>
#include <util/functions.h>
#include "packetreader.h"
#include "peer.h"


namespace bt
{
#ifdef LOG_PACKET
	static void LogPacket(const Uint8* data,Uint32 size,Uint32 len)
	{
		TQString file = TQString("/tmp/kt-packetreader-%1.log").arg(getpid());
		File fptr;
		if (!fptr.open(file,"a"))
			return;
		
		
		TQString tmp = TQString("PACKET len = %1, type = %2\nDATA: \n").arg(len).arg(data[0]);
		
		fptr.write(tmp.ascii(),tmp.length());
		
		Uint32 j = 0;
		if (size <= 40)
		{
			for (Uint32 i = 0;i < size;i++)
			{
				tmp = TQString("0x%1 ").arg(data[i],0,16);
				fptr.write(tmp.ascii(),tmp.length());
				j++;
				if (j > 10)
				{
					fptr.write("\n",1);
					j = 0;
				}
			}
		}
		else
		{
			for (Uint32 i = 0;i < 20;i++)
			{
				tmp = TQString("0x%1 ").arg(data[i],0,16);
				fptr.write(tmp.ascii(),tmp.length());
				j++;
				if (j > 10)
				{
					fptr.write("\n",1);
					j = 0;
				}
			}
			tmp = TQString("\n ... \n");
			fptr.write(tmp.ascii(),tmp.length());
			for (Uint32 i = size - 20;i < size;i++)
			{
				tmp = TQString("0x%1 ").arg(data[i],0,16);
				fptr.write(tmp.ascii(),tmp.length());
				j++;
				if (j > 10)
				{
					fptr.write("\n",1);
					j = 0;
				}
			}
		}
		fptr.write("\n",1);
	}
#endif

	IncomingPacket::IncomingPacket(Uint32 size) : data(0),size(size),read(0)
	{
		data = new Uint8[size];
	}
	
	IncomingPacket::~IncomingPacket()
	{
		delete [] data;
	}
	
	PacketReader::PacketReader(Peer* peer) 
		: peer(peer),error(false)
	{
		packet_queue.setAutoDelete(true);
		len_received = -1;
	}


	PacketReader::~PacketReader()
	{
	}
	
	
	void PacketReader::update()
	{
		if (error)
			return;
		
		mutex.lock();
		// pass packets to peer
		while (packet_queue.count() > 0)
		{
			IncomingPacket* pck = packet_queue.first();
			if (pck->read == pck->size)
			{
				// full packet is read pass it to peer
				peer->packetReady(pck->data,pck->size);
				packet_queue.removeFirst();
			}
			else
			{
				// packet is not yet full, break out of loop
				break;
			}
		}
		mutex.unlock();
	}
	
	Uint32 PacketReader::newPacket(Uint8* buf,Uint32 size)
	{
		Uint32 packet_length = 0;
		Uint32 am_of_len_read = 0;
		if (len_received > 0)
		{
			if (size < 4 - len_received)
			{
				memcpy(len + len_received,buf,size);
				len_received += size;
				return size;
			}
			else
			{
				memcpy(len + len_received,buf,4 - len_received);
				am_of_len_read = 4 - len_received;
				len_received = 0;
				packet_length = ReadUint32(len,0);
				
			}
		}
		else if (size < 4)
		{
			memcpy(len,buf,size);
			len_received = size;
			return size;
		}
		else
		{
			packet_length = ReadUint32(buf,0);
			am_of_len_read = 4;
		}
		
		if (packet_length == 0)
			return am_of_len_read;
		
		if (packet_length > MAX_PIECE_LEN + 13)
		{
			Out(SYS_CON|LOG_DEBUG) << " packet_length too large " << packet_length << endl;

			error = true;
			return size;
		}
		
		IncomingPacket* pck = new IncomingPacket(packet_length);
		packet_queue.append(pck);
		return am_of_len_read + readPacket(buf + am_of_len_read,size - am_of_len_read);
	}

	Uint32 PacketReader::readPacket(Uint8* buf,Uint32 size)
	{
		if (!size)
			return 0;
		
		IncomingPacket* pck = packet_queue.last();
		if (pck->read + size >= pck->size)
		{
			// we can read the full packet
			Uint32 tr = pck->size - pck->read;
			memcpy(pck->data + pck->read,buf,tr);
			pck->read += tr;
			return tr;
		}
		else
		{
			// we can do a partial read
			Uint32 tr = size;
			memcpy(pck->data + pck->read,buf,tr);
			pck->read += tr;
			return tr;
		}
	}
	

	void PacketReader::onDataReady(Uint8* buf,Uint32 size)
	{
		if (error)
			return;
		
		mutex.lock();
		if (packet_queue.count() == 0)
		{
			Uint32 ret = 0;
			while (ret < size && !error)
			{
				ret += newPacket(buf + ret,size - ret);
			}
		}
		else
		{
			Uint32 ret = 0;
			IncomingPacket* pck = packet_queue.last();
			if (pck->read == pck->size) // last packet in queue is fully read
				ret = newPacket(buf,size);
			else
			 	ret = readPacket(buf,size);
			
			while (ret < size  && !error)
			{
				ret += newPacket(buf + ret,size - ret);
			}
		}
		mutex.unlock();
	}
}