File: Packet.cpp

package info (click to toggle)
amule 1%3A2.3.2-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 27,584 kB
  • sloc: cpp: 115,104; ansic: 8,572; sh: 5,729; makefile: 1,706; php: 1,680; perl: 955; yacc: 822; ruby: 729; objc: 692; lex: 633; java: 413; xml: 27; python: 26; awk: 21; sed: 16
file content (321 lines) | stat: -rw-r--r-- 8,190 bytes parent folder | download | duplicates (3)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// This file is part of the aMule Project.
//
// Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
// Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// 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 St, Fifth Floor, Boston, MA  02110-1301, USA
//

#include <zlib.h>		// Needed for uLongf

#include "Packet.h"				// Interface declarations

#include <protocol/Protocols.h>

#include "Logger.h"			// Neeed for AddDebugLogLineN
#include "MemFile.h"			// Needed for CMemFile
#include "OtherStructs.h"		// Needed for Header_Struct
#include "ArchSpecific.h"		// Needed for ENDIAN_*

// Copy constructor
CPacket::CPacket(CPacket &p)
{
	size		= p.size;
	opcode		= p.opcode;
	prot		= p.prot;
	m_bSplitted	= p.m_bSplitted;
	m_bLastSplitted = p.m_bLastSplitted;
	m_bPacked	= p.m_bPacked;
	m_bFromPF	= p.m_bFromPF;
	memcpy(head, p.head, sizeof head);
	tempbuffer	= NULL;
	if (p.completebuffer) {
		completebuffer	= new byte[size + 10];;
		pBuffer	= completebuffer + sizeof(Header_Struct);
	} else {
		completebuffer	= NULL;
		if (p.pBuffer) {
			pBuffer = new byte[size];
		} else {
			pBuffer = NULL;
		}
	}
	if (pBuffer)
		memcpy( pBuffer, p.pBuffer, size );
}

CPacket::CPacket(uint8 protocol)
{
	size		= 0;
	opcode		= 0;
	prot		= protocol;
	m_bSplitted	= false;
	m_bLastSplitted = false;
	m_bPacked	= false;
	m_bFromPF	= false;
	memset(head, 0, sizeof head);
	tempbuffer	= NULL;
	completebuffer	= NULL;
	pBuffer	= NULL;
}

// only used for receiving packets
CPacket::CPacket(byte* rawHeader, byte *buf)
{
	memset(head, 0, sizeof head);
	Header_Struct* header = reinterpret_cast<Header_Struct*>(rawHeader);
	size		= ENDIAN_SWAP_32(header->packetlength) - 1;
	opcode		= header->command;
	prot		= header->eDonkeyID;
	m_bSplitted	= false;
	m_bLastSplitted = false;
	m_bPacked	= false;
	m_bFromPF	= false;
	tempbuffer	= NULL;
	completebuffer	= NULL;
	pBuffer	= buf;
}

CPacket::CPacket(const CMemFile& datafile, uint8 protocol, uint8 ucOpcode)
{
	size		= datafile.GetLength();
	opcode		= ucOpcode;
	prot		= protocol;
	m_bSplitted	= false;
	m_bLastSplitted = false;
	m_bPacked	= false;
	m_bFromPF	= false;
	memset(head, 0, sizeof head);
	tempbuffer = NULL;
	completebuffer = new byte[size + sizeof(Header_Struct)/*Why this 4?*/];
	pBuffer = completebuffer + sizeof(Header_Struct);

	// Write contents of MemFile to buffer (while keeping original position in file)
	off_t position = datafile.GetPosition();
	datafile.Seek(0, wxFromStart);
	datafile.Read(pBuffer, size);
	datafile.Seek(position, wxFromStart);
}

CPacket::CPacket(int8 in_opcode, uint32 in_size, uint8 protocol, bool bFromPF)
{
	size		= in_size;
	opcode		= in_opcode;
	prot		= protocol;
	m_bSplitted	= false;
	m_bLastSplitted = false;
	m_bPacked	= false;
	m_bFromPF	= bFromPF;
	memset(head, 0, sizeof head);
	tempbuffer	= NULL;
	if (in_size) {
		completebuffer = new byte[in_size + sizeof(Header_Struct) + 4 /*Why this 4?*/];
		pBuffer = completebuffer + sizeof(Header_Struct);
		memset(completebuffer, 0, in_size + sizeof(Header_Struct) + 4 /*Why this 4?*/);
	} else {
		completebuffer = NULL;
		pBuffer = NULL;
	}
}

// only used for splitted packets!
CPacket::CPacket(byte* pPacketPart, uint32 nSize, bool bLast, bool bFromPF)
{
	size		= nSize - sizeof(Header_Struct);
	opcode		= 0;
	prot		= 0;
	m_bSplitted	= true;
	m_bLastSplitted	= bLast;
	m_bPacked	= false;
	m_bFromPF	= bFromPF;
	memset(head, 0, sizeof head);
	tempbuffer	= NULL;
	completebuffer	= pPacketPart;
	pBuffer		= NULL;
}

