dhcp_client.h
Go to the documentation of this file.
1 /**
2  * @file dhcp_client.h
3  * @brief DHCP client (Dynamic Host Configuration Protocol)
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_CLIENT_H
32 #define _DHCP_CLIENT_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/socket.h"
37 #include "core/udp.h"
38 #include "dhcp/dhcp_common.h"
39 
40 //DHCP client support
41 #ifndef DHCP_CLIENT_SUPPORT
42  #define DHCP_CLIENT_SUPPORT ENABLED
43 #elif (DHCP_CLIENT_SUPPORT != ENABLED && DHCP_CLIENT_SUPPORT != DISABLED)
44  #error DHCP_CLIENT_SUPPORT parameter is not valid
45 #endif
46 
47 //DHCP client tick interval
48 #ifndef DHCP_CLIENT_TICK_INTERVAL
49  #define DHCP_CLIENT_TICK_INTERVAL 200
50 #elif (DHCP_CLIENT_TICK_INTERVAL < 10)
51  #error DHCP_CLIENT_TICK_INTERVAL parameter is not valid
52 #endif
53 
54 //Host name option support
55 #ifndef DHCP_CLIENT_HOSTNAME_OPTION_SUPPORT
56  #define DHCP_CLIENT_HOSTNAME_OPTION_SUPPORT ENABLED
57 #elif (DHCP_CLIENT_HOSTNAME_OPTION_SUPPORT != ENABLED && DHCP_CLIENT_HOSTNAME_OPTION_SUPPORT != DISABLED)
58  #error DHCP_CLIENT_HOSTNAME_OPTION_SUPPORT parameter is not valid
59 #endif
60 
61 //Maximum length of host name
62 #ifndef DHCP_CLIENT_MAX_HOSTNAME_LEN
63  #define DHCP_CLIENT_MAX_HOSTNAME_LEN 24
64 #elif (DHCP_CLIENT_MAX_HOSTNAME_LEN < 1)
65  #error DHCP_CLIENT_MAX_HOSTNAME_LEN parameter is not valid
66 #endif
67 
68 //Client identifier option support
69 #ifndef DHCP_CLIENT_ID_OPTION_SUPPORT
70  #define DHCP_CLIENT_ID_OPTION_SUPPORT DISABLED
71 #elif (DHCP_CLIENT_ID_OPTION_SUPPORT != ENABLED && DHCP_CLIENT_ID_OPTION_SUPPORT != DISABLED)
72  #error DHCP_CLIENT_ID_OPTION_SUPPORT parameter is not valid
73 #endif
74 
75 //Maximum size of client identifier
76 #ifndef DHCP_CLIENT_MAX_ID_SIZE
77  #define DHCP_CLIENT_MAX_ID_SIZE 15
78 #elif (DHCP_CLIENT_MAX_ID_SIZE < 1)
79  #error DHCP_CLIENT_MAX_ID_SIZE parameter is not valid
80 #endif
81 
82 //Random delay before sending the first message
83 #ifndef DHCP_CLIENT_INIT_DELAY
84  #define DHCP_CLIENT_INIT_DELAY 2000
85 #elif (DHCP_CLIENT_INIT_DELAY < 0)
86  #error DHCP_CLIENT_INIT_DELAY parameter is not valid
87 #endif
88 
89 //Initial retransmission timeout (DHCPDISCOVER)
90 #ifndef DHCP_CLIENT_DISCOVER_INIT_RT
91  #define DHCP_CLIENT_DISCOVER_INIT_RT 4000
92 #elif (DHCP_CLIENT_DISCOVER_INIT_RT < 1000)
93  #error DHCP_CLIENT_DISCOVER_INIT_RT parameter is not valid
94 #endif
95 
96 //Maximum retransmission timeout (DHCPDISCOVER)
97 #ifndef DHCP_CLIENT_DISCOVER_MAX_RT
98  #define DHCP_CLIENT_DISCOVER_MAX_RT 16000
99 #elif (DHCP_CLIENT_DISCOVER_MAX_RT < 1000)
100  #error DHCP_CLIENT_DISCOVER_MAX_RT parameter is not valid
101 #endif
102 
103 //Maximum retransmission count (DHCPREQUEST)
104 #ifndef DHCP_CLIENT_REQUEST_MAX_RC
105  #define DHCP_CLIENT_REQUEST_MAX_RC 4
106 #elif (DHCP_CLIENT_REQUEST_MAX_RC < 1)
107  #error DHCP_CLIENT_REQUEST_MAX_RC parameter is not valid
108 #endif
109 
110 //Initial retransmission timeout (DHCPREQUEST)
111 #ifndef DHCP_CLIENT_REQUEST_INIT_RT
112  #define DHCP_CLIENT_REQUEST_INIT_RT 4000
113 #elif (DHCP_CLIENT_REQUEST_INIT_RT < 1000)
114  #error DHCP_CLIENT_REQUEST_INIT_RT parameter is not valid
115 #endif
116 
117 //Maximum retransmission timeout (DHCPREQUEST)
118 #ifndef DHCP_CLIENT_REQUEST_MAX_RT
119  #define DHCP_CLIENT_REQUEST_MAX_RT 64000
120 #elif (DHCP_CLIENT_REQUEST_MAX_RT < 1000)
121  #error DHCP_CLIENT_REQUEST_MAX_RT parameter is not valid
122 #endif
123 
124 //Minimum delay between DHCPREQUEST messages in RENEWING and REBINDING states
125 #ifndef DHCP_CLIENT_REQUEST_MIN_DELAY
126  #define DHCP_CLIENT_REQUEST_MIN_DELAY 60000
127 #elif (DHCP_CLIENT_REQUEST_MIN_DELAY < 1000)
128  #error DHCP_CLIENT_REQUEST_MIN_DELAY parameter is not valid
129 #endif
130 
131 //Number of probe packets
132 #ifndef DHCP_CLIENT_PROBE_NUM
133  #define DHCP_CLIENT_PROBE_NUM 1
134 #elif (DHCP_CLIENT_PROBE_NUM < 0)
135  #error DHCP_CLIENT_PROBE_NUM parameter is not valid
136 #endif
137 
138 //Delay until repeated probe
139 #ifndef DHCP_CLIENT_PROBE_DELAY
140  #define DHCP_CLIENT_PROBE_DELAY 1000
141 #elif (DHCP_CLIENT_PROBE_DELAY < 100)
142  #error DHCP_CLIENT_PROBE_DELAY parameter is not valid
143 #endif
144 
145 //Random factor used to determine the delay between retransmissions
146 #ifndef DHCP_CLIENT_RAND_FACTOR
147  #define DHCP_CLIENT_RAND_FACTOR 1000
148 #elif (DHCP_CLIENT_RAND_FACTOR < 100)
149  #error DHCP_CLIENT_RAND_FACTOR parameter is not valid
150 #endif
151 
152 //Forward declaration of DhcpClientContext structure
153 struct _DhcpClientContext;
154 #define DhcpClientContext struct _DhcpClientContext
155 
156 //C++ guard
157 #ifdef __cplusplus
158 extern "C" {
159 #endif
160 
161 
162 /**
163  * @brief DHCP FSM states
164  **/
165 
166 typedef enum
167 {
177 } DhcpState;
178 
179 
180 /**
181  * @brief DHCP configuration timeout callback
182  **/
183 
184 typedef void (*DhcpTimeoutCallback)(DhcpClientContext *context,
185  NetInterface *interface);
186 
187 
188 /**
189  * @brief Link state change callback
190  **/
191 
192 typedef void (*DhcpLinkChangeCallback)(DhcpClientContext *context,
193  NetInterface *interface, bool_t linkState);
194 
195 
196 /**
197  * @brief FSM state change callback
198  **/
199 
201  NetInterface *interface, DhcpState state);
202 
203 
204 /**
205  * @brief DHCP client settings
206  **/
207 
208 typedef struct
209 {
210  NetInterface *interface; ///<Network interface to configure
211  uint_t ipAddrIndex; ///<Index of the IP address to be configured
212 #if (DHCP_CLIENT_HOSTNAME_OPTION_SUPPORT == ENABLED)
214 #endif
215 #if (DHCP_CLIENT_ID_OPTION_SUPPORT == ENABLED)
216  uint8_t clientId[DHCP_CLIENT_MAX_ID_SIZE]; ///<Client identifier
217  size_t clientIdLength; ///<Length of the client identifier
218 #endif
219  bool_t rapidCommit; ///<Quick configuration using rapid commit
220  bool_t manualDnsConfig; ///<Force manual DNS configuration
221  systime_t timeout; ///<DHCP configuration timeout
222  DhcpTimeoutCallback timeoutEvent; ///<DHCP configuration timeout event
223  DhcpLinkChangeCallback linkChangeEvent; ///<Link state change event
224  DhcpStateChangeCallback stateChangeEvent; ///<FSM state change event
226 
227 
228 /**
229  * @brief DHCP client context
230  **/
231 
233 {
234  DhcpClientSettings settings; ///<DHCP client settings
235  bool_t running; ///<This flag tells whether the DHCP client is running or not
236  DhcpState state; ///<Current state of the FSM
237  bool_t timeoutEventDone; ///<Timeout callback function has been called
238  systime_t timestamp; ///<Timestamp to manage retransmissions
239  systime_t timeout; ///<Timeout value
240  systime_t retransmitTimeout; ///<Retransmission timeout
241  uint_t retransmitCount; ///<Retransmission counter
242  Ipv4Addr serverIpAddr; ///<DHCP server IPv4 address
243  Ipv4Addr requestedIpAddr; ///<Requested IPv4 address
244  uint32_t transactionId; ///<Value to match requests with replies
245  systime_t configStartTime; ///<Address acquisition or renewal process start time
246  systime_t leaseStartTime; ///<Lease start time
247  uint32_t leaseTime; ///<Lease time
248  uint32_t t1; ///<Time at which the client enters the RENEWING state
249  uint32_t t2; ///<Time at which the client enters the REBINDING state
250 };
251 
252 
253 //Tick counter to handle periodic operations
255 
256 //DHCP client related functions
262 
263 void dhcpClientTick(DhcpClientContext *context);
265 
275 
279 
280 void dhcpClientProcessMessage(NetInterface *interface,
281  const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader,
282  const NetBuffer *buffer, size_t offset, void *param);
283 
285  const DhcpMessage *message, size_t length);
286 
288  const DhcpMessage *message, size_t length);
289 
291  const DhcpMessage *message, size_t length);
292 
294 
296 
298  DhcpState newState, systime_t delay);
299 
301 
302 //C++ guard
303 #ifdef __cplusplus
304 }
305 #endif
306 
307 #endif
bool_t running
This flag tells whether the DHCP client is running or not.
Definition: dhcp_client.h:235
void dhcpClientParseAck(DhcpClientContext *context, const DhcpMessage *message, size_t length)
Parse DHCPACK message.
Definition: dhcp_client.c:1485
uint8_t length
Definition: dtls_misc.h:149
int bool_t
Definition: compiler_port.h:49
void dhcpClientChangeState(DhcpClientContext *context, DhcpState newState, systime_t delay)
Update DHCP FSM state.
Definition: dhcp_client.c:1859
void dhcpClientParseOffer(DhcpClientContext *context, const DhcpMessage *message, size_t length)
Parse DHCPOFFER message.
Definition: dhcp_client.c:1433
error_t dhcpClientSendDiscover(DhcpClientContext *context)
Send DHCPDISCOVER message.
Definition: dhcp_client.c:982
DhcpLinkChangeCallback linkChangeEvent
Link state change event.
Definition: dhcp_client.h:223
void dhcpClientStateSelecting(DhcpClientContext *context)
SELECTING state.
Definition: dhcp_client.c:466
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
uint_t retransmitCount
Retransmission counter.
Definition: dhcp_client.h:241
systime_t configStartTime
Address acquisition or renewal process start time.
Definition: dhcp_client.h:245
systime_t retransmitTimeout
Retransmission timeout.
Definition: dhcp_client.h:240
void dhcpClientParseNak(DhcpClientContext *context, const DhcpMessage *message, size_t length)
Parse DHCPNAK message.
Definition: dhcp_client.c:1720
bool_t rapidCommit
Quick configuration using rapid commit.
Definition: dhcp_client.h:219
DhcpTimeoutCallback timeoutEvent
DHCP configuration timeout event.
Definition: dhcp_client.h:222
void dhcpClientStateRebinding(DhcpClientContext *context)
REBINDING state.
Definition: dhcp_client.c:902
systime_t dhcpClientTickCounter
Definition: dhcp_client.c:55
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:239
void dhcpClientStateBound(DhcpClientContext *context)
BOUND state.
Definition: dhcp_client.c:796
#define DhcpClientContext
Definition: dhcp_client.h:154
uint32_t t2
Time at which the client enters the REBINDING state.
Definition: dhcp_client.h:249
DHCP client context.
Definition: dhcp_client.h:232
__start_packed struct @123 DhcpMessage
DHCP message.
IP pseudo header.
Definition: ip.h:90
systime_t timeout
Timeout value.
Definition: dhcp_client.h:239
void dhcpClientTick(DhcpClientContext *context)
DHCP client timer handler.
Definition: dhcp_client.c:284
@ DHCP_STATE_INIT_REBOOT
Definition: dhcp_client.h:171
uint_t ipAddrIndex
Index of the IP address to be configured.
Definition: dhcp_client.h:211
error_t dhcpClientStop(DhcpClientContext *context)
Stop DHCP client.
Definition: dhcp_client.c:227
uint32_t transactionId
Value to match requests with replies.
Definition: dhcp_client.h:244
error_t
Error codes.
Definition: error.h:42
uint16_t dhcpClientComputeElapsedTime(DhcpClientContext *context)
Compute the appropriate secs field.
Definition: dhcp_client.c:1836
#define DHCP_CLIENT_MAX_HOSTNAME_LEN
Definition: dhcp_client.h:63
uint32_t leaseTime
Lease time.
Definition: dhcp_client.h:247
error_t dhcpClientStart(DhcpClientContext *context)
Start DHCP client.
Definition: dhcp_client.c:181
void(* DhcpLinkChangeCallback)(DhcpClientContext *context, NetInterface *interface, bool_t linkState)
Link state change callback.
Definition: dhcp_client.h:192
Ipv4Addr requestedIpAddr
Requested IPv4 address.
Definition: dhcp_client.h:243
#define NetInterface
Definition: net.h:36
void dhcpClientStateRenewing(DhcpClientContext *context)
RENEWING state.
Definition: dhcp_client.c:837
size_t clientIdLength
Length of the client identifier.
Definition: dhcp_client.h:217
error_t dhcpClientSendRequest(DhcpClientContext *context)
Send DHCPREQUEST message.
Definition: dhcp_client.c:1099
void(* DhcpStateChangeCallback)(DhcpClientContext *context, NetInterface *interface, DhcpState state)
FSM state change callback.
Definition: dhcp_client.h:200
DhcpStateChangeCallback stateChangeEvent
FSM state change event.
Definition: dhcp_client.h:224
bool_t timeoutEventDone
Timeout callback function has been called.
Definition: dhcp_client.h:237
void dhcpClientStateInit(DhcpClientContext *context)
INIT state.
Definition: dhcp_client.c:426
void dhcpClientLinkChangeEvent(DhcpClientContext *context)
Callback function for link change event.
Definition: dhcp_client.c:361
@ DHCP_STATE_REBOOTING
Definition: dhcp_client.h:172
__start_packed struct @122 UdpHeader
UDP header.
Definitions common to DHCP client and server.
DHCP client settings.
Definition: dhcp_client.h:208
@ DHCP_STATE_REBINDING
Definition: dhcp_client.h:176
#define DHCP_CLIENT_MAX_ID_SIZE
Definition: dhcp_client.h:77
void dhcpClientCheckTimeout(DhcpClientContext *context)
Manage DHCP configuration timeout.
Definition: dhcp_client.c:1791
systime_t leaseStartTime
Lease start time.
Definition: dhcp_client.h:246
DhcpState dhcpClientGetState(DhcpClientContext *context)
Retrieve current state.
Definition: dhcp_client.c:258
error_t dhcpClientSendDecline(DhcpClientContext *context)
Send DHCPDECLINE message.
Definition: dhcp_client.c:1259
Ipv4Addr serverIpAddr
DHCP server IPv4 address.
Definition: dhcp_client.h:242
systime_t timeout
DHCP configuration timeout.
Definition: dhcp_client.h:221
void dhcpClientStateRequesting(DhcpClientContext *context)
REQUESTING state.
Definition: dhcp_client.c:528
@ DHCP_STATE_REQUESTING
Definition: dhcp_client.h:170
char char_t
Definition: compiler_port.h:43
@ DHCP_STATE_BOUND
Definition: dhcp_client.h:174
void dhcpClientStateRebooting(DhcpClientContext *context)
REBOOTING state.
Definition: dhcp_client.c:647
void dhcpClientGetDefaultSettings(DhcpClientSettings *settings)
Initialize settings with default values.
Definition: dhcp_client.c:75
NetInterface * interface
Network interface to configure.
Definition: dhcp_client.h:210
@ DHCP_STATE_SELECTING
Definition: dhcp_client.h:169
void dhcpClientStateInitReboot(DhcpClientContext *context)
INIT-REBOOT state.
Definition: dhcp_client.c:607
DhcpState
DHCP FSM states.
Definition: dhcp_client.h:166
uint32_t t1
Time at which the client enters the RENEWING state.
Definition: dhcp_client.h:248
@ DHCP_STATE_PROBING
Definition: dhcp_client.h:173
systime_t timestamp
Timestamp to manage retransmissions.
Definition: dhcp_client.h:238
error_t dhcpClientInit(DhcpClientContext *context, const DhcpClientSettings *settings)
DHCP client initialization.
Definition: dhcp_client.c:114
UDP (User Datagram Protocol)
DhcpState state
Current state of the FSM.
Definition: dhcp_client.h:236
bool_t manualDnsConfig
Force manual DNS configuration.
Definition: dhcp_client.h:220
uint8_t message[]
Definition: chap.h:152
Socket API.
char_t hostname[]
Definition: tls.h:1449
void dhcpClientDumpConfig(DhcpClientContext *context)
Dump DHCP configuration for debugging purpose.
Definition: dhcp_client.c:1923
@ DHCP_STATE_INIT
Definition: dhcp_client.h:168
DhcpClientSettings settings
DHCP client settings.
Definition: dhcp_client.h:234
char_t clientId[]
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
void(* DhcpTimeoutCallback)(DhcpClientContext *context, NetInterface *interface)
DHCP configuration timeout callback.
Definition: dhcp_client.h:184
uint32_t systime_t
Definition: compiler_port.h:46
@ DHCP_STATE_RENEWING
Definition: dhcp_client.h:175
void dhcpClientProcessMessage(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader, const NetBuffer *buffer, size_t offset, void *param)
Process incoming DHCP message.
Definition: dhcp_client.c:1350
void dhcpClientStateProbing(DhcpClientContext *context)
PROBING state.
Definition: dhcp_client.c:725