dhcp_common.h
Go to the documentation of this file.
1 /**
2  * @file dhcp_common.h
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 #ifndef _DHCP_COMMON_H
32 #define _DHCP_COMMON_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/ethernet.h"
37 #include "ipv4/ipv4.h"
38 
39 //UDP ports used by DHCP servers and clients
40 #define DHCP_SERVER_PORT 67
41 #define DHCP_CLIENT_PORT 68
42 
43 //Minimum size of DHCP messages
44 #define DHCP_MIN_MSG_SIZE 300
45 //Maximum size of DHCP messages
46 #define DHCP_MAX_MSG_SIZE 548
47 
48 //Hardware type
49 #define DHCP_HARDWARE_TYPE_ETH 1
50 //DHCP magic cookie
51 #define DHCP_MAGIC_COOKIE 0x63825363
52 //Infinite lifetime representation
53 #define DHCP_INFINITE_TIME 0xFFFFFFFF
54 
55 //C++ guard
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 
61 /**
62  * @brief DHCP opcodes
63  **/
64 
65 typedef enum
66 {
69 } DhcpOpcode;
70 
71 
72 /**
73  * @brief DHCP flags
74  **/
75 
76 typedef enum
77 {
80 } DhcpFlags;
81 
82 
83 /**
84  * @brief DHCP message types
85  **/
86 
87 typedef enum
88 {
98 
99 
100 /**
101  * @brief DHCP option codes
102  **/
103 
104 typedef enum
105 {
185 
186 
187 //CodeWarrior or Win32 compiler?
188 #if defined(__CWCC__) || defined(_WIN32)
189  #pragma pack(push, 1)
190 #endif
191 
192 
193 /**
194  * @brief DHCP message
195  **/
196 
197 typedef __start_packed struct
198 {
199  uint8_t op; //0
200  uint8_t htype; //1
201  uint8_t hlen; //2
202  uint8_t hops; //3
203  uint32_t xid; //4-7
204  uint16_t secs; //8-9
205  uint16_t flags; //10-11
206  Ipv4Addr ciaddr; //12-15
207  Ipv4Addr yiaddr; //16-19
208  Ipv4Addr siaddr; //20-23
209  Ipv4Addr giaddr; //24-27
210  MacAddr chaddr; //28-33
211  uint8_t unused[10]; //34-43
212  uint8_t sname[64]; //44-107
213  uint8_t file[128]; //108-235
214  uint32_t magicCookie; //236-239
215  uint8_t options[]; //240
217 
218 
219 /**
220  * @brief DHCP option
221  **/
222 
223 typedef __start_packed struct
224 {
225  uint8_t code; //0
226  uint8_t length; //1
227  uint8_t value[]; //2
229 
230 
231 //CodeWarrior or Win32 compiler?
232 #if defined(__CWCC__) || defined(_WIN32)
233  #pragma pack(pop)
234 #endif
235 
236 
237 //DHCP related functions
238 void dhcpAddOption(DhcpMessage *message, uint8_t optionCode,
239  const void *optionValue, size_t optionLen);
240 
242  size_t length, uint8_t optionCode);
243 
244 //C++ guard
245 #ifdef __cplusplus
246 }
247 #endif
248 
249 #endif
uint16_t secs
Definition: dhcp_common.h:204
Ipv4Addr siaddr
Definition: dhcp_common.h:208
__start_packed struct @124 DhcpOption
DHCP option.
uint8_t hlen
Definition: dhcp_common.h:201
DhcpMessageType
DHCP message types.
Definition: dhcp_common.h:87
DhcpFlags
DHCP flags.
Definition: dhcp_common.h:76
Ipv4Addr ciaddr
Definition: dhcp_common.h:206
uint8_t hops
Definition: dhcp_common.h:202
uint8_t htype
Definition: dhcp_common.h:200
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:239
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
Ethernet.
DhcpOptionCode
DHCP option codes.
Definition: dhcp_common.h:104
__start_packed struct @123 DhcpMessage
DHCP message.
uint8_t value[]
Definition: dhcp_common.h:227
uint8_t sname[64]
Definition: dhcp_common.h:212
uint8_t options[]
Definition: dhcp_common.h:215
DhcpOption * dhcpGetOption(const DhcpMessage *message, size_t length, uint8_t optionCode)
Find the specified option in a DHCP message.
Definition: dhcp_common.c:108
__start_packed struct _Ipv4Header __end_packed
uint8_t op
Definition: dhcp_common.h:199
Ipv4Addr yiaddr
Definition: dhcp_common.h:207
Ipv4Addr giaddr
Definition: dhcp_common.h:209
uint32_t magicCookie
Definition: dhcp_common.h:214
uint32_t xid
Definition: dhcp_common.h:203
uint8_t code
Definition: dhcp_common.h:225
DhcpOpcode
DHCP opcodes.
Definition: dhcp_common.h:65
uint8_t length
Definition: dhcp_common.h:226
MacAddr chaddr
Definition: dhcp_common.h:210
uint8_t file[128]
Definition: dhcp_common.h:213
uint8_t message[]
Definition: chap.h:152
uint16_t flags
Definition: dhcp_common.h:205
IPv4 (Internet Protocol Version 4)
TCP/IP stack core.
uint8_t unused[10]
Definition: dhcp_common.h:211
__start_packed struct @108 MacAddr
MAC address.