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