DHCPCommon.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 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. * Common DHCP routines to manage DHCP packet data.
  29. */
  30. #include "DHCPCommon.h"
  31. #if defined(ENABLE_DHCP_CLIENT) || defined(ENABLE_DHCP_SERVER) || defined(__DOXYGEN__)
  32. /** Sets the given DHCP option in the DHCP packet's option list. This automatically moves the
  33. * end of options terminator past the new option in the options list.
  34. *
  35. * \param[in,out] DHCPOptionList Pointer to the start of the DHCP packet's options list
  36. * \param[in] Option DHCP option to add to the list
  37. * \param[in] DataLen Size in bytes of the option data to add
  38. * \param[in] OptionData Buffer where the option's data is to be sourced from
  39. *
  40. * \return Number of bytes added to the DHCP packet
  41. */
  42. uint8_t DHCPCommon_SetOption(uint8_t* DHCPOptionList,
  43. const uint8_t Option,
  44. const uint8_t DataLen,
  45. void* const OptionData)
  46. {
  47. /* Skip through the DHCP options list until the terminator option is found */
  48. while (*DHCPOptionList != DHCP_OPTION_END)
  49. DHCPOptionList += (DHCPOptionList[1] + 2);
  50. /* Overwrite the existing terminator with the new option, add a new terminator at the end of the list */
  51. DHCPOptionList[0] = Option;
  52. DHCPOptionList[1] = DataLen;
  53. memcpy(&DHCPOptionList[2], OptionData, DataLen);
  54. DHCPOptionList[2 + DataLen] = DHCP_OPTION_END;
  55. /* Calculate the total number of bytes added to the outgoing packet */
  56. return (2 + DataLen);
  57. }
  58. /** Retrieves the given option's data (if present) from the DHCP packet's options list.
  59. *
  60. * \param[in,out] DHCPOptionList Pointer to the start of the DHCP packet's options list
  61. * \param[in] Option DHCP option to retrieve to the list
  62. * \param[out] Destination Buffer where the option's data is to be written to if found
  63. *
  64. * \return Boolean \c true if the option was found in the DHCP packet's options list, \c false otherwise
  65. */
  66. bool DHCPCommon_GetOption(const uint8_t* DHCPOptionList,
  67. const uint8_t Option,
  68. void* const Destination)
  69. {
  70. /* Look through the incoming DHCP packet's options list for the requested option */
  71. while (*DHCPOptionList != DHCP_OPTION_END)
  72. {
  73. /* Check if the current DHCP option in the packet is the one requested */
  74. if (DHCPOptionList[0] == Option)
  75. {
  76. /* Copy request option's data to the destination buffer */
  77. memcpy(Destination, &DHCPOptionList[2], DHCPOptionList[1]);
  78. /* Indicate that the requested option data was successfully retrieved */
  79. return true;
  80. }
  81. /* Skip to next DHCP option in the options list */
  82. DHCPOptionList += (DHCPOptionList[1] + 2);
  83. }
  84. /* Requested option not found in the incoming packet's DHCP options list */
  85. return false;
  86. }
  87. #endif