3      Copyright (C) Dean Camera, 2012. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this 
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in 
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  33  *  Common DHCP routines to manage DHCP packet data. 
  36 #include "DHCPCommon.h" 
  38 #if defined(ENABLE_DHCP_CLIENT) || defined(ENABLE_DHCP_SERVER) || defined(__DOXYGEN__) 
  40 /** Sets the given DHCP option in the DHCP packet's option list. This automatically moves the 
  41  *  end of options terminator past the new option in the options list. 
  43  *  \param[in,out] DHCPOptionList  Pointer to the start of the DHCP packet's options list 
  44  *  \param[in]     Option          DHCP option to add to the list 
  45  *  \param[in]     DataLen         Size in bytes of the option data to add 
  46  *  \param[in]     OptionData      Buffer where the option's data is to be sourced from 
  48  *  \return Number of bytes added to the DHCP packet 
  50 uint8_t DHCPCommon_SetOption(uint8_t* DHCPOptionList
, 
  52                              const uint8_t DataLen
, 
  53                              void* const OptionData
) 
  55         /* Skip through the DHCP options list until the terminator option is found */ 
  56         while (*DHCPOptionList 
!= DHCP_OPTION_END
) 
  57           DHCPOptionList 
+= (DHCPOptionList
[1] + 2); 
  59         /* Overwrite the existing terminator with the new option, add a new terminator at the end of the list */ 
  60         DHCPOptionList
[0] = Option
; 
  61         DHCPOptionList
[1] = DataLen
; 
  62         memcpy(&DHCPOptionList
[2], OptionData
, DataLen
); 
  63         DHCPOptionList
[2 + DataLen
] = DHCP_OPTION_END
; 
  65         /* Calculate the total number of bytes added to the outgoing packet */ 
  69 /** Retrieves the given option's data (if present) from the DHCP packet's options list. 
  71  *  \param[in,out] DHCPOptionList  Pointer to the start of the DHCP packet's options list 
  72  *  \param[in]     Option          DHCP option to retrieve to the list 
  73  *  \param[out]    Destination     Buffer where the option's data is to be written to if found 
  75  *  \return Boolean true if the option was found in the DHCP packet's options list, false otherwise 
  77 bool DHCPCommon_GetOption(const uint8_t* DHCPOptionList
, 
  79                           void* const Destination
) 
  81         /* Look through the incoming DHCP packet's options list for the requested option */ 
  82         while (*DHCPOptionList 
!= DHCP_OPTION_END
) 
  84                 /* Check if the current DHCP option in the packet is the one requested */ 
  85                 if (DHCPOptionList
[0] == Option
) 
  87                         /* Copy request option's data to the destination buffer */ 
  88                         memcpy(Destination
, &DHCPOptionList
[2], DHCPOptionList
[1]); 
  90                         /* Indicate that the requested option data was successfully retrieved */ 
  94                 /* Skip to next DHCP option in the options list */ 
  95                 DHCPOptionList 
+= (DHCPOptionList
[1] + 2); 
  98         /* Requested option not found in the incoming packet's DHCP options list */