Moved out each demos' functionality library files (e.g. Ring Buffer library) to ...
[pub/lufa.git] / Demos / Device / RNDISEthernet / Lib / ARP.c
diff --git a/Demos/Device/RNDISEthernet/Lib/ARP.c b/Demos/Device/RNDISEthernet/Lib/ARP.c
new file mode 100644 (file)
index 0000000..0f108c2
--- /dev/null
@@ -0,0 +1,85 @@
+/*\r
+             LUFA Library\r
+     Copyright (C) Dean Camera, 2009.\r
+              \r
+  dean [at] fourwalledcubicle [dot] com\r
+      www.fourwalledcubicle.com\r
+*/\r
+\r
+/*\r
+  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, and distribute this software\r
+  and its documentation for any purpose and without fee is hereby\r
+  granted, provided that the above copyright notice appear in all\r
+  copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting\r
+  documentation, and that the name of the author not be used in\r
+  advertising or publicity pertaining to distribution of the\r
+  software without specific, written prior permission.\r
+\r
+  The author disclaim all warranties with regard to this\r
+  software, including all implied warranties of merchantability\r
+  and fitness.  In no event shall the author be liable for any\r
+  special, indirect or consequential damages or any damages\r
+  whatsoever resulting from loss of use, data or profits, whether\r
+  in an action of contract, negligence or other tortious action,\r
+  arising out of or in connection with the use or performance of\r
+  this software.\r
+*/\r
+\r
+/** \file\r
+ *\r
+ *  Address Resolution Protocol (ARP) packet handling routines. This protocol handles the\r
+ *  conversion of physical MAC addresses to protocol IP addresses between the host and the\r
+ *  device.\r
+ */\r
\r
+#include "ARP.h"\r
+\r
+/** Processes an ARP packet inside an Ethernet frame, and writes the appropriate response\r
+ *  to the output Ethernet frame if the host is requesting the IP or MAC address of the\r
+ *  virtual server device on the network.\r
+ *\r
+ *  \param InDataStart   Pointer to the start of the incoming packet's ARP header\r
+ *  \param OutDataStart  Pointer to the start of the outgoing packet's ARP header\r
+ *\r
+ *  \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE otherwise\r
+ */\r
+int16_t ARP_ProcessARPPacket(void* InDataStart, void* OutDataStart)\r
+{\r
+       DecodeARPHeader(InDataStart);\r
+\r
+       ARP_Header_t* ARPHeaderIN  = (ARP_Header_t*)InDataStart;\r
+       ARP_Header_t* ARPHeaderOUT = (ARP_Header_t*)OutDataStart;\r
+\r
+       /* Ensure that the ARP request is a IPv4 request packet */\r
+       if ((SwapEndian_16(ARPHeaderIN->ProtocolType) == ETHERTYPE_IPV4) &&\r
+           (SwapEndian_16(ARPHeaderIN->Operation) == ARP_OPERATION_REQUEST))\r
+       {\r
+               /* If the ARP packet is requesting the MAC or IP of the virtual webserver, return the response */\r
+               if (IP_COMPARE(&ARPHeaderIN->TPA, &ServerIPAddress) || \r
+                   MAC_COMPARE(&ARPHeaderIN->THA, &ServerMACAddress))\r
+               {\r
+                       /* Fill out the ARP response header */\r
+                       ARPHeaderOUT->HardwareType = ARPHeaderIN->HardwareType;\r
+                       ARPHeaderOUT->ProtocolType = ARPHeaderIN->ProtocolType;\r
+                       ARPHeaderOUT->HLEN         = ARPHeaderIN->HLEN;\r
+                       ARPHeaderOUT->PLEN         = ARPHeaderIN->PLEN;\r
+                       ARPHeaderOUT->Operation    = SwapEndian_16(ARP_OPERATION_REPLY);\r
+\r
+                       /* Copy over the sender MAC/IP to the target fields for the response */\r
+                       ARPHeaderOUT->THA = ARPHeaderIN->SHA;\r
+                       ARPHeaderOUT->TPA = ARPHeaderIN->SPA;\r
+\r
+                       /* Copy over the new sender MAC/IP - MAC and IP addresses of the virtual webserver */\r
+                       ARPHeaderOUT->SHA = ServerMACAddress;\r
+                       ARPHeaderOUT->SPA = ServerIPAddress;\r
+\r
+                       /* Return the size of the response so far */\r
+                       return sizeof(ARP_Header_t);\r
+               }\r
+       }\r
+       \r
+       return NO_RESPONSE;\r
+}\r