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-2024 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 2.4.4
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 {
70 
71 
72 /**
73  * @brief DHCP flags
74  **/
75 
76 typedef enum
77 {
79  DHCP_FLAG_BROADCAST = 0x8000
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 {
191  DHCP_OPT_END = 255
193 
194 
195 //CC-RX, CodeWarrior or Win32 compiler?
196 #if defined(__CCRX__)
197  #pragma pack
198 #elif defined(__CWCC__) || defined(_WIN32)
199  #pragma pack(push, 1)
200 #endif
201 
202 
203 /**
204  * @brief DHCP message
205  **/
206 
208 {
209  uint8_t op; //0
210  uint8_t htype; //1
211  uint8_t hlen; //2
212  uint8_t hops; //3
213  uint32_t xid; //4-7
214  uint16_t secs; //8-9
215  uint16_t flags; //10-11
216  Ipv4Addr ciaddr; //12-15
217  Ipv4Addr yiaddr; //16-19
218  Ipv4Addr siaddr; //20-23
219  Ipv4Addr giaddr; //24-27
220  MacAddr chaddr; //28-33
221  uint8_t unused[10]; //34-43
222  uint8_t sname[64]; //44-107
223  uint8_t file[128]; //108-235
224  uint32_t magicCookie; //236-239
225  uint8_t options[]; //240
227 
228 
229 /**
230  * @brief DHCP option
231  **/
232 
233 typedef __packed_struct
234 {
235  uint8_t code; //0
236  uint8_t length; //1
237  uint8_t value[]; //2
239 
240 
241 //CC-RX, CodeWarrior or Win32 compiler?
242 #if defined(__CCRX__)
243  #pragma unpack
244 #elif defined(__CWCC__) || defined(_WIN32)
245  #pragma pack(pop)
246 #endif
247 
248 //DHCP related functions
249 error_t dhcpAddOption(DhcpMessage *message, size_t *messageLen,
250  uint8_t optionCode, const void *optionValue, size_t optionLen);
251 
253  uint8_t optionCode);
254 
255 //C++ guard
256 #ifdef __cplusplus
257 }
258 #endif
259 
260 #endif
uint16_t secs
Definition: dhcp_common.h:214
@ DHCP_OPT_TCP_KEEPALIVE_GARBAGE
Definition: dhcp_common.h:145
error_t dhcpAddOption(DhcpMessage *message, size_t *messageLen, uint8_t optionCode, const void *optionValue, size_t optionLen)
Append an option to a DHCP message.
Definition: dhcp_common.c:56
@ DHCP_OPT_SERVER_ID
Definition: dhcp_common.h:160
Ipv4Addr siaddr
Definition: dhcp_common.h:218
@ DHCP_OPT_MOBILE_IP_HOME_AGENT
Definition: dhcp_common.h:172
@ DHCP_OPT_NETBIOS_NBNS_SERVER
Definition: dhcp_common.h:150
uint8_t code
Definition: coap_common.h:179
@ DHCP_OPT_BOOTFILE_NAME
Definition: dhcp_common.h:171
@ DHCP_OPT_IMPRESS_SERVER
Definition: dhcp_common.h:116
typedef __packed_struct
DHCP message.
Definition: dhcp_common.h:208
uint8_t hlen
Definition: dhcp_common.h:211
DhcpMessageType
DHCP message types.
Definition: dhcp_common.h:88
@ DHCP_OPT_BOOT_FILE_SIZE
Definition: dhcp_common.h:119
DhcpFlags
DHCP flags.
Definition: dhcp_common.h:77
@ DHCP_OPT_NNTP_SERVER
Definition: dhcp_common.h:175
@ DHCP_OPT_TCP_KEEPALIVE_INTERVAL
Definition: dhcp_common.h:144
uint8_t message[]
Definition: chap.h:154
@ DHCP_OPT_TIME_OFFSET
Definition: dhcp_common.h:108
@ DHCP_OPT_DEFAULT_WWW_SERVER
Definition: dhcp_common.h:176
@ DHCP_OPT_REQUESTED_IP_ADDR
Definition: dhcp_common.h:156
Ipv4Addr ciaddr
Definition: dhcp_common.h:216
@ DHCP_MSG_TYPE_ACK
Definition: dhcp_common.h:93
@ DHCP_OPT_DOMAIN_NAME
Definition: dhcp_common.h:121
@ DHCP_OPT_NETBIOS_NODE_TYPE
Definition: dhcp_common.h:152
@ DHCP_OPT_TFTP_SERVER_NAME
Definition: dhcp_common.h:170
@ DHCP_OPT_STATIC_ROUTE
Definition: dhcp_common.h:139
uint8_t hops
Definition: dhcp_common.h:212
@ DHCP_OPT_CLASSLESS_STATIC_ROUTE
Definition: dhcp_common.h:190
@ DHCP_OPT_TRAILER_ENCAPSULATION
Definition: dhcp_common.h:140
@ DHCP_OPT_NTP_SERVER
Definition: dhcp_common.h:148
uint8_t htype
Definition: dhcp_common.h:210
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:297
@ DHCP_OPT_ROUTER
Definition: dhcp_common.h:109
Ethernet.
DhcpOptionCode
DHCP option codes.
Definition: dhcp_common.h:105
@ DHCP_OPT_MESSAGE
Definition: dhcp_common.h:162
@ DHCP_OPT_END
Definition: dhcp_common.h:191
@ DHCP_OPT_DEFAULT_IP_TTL
Definition: dhcp_common.h:129
@ DHCP_OPT_CAPTIVE_PORTAL
Definition: dhcp_common.h:184
@ DHCP_OPT_MERIT_DUMP_FILE
Definition: dhcp_common.h:120
@ DHCP_OPT_TCP_DEFAULT_TTL
Definition: dhcp_common.h:143
@ DHCP_OPT_X11_DISPLAY_MANAGER
Definition: dhcp_common.h:155
@ DHCP_OPT_DOMAIN_SEARCH
Definition: dhcp_common.h:188
uint8_t value[]
Definition: dhcp_common.h:237
@ DHCP_OPT_OPTION_OVERLOAD
Definition: dhcp_common.h:158
@ DHCP_OPT_PAD
Definition: dhcp_common.h:106
error_t
Error codes.
Definition: error.h:43
@ DHCP_OPT_ARP_CACHE_TIMEOUT
Definition: dhcp_common.h:141
@ DHCP_OPT_VENDOR_CLASS_ID
Definition: dhcp_common.h:166
@ DHCP_OPT_LOG_SERVER
Definition: dhcp_common.h:113
@ DHCP_OPT_NIS_SERVER
Definition: dhcp_common.h:147
@ DHCP_OPT_ALL_SUBNETS_ARE_LOCAL
Definition: dhcp_common.h:133
uint8_t sname[64]
Definition: dhcp_common.h:222
@ DHCP_OPT_RELAY_AGENT_INFO
Definition: dhcp_common.h:183
uint8_t options[]
Definition: dhcp_common.h:225
@ DHCP_OPT_AUTO_CONFIG
Definition: dhcp_common.h:185
@ DHCP_OPT_CLIENT_FQDN
Definition: dhcp_common.h:182
@ DHCP_OPT_HOST_NAME
Definition: dhcp_common.h:118
@ DHCP_OPT_MAX_DATAGRAM_REASSEMBLY_SIZE
Definition: dhcp_common.h:128
@ DHCP_OPT_NETBIOS_NBDD_SERVER
Definition: dhcp_common.h:151
DhcpOption * dhcpGetOption(const DhcpMessage *message, size_t length, uint8_t optionCode)
Search a DHCP message for a given option.
Definition: dhcp_common.c:118
@ DHCP_OPT_X11_FONT_SERVER
Definition: dhcp_common.h:154
@ DHCP_OPT_STREET_TALK_SERVER
Definition: dhcp_common.h:179
@ DHCP_OPT_SWAP_SERVER
Definition: dhcp_common.h:122
@ DHCP_OPT_ROOT_PATH
Definition: dhcp_common.h:123
@ DHCP_OPT_IP_FORWARDING
Definition: dhcp_common.h:125
@ DHCP_OPT_RESOURCE_LOCATION_SERVER
Definition: dhcp_common.h:117
@ DHCP_OPT_IP_ADDRESS_LEASE_TIME
Definition: dhcp_common.h:157
@ DHCP_OPT_NON_LOCAL_SOURCE_ROUTING
Definition: dhcp_common.h:126
DhcpMessage
Definition: dhcp_common.h:226
@ DHCP_OPT_DHCP_MESSAGE_TYPE
Definition: dhcp_common.h:159
@ DHCP_OPT_POP3_SERVER
Definition: dhcp_common.h:174
@ DHCP_OPT_EXTENSIONS_PATH
Definition: dhcp_common.h:124
@ DHCP_MSG_TYPE_DECLINE
Definition: dhcp_common.h:92
@ DHCP_OPT_BROADCAST_ADDR
Definition: dhcp_common.h:134
@ DHCP_FLAG_BROADCAST
Definition: dhcp_common.h:79
@ DHCP_OPT_NETBIOS_SCOPE
Definition: dhcp_common.h:153
uint8_t unused[10]
Definition: dhcp_common.h:221
@ DHCP_OPT_MAX_DHCP_MESSAGE_SIZE
Definition: dhcp_common.h:163
@ DHCP_OPT_DEFAULT_IRC_SERVER
Definition: dhcp_common.h:178
MacAddr
Definition: ethernet.h:195
@ DHCP_MSG_TYPE_INFORM
Definition: dhcp_common.h:96
Ipv4Addr yiaddr
Definition: dhcp_common.h:217
Ipv4Addr giaddr
Definition: dhcp_common.h:219
@ DHCP_OPCODE_BOOTREPLY
Definition: dhcp_common.h:68
uint32_t magicCookie
Definition: dhcp_common.h:224
uint32_t xid
Definition: dhcp_common.h:213
@ DHCP_OPT_PERFORM_MASK_DISCOVERY
Definition: dhcp_common.h:135
@ DHCP_OPT_STDA_SERVER
Definition: dhcp_common.h:180
@ DHCP_OPT_LPR_SERVER
Definition: dhcp_common.h:115
@ DHCP_OPCODE_BOOTREQUEST
Definition: dhcp_common.h:67
DhcpOpcode
DHCP opcodes.
Definition: dhcp_common.h:66
@ DHCP_OPT_INTERFACE_MTU
Definition: dhcp_common.h:132
@ DHCP_OPT_NAME_SERVICE_SEARCH
Definition: dhcp_common.h:186
uint8_t length
Definition: dhcp_common.h:236
@ DHCP_OPT_SIP_SERVER
Definition: dhcp_common.h:189
@ DHCP_OPT_MASK_SUPPLIER
Definition: dhcp_common.h:136
@ DHCP_OPT_SMTP_SERVER
Definition: dhcp_common.h:173
@ DHCP_OPT_PATH_MTU_AGING_TIMEOUT
Definition: dhcp_common.h:130
MacAddr chaddr
Definition: dhcp_common.h:220
@ DHCP_OPT_REBINDING_TIME_VALUE
Definition: dhcp_common.h:165
@ DHCP_OPT_ROUTER_SOLICITATION_ADDR
Definition: dhcp_common.h:138
@ DHCP_OPT_POLICY_FILTER
Definition: dhcp_common.h:127
@ DHCP_OPT_COOKIE_SERVER
Definition: dhcp_common.h:114
@ DHCP_OPT_NISP_DOMAIN
Definition: dhcp_common.h:168
@ DHCP_OPT_SUBNET_MASK
Definition: dhcp_common.h:107
@ DHCP_OPT_DNS_SERVER
Definition: dhcp_common.h:112
uint8_t file[128]
Definition: dhcp_common.h:223
@ DHCP_OPT_PATH_MTU_PLATEAU_TABLE
Definition: dhcp_common.h:131
@ DHCP_OPT_NAME_SERVER
Definition: dhcp_common.h:111
@ DHCP_MSG_TYPE_OFFER
Definition: dhcp_common.h:90
@ DHCP_OPT_SUBNET_SELECTION
Definition: dhcp_common.h:187
DhcpOption
Definition: dhcp_common.h:238
@ DHCP_OPT_NIS_DOMAIN
Definition: dhcp_common.h:146
@ DHCP_MSG_TYPE_REQUEST
Definition: dhcp_common.h:91
@ DHCP_MSG_TYPE_NAK
Definition: dhcp_common.h:94
@ DHCP_OPT_PERFORM_ROUTER_DISCOVERY
Definition: dhcp_common.h:137
uint16_t flags
Definition: dhcp_common.h:215
@ DHCP_OPT_ETHERNET_ENCAPSULATION
Definition: dhcp_common.h:142
@ DHCP_OPT_VENDOR_SPECIFIC_INFO
Definition: dhcp_common.h:149
IPv4 (Internet Protocol Version 4)
@ DHCP_OPT_PARAM_REQUEST_LIST
Definition: dhcp_common.h:161
@ DHCP_FLAG_UNICAST
Definition: dhcp_common.h:78
@ DHCP_OPT_TIME_SERVER
Definition: dhcp_common.h:110
@ DHCP_MSG_TYPE_DISCOVER
Definition: dhcp_common.h:89
TCP/IP stack core.
@ DHCP_OPT_RENEWAL_TIME_VALUE
Definition: dhcp_common.h:164
@ DHCP_OPT_DEFAULT_FINGER_SERVER
Definition: dhcp_common.h:177
@ DHCP_OPT_CLIENT_ID
Definition: dhcp_common.h:167
@ DHCP_MSG_TYPE_RELEASE
Definition: dhcp_common.h:95
@ DHCP_OPT_NISP_SERVER
Definition: dhcp_common.h:169
@ DHCP_OPT_RAPID_COMMIT
Definition: dhcp_common.h:181
uint16_t op
Definition: arp.h:156