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
@ DHCP_OPT_TCP_KEEPALIVE_GARBAGE
Definition: dhcp_common.h:145
@ DHCP_MESSAGE_TYPE_RELEASE
Definition: dhcp_common.h:95
Ipv4Addr siaddr
Definition: dhcp_common.h:208
@ DHCP_OPT_MOBILE_IP_HOME_AGENT
Definition: dhcp_common.h:172
@ DHCP_OPT_NETBIOS_NBNS_SERVER
Definition: dhcp_common.h:150
@ DHCP_OPT_BOOTFILE_NAME
Definition: dhcp_common.h:171
@ DHCP_OPT_IMPRESS_SERVER
Definition: dhcp_common.h:116
__start_packed struct @124 DhcpOption
DHCP option.
uint8_t hlen
Definition: dhcp_common.h:201
DhcpMessageType
DHCP message types.
Definition: dhcp_common.h:87
@ DHCP_OPT_BOOT_FILE_SIZE
Definition: dhcp_common.h:119
DhcpFlags
DHCP flags.
Definition: dhcp_common.h:76
@ DHCP_OPT_NNTP_SERVER
Definition: dhcp_common.h:175
@ DHCP_OPT_TCP_KEEPALIVE_INTERVAL
Definition: dhcp_common.h:144
@ DHCP_OPT_CLIENT_IDENTIFIER
Definition: dhcp_common.h:167
@ DHCP_OPT_TIME_OFFSET
Definition: dhcp_common.h:108
@ DHCP_OPT_DEFAULT_WWW_SERVER
Definition: dhcp_common.h:176
Ipv4Addr ciaddr
Definition: dhcp_common.h:206
@ DHCP_MESSAGE_TYPE_OFFER
Definition: dhcp_common.h:90
@ 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
@ DHCP_OPT_SERVER_IDENTIFIER
Definition: dhcp_common.h:160
@ DHCP_MESSAGE_TYPE_ACK
Definition: dhcp_common.h:93
uint8_t hops
Definition: dhcp_common.h:202
@ DHCP_OPT_TRAILER_ENCAPSULATION
Definition: dhcp_common.h:140
@ DHCP_OPT_NTP_SERVER
Definition: dhcp_common.h:148
@ DHCP_OPT_ROUTER_SOLICITATION_ADDRESS
Definition: dhcp_common.h:138
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
@ DHCP_OPT_ROUTER
Definition: dhcp_common.h:109
Ethernet.
DhcpOptionCode
DHCP option codes.
Definition: dhcp_common.h:104
@ DHCP_MESSAGE_TYPE_REQUEST
Definition: dhcp_common.h:91
__start_packed struct @123 DhcpMessage
DHCP message.
@ DHCP_OPT_MESSAGE
Definition: dhcp_common.h:162
@ DHCP_OPT_END
Definition: dhcp_common.h:183
@ DHCP_OPT_DEFAULT_IP_TTL
Definition: dhcp_common.h:129
@ DHCP_OPT_CAPTIVE_PORTAL
Definition: dhcp_common.h:182
@ 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
uint8_t value[]
Definition: dhcp_common.h:227
@ DHCP_OPT_OPTION_OVERLOAD
Definition: dhcp_common.h:158
@ DHCP_OPT_PAD
Definition: dhcp_common.h:106
@ DHCP_OPT_ARP_CACHE_TIMEOUT
Definition: dhcp_common.h:141
@ DHCP_MESSAGE_TYPE_DISCOVER
Definition: dhcp_common.h:89
@ DHCP_OPT_VENDOR_CLASS_IDENTIFIER
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:212
uint8_t options[]
Definition: dhcp_common.h:215
@ 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)
Find the specified option in a DHCP message.
Definition: dhcp_common.c:108
@ DHCP_OPT_X11_FONT_SERVER
Definition: dhcp_common.h:154
@ 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
__start_packed struct _Ipv4Header __end_packed
@ DHCP_OPT_NON_LOCAL_SOURCE_ROUTING
Definition: dhcp_common.h:126
@ 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_FLAG_BROADCAST
Definition: dhcp_common.h:79
@ DHCP_OPT_NETBIOS_SCOPE
Definition: dhcp_common.h:153
@ DHCP_OPT_STREETTALK_SERVER
Definition: dhcp_common.h:179
@ DHCP_OPT_MAX_DHCP_MESSAGE_SIZE
Definition: dhcp_common.h:163
@ DHCP_MESSAGE_TYPE_NAK
Definition: dhcp_common.h:94
@ DHCP_OPT_DEFAULT_IRC_SERVER
Definition: dhcp_common.h:178
uint8_t op
Definition: dhcp_common.h:199
Ipv4Addr yiaddr
Definition: dhcp_common.h:207
Ipv4Addr giaddr
Definition: dhcp_common.h:209
@ DHCP_OPCODE_BOOTREPLY
Definition: dhcp_common.h:68
uint32_t magicCookie
Definition: dhcp_common.h:214
uint32_t xid
Definition: dhcp_common.h:203
@ DHCP_OPT_PERFORM_MASK_DISCOVERY
Definition: dhcp_common.h:135
uint8_t code
Definition: dhcp_common.h:225
@ 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:65
@ DHCP_OPT_INTERFACE_MTU
Definition: dhcp_common.h:132
uint8_t length
Definition: dhcp_common.h:226
@ 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:210
@ DHCP_OPT_REBINDING_TIME_VALUE
Definition: dhcp_common.h:165
@ DHCP_OPT_POLICY_FILTER
Definition: dhcp_common.h:127
@ DHCP_OPT_COOKIE_SERVER
Definition: dhcp_common.h:114
@ DHCP_MESSAGE_TYPE_DECLINE
Definition: dhcp_common.h:92
@ 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:213
@ DHCP_OPT_PATH_MTU_PLATEAU_TABLE
Definition: dhcp_common.h:131
@ DHCP_OPT_NAME_SERVER
Definition: dhcp_common.h:111
uint8_t message[]
Definition: chap.h:152
@ DHCP_OPT_NIS_DOMAIN
Definition: dhcp_common.h:146
@ DHCP_OPT_PERFORM_ROUTER_DISCOVERY
Definition: dhcp_common.h:137
uint16_t flags
Definition: dhcp_common.h:205
@ 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
TCP/IP stack core.
@ DHCP_OPT_REQUESTED_IP_ADDRESS
Definition: dhcp_common.h:156
@ DHCP_OPT_RENEWAL_TIME_VALUE
Definition: dhcp_common.h:164
@ DHCP_OPT_DEFAULT_FINGER_SERVER
Definition: dhcp_common.h:177
uint8_t unused[10]
Definition: dhcp_common.h:211
@ DHCP_MESSAGE_TYPE_INFORM
Definition: dhcp_common.h:96
@ DHCP_OPT_NISP_SERVER
Definition: dhcp_common.h:169
@ DHCP_OPT_BROADCAST_ADDRESS
Definition: dhcp_common.h:134
@ DHCP_OPT_RAPID_COMMIT
Definition: dhcp_common.h:181
__start_packed struct @108 MacAddr
MAC address.