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