dhcp_common.c
Go to the documentation of this file.
1 /**
2  * @file dhcp_common.c
3  * @brief Definitions common to DHCP client and server
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.6
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL DHCP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "dhcp/dhcp_common.h"
37 #include "debug.h"
38 
39 //Check TCP/IP stack configuration
40 #if (IPV4_SUPPORT == ENABLED)
41 
42 
43 /**
44  * @brief Append an option to a DHCP message
45  * @param[in] message Pointer to the DHCP message
46  * @param[in] optionCode Option code
47  * @param[in] optionValue Option value
48  * @param[in] optionLen Length of the option value
49  **/
50 
51 void dhcpAddOption(DhcpMessage *message, uint8_t optionCode,
52  const void *optionValue, size_t optionLen)
53 {
54  size_t n;
55  DhcpOption *option;
56 
57  //Point to the very first option
58  n = 0;
59 
60  //Parse DHCP options
61  while(1)
62  {
63  //Point to the current option
64  option = (DhcpOption *) (message->options + n);
65 
66  //End option detected?
67  if(option->code == DHCP_OPT_END)
68  break;
69 
70  //Jump to next the next option
71  n += sizeof(DhcpOption) + option->length;
72  }
73 
74  //Sanity check
75  if(optionLen <= UINT8_MAX)
76  {
77  //Point to the buffer where the option is to be written
78  option = (DhcpOption *) (message->options + n);
79 
80  //Option code
81  option->code = optionCode;
82  //Option length
83  option->length = (uint8_t) optionLen;
84  //Option value
85  memcpy(option->value, optionValue, optionLen);
86 
87  //Jump to next the next option
88  n += sizeof(DhcpOption) + option->length;
89 
90  //Point to the buffer where the option is to be written
91  option = (DhcpOption *) (message->options + n);
92 
93  //Always terminate the options field with 255
94  option->code = DHCP_OPT_END;
95  }
96 }
97 
98 
99 /**
100  * @brief Find the specified option in a DHCP message
101  * @param[in] message Pointer to the DHCP message
102  * @param[in] length Length of the message
103  * @param[in] optionCode Code of the option to find
104  * @return If the specified option is found, a pointer to the corresponding
105  * option is returned. Otherwise NULL pointer is returned
106  **/
107 
109  size_t length, uint8_t optionCode)
110 {
111  uint_t i;
112  DhcpOption *option;
113 
114  //Make sure the DHCP header is valid
115  if(length < sizeof(DhcpMessage))
116  return NULL;
117  //Get the length of the options field
118  length -= sizeof(DhcpMessage);
119 
120  //Parse DHCP options
121  for(i = 0; i < length; i++)
122  {
123  //Point to the current option
124  option = (DhcpOption *) (message->options + i);
125 
126  //Pad option detected?
127  if(option->code == DHCP_OPT_PAD)
128  continue;
129  //End option detected?
130  if(option->code == DHCP_OPT_END)
131  break;
132  //Check option length
133  if((i + 1) >= length || (i + 1 + option->length) >= length)
134  break;
135 
136  //Current option code matches the specified one?
137  if(option->code == optionCode)
138  return option;
139 
140  //Jump to the next option
141  i += option->length + 1;
142  }
143 
144  //Specified option code not found
145  return NULL;
146 }
147 
148 #endif
uint8_t length
Definition: dtls_misc.h:149
__start_packed struct @124 DhcpOption
DHCP option.
__start_packed struct @123 DhcpMessage
DHCP message.
@ DHCP_OPT_END
Definition: dhcp_common.h:183
@ DHCP_OPT_PAD
Definition: dhcp_common.h:106
Definitions common to DHCP client and server.
uint8_t n
uint8_t message[]
Definition: chap.h:152
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
void dhcpAddOption(DhcpMessage *message, uint8_t optionCode, const void *optionValue, size_t optionLen)
Append an option to a DHCP message.
Definition: dhcp_common.c:51
Debugging facilities.
DhcpOption * dhcpGetOption(const DhcpMessage *message, size_t length, uint8_t optionCode)
Find the specified option in a DHCP message.
Definition: dhcp_common.c:108