]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/ethernet/lwip-1.4.0/ports/win32/ethernetif.c
***IMMINENT RELEASE NOTICE***
[freertos] / FreeRTOS / Demo / Common / ethernet / lwip-1.4.0 / ports / win32 / ethernetif.c
1 /*\r
2     FreeRTOS V8.1.0 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* WinPCap includes. */\r
67 #define HAVE_REMOTE\r
68 #include "pcap.h"\r
69 \r
70 /* FreeRTOS includes. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 #include "queue.h"\r
74 \r
75 /* lwIP includes. */\r
76 #include "lwip/opt.h"\r
77 #include "lwip/def.h"\r
78 #include "lwip/mem.h"\r
79 #include "lwip/pbuf.h"\r
80 #include "lwip/sys.h"\r
81 #include <lwip/stats.h>\r
82 #include <lwip/snmp.h>\r
83 #include "netif/etharp.h"\r
84 \r
85 /* Define those to better describe your network interface. */\r
86 #define IFNAME0 'w'\r
87 #define IFNAME1 'p'\r
88 \r
89 #define netifMAX_MTU 1500\r
90 \r
91 struct xEthernetIf\r
92 {\r
93         struct eth_addr *ethaddr;\r
94         /* Add whatever per-interface state that is needed here. */\r
95 };\r
96 \r
97 /*\r
98  * Place received packet in a pbuf and send a message to the tcpip task to let\r
99  * it know new data has arrived.\r
100  */\r
101 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength );\r
102 \r
103 /*\r
104  * Copy the received data into a pbuf.\r
105  */\r
106 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength );\r
107 \r
108 /*\r
109  * Send data from a pbuf to the hardware.\r
110  */\r
111 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p );\r
112 \r
113 /*\r
114  * Perform any hardware and/or driver initialisation necessary.\r
115  */\r
116 static void prvLowLevelInit( struct netif *pxNetIf );\r
117 \r
118 /*\r
119  * Query the computer the simulation is being executed on to find the network\r
120  * interfaces it has installed.\r
121  */\r
122 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void );\r
123 \r
124 /*\r
125  * Open the network interface.  The number of the interface to be opened is set\r
126  * by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.\r
127  */\r
128 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces );\r
129 \r
130 /*\r
131  * Interrupts cannot truely be simulated using WinPCap.  In reality this task\r
132  * just polls the interface. \r
133  */\r
134 static void prvInterruptSimulator( void *pvParameters );\r
135 \r
136 /*\r
137  * Configure the capture filter to allow blocking reads, and to filter out\r
138  * packets that are not of interest to this demo.\r
139  */\r
140 static void prvConfigureCaptureBehaviour( void );\r
141 \r
142 /*-----------------------------------------------------------*/\r
143 \r
144 /* The WinPCap interface being used. */\r
145 static pcap_t *pxOpenedInterfaceHandle = NULL;\r
146 \r
147 /* Parameter required for WinPCap API functions. */\r
148 static char cErrorBuffer[ PCAP_ERRBUF_SIZE ];\r
149 \r
150 /* The network interface that was opened. */\r
151 static struct netif *pxlwIPNetIf = NULL;\r
152 \r
153 /*-----------------------------------------------------------*/\r
154 \r
155 /**\r
156  * In this function, the hardware should be initialized.\r
157  * Called from ethernetif_init().\r
158  *\r
159  * @param pxNetIf the already initialized lwip network interface structure\r
160  *              for this ethernetif.\r
161  */\r
162 static void prvLowLevelInit( struct netif *pxNetIf )\r
163 {\r
164 pcap_if_t *pxAllNetworkInterfaces;\r
165   \r
166         /* set MAC hardware address length */\r
167         pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;\r
168 \r
169         /* set MAC hardware address */\r
170         pxNetIf->hwaddr[ 0 ] = configMAC_ADDR0;\r
171         pxNetIf->hwaddr[ 1 ] = configMAC_ADDR1;\r
172         pxNetIf->hwaddr[ 2 ] = configMAC_ADDR2;\r
173         pxNetIf->hwaddr[ 3 ] = configMAC_ADDR3;\r
174         pxNetIf->hwaddr[ 4 ] = configMAC_ADDR4;\r
175         pxNetIf->hwaddr[ 5 ] = configMAC_ADDR5;\r
176 \r
177         /* device capabilities */\r
178         /* don't set pxNetIf_FLAG_ETHARP if this device is not an ethernet one */\r
179         pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;\r
180  \r
181         /* Query the computer the simulation is being executed on to find the \r
182         network interfaces it has installed. */\r
183         pxAllNetworkInterfaces = prvPrintAvailableNetworkInterfaces();\r
184         \r
185         /* Open the network interface.  The number of the interface to be opened is \r
186         set by the configNETWORK_INTERFACE_TO_USE constant in FreeRTOSConfig.h.\r
187         Calling this function will set the pxOpenedInterfaceHandle variable.  If,\r
188         after calling this function, pxOpenedInterfaceHandle is equal to NULL, then\r
189         the interface could not be opened. */\r
190         if( pxAllNetworkInterfaces != NULL )\r
191         {\r
192                 prvOpenSelectedNetworkInterface( pxAllNetworkInterfaces );\r
193         }\r
194 \r
195         /* Remember which interface was opened as it is used in the interrupt \r
196         simulator task. */\r
197         pxlwIPNetIf = pxNetIf;\r
198 }\r
199 \r
200 /**\r
201  * This function should do the actual transmission of the packet. The packet is\r
202  * contained in the pbuf that is passed to the function. This pbuf\r
203  * might be chained.\r
204  *\r
205  * @param pxNetIf the lwip network interface structure for this ethernetif\r
206  * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)\r
207  * @return ERR_OK if the packet could be sent\r
208  *               an err_t value if the packet couldn't be sent\r
209  *\r
210  * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to\r
211  *         strange results. You might consider waiting for space in the DMA queue\r
212  *         to become availale since the stack doesn't retry to send a packet\r
213  *         dropped because of memory failure (except for the TCP timers).\r
214  */\r
215 \r
216 static err_t prvLowLevelOutput( struct netif *pxNetIf, struct pbuf *p )\r
217 {\r
218 \r
219         /* This is taken from lwIP example code and therefore does not conform\r
220         to the FreeRTOS coding standard. */\r
221 \r
222 struct pbuf *q;\r
223 static unsigned char ucBuffer[ 1520 ];\r
224 unsigned char *pucBuffer = ucBuffer;\r
225 unsigned char *pucChar;\r
226 struct eth_hdr *pxHeader;\r
227 u16_t usTotalLength = p->tot_len - ETH_PAD_SIZE;\r
228 err_t xReturn = ERR_OK;\r
229 \r
230         ( void ) pxNetIf;\r
231 \r
232         #if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF\r
233                 LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);\r
234         #endif\r
235 \r
236         /* Initiate transfer. */\r
237         if( p->len == p->tot_len ) \r
238         {\r
239                 /* No pbuf chain, don't have to copy -> faster. */\r
240                 pucBuffer = &( ( unsigned char * ) p->payload )[ ETH_PAD_SIZE ];\r
241         } \r
242         else \r
243         {\r
244                 /* pbuf chain, copy into contiguous ucBuffer. */\r
245                 if( p->tot_len >= sizeof( ucBuffer ) ) \r
246                 {\r
247                         LINK_STATS_INC( link.lenerr );\r
248                         LINK_STATS_INC( link.drop );\r
249                         snmp_inc_ifoutdiscards( pxNetIf );\r
250                         xReturn = ERR_BUF;\r
251                 }\r
252                 else\r
253                 {\r
254                         pucChar = ucBuffer;\r
255 \r
256                         for( q = p; q != NULL; q = q->next ) \r
257                         {\r
258                                 /* Send the data from the pbuf to the interface, one pbuf at a\r
259                                 time. The size of the data in each pbuf is kept in the ->len\r
260                                 variable. */\r
261                                 /* send data from(q->payload, q->len); */\r
262                                 LWIP_DEBUGF( NETIF_DEBUG, ("NETIF: send pucChar %p q->payload %p q->len %i q->next %p\n", pucChar, q->payload, ( int ) q->len, ( void* ) q->next ) );\r
263                                 if( q == p ) \r
264                                 {\r
265                                         memcpy( pucChar, &( ( char * ) q->payload )[ ETH_PAD_SIZE ], q->len - ETH_PAD_SIZE );\r
266                                         pucChar += q->len - ETH_PAD_SIZE;\r
267                                 } \r
268                                 else \r
269                                 {\r
270                                         memcpy( pucChar, q->payload, q->len );\r
271                                         pucChar += q->len;\r
272                                 }\r
273                         }\r
274                 }\r
275         }\r
276 \r
277         if( xReturn == ERR_OK )\r
278         {\r
279                 /* signal that packet should be sent */\r
280                 if( pcap_sendpacket( pxOpenedInterfaceHandle, pucBuffer, usTotalLength ) < 0 ) \r
281                 {\r
282                         LINK_STATS_INC( link.memerr );\r
283                         LINK_STATS_INC( link.drop );\r
284                         snmp_inc_ifoutdiscards( pxNetIf );\r
285                         xReturn = ERR_BUF;\r
286                 }\r
287                 else\r
288                 {\r
289                         LINK_STATS_INC( link.xmit );\r
290                         snmp_add_ifoutoctets( pxNetIf, usTotalLength );\r
291                         pxHeader = ( struct eth_hdr * )p->payload;\r
292 \r
293                         if( ( pxHeader->dest.addr[ 0 ] & 1 ) != 0 ) \r
294                         {\r
295                                 /* broadcast or multicast packet*/\r
296                                 snmp_inc_ifoutnucastpkts( pxNetIf );\r
297                         } \r
298                         else \r
299                         {\r
300                                 /* unicast packet */\r
301                                 snmp_inc_ifoutucastpkts( pxNetIf );\r
302                         }\r
303                 }\r
304         }\r
305         \r
306         return xReturn;\r
307 }\r
308 \r
309 /**\r
310  * Should allocate a pbuf and transfer the bytes of the incoming\r
311  * packet from the interface into the pbuf.\r
312  *\r
313  * @param pxNetIf the lwip network interface structure for this ethernetif\r
314  * @return a pbuf filled with the received packet (including MAC header)\r
315  *               NULL on memory error\r
316  */\r
317 static struct pbuf *prvLowLevelInput( const unsigned char * const pucInputData, long lDataLength )\r
318 {\r
319 struct pbuf *p = NULL, *q;\r
320 \r
321         if( lDataLength > 0 )\r
322         {\r
323                 #if ETH_PAD_SIZE\r
324                         len += ETH_PAD_SIZE; /* allow room for Ethernet padding */\r
325                 #endif\r
326 \r
327                 /* We allocate a pbuf chain of pbufs from the pool. */\r
328                 p = pbuf_alloc( PBUF_RAW, lDataLength, PBUF_POOL );\r
329   \r
330                 if( p != NULL ) \r
331                 {\r
332                         #if ETH_PAD_SIZE\r
333                                 pbuf_header( p, -ETH_PAD_SIZE ); /* drop the padding word */\r
334                         #endif\r
335 \r
336                         /* We iterate over the pbuf chain until we have read the entire\r
337                         * packet into the pbuf. */\r
338                         lDataLength = 0;\r
339                         for( q = p; q != NULL; q = q->next ) \r
340                         {\r
341                                 /* Read enough bytes to fill this pbuf in the chain. The\r
342                                 * available data in the pbuf is given by the q->len\r
343                                 * variable.\r
344                                 * This does not necessarily have to be a memcpy, you can also preallocate\r
345                                 * pbufs for a DMA-enabled MAC and after receiving truncate it to the\r
346                                 * actually received size. In this case, ensure the usTotalLength member of the\r
347                                 * pbuf is the sum of the chained pbuf len members.\r
348                                 */\r
349                                 memcpy( q->payload, &( pucInputData[ lDataLength ] ), q->len );\r
350                                 lDataLength += q->len;\r
351                         }\r
352 \r
353                         #if ETH_PAD_SIZE\r
354                                 pbuf_header( p, ETH_PAD_SIZE ); /* reclaim the padding word */\r
355                         #endif\r
356 \r
357                         LINK_STATS_INC( link.recv );\r
358                 }\r
359         }\r
360 \r
361         return p;  \r
362 }\r
363 \r
364 /**\r
365  * This function should be called when a packet is ready to be read\r
366  * from the interface. It uses the function prvLowLevelInput() that\r
367  * should handle the actual reception of bytes from the network\r
368  * interface. Then the type of the received packet is determined and\r
369  * the appropriate input function is called.\r
370  *\r
371  * @param pxNetIf the lwip network interface structure for this ethernetif\r
372  */\r
373 static void prvEthernetInput( const unsigned char * const pucInputData, long lInputLength )\r
374 {\r
375         /* This is taken from lwIP example code and therefore does not conform\r
376         to the FreeRTOS coding standard. */\r
377         \r
378 struct eth_hdr *pxHeader;\r
379 struct pbuf *p;\r
380 \r
381         /* move received packet into a new pbuf */\r
382         p = prvLowLevelInput( pucInputData, lInputLength );\r
383 \r
384         /* no packet could be read, silently ignore this */\r
385         if( p != NULL ) \r
386         {\r
387                 /* points to packet payload, which starts with an Ethernet header */\r
388                 pxHeader = p->payload;\r
389 \r
390                 switch( htons( pxHeader->type ) ) \r
391                 {\r
392                         /* IP or ARP packet? */\r
393                         case ETHTYPE_IP:\r
394                         case ETHTYPE_ARP:\r
395                                                                 /* full packet send to tcpip_thread to process */\r
396                                                                 if( pxlwIPNetIf->input( p, pxlwIPNetIf ) != ERR_OK )\r
397                                                                 { \r
398                                                                         LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_input: IP input error\n" ) );\r
399                                                                         pbuf_free(p);\r
400                                                                         p = NULL;\r
401                                                                 }\r
402                                                                 break;\r
403 \r
404                         default:\r
405                                                                 pbuf_free( p );\r
406                                                                 p = NULL;\r
407                         break;\r
408                 }\r
409         }\r
410 }\r
411 \r
412 /**\r
413  * Should be called at the beginning of the program to set up the\r
414  * network interface. It calls the function prvLowLevelInit() to do the\r
415  * actual setup of the hardware.\r
416  *\r
417  * This function should be passed as a parameter to netif_add().\r
418  *\r
419  * @param pxNetIf the lwip network interface structure for this ethernetif\r
420  * @return ERR_OK if the loopif is initialized\r
421  *               ERR_MEM if private data couldn't be allocated\r
422  *               any other err_t on error\r
423  */\r
424 err_t ethernetif_init( struct netif *pxNetIf )\r
425 {\r
426 err_t xReturn = ERR_OK;\r
427 \r
428         /* This is taken from lwIP example code and therefore does not conform\r
429         to the FreeRTOS coding standard. */\r
430         \r
431 struct xEthernetIf *pxEthernetIf;\r
432 \r
433         LWIP_ASSERT( "pxNetIf != NULL", ( pxNetIf != NULL ) );\r
434         \r
435         pxEthernetIf = mem_malloc( sizeof( struct xEthernetIf ) );\r
436         if( pxEthernetIf == NULL ) \r
437         {\r
438                 LWIP_DEBUGF(NETIF_DEBUG, ( "ethernetif_init: out of memory\n" ) );\r
439                 xReturn = ERR_MEM;\r
440         }\r
441         else\r
442         {\r
443                 #if LWIP_NETIF_HOSTNAME\r
444                 {\r
445                         /* Initialize interface hostname */\r
446                         pxNetIf->hostname = "lwip";\r
447                 }\r
448                 #endif /* LWIP_NETIF_HOSTNAME */\r
449 \r
450                 pxNetIf->state = pxEthernetIf;\r
451                 pxNetIf->name[ 0 ] = IFNAME0;\r
452                 pxNetIf->name[ 1 ] = IFNAME1;\r
453 \r
454                 /* We directly use etharp_output() here to save a function call.\r
455                 * You can instead declare your own function an call etharp_output()\r
456                 * from it if you have to do some checks before sending (e.g. if link\r
457                 * is available...) */\r
458                 pxNetIf->output = etharp_output;\r
459                 pxNetIf->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;\r
460                 pxNetIf->hwaddr_len = ETHARP_HWADDR_LEN;\r
461                 pxNetIf->mtu = netifMAX_MTU;\r
462                 pxNetIf->linkoutput = prvLowLevelOutput;\r
463 \r
464                 pxEthernetIf->ethaddr = ( struct eth_addr * ) &( pxNetIf->hwaddr[ 0 ] );\r
465   \r
466                 /* initialize the hardware */\r
467                 prvLowLevelInit( pxNetIf );\r
468 \r
469                 /* Was an interface opened? */\r
470                 if( pxOpenedInterfaceHandle == NULL )\r
471                 {\r
472                         /* Probably an invalid adapter number was defined in \r
473                         FreeRTOSConfig.h. */\r
474                         xReturn = ERR_VAL;\r
475                         configASSERT( pxOpenedInterfaceHandle );\r
476                 }\r
477         }\r
478 \r
479         return xReturn;\r
480 }\r
481 /*-----------------------------------------------------------*/\r
482 \r
483 static pcap_if_t * prvPrintAvailableNetworkInterfaces( void )\r
484 {       \r
485 pcap_if_t * pxAllNetworkInterfaces = NULL, *xInterface;\r
486 long lInterfaceNumber = 1;\r
487 \r
488         if( pcap_findalldevs_ex( PCAP_SRC_IF_STRING, NULL, &pxAllNetworkInterfaces, cErrorBuffer ) == -1 )\r
489         {\r
490                 printf( "\r\nCould not obtain a list of network interfaces\r\n%s\r\n", cErrorBuffer );\r
491                 pxAllNetworkInterfaces = NULL;\r
492         }\r
493 \r
494         if( pxAllNetworkInterfaces != NULL )\r
495         {\r
496                 /* Print out the list of network interfaces.  The first in the list\r
497                 is interface '1', not interface '0'. */\r
498                 for( xInterface = pxAllNetworkInterfaces; xInterface != NULL; xInterface = xInterface->next )\r
499                 {\r
500                         printf( "%d. %s", lInterfaceNumber, xInterface->name );\r
501                         \r
502                         if( xInterface->description != NULL )\r
503                         {\r
504                                 printf( " (%s)\r\n", xInterface->description );\r
505                         }\r
506                         else\r
507                         {\r
508                                 printf( " (No description available)\r\n") ;\r
509                         }\r
510                         \r
511                         lInterfaceNumber++;\r
512                 }\r
513         }\r
514 \r
515         if( lInterfaceNumber == 1 )\r
516         {\r
517                 /* The interface number was never incremented, so the above for() loop\r
518                 did not execute meaning no interfaces were found. */\r
519                 printf( " \r\nNo network interfaces were found.\r\n" );\r
520                 pxAllNetworkInterfaces = NULL;\r
521         }\r
522 \r
523         printf( "\r\nThe interface that will be opened is set by configNETWORK_INTERFACE_TO_USE which should be defined in FreeRTOSConfig.h\r\n" );\r
524         printf( "Attempting to open interface number %d.\r\n", configNETWORK_INTERFACE_TO_USE );\r
525         \r
526         if( ( configNETWORK_INTERFACE_TO_USE < 1L ) || ( configNETWORK_INTERFACE_TO_USE > lInterfaceNumber ) )\r
527         {\r
528                 printf("\r\nconfigNETWORK_INTERFACE_TO_USE is not in the valid range.\r\n" );\r
529                 \r
530                 if( pxAllNetworkInterfaces != NULL )\r
531                 {\r
532                         /* Free the device list, as no devices are going to be opened. */\r
533                         pcap_freealldevs( pxAllNetworkInterfaces );\r
534                         pxAllNetworkInterfaces = NULL;\r
535                 }\r
536         }\r
537 \r
538         return pxAllNetworkInterfaces;\r
539 }\r
540 /*-----------------------------------------------------------*/\r
541 \r
542 static void prvOpenSelectedNetworkInterface( pcap_if_t *pxAllNetworkInterfaces )\r
543 {\r
544 pcap_if_t *xInterface;\r
545 long x;\r
546 \r
547         /* Walk the list of devices until the selected device is located. */\r
548         xInterface = pxAllNetworkInterfaces;\r
549         for( x = 0L; x < ( configNETWORK_INTERFACE_TO_USE - 1L ); x++ )\r
550         {\r
551                 xInterface = xInterface->next;\r
552         }\r
553 \r
554         /* Open the selected interface. */\r
555         pxOpenedInterfaceHandle = pcap_open(    xInterface->name,                       /* The name of the selected interface. */\r
556                                                                                         netifMAX_MTU,                           /* The size of the packet to capture. */\r
557                                                                                         PCAP_OPENFLAG_PROMISCUOUS,      /* Open in promiscious mode as the MAC and \r
558                                                                                                                                                 IP address is going to be "simulated", and \r
559                                                                                                                                                 not be the real MAC and IP address.  This allows\r
560                                                                                                                                                 trafic to the simulated IP address to be routed\r
561                                                                                                                                                 to uIP, and trafic to the real IP address to be\r
562                                                                                                                                                 routed to the Windows TCP/IP stack. */\r
563                                                                                         0L,                                             /* The read time out.  This is going to block\r
564                                                                                                                                                 until data is available. */\r
565                                                                                         NULL,                                           /* No authentication is required as this is\r
566                                                                                                                                                 not a remote capture session. */\r
567                                                                                         cErrorBuffer                    \r
568                                                                            );\r
569                                                                            \r
570         if ( pxOpenedInterfaceHandle == NULL )\r
571         {\r
572                 printf( "\r\n%s is not supported by WinPcap and cannot be opened\r\n", xInterface->name );\r
573         }\r
574         else\r
575         {\r
576                 /* Configure the capture filter to allow blocking reads, and to filter \r
577                 out packets that are not of interest to this demo. */\r
578                 prvConfigureCaptureBehaviour();\r
579         }\r
580 \r
581         /* The device list is no longer required. */\r
582         pcap_freealldevs( pxAllNetworkInterfaces );\r
583 }\r
584 /*-----------------------------------------------------------*/\r
585 \r
586 static void prvInterruptSimulator( void *pvParameters )\r
587 {\r
588 static struct pcap_pkthdr *pxHeader;\r
589 const unsigned char *pucPacketData;\r
590 extern QueueHandle_t xEMACEventQueue;\r
591 long lResult;\r
592 \r
593         /* Just to kill the compiler warning. */\r
594         ( void ) pvParameters;\r
595 \r
596         for( ;; )\r
597         {\r
598                 /* Get the next packet. */\r
599                 lResult = pcap_next_ex( pxOpenedInterfaceHandle, &pxHeader, &pucPacketData );\r
600                 if( lResult == 1 )\r
601                 {\r
602                         if( pxlwIPNetIf != NULL )\r
603                         {\r
604                                 prvEthernetInput( pucPacketData, pxHeader->len );\r
605                         }\r
606                 }\r
607                 else\r
608                 {\r
609                         /* There is no real way of simulating an interrupt.  \r
610                         Make sure other tasks can run. */\r
611                         vTaskDelay( 5 );\r
612                 }\r
613         }\r
614 }\r
615 /*-----------------------------------------------------------*/\r
616 \r
617 static void prvConfigureCaptureBehaviour( void )\r
618 {\r
619 struct bpf_program xFilterCode;\r
620 const long lMinBytesToCopy = 10L, lBlocking = 1L;\r
621 unsigned long ulNetMask;\r
622 \r
623         /* Unblock a read as soon as anything is received. */\r
624         pcap_setmintocopy( pxOpenedInterfaceHandle, lMinBytesToCopy );\r
625 \r
626         /* Allow blocking. */\r
627         pcap_setnonblock( pxOpenedInterfaceHandle, lBlocking, cErrorBuffer );\r
628 \r
629         /* Set up a filter so only the packets of interest are passed to the lwIP\r
630         stack.  cErrorBuffer is used for convenience to create the string.  Don't\r
631         confuse this with an error message. */\r
632         sprintf( cErrorBuffer, "broadcast or multicast or host %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
633 \r
634         ulNetMask = ( configNET_MASK3 << 24UL ) | ( configNET_MASK2 << 16UL ) | ( configNET_MASK1 << 8L ) | configNET_MASK0;\r
635 \r
636         if( pcap_compile(pxOpenedInterfaceHandle, &xFilterCode, cErrorBuffer, 1, ulNetMask ) < 0 )\r
637         {\r
638                 printf( "\r\nThe packet filter string is invalid\r\n" );\r
639         }\r
640         else\r
641         {       \r
642                 if( pcap_setfilter( pxOpenedInterfaceHandle, &xFilterCode ) < 0 )\r
643                 {\r
644                         printf( "\r\nAn error occurred setting the packet filter.\r\n" );\r
645                 }\r
646         }\r
647 \r
648         /* Create a task that simulates an interrupt in a real system.  This will\r
649         block waiting for packets, then send a message to the uIP task when data\r
650         is available. */\r
651         xTaskCreate( prvInterruptSimulator, "MAC_ISR", configMINIMAL_STACK_SIZE, NULL, configMAC_ISR_SIMULATOR_PRIORITY, NULL );\r
652 }\r
653 \r