Moved out each demos' functionality library files (e.g. Ring Buffer library) to ...
[pub/lufa.git] / Demos / Device / RNDISEthernet / Lib / ICMP.c
diff --git a/Demos/Device/RNDISEthernet/Lib/ICMP.c b/Demos/Device/RNDISEthernet/Lib/ICMP.c
new file mode 100644 (file)
index 0000000..da4ffcf
--- /dev/null
@@ -0,0 +1,79 @@
+/*\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
+ *  Internet Control Message Protocol (ICMP) packet handling routines. This protocol handles\r
+ *  Echo requests from the host, to indicate a successful network connection between the host\r
+ *  and the virtual server.\r
+ */\r
\r
+#include "ICMP.h"\r
+\r
+/** Processes an ICMP packet inside an Ethernet frame, and writes the appropriate response\r
+ *  to the output Ethernet frame if the host is issuing a ICMP ECHO request.\r
+ *\r
+ *  \param InDataStart   Pointer to the start of the incoming packet's ICMP header\r
+ *  \param OutDataStart  Pointer to the start of the outgoing packet's ICMP header\r
+ *\r
+ *  \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE otherwise\r
+ */\r
+int16_t ICMP_ProcessICMPPacket(void* InDataStart, void* OutDataStart)\r
+{\r
+       ICMP_Header_t* ICMPHeaderIN  = (ICMP_Header_t*)InDataStart;\r
+       ICMP_Header_t* ICMPHeaderOUT = (ICMP_Header_t*)OutDataStart;\r
+\r
+       DecodeICMPHeader(InDataStart);\r
+\r
+       /* Determine if the ICMP packet is an echo request (ping) */\r
+       if (ICMPHeaderIN->Type == ICMP_TYPE_ECHOREQUEST)\r
+       {\r
+               /* Fill out the ICMP response packet */\r
+               ICMPHeaderOUT->Type     = ICMP_TYPE_ECHOREPLY;\r
+               ICMPHeaderOUT->Code     = 0;\r
+               ICMPHeaderOUT->Checksum = 0;\r
+               ICMPHeaderOUT->Id       = ICMPHeaderIN->Id;\r
+               ICMPHeaderOUT->Sequence = ICMPHeaderIN->Sequence;\r
+               \r
+               uint16_t DataSize = FrameIN.FrameLength - ((((uint16_t)InDataStart + sizeof(ICMP_Header_t)) - (uint16_t)FrameIN.FrameData));\r
+               \r
+               /* Copy the remaining payload to the response - echo requests should echo back any sent data */\r
+               memcpy(&((uint8_t*)OutDataStart)[sizeof(ICMP_Header_t)],\r
+                      &((uint8_t*)InDataStart)[sizeof(ICMP_Header_t)],\r
+                          DataSize);\r
+\r
+               ICMPHeaderOUT->Checksum = Ethernet_Checksum16(ICMPHeaderOUT, (DataSize + sizeof(ICMP_Header_t)));\r
+\r
+               /* Return the size of the response so far */\r
+               return (DataSize + sizeof(ICMP_Header_t));\r
+       }\r
+       \r
+       return NO_RESPONSE;\r
+}\r