Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ICMP.c 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * Internet Control Message Protocol (ICMP) packet handling routines. This protocol handles
  29. * Echo requests from the host, to indicate a successful network connection between the host
  30. * and the virtual server.
  31. */
  32. #include "ICMP.h"
  33. /** Processes an ICMP packet inside an Ethernet frame, and writes the appropriate response
  34. * to the output Ethernet frame if the host is issuing a ICMP ECHO request.
  35. *
  36. * \param[in] FrameIN Pointer to the incoming Ethernet frame information structure
  37. * \param[in] InDataStart Pointer to the start of the incoming packet's ICMP header
  38. * \param[out] OutDataStart Pointer to the start of the outgoing packet's ICMP header
  39. *
  40. * \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE otherwise
  41. */
  42. int16_t ICMP_ProcessICMPPacket(Ethernet_Frame_Info_t* const FrameIN,
  43. void* InDataStart,
  44. void* OutDataStart)
  45. {
  46. ICMP_Header_t* ICMPHeaderIN = (ICMP_Header_t*)InDataStart;
  47. ICMP_Header_t* ICMPHeaderOUT = (ICMP_Header_t*)OutDataStart;
  48. DecodeICMPHeader(InDataStart);
  49. /* Determine if the ICMP packet is an echo request (ping) */
  50. if (ICMPHeaderIN->Type == ICMP_TYPE_ECHOREQUEST)
  51. {
  52. /* Fill out the ICMP response packet */
  53. ICMPHeaderOUT->Type = ICMP_TYPE_ECHOREPLY;
  54. ICMPHeaderOUT->Code = 0;
  55. ICMPHeaderOUT->Checksum = 0;
  56. ICMPHeaderOUT->Id = ICMPHeaderIN->Id;
  57. ICMPHeaderOUT->Sequence = ICMPHeaderIN->Sequence;
  58. intptr_t DataSize = FrameIN->FrameLength - ((((intptr_t)InDataStart + sizeof(ICMP_Header_t)) - (intptr_t)FrameIN->FrameData));
  59. /* Copy the remaining payload to the response - echo requests should echo back any sent data */
  60. memmove(&((uint8_t*)OutDataStart)[sizeof(ICMP_Header_t)],
  61. &((uint8_t*)InDataStart)[sizeof(ICMP_Header_t)],
  62. DataSize);
  63. ICMPHeaderOUT->Checksum = Ethernet_Checksum16(ICMPHeaderOUT, (DataSize + sizeof(ICMP_Header_t)));
  64. /* Return the size of the response so far */
  65. return (DataSize + sizeof(ICMP_Header_t));
  66. }
  67. return NO_RESPONSE;
  68. }