CPacket::~CPacket()
{
	// Never deletes pBuffer when completebuffer is not NULL
	if (completebuffer) {
		delete [] completebuffer;
	} else if (pBuffer) {
	// On the other hand, if completebuffer is NULL and pBuffer is not NULL
		delete [] pBuffer;
	}

	if (tempbuffer) {
		delete [] tempbuffer;
	}
}

uint32 CPacket::GetPacketSizeFromHeader(const byte* rawHeader)
{
	const Header_Struct* header = reinterpret_cast<const Header_Struct*>(rawHeader);
	uint32 size = ENDIAN_SWAP_32(header->packetlength);
	if (size < 1 || size >= 0x7ffffff0u)
		return 0;
	return size - 1;
}

void CPacket::CopyToDataBuffer(unsigned int offset, const byte* data, unsigned int n)
{
	wxASSERT(offset + n <= size + 1);
	memcpy(pBuffer + offset, data, n);
}

byte* CPacket::GetPacket() {
	if (completebuffer) {
		if (!m_bSplitted) {
			memcpy(completebuffer, GetHeader(), sizeof(Header_Struct));
		}
		return completebuffer;
	} else {
		if (tempbuffer){
			delete [] tempbuffer;
			tempbuffer = NULL;
		}
		tempbuffer = new byte[size + sizeof(Header_Struct) + 4 /* why this 4?*/];
		memcpy(tempbuffer    , GetHeader(), sizeof(Header_Struct));
		memcpy(tempbuffer + sizeof(Header_Struct), pBuffer    , size);
		return tempbuffer;
	}
}

byte* CPacket::DetachPacket() {
	if (completebuffer) {
		if (!m_bSplitted) {
			memcpy(completebuffer, GetHeader(), sizeof(Header_Struct));
		}
		byte* result = completebuffer;
		completebuffer = pBuffer = NULL;
		return result;
	} else{
		if (tempbuffer){
			delete[] tempbuffer;
			tempbuffer = NULL;
		}
		tempbuffer = new byte[size+sizeof(Header_Struct)+4 /* Why this 4?*/];
		memcpy(tempbuffer,GetHeader(),sizeof(Header_Struct));
		memcpy(tempbuffer+sizeof(Header_Struct),pBuffer,size);
		byte* result = tempbuffer;
		tempbuffer = 0;
		return result;
	}
}

byte* CPacket::GetHeader() {
	wxASSERT( !m_bSplitted );

	Header_Struct* header = reinterpret_cast<Header_Struct*>(head);
	header->command = opcode;
	header->eDonkeyID =  prot;
	header->packetlength = ENDIAN_SWAP_32(size + 1);

	return head;
}

byte* CPacket::GetUDPHeader() {
	wxASSERT( !m_bSplitted );

	memset(head, 0, 6);
	UDP_Header_Struct* header = reinterpret_cast<UDP_Header_Struct*>(head);
	header->eDonkeyID =  prot;
	header->command = opcode;

	return head;
}


void CPacket::PackPacket()
{
	wxASSERT(!m_bSplitted);

	uLongf newsize = size + 300;
	byte* output = new byte[newsize];

	uint16 result = compress2(output, &newsize, pBuffer, size, Z_BEST_COMPRESSION);

	if (result != Z_OK || size <= newsize) {
		delete[] output;
		return;
	}

	if (prot == OP_KADEMLIAHEADER) {
		prot = OP_KADEMLIAPACKEDPROT;
	} else {
		prot = OP_PACKEDPROT;
	}

	memcpy(pBuffer, output, newsize);
	delete[] output;
	m_bPacked = true;

	size = newsize;
}


bool CPacket::UnPackPacket(uint32 uMaxDecompressedSize) {
	wxASSERT( prot == OP_PACKEDPROT || prot == OP_ED2KV2PACKEDPROT);
	// OP_ED2KV2PACKEDPROT is experimental aMule test code,
	// this should not happen yet. Leave a warining in the log.
	if (prot == OP_ED2KV2PACKEDPROT) {
		AddDebugLogLineN(logPacketErrors,
			wxT("Received OP_ED2KV2PACKEDPROT."));
	}

	uint32 nNewSize = size * 10 + 300;

	if (nNewSize > uMaxDecompressedSize){
		nNewSize = uMaxDecompressedSize;
	}

	byte* unpack = new byte[nNewSize];
	uLongf unpackedsize = nNewSize;
	uint16 result = uncompress(unpack, &unpackedsize, pBuffer, size);

	if (result == Z_OK) {
		wxASSERT( completebuffer == NULL );
		wxASSERT( pBuffer != NULL );

		size = unpackedsize;
		delete[] pBuffer;
		pBuffer = unpack;
		prot = OP_EMULEPROT;
		return true;
	}

	delete[] unpack;
	return false;
}


void CPacket::Copy16ToDataBuffer(const void* data)
{
	md4cpy(pBuffer, data);
}


void CPacket::CopyUInt32ToDataBuffer(uint32 data, unsigned int offset)
{
	wxCHECK_RET(offset <= size - sizeof(uint32), wxT("Bad offset in CopyUInt32ToDataBuffer."));
	PokeUInt32( pBuffer + offset, data );
}
// File_checked_for_headers