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