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-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_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 //Random delay before sending the first message
55 #ifndef DHCP_CLIENT_INIT_DELAY
56  #define DHCP_CLIENT_INIT_DELAY 2000
57 #elif (DHCP_CLIENT_INIT_DELAY < 0)
58  #error DHCP_CLIENT_INIT_DELAY parameter is not valid
59 #endif
60 
61 //Initial retransmission timeout (DHCPDISCOVER)
62 #ifndef DHCP_CLIENT_DISCOVER_INIT_RT
63  #define DHCP_CLIENT_DISCOVER_INIT_RT 4000
64 #elif (DHCP_CLIENT_DISCOVER_INIT_RT < 1000)
65  #error DHCP_CLIENT_DISCOVER_INIT_RT parameter is not valid
66 #endif
67 
68 //Maximum retransmission timeout (DHCPDISCOVER)
69 #ifndef DHCP_CLIENT_DISCOVER_MAX_RT
70  #define DHCP_CLIENT_DISCOVER_MAX_RT 16000
71 #elif (DHCP_CLIENT_DISCOVER_MAX_RT < 1000)
72  #error DHCP_CLIENT_DISCOVER_MAX_RT parameter is not valid
73 #endif
74 
75 //Maximum retransmission count (DHCPREQUEST)
76 #ifndef DHCP_CLIENT_REQUEST_MAX_RC
77  #define DHCP_CLIENT_REQUEST_MAX_RC 4
78 #elif (DHCP_CLIENT_REQUEST_MAX_RC < 1)
79  #error DHCP_CLIENT_REQUEST_MAX_RC parameter is not valid
80 #endif
81 
82 //Initial retransmission timeout (DHCPREQUEST)
83 #ifndef DHCP_CLIENT_REQUEST_INIT_RT
84  #define DHCP_CLIENT_REQUEST_INIT_RT 4000
85 #elif (DHCP_CLIENT_REQUEST_INIT_RT < 1000)
86  #error DHCP_CLIENT_REQUEST_INIT_RT parameter is not valid
87 #endif
88 
89 //Maximum retransmission timeout (DHCPREQUEST)
90 #ifndef DHCP_CLIENT_REQUEST_MAX_RT
91  #define DHCP_CLIENT_REQUEST_MAX_RT 64000
92 #elif (DHCP_CLIENT_REQUEST_MAX_RT < 1000)
93  #error DHCP_CLIENT_REQUEST_MAX_RT parameter is not valid
94 #endif
95 
96 //Minimum delay between DHCPREQUEST messages in RENEWING and REBINDING states
97 #ifndef DHCP_CLIENT_REQUEST_MIN_DELAY
98  #define DHCP_CLIENT_REQUEST_MIN_DELAY 60000
99 #elif (DHCP_CLIENT_REQUEST_MIN_DELAY < 1000)
100  #error DHCP_CLIENT_REQUEST_MIN_DELAY parameter is not valid
101 #endif
102 
103 //Number of probe packets
104 #ifndef DHCP_CLIENT_PROBE_NUM
105  #define DHCP_CLIENT_PROBE_NUM 1
106 #elif (DHCP_CLIENT_PROBE_NUM < 0)
107  #error DHCP_CLIENT_PROBE_NUM parameter is not valid
108 #endif
109 
110 //Delay until repeated probe
111 #ifndef DHCP_CLIENT_PROBE_DELAY
112  #define DHCP_CLIENT_PROBE_DELAY 1000
113 #elif (DHCP_CLIENT_PROBE_DELAY < 100)
114  #error DHCP_CLIENT_PROBE_DELAY parameter is not valid
115 #endif
116 
117 //Number of announcement packets
118 #ifndef DHCP_CLIENT_ANNOUNCE_NUM
119  #define DHCP_CLIENT_ANNOUNCE_NUM 1
120 #elif (DHCP_CLIENT_ANNOUNCE_NUM < 0)
121  #error DHCP_CLIENT_ANNOUNCE_NUM parameter is not valid
122 #endif
123 
124 //Time between announcement packets
125 #ifndef DHCP_CLIENT_ANNOUNCE_INTERVAL
126  #define DHCP_CLIENT_ANNOUNCE_INTERVAL 1000
127 #elif (DHCP_CLIENT_ANNOUNCE_INTERVAL < 100)
128  #error DHCP_CLIENT_ANNOUNCE_INTERVAL parameter is not valid
129 #endif
130 
131 //Random factor used to determine the delay between retransmissions
132 #ifndef DHCP_CLIENT_RAND_FACTOR
133  #define DHCP_CLIENT_RAND_FACTOR 1000
134 #elif (DHCP_CLIENT_RAND_FACTOR < 100)
135  #error DHCP_CLIENT_RAND_FACTOR parameter is not valid
136 #endif
137 
138 //Application specific context
139 #ifndef DHCP_CLIENT_PRIVATE_CONTEXT
140  #define DHCP_CLIENT_PRIVATE_CONTEXT
141 #endif
142 
143 //Forward declaration of DhcpClientContext structure
144 struct _DhcpClientContext;
145 #define DhcpClientContext struct _DhcpClientContext
146 
147 //C++ guard
148 #ifdef __cplusplus
149 extern "C" {
150 #endif
151 
152 
153 /**
154  * @brief DHCP FSM states
155  **/
156 
157 typedef enum
158 {
170 
171 
172 /**
173  * @brief DHCP configuration timeout callback
174  **/
175 
177  NetInterface *interface);
178 
179 
180 /**
181  * @brief Link state change callback
182  **/
183 
185  NetInterface *interface, bool_t linkState);
186 
187 
188 /**
189  * @brief FSM state change callback
190  **/
191 
193  NetInterface *interface, DhcpState state);
194 
195 
196 /**
197  * @brief Add DHCP options callback
198  **/
199 
202 
203 
204 /**
205  * @brief Parse DHCP options callback
206  **/
207 
209  const DhcpMessage *message, size_t length, DhcpMessageType type);
210 
211 
212 /**
213  * @brief DHCP client settings
214  **/
215 
216 typedef struct
217 {
218  NetInterface *interface; ///<Network interface to configure
219  uint_t ipAddrIndex; ///<Index of the IP address to be configured
220  bool_t rapidCommit; ///<Quick configuration using rapid commit
221  bool_t manualDnsConfig; ///<Force manual DNS configuration
222  systime_t timeout; ///<DHCP configuration timeout
223  DhcpClientTimeoutCallback timeoutEvent; ///<DHCP configuration timeout event
224  DhcpClientLinkChangeCallback linkChangeEvent; ///<Link state change event
226  DhcpClientAddOptionsCallback addOptionsCallback; ///<Add DHCP options callback
227  DhcpClientParseOptionsCallback parseOptionsCallback; ///<Parse DHCP options callback
229 
230 
231 /**
232  * @brief DHCP client context
233  **/
234 
236 {
237  DhcpClientSettings settings; ///<DHCP client settings
238  bool_t running; ///<This flag tells whether the DHCP client is running or not
239  DhcpState state; ///<Current state of the FSM
240  bool_t timeoutEventDone; ///<Timeout callback function has been called
241  systime_t timestamp; ///<Timestamp to manage retransmissions
242  systime_t timeout; ///<Timeout value
243  systime_t retransmitTimeout; ///<Retransmission timeout
244  uint_t retransmitCount; ///<Retransmission counter
245  Ipv4Addr serverIpAddr; ///<DHCP server IPv4 address
246  Ipv4Addr requestedIpAddr; ///<Requested IPv4 address
247  uint32_t transactionId; ///<Value to match requests with replies
248  systime_t configStartTime; ///<Address acquisition or renewal process start time
249  systime_t leaseStartTime; ///<Lease start time
250  uint32_t leaseTime; ///<Lease time
251  uint32_t t1; ///<Time at which the client enters the RENEWING state
252  uint32_t t2; ///<Time at which the client enters the REBINDING state
253  DHCP_CLIENT_PRIVATE_CONTEXT ///<Application specific context
254 };
255 
256 
257 //DHCP client related functions
259 
261  const DhcpClientSettings *settings);
262 
265 
268 
269 //C++ guard
270 #ifdef __cplusplus
271 }
272 #endif
273 
274 #endif
bool_t running
This flag tells whether the DHCP client is running or not.
Definition: dhcp_client.h:238
void(* DhcpClientAddOptionsCallback)(DhcpClientContext *context, DhcpMessage *message, size_t *length, DhcpMessageType type)
Add DHCP options callback.
Definition: dhcp_client.h:200
int bool_t
Definition: compiler_port.h:53
@ DHCP_STATE_ANNOUNCING
Definition: dhcp_client.h:165
DhcpMessageType
DHCP message types.
Definition: dhcp_common.h:88
DhcpClientLinkChangeCallback linkChangeEvent
Link state change event.
Definition: dhcp_client.h:224
uint8_t message[]
Definition: chap.h:154
uint_t retransmitCount
Retransmission counter.
Definition: dhcp_client.h:244
void(* DhcpClientStateChangeCallback)(DhcpClientContext *context, NetInterface *interface, DhcpState state)
FSM state change callback.
Definition: dhcp_client.h:192
systime_t configStartTime
Address acquisition or renewal process start time.
Definition: dhcp_client.h:248
systime_t retransmitTimeout
Retransmission timeout.
Definition: dhcp_client.h:243
bool_t rapidCommit
Quick configuration using rapid commit.
Definition: dhcp_client.h:220
uint8_t type
Definition: coap_common.h:176
DhcpClientStateChangeCallback stateChangeEvent
FSM state change event.
Definition: dhcp_client.h:225
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:297
#define DhcpClientContext
Definition: dhcp_client.h:145
uint32_t t2
Time at which the client enters the REBINDING state.
Definition: dhcp_client.h:252
DHCP client context.
Definition: dhcp_client.h:236
systime_t timeout
Timeout value.
Definition: dhcp_client.h:242
@ DHCP_STATE_INIT_REBOOT
Definition: dhcp_client.h:162
uint_t ipAddrIndex
Index of the IP address to be configured.
Definition: dhcp_client.h:219
error_t dhcpClientStop(DhcpClientContext *context)
Stop DHCP client.
Definition: dhcp_client.c:194
uint32_t transactionId
Value to match requests with replies.
Definition: dhcp_client.h:247
void(* DhcpClientTimeoutCallback)(DhcpClientContext *context, NetInterface *interface)
DHCP configuration timeout callback.
Definition: dhcp_client.h:176
error_t
Error codes.
Definition: error.h:43
DhcpClientTimeoutCallback timeoutEvent
DHCP configuration timeout event.
Definition: dhcp_client.h:223
uint32_t leaseTime
Lease time.
Definition: dhcp_client.h:250
error_t dhcpClientStart(DhcpClientContext *context)
Start DHCP client.
Definition: dhcp_client.c:135
Ipv4Addr requestedIpAddr
Requested IPv4 address.
Definition: dhcp_client.h:246
#define NetInterface
Definition: net.h:36
bool_t timeoutEventDone
Timeout callback function has been called.
Definition: dhcp_client.h:240
DhcpMessage
Definition: dhcp_common.h:226
@ DHCP_STATE_REBOOTING
Definition: dhcp_client.h:163
Definitions common to DHCP client and server.
DHCP client settings.
Definition: dhcp_client.h:217
uint8_t length
Definition: tcp.h:368
@ DHCP_STATE_REBINDING
Definition: dhcp_client.h:168
systime_t leaseStartTime
Lease start time.
Definition: dhcp_client.h:249
DhcpState dhcpClientGetState(DhcpClientContext *context)
Retrieve current state.
Definition: dhcp_client.c:296
Ipv4Addr serverIpAddr
DHCP server IPv4 address.
Definition: dhcp_client.h:245
error_t dhcpClientRelease(DhcpClientContext *context)
Release DHCP lease.
Definition: dhcp_client.c:237
systime_t timeout
DHCP configuration timeout.
Definition: dhcp_client.h:222
@ DHCP_STATE_REQUESTING
Definition: dhcp_client.h:161
uint32_t systime_t
System time.
@ DHCP_STATE_BOUND
Definition: dhcp_client.h:166
void dhcpClientGetDefaultSettings(DhcpClientSettings *settings)
Initialize settings with default values.
Definition: dhcp_client.c:57
NetInterface * interface
Network interface to configure.
Definition: dhcp_client.h:218
@ DHCP_STATE_SELECTING
Definition: dhcp_client.h:160
DhcpState
DHCP FSM states.
Definition: dhcp_client.h:158
uint32_t t1
Time at which the client enters the RENEWING state.
Definition: dhcp_client.h:251
@ DHCP_STATE_PROBING
Definition: dhcp_client.h:164
systime_t timestamp
Timestamp to manage retransmissions.
Definition: dhcp_client.h:241
void(* DhcpClientLinkChangeCallback)(DhcpClientContext *context, NetInterface *interface, bool_t linkState)
Link state change callback.
Definition: dhcp_client.h:184
error_t dhcpClientInit(DhcpClientContext *context, const DhcpClientSettings *settings)
DHCP client initialization.
Definition: dhcp_client.c:92
UDP (User Datagram Protocol)
DhcpState state
Current state of the FSM.
Definition: dhcp_client.h:239
bool_t manualDnsConfig
Force manual DNS configuration.
Definition: dhcp_client.h:221
DhcpClientParseOptionsCallback parseOptionsCallback
Parse DHCP options callback.
Definition: dhcp_client.h:227
Socket API.
DhcpClientAddOptionsCallback addOptionsCallback
Add DHCP options callback.
Definition: dhcp_client.h:226
@ DHCP_STATE_INIT
Definition: dhcp_client.h:159
#define DHCP_CLIENT_PRIVATE_CONTEXT
Definition: dhcp_client.h:140
DhcpClientSettings settings
DHCP client settings.
Definition: dhcp_client.h:237
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
error_t(* DhcpClientParseOptionsCallback)(DhcpClientContext *context, const DhcpMessage *message, size_t length, DhcpMessageType type)
Parse DHCP options callback.
Definition: dhcp_client.h:208
@ DHCP_STATE_RENEWING
Definition: dhcp_client.h:167