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