summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/tools/tcp_dump_packets.c
blob: 8ac6cdb1f2f4650842e9e6999cc80f43c9cf2b28 (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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
 * FreeRTOS+TCP V2.2.1
 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://aws.amazon.com/freertos
 * http://www.FreeRTOS.org
 */

/*
 * tcp_dump_packets.c
 * Used in the PC/Win project to dump Ethernet packets, along with some description.
 * See tools/tcp_dump_packets.md for further description.
 */

/* Standard includes. */
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <ctype.h>

/* FreeRTOS includes. */
#include <FreeRTOS.h>
#include "task.h"

/* FreeRTOS+TCP includes. */
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_Stream_Buffer.h"
#include "FreeRTOS_IP_Private.h"

#if( ipconfigUSE_DUMP_PACKETS != 0 )

#include "tcp_dump_packets.h"

/* The priority of the windows thread. */
#define dumpPROCESS_THREAD_PRIORITY		THREAD_PRIORITY_NORMAL

/* There is a stream buffer between the FreeRTOS tasks sending network packets,
and the Windows thread that writes these packets to disk. The macro 'dumpITEM_COUNT'
determines the number of full-size packets that can be stored in this stream buffer. */
#ifndef dumpITEM_COUNT
	#define	dumpITEM_COUNT				32
#endif

/* Packets are written in hex notation, no more than 16 bytes on a row. */
#ifndef dumpBYTES_PER_ROW
	#define dumpBYTES_PER_ROW			16
#endif

/* The TCP port number reserved for a DNS server. */
#define dnsDNS_PORT						0x0035u

/* Some const values describing the 'flags' in a TCP packet. */
#define tcpTCP_FLAG_FIN					0x0001u /* No more data from sender */
#define tcpTCP_FLAG_SYN					0x0002u /* Synchronize sequence numbers */
#define tcpTCP_FLAG_RST					0x0004u /* Reset the connection */
#define tcpTCP_FLAG_PSH					0x0008u /* Push function: please push buffered data to the recv application */
#define tcpTCP_FLAG_ACK					0x0010u /* Acknowledgment field is significant */

/* A macro to add a type, both as a numeric value, as well as a string. */
#define ADD_TYPE( FLAGS ) \
	vAddType( flag_##FLAGS, #FLAGS )

/*-----------------------------------------------------------*/

static char pcTypeString[ 255 ];
static uint32_t ulTypeMask;

/* The name of the C source file to be written. */
static char pcCodeFileName[ MAX_PATH ];

/* The name of the header file to be written. */
static char pcHeaderFileName[ MAX_PATH ];

/* A stream buffer between the FreeRTOS tasks and the Windows thread. */
static StreamBuffer_t *xPacketBuffer;

/* A process handle of the Windows thread. */
static HANDLE pvProcessHandle;

static UBaseType_t uxNextPacketNumber;
static BaseType_t xFirstPacket = 1;

/* Bollean 'xDumpingReady' becomes true once all desired packet have been collected.
Further packets will be dropped (ignored). */
static volatile BaseType_t xDumpingReady = pdFALSE;

static DumpEntries_t *pxCurrentEntries;

static uint16_t usSourcePort;
static uint16_t usDestinationPort;

typedef struct xBufferheader
{
	size_t uxLength;
	UBaseType_t bIncoming : 1;
} Bufferheader_t;

static DumpEntries_t xExampleEntries = {
	.uxEntryCount = 4,	/* No more than 'dumpMAX_DUMP_ENTRIES' elements. */
	.xEntries = {
		{ .ulMask = flag_IN | flag_UDP,   .uxMax = 2u },
		{ .ulMask = flag_IN | flag_ARP,   .uxMax = 2u },
		{ .ulMask = flag_IN | flag_TCP,   .uxMax = 5u },
		{ .ulMask = flag_IN | flag_SYN,   .uxMax = 1u },
	}
};

const char pcHeaderCode[] =
	"/*\n"
	" * This file was created automatically by 'dump_packets.c'\n"
	" */\n"
	"\n"
	"/* Standard includes. */\n"
	"#include <stdio.h>\n"
	"#include <stdint.h>\n"
	"#include <stdarg.h>\n"
	"#include <io.h>\n"
	"#include <ctype.h>\n"
	"\n"
	"/* FreeRTOS includes. */\n"
	"#include <FreeRTOS.h>\n"
	"#include <task.h>\n\n"
	"#include \"%s\"\n\n";

const char pcHeaderHeader[] =
	"/*\n"
	" * This file was created automatically by 'dump_packets.c'\n"
	" */\n"
	"\n"
	"#ifndef PACKET_LIST_H\n\n"
	"#define PACKET_LIST_H\n\n"
	"typedef struct xDumpPacket\n"
	"{\n"
	"	const uint8_t *pucData;\n"
	"	size_t uxLength;\n"
	"	uint32_t ulType;\n"
	"	uint16_t usSource;\n"
	"	uint16_t usDestination;\n"
	"} DumpPacket_t;\n\n";

/*-----------------------------------------------------------*/

/* The Windows thread that actually writes the network packets to a C source and header file. */
static DWORD WINAPI prvWritePackets( LPVOID lpParameter );

static void vAddProtocolTags( uint8_t *pucEthernetBuffer, BaseType_t xIPType );
static void vDetermineMessageType( uint8_t *pucBuffer, BaseType_t xIncoming );
static void vActualDump( uint8_t *pucBuffer, size_t uxLength, BaseType_t xIncoming );
static void vAddType( uint32_t ulFlags, const char *pcFlagName );
static void vWriteHeaderFile( void );

/*-----------------------------------------------------------*/

void dump_packet_init( const char *pcFileName, DumpEntries_t *pxEntries )
{
size_t uxIndex;

	snprintf( pcCodeFileName, sizeof pcCodeFileName, "%s.c", pcFileName );
	snprintf( pcHeaderFileName, sizeof pcHeaderFileName, "%s.h", pcFileName );

	if( pxEntries == NULL )
	{
		pxEntries = &( xExampleEntries );
	}
	configASSERT( pxEntries->uxEntryCount > 0 );
	configASSERT( pxEntries->uxEntryCount <= dumpMAX_DUMP_ENTRIES );

	for( uxIndex = 0; uxIndex < pxEntries->uxEntryCount; uxIndex++ )
	{
		pxEntries->xEntries[ uxIndex ].uxCount = 0;
	}

	pxCurrentEntries = pxEntries;

	if( xPacketBuffer == NULL )
	{
	size_t uxLength, uxSize;

		/* Enough space for e.g. 32 buffers and length words. */
		uxLength = dumpITEM_COUNT * ( sizeof( void * ) + ipconfigNETWORK_MTU + ipSIZE_OF_ETH_HEADER );
		uxSize = ( sizeof( *xPacketBuffer )  + uxLength ) - sizeof( xPacketBuffer->ucArray );
		xPacketBuffer = ( StreamBuffer_t * ) pvPortMalloc( uxSize );
		configASSERT( xPacketBuffer != NULL );
		vStreamBufferClear( xPacketBuffer );
		xPacketBuffer->LENGTH = uxLength;
	}

	if( pvProcessHandle == NULL )
	{
		pvProcessHandle = CreateThread( NULL, 0, prvWritePackets, NULL, CREATE_SUSPENDED, NULL );
		if( pvProcessHandle != NULL )
		{
			SetThreadPriority( pvProcessHandle, dumpPROCESS_THREAD_PRIORITY );
			SetThreadPriorityBoost( pvProcessHandle, TRUE );
			SetThreadAffinityMask( pvProcessHandle, 0x0E );
			ResumeThread( pvProcessHandle );
		}
	}
}
/*-----------------------------------------------------------*/

void dump_packet( const uint8_t *pucBuffer, size_t uxLength, BaseType_t xIncoming )
{
	/* This function shall be called from a normal FreeRTOS task only. */
	if( xPacketBuffer != NULL )
	{
		if( xDumpingReady == pdFALSE )
		{
		size_t uxSpace = uxStreamBufferGetSpace( xPacketBuffer );
		size_t uxNeeded = uxLength + sizeof( size_t );

			if( uxNeeded < uxSpace )
			{
			Bufferheader_t xheader;

				xheader.uxLength = uxLength;
				xheader.bIncoming = xIncoming;
				uxStreamBufferAdd( xPacketBuffer, 0u, ( const uint8_t * ) &( xheader ), sizeof( xheader ) );
				uxStreamBufferAdd( xPacketBuffer, 0u, pucBuffer, uxLength );
			}
			else
			{
				/* Drop this packet. */
			}
		}
		else
		{
			/* The Windows thread 'prvWritePackets()' had received enough packets.
			The packet buffer may be freed. */
			vPortFree( xPacketBuffer );
			xPacketBuffer = NULL;
		}
	}
}
/*-----------------------------------------------------------*/

static DWORD WINAPI prvWritePackets( LPVOID lpParameter )
{
	/* This is a Windows thread, not a FreeRTOS task. FreeRTOS API's may not be called. */
	for( ;; )
	{
		Sleep( 100 );

		while( ( xPacketBuffer != NULL ) && ( xDumpingReady == pdFALSE ) )
		{
		Bufferheader_t xHeader;
		size_t uxBytes = uxStreamBufferGetSize( xPacketBuffer );

			if( uxBytes <= sizeof( xHeader ) )
				break;

			/* Peek the number of bytes available. */
			uxStreamBufferGet( xPacketBuffer, 0u, ( uint8_t * ) &( xHeader ), sizeof( xHeader ), pdTRUE );
			if( uxBytes >= sizeof( xHeader ) + xHeader.uxLength );
			{
			size_t xBytesRead;
			uint8_t pcBuffer[ ipconfigNETWORK_MTU ];
			size_t xActualCount;

				uxStreamBufferGet( xPacketBuffer, 0u, NULL, sizeof( xHeader ), pdFALSE );
				xActualCount = uxStreamBufferGet( xPacketBuffer, 0u, pcBuffer, xHeader.uxLength, pdFALSE );
				vActualDump( pcBuffer, xActualCount, xHeader.bIncoming );
			}
		}
	}
}
/*-----------------------------------------------------------*/

static int _fprintf( FILE *pxHandle, const char* pcFormat, ... )
{
char pcString[ 255 ];
BaseType_t iCount;

	va_list args;
	va_start (args, pcFormat);
	iCount = vsnprintf( pcString, sizeof pcString, pcFormat, args);
	va_end (args);
	fwrite( pcString, 1u, iCount, pxHandle );

	return iCount;
}
/*-----------------------------------------------------------*/

static void vWriteHeaderFile( void )
{
FILE *outfile;

	outfile = fopen( pcHeaderFileName, "w" );
	if( outfile != NULL )
	{
		fwrite( pcHeaderHeader, 1u, sizeof( pcHeaderHeader ) - 1u, outfile );
		_fprintf( outfile, "#define dumpPACKET_COUNT %lu\n\n",
			( uxNextPacketNumber < 1u ) ? 1u : uxNextPacketNumber );
		_fprintf( outfile, "extern DumpPacket_t *xPacketList[ dumpPACKET_COUNT ];\n\n" );
		_fprintf( outfile, "#endif PACKET_LIST_H\n" );

		fclose ( outfile );
	}
}
/*-----------------------------------------------------------*/

static void vAddType( uint32_t ulFlags, const char *pcFlagName )
{
size_t uxLength = strlen( pcTypeString );
char pcString[ 64 ];
BaseType_t iCount;

	ulTypeMask |= ulFlags;
 
	if( uxLength == 0 )
	{
		snprintf( pcTypeString, sizeof pcTypeString, "%s", pcFlagName );
	}
	else
	{
		snprintf( pcTypeString + uxLength, sizeof pcTypeString - 1, " | %s", pcFlagName );
	}
}
/*-----------------------------------------------------------*/

static void vAddProtocolTags( uint8_t *pucEthernetBuffer, BaseType_t xIPType )
{
ProtocolHeaders_t *pxProtocolHeaders;
#if( ipconfigUSE_IPv6 != 0 )
	const IPHeader_IPv6_t * pxIPHeader_IPv6;
#endif
UBaseType_t uxHeaderLength;
uint8_t ucProtocol;
IPPacket_t * pxIPPacket;
IPHeader_t * pxIPHeader;

	pxIPPacket = ( IPPacket_t * ) pucEthernetBuffer;
	pxIPHeader = &( pxIPPacket->xIPHeader );
	#if( ipconfigUSE_IPv6 != 0 )
	pxIPHeader_IPv6 = ipPOINTER_CAST( const IPHeader_IPv6_t *, &( pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER ] ) );
	if( pxIPPacket->xEthernetHeader.usFrameType == ipIPv6_FRAME_TYPE )
	{
		uxHeaderLength = ipSIZE_OF_IPv6_HEADER;
		ucProtocol = pxIPHeader_IPv6->ucNextHeader;
		pxProtocolHeaders = ipPOINTER_CAST( ProtocolHeaders_t *, &( pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv6_HEADER ] ) );
	}
	else
	#endif
	{
	size_t uxLength = ( size_t ) pxIPHeader->ucVersionHeaderLength;

		/* Check if the IP headers are acceptable and if it has our destination.
		The lowest four bits of 'ucVersionHeaderLength' indicate the IP-header
		length in multiples of 4. */
		uxHeaderLength = ( size_t ) ( ( uxLength & 0x0Fu ) << 2 );
		ucProtocol = pxIPPacket->xIPHeader.ucProtocol;
		pxProtocolHeaders = ipPOINTER_CAST( ProtocolHeaders_t *, &( pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER + uxHeaderLength ] ) );
	}

	switch( ucProtocol )
	{
		case ipPROTOCOL_ICMP :
			ADD_TYPE( ICMP4 );
			break;

#if( ipconfigUSE_IPv6 != 0 )
		case ipPROTOCOL_ICMP_IPv6:
			ADD_TYPE( ICMP6 );
			break;
#endif

		case ipPROTOCOL_UDP :
			{
				ADD_TYPE( UDP );
				usSourcePort = pxProtocolHeaders->xUDPHeader.usSourcePort;
				usDestinationPort = pxProtocolHeaders->xUDPHeader.usDestinationPort;
				if( usSourcePort == FreeRTOS_htons( dnsDNS_PORT) )
				{
					ADD_TYPE( DNS );
					ADD_TYPE( REPLY );
				}
				else if( usDestinationPort == FreeRTOS_htons( dnsDNS_PORT) )
				{
					ADD_TYPE( DNS );
					ADD_TYPE( REQUEST );
				}
			}
			break;
#if ipconfigUSE_TCP == 1
		case ipPROTOCOL_TCP :
			{
				ADD_TYPE( TCP );
				usSourcePort = pxProtocolHeaders->xTCPHeader.usSourcePort;
				usDestinationPort = pxProtocolHeaders->xTCPHeader.usDestinationPort;
				if( ( pxProtocolHeaders->xTCPHeader.ucTCPFlags & tcpTCP_FLAG_SYN ) != 0u )
				{
					ADD_TYPE( SYN );
				}
				if( ( pxProtocolHeaders->xTCPHeader.ucTCPFlags & tcpTCP_FLAG_FIN ) != 0u )
				{
					ADD_TYPE( FIN );
				}
				if( ( pxProtocolHeaders->xTCPHeader.ucTCPFlags & tcpTCP_FLAG_RST ) != 0u )
				{
					ADD_TYPE( RST );
				}
				if( ( pxProtocolHeaders->xTCPHeader.ucTCPFlags & tcpTCP_FLAG_ACK ) != 0u )
				{
					ADD_TYPE( ACK );
				}
			}
			break;
#endif
	}
}
/*-----------------------------------------------------------*/

