3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this 
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in 
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  33  *  Ethernet frame packet handling routines. This protocol handles the processing of raw Ethernet 
  34  *  frames sent and received, deferring the processing of sub-packet protocols to the appropriate 
  35  *  protocol handlers, such as DHCP or ARP. 
  40 /** Ethernet Frame buffer structure, to hold the incoming Ethernet frame from the host. */ 
  41 Ethernet_Frame_Info_t FrameIN
; 
  43 /** Ethernet Frame buffer structure, to hold the outgoing Ethernet frame to the host. */ 
  44 Ethernet_Frame_Info_t FrameOUT
; 
  46 /** Constant for convenience when checking against or setting a MAC address to the virtual server MAC address. */ 
  47 const MAC_Address_t ServerMACAddress    
= {SERVER_MAC_ADDRESS
}; 
  49 /** Constant for convenience when checking against or setting an IP address to the virtual server IP address. */ 
  50 const IP_Address_t  ServerIPAddress     
= {SERVER_IP_ADDRESS
}; 
  52 /** Constant for convenience when checking against or setting a MAC address to the broadcast MAC address. */ 
  53 const MAC_Address_t BroadcastMACAddress 
= {BROADCAST_MAC_ADDRESS
}; 
  55 /** Constant for convenience when checking against or setting a IP address to the broadcast IP address. */ 
  56 const IP_Address_t  BroadcastIPAddress  
= {BROADCAST_IP_ADDRESS
}; 
  58 /** Constant for convenience when checking against or setting an IP address to the client (host) IP address. */ 
  59 const IP_Address_t  ClientIPAddress     
= {CLIENT_IP_ADDRESS
}; 
  62 /** Processes an incoming Ethernet frame, and writes the appropriate response to the output Ethernet 
  63  *  frame buffer if the sub protocol handlers create a valid response. 
  65 void Ethernet_ProcessPacket(void) 
  67         DecodeEthernetFrameHeader(FrameIN
.FrameData
); 
  69         /* Cast the incoming Ethernet frame to the Ethernet header type */ 
  70         Ethernet_Frame_Header_t
* FrameINHeader  
= (Ethernet_Frame_Header_t
*)&FrameIN
.FrameData
; 
  71         Ethernet_Frame_Header_t
* FrameOUTHeader 
= (Ethernet_Frame_Header_t
*)&FrameOUT
.FrameData
; 
  73         int16_t                  RetSize        
= NO_RESPONSE
; 
  75         /* Ensure frame is addressed to either all (broadcast) or the virtual webserver, and is a type II frame */ 
  76         if ((MAC_COMPARE(&FrameINHeader
->Destination
, &ServerMACAddress
) || 
  77              MAC_COMPARE(&FrameINHeader
->Destination
, &BroadcastMACAddress
)) && 
  78                 (SwapEndian_16(FrameIN
.FrameLength
) > ETHERNET_VER2_MINSIZE
)) 
  80                 /* Process the packet depending on its protocol */ 
  81                 switch (SwapEndian_16(FrameINHeader
->EtherType
)) 
  84                                 RetSize 
= ARP_ProcessARPPacket(&FrameIN
.FrameData
[sizeof(Ethernet_Frame_Header_t
)], 
  85                                                                &FrameOUT
.FrameData
[sizeof(Ethernet_Frame_Header_t
)]); 
  88                                 RetSize 
= IP_ProcessIPPacket(&FrameIN
.FrameData
[sizeof(Ethernet_Frame_Header_t
)], 
  89                                                              &FrameOUT
.FrameData
[sizeof(Ethernet_Frame_Header_t
)]); 
  93                 /* Protocol processing routine has filled a response, complete the ethernet frame header */ 
  96                         /* Fill out the response Ethernet frame header */ 
  97                         FrameOUTHeader
->Source          
= ServerMACAddress
; 
  98                         FrameOUTHeader
->Destination     
= FrameINHeader
->Source
; 
  99                         FrameOUTHeader
->EtherType       
= FrameINHeader
->EtherType
; 
 101                         /* Set the response length in the buffer and indicate that a response is ready to be sent */ 
 102                         FrameOUT
.FrameLength            
= (sizeof(Ethernet_Frame_Header_t
) + RetSize
); 
 106         /* Check if the packet was processed */ 
 107         if (RetSize 
!= NO_PROCESS
) 
 109                 /* Clear the frame buffer */ 
 110                 FrameIN
.FrameLength 
= 0; 
 114 /** Calculates the appropriate ethernet checksum, consisting of the addition of the one's 
 115  *  compliment of each word, complimented. 
 117  *  \param[in] Data   Pointer to the packet buffer data whose checksum must be calculated 
 118  *  \param[in] Bytes  Number of bytes in the data buffer to process 
 120  *  \return A 16-bit Ethernet checksum value 
 122 uint16_t Ethernet_Checksum16(void* Data
, 
 125         uint16_t* Words    
= (uint16_t*)Data
; 
 126         uint32_t  Checksum 
= 0; 
 128         for (uint16_t CurrWord 
= 0; CurrWord 
< (Bytes 
>> 1); CurrWord
++) 
 129           Checksum 
+= Words
[CurrWord
]; 
 131         while (Checksum 
& 0xFFFF0000) 
 132           Checksum 
= ((Checksum 
& 0xFFFF) + (Checksum 
>> 16));