]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/Projects/Webserver/Lib/DHCPCommon.c
6d80f65caeffc4f6c9b8ce9fadd260bb09223898
[qmk_firmware.git] / lib / lufa / Projects / Webserver / Lib / DHCPCommon.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2017.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2017  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
21   The author disclaims 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
28   this software.
29 */
30
31 /** \file
32  *
33  *  Common DHCP routines to manage DHCP packet data.
34  */
35
36 #include "DHCPCommon.h"
37
38 #if defined(ENABLE_DHCP_CLIENT) || defined(ENABLE_DHCP_SERVER) || defined(__DOXYGEN__)
39
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.
42  *
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
47  *
48  *  \return Number of bytes added to the DHCP packet
49  */
50 uint8_t DHCPCommon_SetOption(uint8_t* DHCPOptionList,
51                              const uint8_t Option,
52                              const uint8_t DataLen,
53                              void* const OptionData)
54 {
55         /* Skip through the DHCP options list until the terminator option is found */
56         while (*DHCPOptionList != DHCP_OPTION_END)
57           DHCPOptionList += (DHCPOptionList[1] + 2);
58
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;
64
65         /* Calculate the total number of bytes added to the outgoing packet */
66         return (2 + DataLen);
67 }
68
69 /** Retrieves the given option's data (if present) from the DHCP packet's options list.
70  *
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
74  *
75  *  \return Boolean \c true if the option was found in the DHCP packet's options list, \c false otherwise
76  */
77 bool DHCPCommon_GetOption(const uint8_t* DHCPOptionList,
78                           const uint8_t Option,
79                           void* const Destination)
80 {
81         /* Look through the incoming DHCP packet's options list for the requested option */
82         while (*DHCPOptionList != DHCP_OPTION_END)
83         {
84                 /* Check if the current DHCP option in the packet is the one requested */
85                 if (DHCPOptionList[0] == Option)
86                 {
87                         /* Copy request option's data to the destination buffer */
88                         memcpy(Destination, &DHCPOptionList[2], DHCPOptionList[1]);
89
90                         /* Indicate that the requested option data was successfully retrieved */
91                         return true;
92                 }
93
94                 /* Skip to next DHCP option in the options list */
95                 DHCPOptionList += (DHCPOptionList[1] + 2);
96         }
97
98         /* Requested option not found in the incoming packet's DHCP options list */
99         return false;
100 }
101
102 #endif
103