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  *  Address Resolution Protocol (ARP) packet handling routines. This protocol handles the 
  34  *  conversion of physical MAC addresses to protocol IP addresses between the host and the 
  40 /** Processes an ARP packet inside an Ethernet frame, and writes the appropriate response 
  41  *  to the output Ethernet frame if the host is requesting the IP or MAC address of the 
  42  *  virtual server device on the network. 
  44  *  \param[in] InDataStart    Pointer to the start of the incoming packet's ARP header 
  45  *  \param[out] OutDataStart  Pointer to the start of the outgoing packet's ARP header 
  47  *  \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE otherwise 
  49 int16_t ARP_ProcessARPPacket(void* InDataStart
, 
  52         DecodeARPHeader(InDataStart
); 
  54         ARP_Header_t
* ARPHeaderIN  
= (ARP_Header_t
*)InDataStart
; 
  55         ARP_Header_t
* ARPHeaderOUT 
= (ARP_Header_t
*)OutDataStart
; 
  57         /* Ensure that the ARP request is a IPv4 request packet */ 
  58         if ((SwapEndian_16(ARPHeaderIN
->ProtocolType
) == ETHERTYPE_IPV4
) && 
  59             (SwapEndian_16(ARPHeaderIN
->Operation
) == ARP_OPERATION_REQUEST
)) 
  61                 /* If the ARP packet is requesting the MAC or IP of the virtual webserver, return the response */ 
  62                 if (IP_COMPARE(&ARPHeaderIN
->TPA
, &ServerIPAddress
) || 
  63                     MAC_COMPARE(&ARPHeaderIN
->THA
, &ServerMACAddress
)) 
  65                         /* Fill out the ARP response header */ 
  66                         ARPHeaderOUT
->HardwareType 
= ARPHeaderIN
->HardwareType
; 
  67                         ARPHeaderOUT
->ProtocolType 
= ARPHeaderIN
->ProtocolType
; 
  68                         ARPHeaderOUT
->HLEN         
= ARPHeaderIN
->HLEN
; 
  69                         ARPHeaderOUT
->PLEN         
= ARPHeaderIN
->PLEN
; 
  70                         ARPHeaderOUT
->Operation    
= SwapEndian_16(ARP_OPERATION_REPLY
); 
  72                         /* Copy over the sender MAC/IP to the target fields for the response */ 
  73                         ARPHeaderOUT
->THA 
= ARPHeaderIN
->SHA
; 
  74                         ARPHeaderOUT
->TPA 
= ARPHeaderIN
->SPA
; 
  76                         /* Copy over the new sender MAC/IP - MAC and IP addresses of the virtual webserver */ 
  77                         ARPHeaderOUT
->SHA 
= ServerMACAddress
; 
  78                         ARPHeaderOUT
->SPA 
= ServerIPAddress
; 
  80                         /* Return the size of the response so far */ 
  81                         return sizeof(ARP_Header_t
);