]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/portable/NetworkInterface/WinPCap/NetworkInterface.c
Update license information text files for the CLI, TCP and UDP products to be correct...
[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 static BaseType_t xInvalidInterfaceDetected = pdFALSE;\r
240 \r
241         if( xInvalidInterfaceDetected == pdFALSE )\r
242         {\r
243                 if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )\r
244                 {\r
245                         printf( "Could not obtain a list of network interfaces\n%s\n", cErrorBuffer );\r
246                         pxAllNetworkInterfaces = NULL;\r
247                 }\r
248                 else\r
249                 {\r
250                         printf( "\r\n\r\nThe following network interfaces are available:\r\n\r\n" );\r
251                 }\r
252 \r
253                 if( pxAllNetworkInterfaces != NULL )\r
254                 {\r
255                         /* Print out the list of network interfaces.  The first in the list\r
256                         is interface '1', not interface '0'. */\r
257                         for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )\r
258                         {\r
259                                 /* The descriptions of the devices can be full of spaces, clean them\r
260                                 a little.  printf() can only be used here because the network is not\r
261                                 up yet - so no other network tasks will be running. */\r
262                                 printf( "Interface %d - %s\n", lInterfaceNumber, prvRemoveSpaces( cBuffer, sizeof( cBuffer ), xInterface->name ) );\r
263                                 printf( "              (%s)\n", prvRemoveSpaces(cBuffer, sizeof( cBuffer ), xInterface->description ? xInterface->description : "No description" ) );\r
264                                 printf( "\n" );\r
265                                 lInterfaceNumber++;\r
266                         }\r
267                 }\r
268 \r
269                 if( lInterfaceNumber == 1 )\r
270                 {\r
271                         /* The interface number was never incremented, so the above for() loop\r
272                         did not execute meaning no interfaces were found. */\r
273                         printf( " \nNo network interfaces were found.\n" );\r
274                         pxAllNetworkInterfaces = NULL;\r
275                 }\r
276 \r
277                 printf( "\r\nThe interface that will be opened is set by " );\r
278                 printf( "\"configNETWORK_INTERFACE_TO_USE\", which\r\nshould be defined in FreeRTOSConfig.h\r\n" );\r
279 \r
280                 if( ( xConfigNextworkInterfaceToUse < 1L ) || ( xConfigNextworkInterfaceToUse >= lInterfaceNumber ) )\r
281                 {\r
282                         printf( "\r\nERROR:  configNETWORK_INTERFACE_TO_USE is set to %d, which is an invalid value.\r\n", xConfigNextworkInterfaceToUse );\r
283                         printf( "Please set configNETWORK_INTERFACE_TO_USE to one of the interface numbers listed above,\r\n" );\r
284                         printf( "then re-compile and re-start the application.  Only Ethernet (as opposed to WiFi)\r\n" );\r
285                         printf( "interfaces are supported.\r\n\r\nHALTING\r\n\r\n\r\n" );\r
286                         xInvalidInterfaceDetected = pdTRUE;\r
287 \r
288                         if( pxAllNetworkInterfaces != NULL )\r
289                         {\r
290                                 /* Free the device list, as no devices are going to be opened. */\r
291                                 pcap_freealldevs( pxAllNetworkInterfaces );\r
292                                 pxAllNetworkInterfaces = NULL;\r
293                         }\r
294                 }\r
295                 else\r
296                 {\r
297                         printf( "Attempting to open interface number %d.\n", xConfigNextworkInterfaceToUse );\r
298                 }\r
299         }\r
300 \r
301         return pxAllNetworkInterfaces;\r
302 }\r
303 /*-----------------------------------------------------------*/\r
304 \r
305 static void prvOpenInterface( const char *pucName )\r
306 {\r
307 static char pucInterfaceName[ 256 ];\r
308 \r
309         if( pucName != NULL )\r
310         {\r
311                 strncpy( pucInterfaceName, pucName, sizeof( pucInterfaceName ) );\r
312         }\r
313 \r
314         pxOpenedInterfaceHandle = pcap_open(    pucInterfaceName,               /* The name of the selected interface. */\r
315                                                                                         ipTOTAL_ETHERNET_FRAME_SIZE, /* The size of the packet to capture. */\r
316                                                                                         PCAP_OPENFLAG_PROMISCUOUS,      /* Open in promiscuous mode as the MAC and\r
317                                                                                                                                                 IP address is going to be "simulated", and\r
318                                                                                                                                                 not be the real MAC and IP address.  This allows\r
319                                                                                                                                                 traffic to the simulated IP address to be routed\r
320                                                                                                                                                 to uIP, and traffic to the real IP address to be\r
321                                                                                                                                                 routed to the Windows TCP/IP stack. */\r
322                                                                                         100,\r
323                                                                                         NULL,                                   /* No authentication is required as this is\r
324                                                                                                                                                 not a remote capture session. */\r
325                                                                                         cErrorBuffer\r
326                                                                            );\r
327 \r
328         if ( pxOpenedInterfaceHandle == NULL )\r
329         {\r
330                 printf( "\n%s is not supported by WinPcap and cannot be opened\n", pucInterfaceName );\r
331         }\r
332         else\r
333         {\r
334                 /* Configure the capture filter to allow blocking reads, and to filter\r
335                 out packets that are not of interest to this demo. */\r
336                 prvConfigureCaptureBehaviour();\r
337         }\r
338 }\r
339 /*-----------------------------------------------------------*/\r
340 \r
341 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )\r
342 {\r
343 pcap_if_t *xInterface;\r
344 int32_t x;\r
345 \r
346         /* Walk the list of devices until the selected device is located. */\r
347         xInterface = pxAllNetworkInterfaces;\r
348         for( x = 0L; x < ( xConfigNextworkInterfaceToUse - 1L ); x++ )\r
349         {\r
350                 xInterface = xInterface->next;\r
351         }\r
352 \r
353         /* Open the selected interface. */\r
354         prvOpenInterface( xInterface->name );\r
355 \r
356         /* The device list is no longer required. */\r
357         pcap_freealldevs( pxAllNetworkInterfaces );\r
358 }\r
359 /*-----------------------------------------------------------*/\r
360 \r
361 static void prvConfigureCaptureBehaviour( void )\r
362 {\r
363 struct bpf_program xFilterCode;\r
364 uint32_t ulNetMask;\r
365 \r
366         /* Set up a filter so only the packets of interest are passed to the IP\r
367         stack.  cErrorBuffer is used for convenience to create the string.  Don't\r
368         confuse this with an error message. */\r
369         sprintf( cErrorBuffer, "broadcast or multicast or ether host %x:%x:%x:%x:%x:%x",\r
370                 ucMACAddress[0], ucMACAddress[1], ucMACAddress[2], ucMACAddress[3], ucMACAddress[4], ucMACAddress[5] );\r
371 \r
372         ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;\r
373 \r
374         if( pcap_compile( pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )\r
375         {\r
376                 printf( "\nThe packet filter string is invalid\n" );\r
377         }\r
378         else\r
379         {\r
380                 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )\r
381                 {\r
382                         printf( "\nAn error occurred setting the packet filter.\n" );\r
383                 }\r
384         }\r
385 \r
386         /* Create the buffers used to pass packets between the FreeRTOS simulator\r
387         and the Win32 threads that are handling WinPCAP. */\r
388         prvCreateThreadSafeBuffers();\r
389 \r
390         if( pvSendEvent == NULL )\r
391         {\r
392                 /* Create event used to signal the Win32 WinPCAP Tx thread. */\r
393                 pvSendEvent = CreateEvent( NULL, FALSE, TRUE, NULL );\r
394 \r
395                 /* Create the Win32 thread that handles WinPCAP Rx. */\r
396                 vWinPcapRecvThreadHandle = CreateThread(\r
397                         NULL,   /* Pointer to thread security attributes. */\r
398                         0,              /* Initial thread stack size, in bytes. */\r
399                         prvWinPcapRecvThread,   /* Pointer to thread function. */\r
400                         NULL,   /* Argument for new thread. */\r
401                         0,              /* Creation flags. */\r
402                         NULL );\r
403 \r
404                 /* Use the cores that are not used by the FreeRTOS tasks. */\r
405                 SetThreadAffinityMask( vWinPcapRecvThreadHandle, ~0x01u );\r
406 \r
407                 /* Create the Win32 thread that handlers WinPCAP Tx. */\r
408                 vWinPcapSendThreadHandle = CreateThread(\r
409                         NULL,   /* Pointer to thread security attributes. */\r
410                         0,              /* initial thread stack size, in bytes. */\r
411                         prvWinPcapSendThread,   /* Pointer to thread function. */\r
412                         NULL,   /* Argument for new thread. */\r
413                         0,              /* Creation flags. */\r
414                         NULL );\r
415 \r
416                 /* Use the cores that are not used by the FreeRTOS tasks. */\r
417                 SetThreadAffinityMask( vWinPcapSendThreadHandle, ~0x01u );\r
418 \r
419                 /* Create a task that simulates an interrupt in a real system.  This will\r
420                 block waiting for packets, then send a message to the IP task when data\r
421                 is available. */\r
422                 xTaskCreate( prvInterruptSimulatorTask, "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );\r
423         }\r
424 }\r
425 /*-----------------------------------------------------------*/\r
426 \r
427 /* WinPCAP function. */\r
428 void pcap_callback( u_char *user, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data )\r
429 {\r
430         (void)user;\r
431 \r
432         /* THIS IS CALLED FROM A WINDOWS THREAD - DO NOT ATTEMPT ANY FREERTOS CALLS\r
433         OR TO PRINT OUT MESSAGES HERE. */\r
434 \r
435         /* Pass data to the FreeRTOS simulator on a thread safe circular buffer. */\r
436         if( ( pkt_header->caplen <= ( ipconfigNETWORK_MTU + ipSIZE_OF_ETH_HEADER ) ) &&\r
437                 ( uxStreamBufferGetSpace( xRecvBuffer ) >= ( ( ( size_t ) pkt_header->caplen ) + sizeof( *pkt_header ) ) ) )\r
438         {\r
439                 uxStreamBufferAdd( xRecvBuffer, 0, ( const uint8_t* ) pkt_header, sizeof( *pkt_header ) );\r
440                 uxStreamBufferAdd( xRecvBuffer, 0, ( const uint8_t* ) pkt_data, ( size_t ) pkt_header->caplen );\r
441         }\r
442 }\r
443 /*-----------------------------------------------------------*/\r
444 \r
445 DWORD WINAPI prvWinPcapRecvThread ( void *pvParam )\r
446 {\r
447         ( void ) pvParam;\r
448 \r
449         /* THIS IS A WINDOWS THREAD - DO NOT ATTEMPT ANY FREERTOS CALLS OR TO PRINT\r
450         OUT MESSAGES HERE. */\r
451 \r
452         for( ;; )\r
453         {\r
454                 pcap_dispatch( pxOpenedInterfaceHandle, 1, pcap_callback, ( u_char * ) "mydata" );\r
455         }\r
456 }\r
457 /*-----------------------------------------------------------*/\r
458 \r
459 DWORD WINAPI prvWinPcapSendThread( void *pvParam )\r
460 {\r
461 size_t xLength;\r
462 uint8_t ucBuffer[ ipconfigNETWORK_MTU + ipSIZE_OF_ETH_HEADER ];\r
463 static char cErrorMessage[ 1024 ];\r
464 const DWORD xMaxMSToWait = 1000;\r
465 \r
466         /* THIS IS A WINDOWS THREAD - DO NOT ATTEMPT ANY FREERTOS CALLS OR TO PRINT\r
467         OUT MESSAGES HERE. */\r
468 \r
469         /* Remove compiler warnings about unused parameters. */\r
470         ( void ) pvParam;\r
471 \r
472         for( ;; )\r
473         {\r
474                 /* Wait until notified of something to send. */\r
475                 WaitForSingleObject( pvSendEvent, xMaxMSToWait );\r
476 \r
477                 /* Is there more than the length value stored in the circular buffer\r
478                 used to pass data from the FreeRTOS simulator into this Win32 thread? */\r
479                 while( uxStreamBufferGetSize( xSendBuffer ) > sizeof( xLength ) )\r
480                 {\r
481                         uxStreamBufferGet( xSendBuffer, 0, ( uint8_t * ) &xLength, sizeof( xLength ), pdFALSE );\r
482                         uxStreamBufferGet( xSendBuffer, 0, ( uint8_t* ) ucBuffer, xLength, pdFALSE );\r
483                         if( pcap_sendpacket( pxOpenedInterfaceHandle, ucBuffer, xLength  ) != 0 )\r
484                         {\r
485                                 ulWinPCAPSendFailures++;\r
486                         }\r
487                 }\r
488         }\r
489 }\r
490 /*-----------------------------------------------------------*/\r
491 \r
492 static void prvInterruptSimulatorTask( void *pvParameters )\r
493 {\r
494 struct pcap_pkthdr xHeader;\r
495 static struct pcap_pkthdr *pxHeader;\r
496 const uint8_t *pucPacketData;\r
497 uint8_t ucRecvBuffer[ ipconfigNETWORK_MTU + ipSIZE_OF_ETH_HEADER ];\r
498 NetworkBufferDescriptor_t *pxNetworkBuffer;\r
499 IPStackEvent_t xRxEvent = { eNetworkRxEvent, NULL };\r
500 eFrameProcessingResult_t eResult;\r
501 \r
502         /* Remove compiler warnings about unused parameters. */\r
503         ( void ) pvParameters;\r
504 \r
505         for( ;; )\r
506         {\r
507                 /* Does the circular buffer used to pass data from the Win32 thread that\r
508                 handles WinPCAP Rx into the FreeRTOS simulator contain another packet? */\r
509                 if( uxStreamBufferGetSize( xRecvBuffer ) > sizeof( xHeader ) )\r
510                 {\r
511                         /* Get the next packet. */\r
512                         uxStreamBufferGet( xRecvBuffer, 0, (uint8_t*)&xHeader, sizeof( xHeader ), pdFALSE );\r
513                         uxStreamBufferGet( xRecvBuffer, 0, (uint8_t*)ucRecvBuffer, ( size_t ) xHeader.len, pdFALSE );\r
514                         pucPacketData = ucRecvBuffer;\r
515                         pxHeader = &xHeader;\r
516 \r
517                         iptraceNETWORK_INTERFACE_RECEIVE();\r
518 \r
519                         eResult = ipCONSIDER_FRAME_FOR_PROCESSING( pucPacketData );\r
520                         if( eResult == eProcessBuffer )\r
521                         {\r
522                                 /* Will the data fit into the frame buffer? */\r
523                                 if( pxHeader->len <= ipTOTAL_ETHERNET_FRAME_SIZE )\r
524                                 {\r
525                                         /* Obtain a buffer into which the data can be placed.  This\r
526                                         is only an interrupt simulator, not a real interrupt, so it\r
527                                         is ok to call the task level function here, but note that\r
528                                         some buffer implementations cannot be called from a real\r
529                                         interrupt. */\r
530                                         pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( pxHeader->len, 0 );\r
531 \r
532                                         if( pxNetworkBuffer != NULL )\r
533                                         {\r
534                                                 memcpy( pxNetworkBuffer->pucEthernetBuffer, pucPacketData, pxHeader->len );\r
535                                                 pxNetworkBuffer->xDataLength = ( size_t ) pxHeader->len;\r
536 \r
537                                                 #if( niDISRUPT_PACKETS == 1 )\r
538                                                 {\r
539                                                         pxNetworkBuffer = vRxFaultInjection( pxNetworkBuffer, pucPacketData );\r
540                                                 }\r
541                                                 #endif /* niDISRUPT_PACKETS */\r
542 \r
543                                                 if( pxNetworkBuffer != NULL )\r
544                                                 {\r
545                                                         xRxEvent.pvData = ( void * ) pxNetworkBuffer;\r
546 \r
547                                                         /* Data was received and stored.  Send a message to\r
548                                                         the IP task to let it know. */\r
549                                                         if( xSendEventStructToIPTask( &xRxEvent, ( TickType_t ) 0 ) == pdFAIL )\r
550                                                         {\r
551                                                                 /* The buffer could not be sent to the stack so\r
552                                                                 must be released again.  This is only an\r
553                                                                 interrupt simulator, not a real interrupt, so it\r
554                                                                 is ok to use the task level function here, but\r
555                                                                 note no all buffer implementations will allow\r
556                                                                 this function to be executed from a real\r
557                                                                 interrupt. */\r
558                                                                 vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );\r
559                                                                 iptraceETHERNET_RX_EVENT_LOST();\r
560                                                         }\r
561                                                 }\r
562                                                 else\r
563                                                 {\r
564                                                         /* The packet was already released or stored inside\r
565                                                         vRxFaultInjection().  Don't release it here. */\r
566                                                 }\r
567                                         }\r
568                                         else\r
569                                         {\r
570                                                 iptraceETHERNET_RX_EVENT_LOST();\r
571                                         }\r
572                                 }\r
573                                 else\r
574                                 {\r
575                                         /* Log that a packet was dropped because it would have\r
576                                         overflowed the buffer, but there may be more buffers to\r
577                                         process. */\r
578                                 }\r
579                         }\r
580                 }\r
581                 else\r
582                 {\r
583                         /* There is no real way of simulating an interrupt.  Make sure\r
584                         other tasks can run. */\r
585                         vTaskDelay( configWINDOWS_MAC_INTERRUPT_SIMULATOR_DELAY );\r
586                 }\r
587         }\r
588 }\r
589 /*-----------------------------------------------------------*/\r
590 \r
591 static const char *prvRemoveSpaces( char *pcBuffer, int aBuflen, const char *pcMessage )\r
592 {\r
593         char *pcTarget = pcBuffer;\r
594 \r
595         /* Utility function used to formap messages being printed only. */\r
596         while( ( *pcMessage != 0 ) && ( pcTarget < ( pcBuffer + aBuflen - 1 ) ) )\r
597         {\r
598                 *( pcTarget++ ) = *pcMessage;\r
599 \r
600                 if( isspace( *pcMessage ) != pdFALSE )\r
601                 {\r
602                         while( isspace( *pcMessage ) != pdFALSE )\r
603                         {\r
604                                 pcMessage++;\r
605                         }\r
606                 }\r
607                 else\r
608                 {\r
609                         pcMessage++;\r
610                 }\r
611         }\r
612 \r
613         *pcTarget = '\0';\r
614 \r
615         return pcBuffer;\r
616 }\r
617 \r