static void vDetermineMessageType( uint8_t *pucBuffer, BaseType_t xIncoming )
{
EthernetHeader_t *pxEthernetHeader;

	if( xIncoming != 0 )
	{
		ADD_TYPE( IN );
	}
	else
	{
		ADD_TYPE( OUT );
	}
	pxEthernetHeader = ( EthernetHeader_t * ) pucBuffer;

	/* Interpret the received Ethernet packet. */
	switch( pxEthernetHeader->usFrameType )
	{
		case ipARP_FRAME_TYPE :
			{
			ARPPacket_t * pxARPFrame;
			ARPHeader_t *pxARPHeader;

				/* The Ethernet frame contains an ARP packet. */
				ADD_TYPE( FRAME_ARP );
				pxARPFrame = ( ARPPacket_t * ) pucBuffer;
				pxARPHeader = &( pxARPFrame->xARPHeader );
				ADD_TYPE( ARP );
				switch( pxARPHeader->usOperation )
				{
				case ipARP_REQUEST:
					ADD_TYPE( REQUEST );
					break;
				case ipARP_REPLY:
					ADD_TYPE( REPLY );
					break;
				default:
					ADD_TYPE( UNKNOWN );
					break;
				}
			}
			break;

		case ipIPv4_FRAME_TYPE :
			{
				ADD_TYPE( FRAME_4 );
				vAddProtocolTags( pucBuffer, 4 );
			}
			break;
			
	#if( ipconfigUSE_IPv6 != 0 )
		case ipIPv6_FRAME_TYPE :
			{
				ADD_TYPE( FRAME_6 );
				vAddProtocolTags( pucBuffer, 6 );
			}
			break;
	#endif
		default :
			/* No other packet types are handled.  Nothing to do. */
			ADD_TYPE( Unknown_FRAME );
			break;
	}
}
/*-----------------------------------------------------------*/

