]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/portable/NetworkInterface/WinPCap/NetworkInterface.c
c4a12515b0891a151d7a5063428e77157ac80cf5
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / portable / NetworkInterface / WinPCap / NetworkInterface.c
1 /*
2 FreeRTOS+TCP V2.0.11
3 Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy of
6 this software and associated documentation files (the "Software"), to deal in
7 the Software without restriction, including without limitation the rights to
8 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 the Software, and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22  http://aws.amazon.com/freertos
23  http://www.FreeRTOS.org
24 */
25
26 /* WinPCap includes. */
27 #define HAVE_REMOTE
28 #include "pcap.h"
29
30 /* FreeRTOS includes. */
31 #include "FreeRTOS.h"
32 #include "task.h"
33 #include "semphr.h"
34
35 /* FreeRTOS+TCP includes. */
36 #include "FreeRTOS_IP.h"
37 #include "FreeRTOS_IP_Private.h"
38 #include "NetworkBufferManagement.h"
39
40 /* Thread-safe circular buffers are being used to pass data to and from the PCAP
41 access functions. */
42 #include "Win32-Extensions.h"
43 #include "FreeRTOS_Stream_Buffer.h"
44
45 /* Sizes of the thread safe circular buffers used to pass data to and from the
46 WinPCAP Windows threads. */
47 #define xSEND_BUFFER_SIZE  32768
48 #define xRECV_BUFFER_SIZE  32768
49
50 /* If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 1, then the Ethernet
51 driver will filter incoming packets and only pass the stack those packets it
52 considers need processing. */
53 #if( ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 0 )
54         #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer
55 #else
56         #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )
57 #endif
58
59 /* Used to insert test code only. */
60 #define niDISRUPT_PACKETS       0
61
62 /*-----------------------------------------------------------*/
63
64 /*
65  * Windows threads that are outside of the control of the FreeRTOS simulator are
66  * used to interface with the WinPCAP libraries.
67  */
68 DWORD WINAPI prvWinPcapRecvThread( void *pvParam );
69 DWORD WINAPI prvWinPcapSendThread( void *pvParam );
70
71 /*
72  * Print out a numbered list of network interfaces that are available on the
73  * host computer.
74  */
75 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );
76
77 /*
78  * Open the network interface.  The number of the interface to be opened is set
79  * by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
80  */
81 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );
82 static int prvOpenInterface( const char *pucName );
83
84 /*
85  * Configure the capture filter to allow blocking reads, and to filter out
86  * packets that are not of interest to this demo.
87  */
88 static void prvConfigureCaptureBehaviour( void );
89
90 /*
91  * A function that simulates Ethernet interrupts by periodically polling the
92  * WinPCap interface for new data.
93  */
94 static void prvInterruptSimulatorTask( void *pvParameters );
95
96 /*
97  * Create the buffers that are used to pass data between the FreeRTOS simulator
98  * and the Win32 threads that manage WinPCAP.
99  */
100 static void prvCreateThreadSafeBuffers( void );
101
102 /*
103  * Utility function used to format print messages only.
104  */
105 static const char *prvRemoveSpaces( char *pcBuffer, int aBuflen, const char *pcMessage );
106
107 /*-----------------------------------------------------------*/
108
109 /* Required by the WinPCap library. */
110 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];
111
112 /* An event used to wake up the Win32 thread that sends data through the WinPCAP
113 library. */
114 static void *pvSendEvent = NULL;
115
116 /* _HT_ made the PCAP interface number configurable through the program's
117 parameters in order to test in different machines. */
118 static BaseType_t xConfigNetworkInterfaceToUse = configNETWORK_INTERFACE_TO_USE;
119
120 /* Handles to the Windows threads that handle the PCAP IO. */
121 static HANDLE vWinPcapRecvThreadHandle = NULL;
122 static HANDLE vWinPcapSendThreadHandle = NULL;;
123
124 /* The interface being used by WinPCap. */
125 static pcap_t *pxOpenedInterfaceHandle = NULL;
126
127 /* Circular buffers used by the PCAP Win32 threads. */
128 static StreamBuffer_t *xSendBuffer = NULL;
129 static StreamBuffer_t *xRecvBuffer = NULL;
130
131 /* The MAC address initially set to the constants defined in FreeRTOSConfig.h. */
132 extern uint8_t ucMACAddress[ 6 ];
133
134 /* Logs the number of WinPCAP send failures, for viewing in the debugger only. */
135 static volatile uint32_t ulWinPCAPSendFailures = 0;
136
137 /*-----------------------------------------------------------*/
138
139 BaseType_t xNetworkInterfaceInitialise( void )
140 {
141 BaseType_t xReturn = pdFALSE;
142 pcap_if_t *pxAllNetworkInterfaces;
143
144         /* Query the computer the simulation is being executed on to find the
145         network interfaces it has installed. */
146         pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();
147
148         /* Open the network interface.  The number of the interface to be opened is
149         set by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.
150         Calling this function will set the pxOpenedInterfaceHandle variable.  If,
151         after calling this function, pxOpenedInterfaceHandle is equal to NULL, then
152         the interface could not be opened. */
153         if( pxAllNetworkInterfaces != NULL )
154         {
155                 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );
156         }
157
158         if( pxOpenedInterfaceHandle != NULL )
159         {
160                 xReturn = pdPASS;
161         }
162
163         return xReturn;
164 }
165 /*-----------------------------------------------------------*/
166
167 static void prvCreateThreadSafeBuffers( void )
168 {
169         /* The buffer used to pass data to be transmitted from a FreeRTOS task to
170         the Win32 thread that sends via the WinPCAP library. */
171         if( xSendBuffer == NULL)
172         {
173                 xSendBuffer = ( StreamBuffer_t * ) malloc( sizeof( *xSendBuffer ) - sizeof( xSendBuffer->ucArray ) + xSEND_BUFFER_SIZE + 1 );
174                 configASSERT( xSendBuffer );
175                 memset( xSendBuffer, '\0', sizeof( *xSendBuffer ) - sizeof( xSendBuffer->ucArray ) );
176                 xSendBuffer->LENGTH = xSEND_BUFFER_SIZE + 1;
177         }
178
179         /* The buffer used to pass received data from the Win32 thread that receives
180         via the WinPCAP library to the FreeRTOS task. */
181         if( xRecvBuffer == NULL)
182         {
183                 xRecvBuffer = ( StreamBuffer_t * ) malloc( sizeof( *xRecvBuffer ) - sizeof( xRecvBuffer->ucArray ) + xRECV_BUFFER_SIZE + 1 );
184                 configASSERT( xRecvBuffer );
185                 memset( xRecvBuffer, '\0', sizeof( *xRecvBuffer ) - sizeof( xRecvBuffer->ucArray ) );
186                 xRecvBuffer->LENGTH = xRECV_BUFFER_SIZE + 1;
187         }
188 }
189 /*-----------------------------------------------------------*/
190
191 BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxNetworkBuffer, BaseType_t bReleaseAfterSend )
192 {
193 size_t xSpace;
194
195         iptraceNETWORK_INTERFACE_TRANSMIT();
196         configASSERT( xIsCallingFromIPTask() == pdTRUE );
197
198         /* Both the length of the data being sent and the actual data being sent
199         are placed in the thread safe buffer used to pass data between the FreeRTOS
200         tasks and the Win32 thread that sends data via the WinPCAP library.  Drop
201         the packet if there is insufficient space in the buffer to hold both. */
202         xSpace = uxStreamBufferGetSpace( xSendBuffer );
203
204         if( ( pxNetworkBuffer->xDataLength <= ( ipconfigNETWORK_MTU + ipSIZE_OF_ETH_HEADER ) ) &&
205                 ( xSpace >= ( pxNetworkBuffer->xDataLength + sizeof( pxNetworkBuffer->xDataLength ) ) ) )
206         {
207                 /* First write in the length of the data, then write in the data
208                 itself. */
209                 uxStreamBufferAdd( xSendBuffer, 0, ( const uint8_t * ) &( pxNetworkBuffer->xDataLength ), sizeof( pxNetworkBuffer->xDataLength ) );
210                 uxStreamBufferAdd( xSendBuffer, 0, ( const uint8_t * ) pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer->xDataLength );
211         }
212         else
213         {
214                 FreeRTOS_debug_printf( ( "xNetworkInterfaceOutput: send buffers full to store %lu\n", pxNetworkBuffer->xDataLength ) );
215         }
216
217         /* Kick the Tx task in either case in case it doesn't know the buffer is
218         full. */
219         SetEvent( pvSendEvent );
220
221         /* The buffer has been sent so can be released. */
222         if( bReleaseAfterSend != pdFALSE )
223         {
224                 vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
225         }
226
227         return pdPASS;
228 }
229 /*-----------------------------------------------------------*/
230
231 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )
232 {
233 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;
234 int32_t lInterfaceNumber = 1;
235 char cBuffer[ 512 ];
236 static BaseType_t xInvalidInterfaceDetected = pdFALSE;
237
238         if( xInvalidInterfaceDetected == pdFALSE )
239         {
240                 if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )
241                 {
242                         printf( "Could not obtain a list of network interfaces\n%s\n", cErrorBuffer );
243                         pxAllNetworkInterfaces = NULL;
244                 }
245                 else
246                 {
247                         printf( "\r\n\r\nThe following network interfaces are available:\r\n\r\n" );
248                 }
249
250                 if( pxAllNetworkInterfaces != NULL )
251                 {
252                         /* Print out the list of network interfaces.  The first in the list
253                         is interface '1', not interface '0'. */
254                         for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )
255                         {
256                                 /* The descriptions of the devices can be full of spaces, clean them
257                                 a little.  printf() can only be used here because the network is not
258                                 up yet - so no other network tasks will be running. */
259                                 printf( "Interface %d - %s\n", lInterfaceNumber, prvRemoveSpaces( cBuffer, sizeof( cBuffer ), xInterface->name ) );
260                                 printf( "              (%s)\n", prvRemoveSpaces(cBuffer, sizeof( cBuffer ), xInterface->description ? xInterface->description : "No description" ) );
261                                 printf( "\n" );
262                                 lInterfaceNumber++;
263                         }
264                 }
265
266                 if( lInterfaceNumber == 1 )
267                 {
268                         /* The interface number was never incremented, so the above for() loop
269                         did not execute meaning no interfaces were found. */
270                         printf( " \nNo network interfaces were found.\n" );
271                         pxAllNetworkInterfaces = NULL;
272                 }
273
274                 printf( "\r\nThe interface that will be opened is set by " );
275                 printf( "\"configNETWORK_INTERFACE_TO_USE\", which\r\nshould be defined in FreeRTOSConfig.h\r\n" );
276
277                 if( ( xConfigNetworkInterfaceToUse < 1L ) || ( xConfigNetworkInterfaceToUse >= lInterfaceNumber ) )
278                 {
279                         printf( "\r\nERROR:  configNETWORK_INTERFACE_TO_USE is set to %d, which is an invalid value.\r\n", xConfigNetworkInterfaceToUse );
280                         printf( "Please set configNETWORK_INTERFACE_TO_USE to one of the interface numbers listed above,\r\n" );
281                         printf( "then re-compile and re-start the application.  Only Ethernet (as opposed to WiFi)\r\n" );
282                         printf( "interfaces are supported.\r\n\r\nHALTING\r\n\r\n\r\n" );
283                         xInvalidInterfaceDetected = pdTRUE;
284
285                         if( pxAllNetworkInterfaces != NULL )
286                         {
287                                 /* Free the device list, as no devices are going to be opened. */
288                                 pcap_freealldevs( pxAllNetworkInterfaces );
289                                 pxAllNetworkInterfaces = NULL;
290                         }
291                 }
292                 else
293                 {
294                         printf( "Attempting to open interface number %d.\n", xConfigNetworkInterfaceToUse );
295                 }
296         }
297
298         return pxAllNetworkInterfaces;
299 }
300 /*-----------------------------------------------------------*/
301
302 static int prvOpenInterface( const char *pucName )
303 {
304 static char pucInterfaceName[ 256 ];
305
306         if( pucName != NULL )
307         {
308                 strncpy( pucInterfaceName, pucName, sizeof( pucInterfaceName ) );
309         }
310
311         pxOpenedInterfaceHandle = pcap_open(    pucInterfaceName,               /* The name of the selected interface. */
312                                                                                         ipTOTAL_ETHERNET_FRAME_SIZE, /* The size of the packet to capture. */
313                                                                                         PCAP_OPENFLAG_PROMISCUOUS,      /* Open in promiscuous mode as the MAC and
314                                                                                                                                                 IP address is going to be "simulated", and
315                                                                                                                                                 not be the real MAC and IP address.  This allows
316                                                                                                                                                 traffic to the simulated IP address to be routed
317                                                                                                                                                 to uIP, and traffic to the real IP address to be
318                                                                                                                                                 routed to the Windows TCP/IP stack. */
319                                                                                         100,
320                                                                                         NULL,                                   /* No authentication is required as this is
321                                                                                                                                                 not a remote capture session. */
322                                                                                         cErrorBuffer
323                                                                            );
324
325         if ( pxOpenedInterfaceHandle == NULL )
326         {
327                 printf( "\n%s is not supported by WinPcap and cannot be opened\n", pucInterfaceName );
328                 return 1;
329         }
330         else
331         {
332                 /* Configure the capture filter to allow blocking reads, and to filter
333                 out packets that are not of interest to this demo. */
334                 prvConfigureCaptureBehaviour();
335         }
336         return 0;
337 }
338 /*-----------------------------------------------------------*/
339
340 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )
341 {
342 pcap_if_t *pxInterface;
343 int32_t x;
344
345         /* Walk the list of devices until the selected device is located. */
346         pxInterface = pxAllNetworkInterfaces;
347         for( x = 0L; x < ( xConfigNetworkInterfaceToUse - 1L ); x++ )
348         {
349                 pxInterface = pxInterface->next;
350         }
351
352         /* Open the selected interface. */
353         if( prvOpenInterface( pxInterface->name ) == 0 )
354         {
355                 printf( "Successfully opened interface number %d.\n", x + 1 );
356         }
357         else
358         {
359                 printf( "Failed to open interface number %d.\n", x + 1 );
360         }
361
362         /* The device list is no longer required. */
363         pcap_freealldevs( pxAllNetworkInterfaces );
364 }
365 /*-----------------------------------------------------------*/
366
367 static void prvConfigureCaptureBehaviour( void )
368 {
369 struct bpf_program xFilterCode;
370 uint32_t ulNetMask;
371
372         /* Set up a filter so only the packets of interest are passed to the IP
373         stack.  cErrorBuffer is used for convenience to create the string.  Don't
374         confuse this with an error message. */
375         sprintf( cErrorBuffer, "broadcast or multicast or ether host %x:%x:%x:%x:%x:%x",
376                 ucMACAddress[0], ucMACAddress[1], ucMACAddress[2], ucMACAddress[3], ucMACAddress[4], ucMACAddress[5] );
377
378         ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;
379
380         if( pcap_compile( pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )
381         {
382                 printf( "\nThe packet filter string is invalid\n" );
383         }
384         else
385         {
386                 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )
387                 {
388                         printf( "\nAn error occurred setting the packet filter.\n" );
389                 }
390                 /* When pcap_compile() succeeds, it allocates memory for the memory pointed to by the bpf_program struct 
391                 parameter.pcap_freecode() will free that memory. */
392                 pcap_freecode( &xFilterCode );
393         }
394
395         /* Create the buffers used to pass packets between the FreeRTOS simulator
396         and the Win32 threads that are handling WinPCAP. */
397         prvCreateThreadSafeBuffers();
398
399         if( pvSendEvent == NULL )
400         {
401                 /* Create event used to signal the Win32 WinPCAP Tx thread. */
402                 pvSendEvent = CreateEvent( NULL, FALSE, TRUE, NULL );
403
404                 /* Create the Win32 thread that handles WinPCAP Rx. */
405                 vWinPcapRecvThreadHandle = CreateThread(
406                         NULL,   /* Pointer to thread security attributes. */
407                         0,              /* Initial thread stack size, in bytes. */
408                         prvWinPcapRecvThread,   /* Pointer to thread function. */
409                         NULL,   /* Argument for new thread. */
410                         0,              /* Creation flags. */
411                         NULL );
412
413                 /* Use the cores that are not used by the FreeRTOS tasks. */
414                 SetThreadAffinityMask( vWinPcapRecvThreadHandle, ~0x01u );
415
416                 /* Create the Win32 thread that handlers WinPCAP Tx. */
417                 vWinPcapSendThreadHandle = CreateThread(
418                         NULL,   /* Pointer to thread security attributes. */
419                         0,              /* initial thread stack size, in bytes. */
420                         prvWinPcapSendThread,   /* Pointer to thread function. */
421                         NULL,   /* Argument for new thread. */
422                         0,              /* Creation flags. */
423                         NULL );
424
425                 /* Use the cores that are not used by the FreeRTOS tasks. */
426                 SetThreadAffinityMask( vWinPcapSendThreadHandle, ~0x01u );
427
428                 /* Create a task that simulates an interrupt in a real system.  This will
429                 block waiting for packets, then send a message to the IP task when data
430                 is available. */
431                 xTaskCreate( prvInterruptSimulatorTask, "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );
432         }
433 }
434 /*-----------------------------------------------------------*/
435
436 /* WinPCAP function. */
437 void pcap_callback( u_char *user, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data )
438 {
439         (void)user;
440
441         /* THIS IS CALLED FROM A WINDOWS THREAD - DO NOT ATTEMPT ANY FREERTOS CALLS
442         OR TO PRINT OUT MESSAGES HERE. */
443
444         /* Pass data to the FreeRTOS simulator on a thread safe circular buffer. */
445         if( ( pkt_header->caplen <= ( ipconfigNETWORK_MTU + ipSIZE_OF_ETH_HEADER ) ) &&
446                 ( uxStreamBufferGetSpace( xRecvBuffer ) >= ( ( ( size_t ) pkt_header->caplen ) + sizeof( *pkt_header ) ) ) )
447         {
448                 uxStreamBufferAdd( xRecvBuffer, 0, ( const uint8_t* ) pkt_header, sizeof( *pkt_header ) );
449                 uxStreamBufferAdd( xRecvBuffer, 0, ( const uint8_t* ) pkt_data, ( size_t ) pkt_header->caplen );
450         }
451 }
452 /*-----------------------------------------------------------*/
453
454 DWORD WINAPI prvWinPcapRecvThread ( void *pvParam )
455 {
456         ( void ) pvParam;
457
458         /* THIS IS A WINDOWS THREAD - DO NOT ATTEMPT ANY FREERTOS CALLS OR TO PRINT
459         OUT MESSAGES HERE. */
460
461         for( ;; )
462         {
463                 pcap_dispatch( pxOpenedInterfaceHandle, 1, pcap_callback, ( u_char * ) "mydata" );
464         }
465 }
466 /*-----------------------------------------------------------*/
467
468 DWORD WINAPI prvWinPcapSendThread( void *pvParam )
469 {
470 size_t xLength;
471 uint8_t ucBuffer[ ipconfigNETWORK_MTU + ipSIZE_OF_ETH_HEADER ];
472 static char cErrorMessage[ 1024 ];
473 const DWORD xMaxMSToWait = 1000;
474
475         /* THIS IS A WINDOWS THREAD - DO NOT ATTEMPT ANY FREERTOS CALLS OR TO PRINT
476         OUT MESSAGES HERE. */
477
478         /* Remove compiler warnings about unused parameters. */
479         ( void ) pvParam;
480
481         for( ;; )
482         {
483                 /* Wait until notified of something to send. */
484                 WaitForSingleObject( pvSendEvent, xMaxMSToWait );
485
486                 /* Is there more than the length value stored in the circular buffer
487                 used to pass data from the FreeRTOS simulator into this Win32 thread? */
488                 while( uxStreamBufferGetSize( xSendBuffer ) > sizeof( xLength ) )
489                 {
490                         uxStreamBufferGet( xSendBuffer, 0, ( uint8_t * ) &xLength, sizeof( xLength ), pdFALSE );
491                         uxStreamBufferGet( xSendBuffer, 0, ( uint8_t* ) ucBuffer, xLength, pdFALSE );
492                         if( pcap_sendpacket( pxOpenedInterfaceHandle, ucBuffer, xLength  ) != 0 )
493                         {
494                                 ulWinPCAPSendFailures++;
495                         }
496                 }
497         }
498 }
499 /*-----------------------------------------------------------*/
500
501 static void prvInterruptSimulatorTask( void *pvParameters )
502 {
503 struct pcap_pkthdr xHeader;
504 static struct pcap_pkthdr *pxHeader;
505 const uint8_t *pucPacketData;
506 uint8_t ucRecvBuffer[ ipconfigNETWORK_MTU + ipSIZE_OF_ETH_HEADER ];
507 NetworkBufferDescriptor_t *pxNetworkBuffer;
508 IPStackEvent_t xRxEvent = { eNetworkRxEvent, NULL };
509 eFrameProcessingResult_t eResult;
510
511         /* Remove compiler warnings about unused parameters. */
512         ( void ) pvParameters;
513
514         for( ;; )
515         {
516                 /* Does the circular buffer used to pass data from the Win32 thread that
517                 handles WinPCAP Rx into the FreeRTOS simulator contain another packet? */
518                 if( uxStreamBufferGetSize( xRecvBuffer ) > sizeof( xHeader ) )
519                 {
520                         /* Get the next packet. */
521                         uxStreamBufferGet( xRecvBuffer, 0, (uint8_t*)&xHeader, sizeof( xHeader ), pdFALSE );
522                         uxStreamBufferGet( xRecvBuffer, 0, (uint8_t*)ucRecvBuffer, ( size_t ) xHeader.len, pdFALSE );
523                         pucPacketData = ucRecvBuffer;
524                         pxHeader = &xHeader;
525
526                         iptraceNETWORK_INTERFACE_RECEIVE();
527
528                         /* Check for minimal size. */
529                         if( pxHeader->len >= sizeof( EthernetHeader_t ) )
530                         {
531                                 eResult = ipCONSIDER_FRAME_FOR_PROCESSING( pucPacketData );
532                         }
533                         else
534                         {
535                                 eResult = eReleaseBuffer;
536                         }
537
538                         if( eResult == eProcessBuffer )
539                         {
540                                 /* Will the data fit into the frame buffer? */
541                                 if( pxHeader->len <= ipTOTAL_ETHERNET_FRAME_SIZE )
542                                 {
543                                         /* Obtain a buffer into which the data can be placed.  This
544                                         is only an interrupt simulator, not a real interrupt, so it
545                                         is ok to call the task level function here, but note that
546                                         some buffer implementations cannot be called from a real
547                                         interrupt. */
548                                         pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( pxHeader->len, 0 );
549
550                                         if( pxNetworkBuffer != NULL )
551                                         {
552                                                 memcpy( pxNetworkBuffer->pucEthernetBuffer, pucPacketData, pxHeader->len );
553                                                 pxNetworkBuffer->xDataLength = ( size_t ) pxHeader->len;
554
555                                                 #if( niDISRUPT_PACKETS == 1 )
556                                                 {
557                                                         pxNetworkBuffer = vRxFaultInjection( pxNetworkBuffer, pucPacketData );
558                                                 }
559                                                 #endif /* niDISRUPT_PACKETS */
560
561                                                 if( pxNetworkBuffer != NULL )
562                                                 {
563                                                         xRxEvent.pvData = ( void * ) pxNetworkBuffer;
564
565                                                         /* Data was received and stored.  Send a message to
566                                                         the IP task to let it know. */
567                                                         if( xSendEventStructToIPTask( &xRxEvent, ( TickType_t ) 0 ) == pdFAIL )
568                                                         {
569                                                                 /* The buffer could not be sent to the stack so
570                                                                 must be released again.  This is only an
571                                                                 interrupt simulator, not a real interrupt, so it
572                                                                 is ok to use the task level function here, but
573                                                                 note no all buffer implementations will allow
574                                                                 this function to be executed from a real
575                                                                 interrupt. */
576                                                                 vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
577                                                                 iptraceETHERNET_RX_EVENT_LOST();
578                                                         }
579                                                 }
580                                                 else
581                                                 {
582                                                         /* The packet was already released or stored inside
583                                                         vRxFaultInjection().  Don't release it here. */
584                                                 }
585                                         }
586                                         else
587                                         {
588                                                 iptraceETHERNET_RX_EVENT_LOST();
589                                         }
590                                 }
591                                 else
592                                 {
593                                         /* Log that a packet was dropped because it would have
594                                         overflowed the buffer, but there may be more buffers to
595                                         process. */
596                                 }
597                         }
598                 }
599                 else
600                 {
601                         /* There is no real way of simulating an interrupt.  Make sure
602                         other tasks can run. */
603                         vTaskDelay( configWINDOWS_MAC_INTERRUPT_SIMULATOR_DELAY );
604                 }
605         }
606 }
607 /*-----------------------------------------------------------*/
608
609 static const char *prvRemoveSpaces( char *pcBuffer, int aBuflen, const char *pcMessage )
610 {
611         char *pcTarget = pcBuffer;
612
613         /* Utility function used to formap messages being printed only. */
614         while( ( *pcMessage != 0 ) && ( pcTarget < ( pcBuffer + aBuflen - 1 ) ) )
615         {
616                 *( pcTarget++ ) = *pcMessage;
617
618                 if( isspace( *pcMessage ) != pdFALSE )
619                 {
620                         while( isspace( *pcMessage ) != pdFALSE )
621                         {
622                                 pcMessage++;
623                         }
624                 }
625                 else
626                 {
627                         pcMessage++;
628                 }
629         }
630
631         *pcTarget = '\0';
632
633         return pcBuffer;
634 }