/* Wait until there is something to do. If the following call exits\r
* due to a time out rather than a message being received, set a\r
* 'NoEvent' value. */\r
- if ( xQueueReceive( xNetworkEventQueue, ( void * ) &xReceivedEvent, xNextIPSleep ) == pdFALSE ) {\r
+ if ( xQueueReceive( xNetworkEventQueue, ( void * ) &xReceivedEvent, xNextIPSleep ) == pdFALSE ) \r
+ {\r
xReceivedEvent.eEventType = eNoEvent;\r
}\r
\r
\r
if( pxNetworkBuffer != NULL )\r
{\r
- /* Set the actual packet size in case a bigger buffer was returned. */\r
- pxNetworkBuffer->xDataLength = \r
- sizeof( UDPPacket_t ) + xRequestedSizeBytes;\r
+ /* Set the actual packet size in case a bigger buffer was returned. */\r
+ pxNetworkBuffer->xDataLength = sizeof( UDPPacket_t ) + xRequestedSizeBytes;\r
\r
/* Leave space for the UPD header. */\r
pvReturn = ( void * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipUDP_PAYLOAD_OFFSET_IPv4 ] );\r
\r
if( pxNewBuffer != NULL )\r
{\r
- /* Set the actual packet size in case a bigger buffer than requested \r
- was returned. */\r
- pxNewBuffer->xDataLength = xNewLength;\r
+ /* Set the actual packet size in case a bigger buffer than requested\r
+ was returned. */\r
+ pxNewBuffer->xDataLength = xNewLength;\r
\r
- /* Copy the original packet information. */\r
+ /* Copy the original packet information. */\r
pxNewBuffer->ulIPAddress = pxNetworkBuffer->ulIPAddress;\r
pxNewBuffer->usPort = pxNetworkBuffer->usPort;\r
pxNewBuffer->usBoundPort = pxNetworkBuffer->usBoundPort;\r
memcpy( ( void * ) ipLOCAL_MAC_ADDRESS, ( void * ) ucMACAddress, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );\r
\r
/* Prepare the sockets interface. */\r
- xReturn = vNetworkSocketsInit();\r
- \r
- if( pdTRUE == xReturn )\r
- {\r
- /* Create the task that processes Ethernet and stack events. */\r
- xReturn = xTaskCreate( prvIPTask, "IP-task", ( uint16_t )ipconfigIP_TASK_STACK_SIZE_WORDS, NULL, ( UBaseType_t )ipconfigIP_TASK_PRIORITY, &xIPTaskHandle );\r
- }\r
+ xReturn = vNetworkSocketsInit();\r
+\r
+ if( pdTRUE == xReturn )\r
+ {\r
+ /* Create the task that processes Ethernet and stack events. */\r
+ xReturn = xTaskCreate( prvIPTask, "IP-task", ( uint16_t )ipconfigIP_TASK_STACK_SIZE_WORDS, NULL, ( UBaseType_t )ipconfigIP_TASK_PRIORITY, &xIPTaskHandle );\r
+ }\r
}\r
else\r
{\r
configASSERT( pxNetworkBuffer );\r
\r
/* Interpret the Ethernet frame. */\r
- if( pxNetworkBuffer->xDataLength >= sizeof( EthernetHeader_t ) )\r
- {\r
- eReturned = ipCONSIDER_FRAME_FOR_PROCESSING( pxNetworkBuffer->pucEthernetBuffer );\r
- pxEthernetHeader = ( EthernetHeader_t * )( pxNetworkBuffer->pucEthernetBuffer );\r
-\r
- if( eReturned == eProcessBuffer )\r
- {\r
- /* Interpret the received Ethernet packet. */\r
- switch( pxEthernetHeader->usFrameType )\r
- {\r
- case ipARP_FRAME_TYPE:\r
- /* The Ethernet frame contains an ARP packet. */\r
- if( pxNetworkBuffer->xDataLength >= sizeof( ARPPacket_t ) )\r
- {\r
- eReturned = eARPProcessPacket( ( ARPPacket_t * )pxNetworkBuffer->pucEthernetBuffer );\r
- }\r
- else\r
- {\r
- eReturned = eReleaseBuffer;\r
- }\r
- break;\r
-\r
- case ipIPv4_FRAME_TYPE:\r
- /* The Ethernet frame contains an IP packet. */\r
- if( pxNetworkBuffer->xDataLength >= sizeof( IPPacket_t ) )\r
- {\r
- eReturned = prvProcessIPPacket( ( IPPacket_t * )pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer );\r
- }\r
- else\r
- {\r
- eReturned = eReleaseBuffer;\r
- }\r
- break;\r
-\r
- default:\r
- /* No other packet types are handled. Nothing to do. */\r
- eReturned = eReleaseBuffer;\r
- break;\r
- }\r
- }\r
- }\r
+ if( pxNetworkBuffer->xDataLength >= sizeof( EthernetHeader_t ) )\r
+ {\r
+ eReturned = ipCONSIDER_FRAME_FOR_PROCESSING( pxNetworkBuffer->pucEthernetBuffer );\r
+ pxEthernetHeader = ( EthernetHeader_t * )( pxNetworkBuffer->pucEthernetBuffer );\r
+\r
+ if( eReturned == eProcessBuffer )\r
+ {\r
+ /* Interpret the received Ethernet packet. */\r
+ switch( pxEthernetHeader->usFrameType )\r
+ {\r
+ case ipARP_FRAME_TYPE:\r
+ /* The Ethernet frame contains an ARP packet. */\r
+ if( pxNetworkBuffer->xDataLength >= sizeof( ARPPacket_t ) )\r
+ {\r
+ eReturned = eARPProcessPacket( ( ARPPacket_t * )pxNetworkBuffer->pucEthernetBuffer );\r
+ }\r
+ else\r
+ {\r
+ eReturned = eReleaseBuffer;\r
+ }\r
+ break;\r
+\r
+ case ipIPv4_FRAME_TYPE:\r
+ /* The Ethernet frame contains an IP packet. */\r
+ if( pxNetworkBuffer->xDataLength >= sizeof( IPPacket_t ) )\r
+ {\r
+ eReturned = prvProcessIPPacket( ( IPPacket_t * )pxNetworkBuffer->pucEthernetBuffer, pxNetworkBuffer );\r
+ }\r
+ else\r
+ {\r
+ eReturned = eReleaseBuffer;\r
+ }\r
+ break;\r
+\r
+ default:\r
+ /* No other packet types are handled. Nothing to do. */\r
+ eReturned = eReleaseBuffer;\r
+ break;\r
+ }\r
+ }\r
+ }\r
\r
/* Perform any actions that resulted from processing the Ethernet frame. */\r
switch( eReturned )\r
UBaseType_t uxHeaderLength = ( UBaseType_t ) ( ( pxIPHeader->ucVersionHeaderLength & 0x0Fu ) << 2 );\r
uint8_t ucProtocol;\r
\r
- /* Bound the calculated header length: take away the Ethernet header size,\r
- then check if the IP header is claiming to be longer than the remaining\r
- total packet size. Also check for minimal header field length. */\r
- if( uxHeaderLength > pxNetworkBuffer->xDataLength - ipSIZE_OF_ETH_HEADER ||\r
- uxHeaderLength < ipSIZE_OF_IPv4_HEADER )\r
- {\r
- return eReleaseBuffer;\r
- }\r
+ /* Bound the calculated header length: take away the Ethernet header size,\r
+ then check if the IP header is claiming to be longer than the remaining\r
+ total packet size. Also check for minimal header field length. */\r
+ if( ( uxHeaderLength > ( pxNetworkBuffer->xDataLength - ipSIZE_OF_ETH_HEADER ) ) ||\r
+ ( uxHeaderLength < ipSIZE_OF_IPv4_HEADER ) )\r
+ {\r
+ return eReleaseBuffer;\r
+ }\r
\r
ucProtocol = pxIPPacket->xIPHeader.ucProtocol;\r
/* Check if the IP headers are acceptable and if it has our destination. */\r
memmove( pucTarget, pucSource, xMoveLen );\r
pxNetworkBuffer->xDataLength -= optlen;\r
\r
- /* Fix-up new version/header length field in IP packet. */\r
- pxIPHeader->ucVersionHeaderLength =\r
- ( pxIPHeader->ucVersionHeaderLength & 0xF0 ) | /* High nibble is the version. */\r
- ( ( ipSIZE_OF_IPv4_HEADER >> 2 ) & 0x0F ); /* Low nibble is the header size, in bytes, divided by four. */\r
+ /* Fix-up new version/header length field in IP packet. */\r
+ pxIPHeader->ucVersionHeaderLength = ( pxIPHeader->ucVersionHeaderLength & 0xF0 ) | /* High nibble is the version. */\r
+ ( ( ipSIZE_OF_IPv4_HEADER >> 2 ) & 0x0F ); /* Low nibble is the header size, in bytes, divided by four. */\r
}\r
\r
/* Add the IP and MAC addresses to the ARP table if they are not\r
be able to validate what it receives. */\r
#if ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )\r
{\r
- if( pxNetworkBuffer->xDataLength >= sizeof( ICMPPacket_t ) )\r
- {\r
- ICMPPacket_t *pxICMPPacket = ( ICMPPacket_t * )( pxNetworkBuffer->pucEthernetBuffer );\r
- if( pxIPHeader->ulDestinationIPAddress == *ipLOCAL_IP_ADDRESS_POINTER )\r
- {\r
- eReturn = prvProcessICMPPacket( pxICMPPacket );\r
- }\r
- }\r
- else\r
- {\r
- eReturn = eReleaseBuffer;\r
- }\r
+ if( pxNetworkBuffer->xDataLength >= sizeof( ICMPPacket_t ) )\r
+ {\r
+ ICMPPacket_t *pxICMPPacket = ( ICMPPacket_t * )( pxNetworkBuffer->pucEthernetBuffer );\r
+ if( pxIPHeader->ulDestinationIPAddress == *ipLOCAL_IP_ADDRESS_POINTER )\r
+ {\r
+ eReturn = prvProcessICMPPacket( pxICMPPacket );\r
+ }\r
+ }\r
+ else\r
+ {\r
+ eReturn = eReleaseBuffer;\r
+ }\r
}\r
#endif /* ( ipconfigREPLY_TO_INCOMING_PINGS == 1 ) || ( ipconfigSUPPORT_OUTGOING_PINGS == 1 ) */\r
break;\r
/* The IP packet contained a UDP frame. */\r
UDPPacket_t *pxUDPPacket = ( UDPPacket_t * ) ( pxNetworkBuffer->pucEthernetBuffer );\r
\r
- /* Only proceed if the payload length indicated in the header\r
- appears to be valid. */\r
- if ( pxNetworkBuffer->xDataLength >= sizeof( UDPPacket_t ) )\r
- {\r
- /* Ensure that downstream UDP packet handling has the lesser\r
- * of: the actual network buffer Ethernet frame length, or \r
- * the sender's UDP packet header payload length, minus the\r
- * size of the UDP header. \r
- *\r
- * The size of the UDP packet structure in this implementation \r
- * includes the size of the Ethernet header, the size of \r
- * the IP header, and the size of the UDP header.\r
- */\r
- \r
- pxNetworkBuffer->xDataLength -= sizeof( UDPPacket_t );\r
- if( ( FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - sizeof( UDPHeader_t ) ) <\r
- pxNetworkBuffer->xDataLength )\r
- {\r
- pxNetworkBuffer->xDataLength = FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - \r
- sizeof( UDPHeader_t );\r
- }\r
-\r
- /* Fields in pxNetworkBuffer (usPort, ulIPAddress) are network order. */\r
- pxNetworkBuffer->usPort = pxUDPPacket->xUDPHeader.usSourcePort;\r
- pxNetworkBuffer->ulIPAddress = pxUDPPacket->xIPHeader.ulSourceIPAddress;\r
-\r
- /* ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM:\r
- * In some cases, the upper-layer checksum has been calculated\r
- * by the NIC driver.\r
- * \r
- * Pass the packet payload to the UDP sockets implementation. */\r
- if( xProcessReceivedUDPPacket( pxNetworkBuffer, \r
- pxUDPPacket->xUDPHeader.usDestinationPort ) == pdPASS )\r
- {\r
- eReturn = eFrameConsumed;\r
- }\r
- }\r
- else\r
- {\r
- eReturn = eReleaseBuffer;\r
- }\r
+ /* Only proceed if the payload length indicated in the header\r
+ appears to be valid. */\r
+ if ( pxNetworkBuffer->xDataLength >= sizeof( UDPPacket_t ) )\r
+ {\r
+ /* Ensure that downstream UDP packet handling has the lesser\r
+ * of: the actual network buffer Ethernet frame length, or\r
+ * the sender's UDP packet header payload length, minus the\r
+ * size of the UDP header.\r
+ *\r
+ * The size of the UDP packet structure in this implementation\r
+ * includes the size of the Ethernet header, the size of\r
+ * the IP header, and the size of the UDP header.\r
+ */\r
+\r
+ pxNetworkBuffer->xDataLength -= sizeof( UDPPacket_t );\r
+ if( ( FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - sizeof( UDPHeader_t ) ) <\r
+ pxNetworkBuffer->xDataLength )\r
+ {\r
+ pxNetworkBuffer->xDataLength = FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usLength ) - sizeof( UDPHeader_t );\r
+ }\r
+\r
+ /* Fields in pxNetworkBuffer (usPort, ulIPAddress) are network order. */\r
+ pxNetworkBuffer->usPort = pxUDPPacket->xUDPHeader.usSourcePort;\r
+ pxNetworkBuffer->ulIPAddress = pxUDPPacket->xIPHeader.ulSourceIPAddress;\r
+\r
+ /* ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM:\r
+ * In some cases, the upper-layer checksum has been calculated\r
+ * by the NIC driver.\r
+ *\r
+ * Pass the packet payload to the UDP sockets implementation. */\r
+ if( xProcessReceivedUDPPacket( pxNetworkBuffer,\r
+ pxUDPPacket->xUDPHeader.usDestinationPort ) == pdPASS )\r
+ {\r
+ eReturn = eFrameConsumed;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ eReturn = eReleaseBuffer;\r
+ }\r
}\r
break;\r
\r
const char *pcType;\r
#endif\r
\r
- /* Check for minimum packet size. */\r
- if( uxBufferLength < sizeof( IPPacket_t ) )\r
- {\r
- return ipINVALID_LENGTH;\r
- }\r
+ /* Check for minimum packet size. */\r
+ if( uxBufferLength < sizeof( IPPacket_t ) )\r
+ {\r
+ return ipINVALID_LENGTH;\r
+ }\r
\r
- /* Parse the packet length. */\r
+ /* Parse the packet length. */\r
pxIPPacket = ( const IPPacket_t * ) pucEthernetBuffer;\r
- \r
- /* Per https://tools.ietf.org/html/rfc791, the four-bit Internet Header \r
- Length field contains the length of the internet header in 32-bit words. */\r
- uxIPHeaderLength = ( UBaseType_t ) \r
- ( sizeof( uint32_t ) * ( pxIPPacket->xIPHeader.ucVersionHeaderLength & 0x0Fu ) ); \r
-\r
- /* Check for minimum packet size. */\r
- if( uxBufferLength < sizeof( IPPacket_t ) + uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER )\r
- {\r
- return ipINVALID_LENGTH;\r
- }\r
- if( uxBufferLength < FreeRTOS_ntohs( pxIPPacket->xIPHeader.usLength ) )\r
- {\r
- return ipINVALID_LENGTH;\r
- }\r
-\r
- /* Identify the next protocol. */\r
- ucProtocol = pxIPPacket->xIPHeader.ucProtocol; \r
-\r
- /* N.B., if this IP packet header includes Options, then the following \r
- assignment results in a pointer into the protocol packet with the Ethernet \r
- and IP headers incorrectly aligned. However, either way, the "third"\r
- protocol (Layer 3 or 4) header will be aligned, which is the convenience \r
- of this calculation. */\r
- pxProtPack = ( ProtocolPacket_t * ) ( pucEthernetBuffer + ( uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER ) );\r
-\r
- /* Switch on the Layer 3/4 protocol. */\r
+\r
+ /* Per https://tools.ietf.org/html/rfc791, the four-bit Internet Header\r
+ Length field contains the length of the internet header in 32-bit words. */\r
+ uxIPHeaderLength = ( UBaseType_t ) ( sizeof( uint32_t ) * ( pxIPPacket->xIPHeader.ucVersionHeaderLength & 0x0Fu ) );\r
+\r
+ /* Check for minimum packet size. */\r
+ if( uxBufferLength < sizeof( IPPacket_t ) + uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER )\r
+ {\r
+ return ipINVALID_LENGTH;\r
+ }\r
+ if( uxBufferLength < FreeRTOS_ntohs( pxIPPacket->xIPHeader.usLength ) )\r
+ {\r
+ return ipINVALID_LENGTH;\r
+ }\r
+\r
+ /* Identify the next protocol. */\r
+ ucProtocol = pxIPPacket->xIPHeader.ucProtocol;\r
+\r
+ /* N.B., if this IP packet header includes Options, then the following\r
+ assignment results in a pointer into the protocol packet with the Ethernet\r
+ and IP headers incorrectly aligned. However, either way, the "third"\r
+ protocol (Layer 3 or 4) header will be aligned, which is the convenience\r
+ of this calculation. */\r
+ pxProtPack = ( ProtocolPacket_t * ) ( pucEthernetBuffer + ( uxIPHeaderLength - ipSIZE_OF_IPv4_HEADER ) );\r
+\r
+ /* Switch on the Layer 3/4 protocol. */\r
if( ucProtocol == ( uint8_t ) ipPROTOCOL_UDP )\r
{\r
- if( uxBufferLength < uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_UDP_HEADER )\r
- {\r
- return ipINVALID_LENGTH;\r
- }\r
+ if( uxBufferLength < ( uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_UDP_HEADER ) )\r
+ {\r
+ return ipINVALID_LENGTH;\r
+ }\r
\r
pusChecksum = ( uint16_t * ) ( &( pxProtPack->xUDPPacket.xUDPHeader.usChecksum ) );\r
#if( ipconfigHAS_DEBUG_PRINTF != 0 )\r
}\r
else if( ucProtocol == ( uint8_t ) ipPROTOCOL_TCP )\r
{\r
- if( uxBufferLength < uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_TCP_HEADER )\r
- {\r
- return ipINVALID_LENGTH;\r
- }\r
- \r
- pusChecksum = ( uint16_t * ) ( &( pxProtPack->xTCPPacket.xTCPHeader.usChecksum ) );\r
+ if( uxBufferLength < ( uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_TCP_HEADER ) )\r
+ {\r
+ return ipINVALID_LENGTH;\r
+ }\r
+\r
+ pusChecksum = ( uint16_t * ) ( &( pxProtPack->xTCPPacket.xTCPHeader.usChecksum ) );\r
#if( ipconfigHAS_DEBUG_PRINTF != 0 )\r
{\r
pcType = "TCP";\r
else if( ( ucProtocol == ( uint8_t ) ipPROTOCOL_ICMP ) ||\r
( ucProtocol == ( uint8_t ) ipPROTOCOL_IGMP ) )\r
{\r
- if( uxBufferLength < uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_ICMP_HEADER )\r
- {\r
- return ipINVALID_LENGTH;\r
- }\r
- \r
- pusChecksum = ( uint16_t * ) ( &( pxProtPack->xICMPPacket.xICMPHeader.usChecksum ) );\r
+ if( uxBufferLength < ( uxIPHeaderLength + ipSIZE_OF_ETH_HEADER + ipSIZE_OF_ICMP_HEADER ) )\r
+ {\r
+ return ipINVALID_LENGTH;\r
+ }\r
+\r
+ pusChecksum = ( uint16_t * ) ( &( pxProtPack->xICMPPacket.xICMPHeader.usChecksum ) );\r
#if( ipconfigHAS_DEBUG_PRINTF != 0 )\r
{\r
if( ucProtocol == ( uint8_t ) ipPROTOCOL_ICMP )\r
return ipUNHANDLED_PROTOCOL;\r
}\r
\r
- /* The protocol and checksum field have been identified. Check the direction\r
- of the packet. */\r
+ /* The protocol and checksum field have been identified. Check the direction\r
+ of the packet. */\r
if( xOutgoingPacket != pdFALSE )\r
{\r
/* This is an outgoing packet. Before calculating the checksum, set it\r
*\r
* Arguments:\r
* ulSum: This argument provides a value to initialize the progressive summation\r
- * of the header's values to. It is often 0, but protocols like TCP or UDP\r
- * can have pseudo-header fields which need to be included in the checksum.\r
+ * of the header's values to. It is often 0, but protocols like TCP or UDP\r
+ * can have pseudo-header fields which need to be included in the checksum.\r
* pucNextData: This argument contains the address of the first byte which this\r
- * method should process. The method's memory iterator is initialized to this value.\r
+ * method should process. The method's memory iterator is initialized to this value.\r
* uxDataLengthBytes: This argument contains the number of bytes that this method\r
- * should process.\r
+ * should process.\r
*/\r
uint16_t usGenerateChecksum( uint32_t ulSum, const uint8_t * pucNextData, size_t uxDataLengthBytes )\r
{\r
\r
void FreeRTOS_UpdateMACAddress( const uint8_t ucMACAddress[ipMAC_ADDRESS_LENGTH_BYTES] )\r
{\r
- /* Copy the MAC address at the start of the default packet header fragment. */\r
- memcpy( ( void * )ipLOCAL_MAC_ADDRESS, ( void * )ucMACAddress, ( size_t )ipMAC_ADDRESS_LENGTH_BYTES );\r
+ /* Copy the MAC address at the start of the default packet header fragment. */\r
+ memcpy( ( void * )ipLOCAL_MAC_ADDRESS, ( void * )ucMACAddress, ( size_t )ipMAC_ADDRESS_LENGTH_BYTES );\r
}\r
/*-----------------------------------------------------------*/\r
\r