static void vActualDump( uint8_t *pucBuffer, size_t uxLength, BaseType_t xIncoming )
{
char pcString[ 513 ];
size_t uxOffset;
size_t uxIndex;
size_t uxCompleteCount = 0;
BaseType_t xUseIt = pdFALSE;

	usSourcePort = 0u;
	usDestinationPort = 0u;
	pcTypeString[ 0 ] = 0;
	ulTypeMask = 0uL;

	if( pxCurrentEntries == NULL )
	{
		return;
	}

	vDetermineMessageType( pucBuffer, xIncoming );

	for( uxIndex = 0; uxIndex < pxCurrentEntries->uxEntryCount; uxIndex++ )
	{
		if( pxCurrentEntries->xEntries[ uxIndex ].uxCount < pxCurrentEntries->xEntries[ uxIndex ].uxMax )
		{
		uint32_t ulMask = pxCurrentEntries->xEntries[ uxIndex ].ulMask;

			if( ( ulMask & ulTypeMask ) == ulMask )
			{
				pxCurrentEntries->xEntries[ uxIndex ].uxCount++;
				xUseIt = pdTRUE;
			}
		}
		else
		{
			uxCompleteCount++;
		}
	}
	FreeRTOS_printf( ( "prvWritePackets: done %d/%d : (%d,%d) (%d,%d) (%d,%d) (%d,%d)\n",
		uxCompleteCount,
		pxCurrentEntries->uxEntryCount,
		pxCurrentEntries->xEntries[ 0 ].uxCount, pxCurrentEntries->xEntries[ 0 ].uxMax,
		pxCurrentEntries->xEntries[ 1 ].uxCount, pxCurrentEntries->xEntries[ 1 ].uxMax,
		pxCurrentEntries->xEntries[ 2 ].uxCount, pxCurrentEntries->xEntries[ 2 ].uxMax,
		pxCurrentEntries->xEntries[ 3 ].uxCount, pxCurrentEntries->xEntries[ 3 ].uxMax ) );
	if( uxCompleteCount >= pxCurrentEntries->uxEntryCount )
	{
		FreeRTOS_printf( ( "prvWritePackets: all %lu packets have been collected\n", pxCurrentEntries->uxEntryCount ) );
		if( pxCurrentEntries != NULL )
		{
			FILE *outfile = fopen( pcCodeFileName, ( xFirstPacket != 0 ) ? "w" : "a+" );
			if ( outfile == NULL )
			{
				FreeRTOS_printf( ( "Can not create '%s'\n", pcCodeFileName ) );
			}
			else
			{
				/*
					Create a list with pointers to each network packet.
					DumpPacket_t *xPacketList[ dumpPACKET_COUNT ] =
					{
						&xPacket_0000,
						&xPacket_0001,
						&xPacket_0002,
						&xPacket_0003,
					}
				*/
				_fprintf( outfile, "\nDumpPacket_t *xPacketList[ dumpPACKET_COUNT ] =\n{\n" );
				for( uxIndex = 0; uxIndex < uxNextPacketNumber; uxIndex++ )
				{
					_fprintf( outfile, "\t&xPacket_%04lu,\n", uxIndex );
				}
				_fprintf( outfile, "};\n" );
				fclose( outfile );
				vWriteHeaderFile();
			}
			pxCurrentEntries = NULL;
			/* Tell the thread and the function dump_packet() that packet
			dumping is ready. */
			xDumpingReady = pdTRUE;
		}
		return;
	}
	if( xUseIt == pdFALSE )
	{
		return;
	}

	printf("prvWritePackets: Read %lu bytes, type %s\n", uxLength, pcTypeString );

	FILE *outfile = fopen( pcCodeFileName, ( xFirstPacket != 0 ) ? "w" : "a+" );
	if ( outfile == NULL )
	{
		FreeRTOS_printf( ( "Can not create '%s'\n", pcCodeFileName ) );
		return;
	}
	if( xFirstPacket != 0 )
	{
	char *pcPtr;
	size_t xLength;

		vWriteHeaderFile( pcHeaderFileName );
		xLength = snprintf( pcString, sizeof pcString, pcHeaderCode, pcHeaderFileName );
		fwrite( pcString, 1u, xLength, outfile );
		xFirstPacket = pdFALSE;
	}

	_fprintf( outfile, "\n/* Packet_%04d */\n", uxNextPacketNumber );
	_fprintf( outfile, "uint8_t ucPacket_%04lx[ %lu ] =\n{\n", uxNextPacketNumber, uxLength );

	for( uxOffset = 0u; uxOffset < uxLength; )
	{
	size_t uxCurLength = 0u;
	size_t uxLast = uxOffset + dumpBYTES_PER_ROW;
	BaseType_t xFirst = pdTRUE;

		if( uxLast > uxLength )
		{
			uxLast = uxLength;
		}
		while( uxOffset < uxLast )
		{
			uxCurLength += snprintf( pcString + uxCurLength, sizeof pcString - uxCurLength, "%s0x%02x",
				( uxCurLength == 0 ) ? "\t" : ", ", pucBuffer[ uxOffset ] );
			uxOffset++;
		}
		if( uxCurLength != 0u )
		{
			uxCurLength += snprintf( pcString + uxCurLength, sizeof pcString - uxCurLength, "%s\n",
				( uxOffset == uxLength )  ? "\n};" : "," );
			fwrite( pcString, 1u, uxCurLength, outfile );
		}
	}

	_fprintf( outfile, "\n");

	_fprintf( outfile,
		"DumpPacket_t xPacket_%04lx =\n{\n"
		"\t.pucData = ucPacket_%04lx,\n"
		"\t.uxLength = %lu,\n"
		"\t.ulType = 0x%lX, /* %s */\n",
		uxNextPacketNumber, uxNextPacketNumber, uxLength, ulTypeMask, pcTypeString );

	if( usSourcePort != 0u )
	{
		_fprintf( outfile,
			"\t.usSource = %u,\n", FreeRTOS_ntohs( usSourcePort ) );
	}
	if( usSourcePort != 0u )
	{
		_fprintf( outfile,
			"\t.usDestination = %u,\n", FreeRTOS_ntohs( usDestinationPort ) );
	}

	_fprintf( outfile,
		"};\n"
		"/*-----------------------------------------------------------*/\n\n" );
	fclose( outfile );
	uxNextPacketNumber++;
}
/*-----------------------------------------------------------*/

#endif	/* ( ipconfigUSE_DUMP_PACKETS != 0 ) */