]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_UDP_IP.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / FreeRTOS_UDP_IP.c
index e7d2a6c50a5035537df8d89b23b5f721da93fda3..14ae96e6480b14e8f9a0faf1c6294aa751b62241 100644 (file)
-/*\r
- * FreeRTOS+TCP V2.2.0\r
- * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
- *\r
- * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
- * this software and associated documentation files (the "Software"), to deal in\r
- * the Software without restriction, including without limitation the rights to\r
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
- * the Software, and to permit persons to whom the Software is furnished to do so,\r
- * subject to the following conditions:\r
- *\r
- * The above copyright notice and this permission notice shall be included in all\r
- * copies or substantial portions of the Software.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
- *\r
- * http://aws.amazon.com/freertos\r
- * http://www.FreeRTOS.org\r
- */\r
-\r
-/* Standard includes. */\r
-#include <stdint.h>\r
-#include <stdio.h>\r
-\r
-/* FreeRTOS includes. */\r
-#include "FreeRTOS.h"\r
-#include "task.h"\r
-#include "queue.h"\r
-#include "semphr.h"\r
-\r
-/* FreeRTOS+TCP includes. */\r
-#include "FreeRTOS_IP.h"\r
-#include "FreeRTOS_Sockets.h"\r
-#include "FreeRTOS_IP_Private.h"\r
-#include "FreeRTOS_UDP_IP.h"\r
-#include "FreeRTOS_ARP.h"\r
-#include "FreeRTOS_DHCP.h"\r
-#include "NetworkInterface.h"\r
-#include "NetworkBufferManagement.h"\r
-\r
-#if( ipconfigUSE_DNS == 1 )\r
-       #include "FreeRTOS_DNS.h"\r
-#endif\r
-\r
-/* The expected IP version and header length coded into the IP header itself. */\r
-#define ipIP_VERSION_AND_HEADER_LENGTH_BYTE ( ( uint8_t ) 0x45 )\r
-\r
-/* Part of the Ethernet and IP headers are always constant when sending an IPv4\r
-UDP packet.  This array defines the constant parts, allowing this part of the\r
-packet to be filled in using a simple memcpy() instead of individual writes. */\r
-UDPPacketHeader_t xDefaultPartUDPPacketHeader =\r
-{\r
-       /* .ucBytes : */\r
-       {\r
-               0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     /* Ethernet source MAC address. */\r
-               0x08, 0x00,                                                     /* Ethernet frame type. */\r
-               ipIP_VERSION_AND_HEADER_LENGTH_BYTE,    /* ucVersionHeaderLength. */\r
-               0x00,                                                                   /* ucDifferentiatedServicesCode. */\r
-               0x00, 0x00,                                                     /* usLength. */\r
-               0x00, 0x00,                                                     /* usIdentification. */\r
-               0x00, 0x00,                                                     /* usFragmentOffset. */\r
-               ipconfigUDP_TIME_TO_LIVE,                               /* ucTimeToLive */\r
-               ipPROTOCOL_UDP,                                                 /* ucProtocol. */\r
-               0x00, 0x00,                                                     /* usHeaderChecksum. */\r
-               0x00, 0x00, 0x00, 0x00                                  /* Source IP address. */\r
-       }\r
-};\r
-/*-----------------------------------------------------------*/\r
-\r
-void vProcessGeneratedUDPPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer )\r
-{\r
-UDPPacket_t *pxUDPPacket;\r
-IPHeader_t *pxIPHeader;\r
-eARPLookupResult_t eReturned;\r
-uint32_t ulIPAddress = pxNetworkBuffer->ulIPAddress;\r
-size_t uxPayloadSize;\r
-\r
-       /* Map the UDP packet onto the start of the frame. */\r
-       pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;\r
-\r
-#if ipconfigSUPPORT_OUTGOING_PINGS == 1\r
-       if( pxNetworkBuffer->usPort == ipPACKET_CONTAINS_ICMP_DATA )\r
-       {\r
-               uxPayloadSize = pxNetworkBuffer->xDataLength - sizeof( ICMPPacket_t );\r
-       }\r
-       else\r
-#endif\r
-       {\r
-               uxPayloadSize = pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t );\r
-       }\r
-\r
-       /* Determine the ARP cache status for the requested IP address. */\r
-       eReturned = eARPGetCacheEntry( &( ulIPAddress ), &( pxUDPPacket->xEthernetHeader.xDestinationAddress ) );\r
-\r
-       if( eReturned != eCantSendPacket )\r
-       {\r
-               if( eReturned == eARPCacheHit )\r
-               {\r
-                       #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )\r
-                               uint8_t ucSocketOptions;\r
-                       #endif\r
-                       iptraceSENDING_UDP_PACKET( pxNetworkBuffer->ulIPAddress );\r
-\r
-                       /* Create short cuts to the data within the packet. */\r
-                       pxIPHeader = &( pxUDPPacket->xIPHeader );\r
-\r
-               #if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )\r
-                       /* Is it possible that the packet is not actually a UDP packet\r
-                       after all, but an ICMP packet. */\r
-                       if( pxNetworkBuffer->usPort != ipPACKET_CONTAINS_ICMP_DATA )\r
-               #endif /* ipconfigSUPPORT_OUTGOING_PINGS */\r
-                       {\r
-                       UDPHeader_t *pxUDPHeader;\r
-\r
-                               pxUDPHeader = &( pxUDPPacket->xUDPHeader );\r
-\r
-                               pxUDPHeader->usDestinationPort = pxNetworkBuffer->usPort;\r
-                               pxUDPHeader->usSourcePort = pxNetworkBuffer->usBoundPort;\r
-                               pxUDPHeader->usLength = ( uint16_t ) ( uxPayloadSize + sizeof( UDPHeader_t ) );\r
-                               pxUDPHeader->usLength = FreeRTOS_htons( pxUDPHeader->usLength );\r
-                               pxUDPHeader->usChecksum = 0u;\r
-                       }\r
-\r
-                       /* memcpy() the constant parts of the header information into\r
-                       the     correct location within the packet.  This fills in:\r
-                               xEthernetHeader.xSourceAddress\r
-                               xEthernetHeader.usFrameType\r
-                               xIPHeader.ucVersionHeaderLength\r
-                               xIPHeader.ucDifferentiatedServicesCode\r
-                               xIPHeader.usLength\r
-                               xIPHeader.usIdentification\r
-                               xIPHeader.usFragmentOffset\r
-                               xIPHeader.ucTimeToLive\r
-                               xIPHeader.ucProtocol\r
-                       and\r
-                               xIPHeader.usHeaderChecksum\r
-                       */\r
-                       /* Save options now, as they will be overwritten by memcpy */\r
-                       #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )\r
-                               ucSocketOptions = pxNetworkBuffer->pucEthernetBuffer[ ipSOCKET_OPTIONS_OFFSET ];\r
-                       #endif\r
-                       /*\r
-                        * Offset the memcpy by the size of a MAC address to start at the packet's\r
-                        * Ethernet header 'source' MAC address; the preceding 'destination' should not be altered.\r
-                        */\r
-                       char *pxUdpSrcAddrOffset = ( char *) pxUDPPacket + sizeof( MACAddress_t );\r
-                       memcpy( pxUdpSrcAddrOffset, xDefaultPartUDPPacketHeader.ucBytes, sizeof( xDefaultPartUDPPacketHeader ) );\r
-\r
-               #if ipconfigSUPPORT_OUTGOING_PINGS == 1\r
-                       if( pxNetworkBuffer->usPort == ipPACKET_CONTAINS_ICMP_DATA )\r
-                       {\r
-                               pxIPHeader->ucProtocol = ipPROTOCOL_ICMP;\r
-                               pxIPHeader->usLength = ( uint16_t ) ( uxPayloadSize + sizeof( IPHeader_t ) + sizeof( ICMPHeader_t ) );\r
-                       }\r
-                       else\r
-               #endif /* ipconfigSUPPORT_OUTGOING_PINGS */\r
-                       {\r
-                               pxIPHeader->usLength = ( uint16_t ) ( uxPayloadSize + sizeof( IPHeader_t ) + sizeof( UDPHeader_t ) );\r
-                       }\r
-\r
-                       pxIPHeader->usLength = FreeRTOS_htons( pxIPHeader->usLength );\r
-                       /* HT:endian: changed back to network endian */\r
-                       pxIPHeader->ulDestinationIPAddress = pxNetworkBuffer->ulIPAddress;\r
-\r
-                       #if( ipconfigUSE_LLMNR == 1 )\r
-                       {\r
-                               /* LLMNR messages are typically used on a LAN and they're\r
-                                * not supposed to cross routers */\r
-                               if( pxNetworkBuffer->ulIPAddress == ipLLMNR_IP_ADDR )\r
-                               {\r
-                                       pxIPHeader->ucTimeToLive = 0x01;\r
-                               }\r
-                       }\r
-                       #endif\r
-\r
-                       #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )\r
-                       {\r
-                               pxIPHeader->usHeaderChecksum = 0u;\r
-                               pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );\r
-                               pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );\r
-\r
-                               if( ( ucSocketOptions & ( uint8_t ) FREERTOS_SO_UDPCKSUM_OUT ) != 0u )\r
-                               {\r
-                                       usGenerateProtocolChecksum( (uint8_t*)pxUDPPacket, pxNetworkBuffer->xDataLength, pdTRUE );\r
-                               }\r
-                               else\r
-                               {\r
-                                       pxUDPPacket->xUDPHeader.usChecksum = 0u;\r
-                               }\r
-                       }\r
-                       #endif\r
-               }\r
-               else if( eReturned == eARPCacheMiss )\r
-               {\r
-                       /* Add an entry to the ARP table with a null hardware address.\r
-                       This allows the ARP timer to know that an ARP reply is\r
-                       outstanding, and perform retransmissions if necessary. */\r
-                       vARPRefreshCacheEntry( NULL, ulIPAddress );\r
-\r
-                       /* Generate an ARP for the required IP address. */\r
-                       iptracePACKET_DROPPED_TO_GENERATE_ARP( pxNetworkBuffer->ulIPAddress );\r
-                       pxNetworkBuffer->ulIPAddress = ulIPAddress;\r
-                       vARPGenerateRequestPacket( pxNetworkBuffer );\r
-               }\r
-               else\r
-               {\r
-                       /* The lookup indicated that an ARP request has already been\r
-                       sent out for the queried IP address. */\r
-                       eReturned = eCantSendPacket;\r
-               }\r
-       }\r
-\r
-       if( eReturned != eCantSendPacket )\r
-       {\r
-               /* The network driver is responsible for freeing the network buffer\r
-               after the packet has been sent. */\r
-\r
-               #if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES )\r
-               {\r
-                       if( pxNetworkBuffer->xDataLength < ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES )\r
-                       {\r
-                       BaseType_t xIndex;\r
-\r
-                               for( xIndex = ( BaseType_t ) pxNetworkBuffer->xDataLength; xIndex < ( BaseType_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES; xIndex++ )\r
-                               {\r
-                                       pxNetworkBuffer->pucEthernetBuffer[ xIndex ] = 0u;\r
-                               }\r
-                               pxNetworkBuffer->xDataLength = ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES;\r
-                       }\r
-               }\r
-               #endif\r
-\r
-               xNetworkInterfaceOutput( pxNetworkBuffer, pdTRUE );\r
-       }\r
-       else\r
-       {\r
-               /* The packet can't be sent (DHCP not completed?).  Just drop the\r
-               packet. */\r
-               vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );\r
-       }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-BaseType_t xProcessReceivedUDPPacket( NetworkBufferDescriptor_t *pxNetworkBuffer, uint16_t usPort )\r
-{\r
-BaseType_t xReturn = pdPASS;\r
-FreeRTOS_Socket_t *pxSocket;\r
-configASSERT(pxNetworkBuffer);\r
-configASSERT(pxNetworkBuffer->pucEthernetBuffer);\r
-\r
-\r
-UDPPacket_t *pxUDPPacket = (UDPPacket_t *) pxNetworkBuffer->pucEthernetBuffer;\r
-\r
-       /* Caller must check for minimum packet size. */\r
-       pxSocket = pxUDPSocketLookup( usPort );\r
-\r
-       if( pxSocket )\r
-       {\r
-\r
-               /* When refreshing the ARP cache with received UDP packets we must be\r
-               careful;  hundreds of broadcast messages may pass and if we're not\r
-               handling them, no use to fill the ARP cache with those IP addresses. */\r
-               vARPRefreshCacheEntry( &( pxUDPPacket->xEthernetHeader.xSourceAddress ), pxUDPPacket->xIPHeader.ulSourceIPAddress );\r
-\r
-               #if( ipconfigUSE_CALLBACKS == 1 )\r
-               {\r
-                       /* Did the owner of this socket register a reception handler ? */\r
-                       if( ipconfigIS_VALID_PROG_ADDRESS( pxSocket->u.xUDP.pxHandleReceive ) )\r
-                       {\r
-                               struct freertos_sockaddr xSourceAddress, destinationAddress;\r
-                               void *pcData = ( void * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipUDP_PAYLOAD_OFFSET_IPv4 ] );\r
-                               FOnUDPReceive_t xHandler = ( FOnUDPReceive_t ) pxSocket->u.xUDP.pxHandleReceive;\r
-                               xSourceAddress.sin_port = pxNetworkBuffer->usPort;\r
-                               xSourceAddress.sin_addr = pxNetworkBuffer->ulIPAddress;\r
-                               destinationAddress.sin_port = usPort;\r
-                               destinationAddress.sin_addr = pxUDPPacket->xIPHeader.ulDestinationIPAddress;\r
-\r
-                               if( xHandler( ( Socket_t ) pxSocket, ( void* ) pcData, ( size_t ) pxNetworkBuffer->xDataLength,\r
-                                       &xSourceAddress, &destinationAddress ) )\r
-                               {\r
-                                       xReturn = pdFAIL; /* FAIL means that we did not consume or release the buffer */\r
-                               }\r
-                       }\r
-               }\r
-               #endif /* ipconfigUSE_CALLBACKS */\r
-\r
-               #if( ipconfigUDP_MAX_RX_PACKETS > 0 )\r
-               {\r
-                       if( xReturn == pdPASS )\r
-                       {\r
-                               if ( listCURRENT_LIST_LENGTH( &( pxSocket->u.xUDP.xWaitingPacketsList ) ) >= pxSocket->u.xUDP.uxMaxPackets )\r
-                               {\r
-                                       FreeRTOS_debug_printf( ( "xProcessReceivedUDPPacket: buffer full %ld >= %ld port %u\n",\r
-                                               listCURRENT_LIST_LENGTH( &( pxSocket->u.xUDP.xWaitingPacketsList ) ),\r
-                                               pxSocket->u.xUDP.uxMaxPackets, pxSocket->usLocalPort ) );\r
-                                       xReturn = pdFAIL; /* we did not consume or release the buffer */\r
-                               }\r
-                       }\r
-               }\r
-               #endif\r
-\r
-               if( xReturn == pdPASS )\r
-               {\r
-                       vTaskSuspendAll();\r
-                       {\r
-                               if( xReturn == pdPASS )\r
-                               {\r
-                                       taskENTER_CRITICAL();\r
-                                       {\r
-                                               /* Add the network packet to the list of packets to be\r
-                                               processed by the socket. */\r
-                                               vListInsertEnd( &( pxSocket->u.xUDP.xWaitingPacketsList ), &( pxNetworkBuffer->xBufferListItem ) );\r
-                                       }\r
-                                       taskEXIT_CRITICAL();\r
-                               }\r
-                       }\r
-                       xTaskResumeAll();\r
-\r
-                       /* Set the socket's receive event */\r
-                       if( pxSocket->xEventGroup != NULL )\r
-                       {\r
-                               xEventGroupSetBits( pxSocket->xEventGroup, eSOCKET_RECEIVE );\r
-                       }\r
-\r
-                       #if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )\r
-                       {\r
-                               if( ( pxSocket->pxSocketSet != NULL ) && ( ( pxSocket->xSelectBits & eSELECT_READ ) != 0 ) )\r
-                               {\r
-                                       xEventGroupSetBits( pxSocket->pxSocketSet->xSelectGroup, eSELECT_READ );\r
-                               }\r
-                       }\r
-                       #endif\r
-\r
-                       #if( ipconfigSOCKET_HAS_USER_SEMAPHORE == 1 )\r
-                       {\r
-                               if( pxSocket->pxUserSemaphore != NULL )\r
-                               {\r
-                                       xSemaphoreGive( pxSocket->pxUserSemaphore );\r
-                               }\r
-                       }\r
-                       #endif\r
-\r
-                       #if( ipconfigUSE_DHCP == 1 )\r
-                       {\r
-                               if( xIsDHCPSocket( pxSocket ) )\r
-                               {\r
-                                       xSendEventToIPTask( eDHCPEvent );\r
-                               }\r
-                       }\r
-                       #endif\r
-               }\r
-       }\r
-       else\r
-       {\r
-               /* There is no socket listening to the target port, but still it might\r
-               be for this node. */\r
-\r
-               #if( ipconfigUSE_DNS == 1 ) && ( ipconfigDNS_USE_CALLBACKS == 1 )\r
-                       /* A DNS reply, check for the source port.  Although the DNS client\r
-                       does open a UDP socket to send a messages, this socket will be\r
-                       closed after a short timeout.  Messages that come late (after the\r
-                       socket is closed) will be treated here. */\r
-                       if( FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usSourcePort ) == ipDNS_PORT )\r
-                       {\r
-                               vARPRefreshCacheEntry( &( pxUDPPacket->xEthernetHeader.xSourceAddress ), pxUDPPacket->xIPHeader.ulSourceIPAddress );\r
-                               xReturn = ( BaseType_t )ulDNSHandlePacket( pxNetworkBuffer );\r
-                       }\r
-                       else\r
-               #endif\r
-\r
-               #if( ipconfigUSE_LLMNR == 1 )\r
-                       /* a LLMNR request, check for the destination port. */\r
-                       if( ( usPort == FreeRTOS_ntohs( ipLLMNR_PORT ) ) ||\r
-                               ( pxUDPPacket->xUDPHeader.usSourcePort == FreeRTOS_ntohs( ipLLMNR_PORT ) ) )\r
-                       {\r
-                               vARPRefreshCacheEntry( &( pxUDPPacket->xEthernetHeader.xSourceAddress ), pxUDPPacket->xIPHeader.ulSourceIPAddress );\r
-                               xReturn = ( BaseType_t )ulDNSHandlePacket( pxNetworkBuffer );\r
-                       }\r
-                       else\r
-               #endif /* ipconfigUSE_LLMNR */\r
-\r
-               #if( ipconfigUSE_NBNS == 1 )\r
-                       /* a NetBIOS request, check for the destination port */\r
-                       if( ( usPort == FreeRTOS_ntohs( ipNBNS_PORT ) ) ||\r
-                               ( pxUDPPacket->xUDPHeader.usSourcePort == FreeRTOS_ntohs( ipNBNS_PORT ) ) )\r
-                       {\r
-                               vARPRefreshCacheEntry( &( pxUDPPacket->xEthernetHeader.xSourceAddress ), pxUDPPacket->xIPHeader.ulSourceIPAddress );\r
-                               xReturn = ( BaseType_t )ulNBNSHandlePacket( pxNetworkBuffer );\r
-                       }\r
-                       else\r
-               #endif /* ipconfigUSE_NBNS */\r
-               {\r
-                       xReturn = pdFAIL;\r
-               }\r
-       }\r
-\r
-       return xReturn;\r
-}\r
-/*-----------------------------------------------------------*/\r
+/*
+ * FreeRTOS+TCP V2.2.0
+ * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+ * the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * http://aws.amazon.com/freertos
+ * http://www.FreeRTOS.org
+ */
+
+/* Standard includes. */
+#include <stdint.h>
+#include <stdio.h>
+
+/* FreeRTOS includes. */
+#include "FreeRTOS.h"
+#include "task.h"
+#include "queue.h"
+#include "semphr.h"
+
+/* FreeRTOS+TCP includes. */
+#include "FreeRTOS_IP.h"
+#include "FreeRTOS_Sockets.h"
+#include "FreeRTOS_IP_Private.h"
+#include "FreeRTOS_UDP_IP.h"
+#include "FreeRTOS_ARP.h"
+#include "FreeRTOS_DHCP.h"
+#include "NetworkInterface.h"
+#include "NetworkBufferManagement.h"
+
+#if( ipconfigUSE_DNS == 1 )
+       #include "FreeRTOS_DNS.h"
+#endif
+
+/* The expected IP version and header length coded into the IP header itself. */
+#define ipIP_VERSION_AND_HEADER_LENGTH_BYTE ( ( uint8_t ) 0x45 )
+
+/* Part of the Ethernet and IP headers are always constant when sending an IPv4
+UDP packet.  This array defines the constant parts, allowing this part of the
+packet to be filled in using a simple memcpy() instead of individual writes. */
+UDPPacketHeader_t xDefaultPartUDPPacketHeader =
+{
+       /* .ucBytes : */
+       {
+               0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     /* Ethernet source MAC address. */
+               0x08, 0x00,                                                     /* Ethernet frame type. */
+               ipIP_VERSION_AND_HEADER_LENGTH_BYTE,    /* ucVersionHeaderLength. */
+               0x00,                                                                   /* ucDifferentiatedServicesCode. */
+               0x00, 0x00,                                                     /* usLength. */
+               0x00, 0x00,                                                     /* usIdentification. */
+               0x00, 0x00,                                                     /* usFragmentOffset. */
+               ipconfigUDP_TIME_TO_LIVE,                               /* ucTimeToLive */
+               ipPROTOCOL_UDP,                                                 /* ucProtocol. */
+               0x00, 0x00,                                                     /* usHeaderChecksum. */
+               0x00, 0x00, 0x00, 0x00                                  /* Source IP address. */
+       }
+};
+/*-----------------------------------------------------------*/
+
+void vProcessGeneratedUDPPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer )
+{
+UDPPacket_t *pxUDPPacket;
+IPHeader_t *pxIPHeader;
+eARPLookupResult_t eReturned;
+uint32_t ulIPAddress = pxNetworkBuffer->ulIPAddress;
+size_t uxPayloadSize;
+
+       /* Map the UDP packet onto the start of the frame. */
+       pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;
+
+#if ipconfigSUPPORT_OUTGOING_PINGS == 1
+       if( pxNetworkBuffer->usPort == ipPACKET_CONTAINS_ICMP_DATA )
+       {
+               uxPayloadSize = pxNetworkBuffer->xDataLength - sizeof( ICMPPacket_t );
+       }
+       else
+#endif
+       {
+               uxPayloadSize = pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t );
+       }
+
+       /* Determine the ARP cache status for the requested IP address. */
+       eReturned = eARPGetCacheEntry( &( ulIPAddress ), &( pxUDPPacket->xEthernetHeader.xDestinationAddress ) );
+
+       if( eReturned != eCantSendPacket )
+       {
+               if( eReturned == eARPCacheHit )
+               {
+                       #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
+                               uint8_t ucSocketOptions;
+                       #endif
+                       iptraceSENDING_UDP_PACKET( pxNetworkBuffer->ulIPAddress );
+
+                       /* Create short cuts to the data within the packet. */
+                       pxIPHeader = &( pxUDPPacket->xIPHeader );
+
+               #if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
+                       /* Is it possible that the packet is not actually a UDP packet
+                       after all, but an ICMP packet. */
+                       if( pxNetworkBuffer->usPort != ipPACKET_CONTAINS_ICMP_DATA )
+               #endif /* ipconfigSUPPORT_OUTGOING_PINGS */
+                       {
+                       UDPHeader_t *pxUDPHeader;
+
+                               pxUDPHeader = &( pxUDPPacket->xUDPHeader );
+
+                               pxUDPHeader->usDestinationPort = pxNetworkBuffer->usPort;
+                               pxUDPHeader->usSourcePort = pxNetworkBuffer->usBoundPort;
+                               pxUDPHeader->usLength = ( uint16_t ) ( uxPayloadSize + sizeof( UDPHeader_t ) );
+                               pxUDPHeader->usLength = FreeRTOS_htons( pxUDPHeader->usLength );
+                               pxUDPHeader->usChecksum = 0u;
+                       }
+
+                       /* memcpy() the constant parts of the header information into
+                       the     correct location within the packet.  This fills in:
+                               xEthernetHeader.xSourceAddress
+                               xEthernetHeader.usFrameType
+                               xIPHeader.ucVersionHeaderLength
+                               xIPHeader.ucDifferentiatedServicesCode
+                               xIPHeader.usLength
+                               xIPHeader.usIdentification
+                               xIPHeader.usFragmentOffset
+                               xIPHeader.ucTimeToLive
+                               xIPHeader.ucProtocol
+                       and
+                               xIPHeader.usHeaderChecksum
+                       */
+                       /* Save options now, as they will be overwritten by memcpy */
+                       #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
+                               ucSocketOptions = pxNetworkBuffer->pucEthernetBuffer[ ipSOCKET_OPTIONS_OFFSET ];
+                       #endif
+                       /*
+                        * Offset the memcpy by the size of a MAC address to start at the packet's
+                        * Ethernet header 'source' MAC address; the preceding 'destination' should not be altered.
+                        */
+                       char *pxUdpSrcAddrOffset = ( char *) pxUDPPacket + sizeof( MACAddress_t );
+                       memcpy( pxUdpSrcAddrOffset, xDefaultPartUDPPacketHeader.ucBytes, sizeof( xDefaultPartUDPPacketHeader ) );
+
+               #if ipconfigSUPPORT_OUTGOING_PINGS == 1
+                       if( pxNetworkBuffer->usPort == ipPACKET_CONTAINS_ICMP_DATA )
+                       {
+                               pxIPHeader->ucProtocol = ipPROTOCOL_ICMP;
+                               pxIPHeader->usLength = ( uint16_t ) ( uxPayloadSize + sizeof( IPHeader_t ) + sizeof( ICMPHeader_t ) );
+                       }
+                       else
+               #endif /* ipconfigSUPPORT_OUTGOING_PINGS */
+                       {
+                               pxIPHeader->usLength = ( uint16_t ) ( uxPayloadSize + sizeof( IPHeader_t ) + sizeof( UDPHeader_t ) );
+                       }
+
+                       pxIPHeader->usLength = FreeRTOS_htons( pxIPHeader->usLength );
+                       /* HT:endian: changed back to network endian */
+                       pxIPHeader->ulDestinationIPAddress = pxNetworkBuffer->ulIPAddress;
+
+                       #if( ipconfigUSE_LLMNR == 1 )
+                       {
+                               /* LLMNR messages are typically used on a LAN and they're
+                                * not supposed to cross routers */
+                               if( pxNetworkBuffer->ulIPAddress == ipLLMNR_IP_ADDR )
+                               {
+                                       pxIPHeader->ucTimeToLive = 0x01;
+                               }
+                       }
+                       #endif
+
+                       #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
+                       {
+                               pxIPHeader->usHeaderChecksum = 0u;
+                               pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );
+                               pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );
+
+                               if( ( ucSocketOptions & ( uint8_t ) FREERTOS_SO_UDPCKSUM_OUT ) != 0u )
+                               {
+                                       usGenerateProtocolChecksum( (uint8_t*)pxUDPPacket, pxNetworkBuffer->xDataLength, pdTRUE );
+                               }
+                               else
+                               {
+                                       pxUDPPacket->xUDPHeader.usChecksum = 0u;
+                               }
+                       }
+                       #endif
+               }
+               else if( eReturned == eARPCacheMiss )
+               {
+                       /* Add an entry to the ARP table with a null hardware address.
+                       This allows the ARP timer to know that an ARP reply is
+                       outstanding, and perform retransmissions if necessary. */
+                       vARPRefreshCacheEntry( NULL, ulIPAddress );
+
+                       /* Generate an ARP for the required IP address. */
+                       iptracePACKET_DROPPED_TO_GENERATE_ARP( pxNetworkBuffer->ulIPAddress );
+                       pxNetworkBuffer->ulIPAddress = ulIPAddress;
+                       vARPGenerateRequestPacket( pxNetworkBuffer );
+               }
+               else
+               {
+                       /* The lookup indicated that an ARP request has already been
+                       sent out for the queried IP address. */
+                       eReturned = eCantSendPacket;
+               }
+       }
+
+       if( eReturned != eCantSendPacket )
+       {
+               /* The network driver is responsible for freeing the network buffer
+               after the packet has been sent. */
+
+               #if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES )
+               {
+                       if( pxNetworkBuffer->xDataLength < ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES )
+                       {
+                       BaseType_t xIndex;
+
+                               for( xIndex = ( BaseType_t ) pxNetworkBuffer->xDataLength; xIndex < ( BaseType_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES; xIndex++ )
+                               {
+                                       pxNetworkBuffer->pucEthernetBuffer[ xIndex ] = 0u;
+                               }
+                               pxNetworkBuffer->xDataLength = ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES;
+                       }
+               }
+               #endif
+
+               xNetworkInterfaceOutput( pxNetworkBuffer, pdTRUE );
+       }
+       else
+       {
+               /* The packet can't be sent (DHCP not completed?).  Just drop the
+               packet. */
+               vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
+       }
+}
+/*-----------------------------------------------------------*/
+
+BaseType_t xProcessReceivedUDPPacket( NetworkBufferDescriptor_t *pxNetworkBuffer, uint16_t usPort )
+{
+BaseType_t xReturn = pdPASS;
+FreeRTOS_Socket_t *pxSocket;
+configASSERT(pxNetworkBuffer);
+configASSERT(pxNetworkBuffer->pucEthernetBuffer);
+
+
+UDPPacket_t *pxUDPPacket = (UDPPacket_t *) pxNetworkBuffer->pucEthernetBuffer;
+
+       /* Caller must check for minimum packet size. */
+       pxSocket = pxUDPSocketLookup( usPort );
+
+       if( pxSocket )
+       {
+
+               /* When refreshing the ARP cache with received UDP packets we must be
+               careful;  hundreds of broadcast messages may pass and if we're not
+               handling them, no use to fill the ARP cache with those IP addresses. */
+               vARPRefreshCacheEntry( &( pxUDPPacket->xEthernetHeader.xSourceAddress ), pxUDPPacket->xIPHeader.ulSourceIPAddress );
+
+               #if( ipconfigUSE_CALLBACKS == 1 )
+               {
+                       /* Did the owner of this socket register a reception handler ? */
+                       if( ipconfigIS_VALID_PROG_ADDRESS( pxSocket->u.xUDP.pxHandleReceive ) )
+                       {
+                               struct freertos_sockaddr xSourceAddress, destinationAddress;
+                               void *pcData = ( void * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipUDP_PAYLOAD_OFFSET_IPv4 ] );
+                               FOnUDPReceive_t xHandler = ( FOnUDPReceive_t ) pxSocket->u.xUDP.pxHandleReceive;
+                               xSourceAddress.sin_port = pxNetworkBuffer->usPort;
+                               xSourceAddress.sin_addr = pxNetworkBuffer->ulIPAddress;
+                               destinationAddress.sin_port = usPort;
+                               destinationAddress.sin_addr = pxUDPPacket->xIPHeader.ulDestinationIPAddress;
+
+                               if( xHandler( ( Socket_t ) pxSocket, ( void* ) pcData, ( size_t ) pxNetworkBuffer->xDataLength,
+                                       &xSourceAddress, &destinationAddress ) )
+                               {
+                                       xReturn = pdFAIL; /* FAIL means that we did not consume or release the buffer */
+                               }
+                       }
+               }
+               #endif /* ipconfigUSE_CALLBACKS */
+
+               #if( ipconfigUDP_MAX_RX_PACKETS > 0 )
+               {
+                       if( xReturn == pdPASS )
+                       {
+                               if ( listCURRENT_LIST_LENGTH( &( pxSocket->u.xUDP.xWaitingPacketsList ) ) >= pxSocket->u.xUDP.uxMaxPackets )
+                               {
+                                       FreeRTOS_debug_printf( ( "xProcessReceivedUDPPacket: buffer full %ld >= %ld port %u\n",
+                                               listCURRENT_LIST_LENGTH( &( pxSocket->u.xUDP.xWaitingPacketsList ) ),
+                                               pxSocket->u.xUDP.uxMaxPackets, pxSocket->usLocalPort ) );
+                                       xReturn = pdFAIL; /* we did not consume or release the buffer */
+                               }
+                       }
+               }
+               #endif
+
+               if( xReturn == pdPASS )
+               {
+                       vTaskSuspendAll();
+                       {
+                               if( xReturn == pdPASS )
+                               {
+                                       taskENTER_CRITICAL();
+                                       {
+                                               /* Add the network packet to the list of packets to be
+                                               processed by the socket. */
+                                               vListInsertEnd( &( pxSocket->u.xUDP.xWaitingPacketsList ), &( pxNetworkBuffer->xBufferListItem ) );
+                                       }
+                                       taskEXIT_CRITICAL();
+                               }
+                       }
+                       xTaskResumeAll();
+
+                       /* Set the socket's receive event */
+                       if( pxSocket->xEventGroup != NULL )
+                       {
+                               xEventGroupSetBits( pxSocket->xEventGroup, eSOCKET_RECEIVE );
+                       }
+
+                       #if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
+                       {
+                               if( ( pxSocket->pxSocketSet != NULL ) && ( ( pxSocket->xSelectBits & eSELECT_READ ) != 0 ) )
+                               {
+                                       xEventGroupSetBits( pxSocket->pxSocketSet->xSelectGroup, eSELECT_READ );
+                               }
+                       }
+                       #endif
+
+                       #if( ipconfigSOCKET_HAS_USER_SEMAPHORE == 1 )
+                       {
+                               if( pxSocket->pxUserSemaphore != NULL )
+                               {
+                                       xSemaphoreGive( pxSocket->pxUserSemaphore );
+                               }
+                       }
+                       #endif
+
+                       #if( ipconfigUSE_DHCP == 1 )
+                       {
+                               if( xIsDHCPSocket( pxSocket ) )
+                               {
+                                       xSendEventToIPTask( eDHCPEvent );
+                               }
+                       }
+                       #endif
+               }
+       }
+       else
+       {
+               /* There is no socket listening to the target port, but still it might
+               be for this node. */
+
+               #if( ipconfigUSE_DNS == 1 ) && ( ipconfigDNS_USE_CALLBACKS == 1 )
+                       /* A DNS reply, check for the source port.  Although the DNS client
+                       does open a UDP socket to send a messages, this socket will be
+                       closed after a short timeout.  Messages that come late (after the
+                       socket is closed) will be treated here. */
+                       if( FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usSourcePort ) == ipDNS_PORT )
+                       {
+                               vARPRefreshCacheEntry( &( pxUDPPacket->xEthernetHeader.xSourceAddress ), pxUDPPacket->xIPHeader.ulSourceIPAddress );
+                               xReturn = ( BaseType_t )ulDNSHandlePacket( pxNetworkBuffer );
+                       }
+                       else
+               #endif
+
+               #if( ipconfigUSE_LLMNR == 1 )
+                       /* a LLMNR request, check for the destination port. */
+                       if( ( usPort == FreeRTOS_ntohs( ipLLMNR_PORT ) ) ||
+                               ( pxUDPPacket->xUDPHeader.usSourcePort == FreeRTOS_ntohs( ipLLMNR_PORT ) ) )
+                       {
+                               vARPRefreshCacheEntry( &( pxUDPPacket->xEthernetHeader.xSourceAddress ), pxUDPPacket->xIPHeader.ulSourceIPAddress );
+                               xReturn = ( BaseType_t )ulDNSHandlePacket( pxNetworkBuffer );
+                       }
+                       else
+               #endif /* ipconfigUSE_LLMNR */
+
+               #if( ipconfigUSE_NBNS == 1 )
+                       /* a NetBIOS request, check for the destination port */
+                       if( ( usPort == FreeRTOS_ntohs( ipNBNS_PORT ) ) ||
+                               ( pxUDPPacket->xUDPHeader.usSourcePort == FreeRTOS_ntohs( ipNBNS_PORT ) ) )
+                       {
+                               vARPRefreshCacheEntry( &( pxUDPPacket->xEthernetHeader.xSourceAddress ), pxUDPPacket->xIPHeader.ulSourceIPAddress );
+                               xReturn = ( BaseType_t )ulNBNSHandlePacket( pxNetworkBuffer );
+                       }
+                       else
+               #endif /* ipconfigUSE_NBNS */
+               {
+                       xReturn = pdFAIL;
+               }
+       }
+
+       return xReturn;
+}
+/*-----------------------------------------------------------*/