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.

ARP.c 3.3KB

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