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-2022 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.1.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 //Random factor used to determine the delay between retransmissions
118 #ifndef DHCP_CLIENT_RAND_FACTOR
119  #define DHCP_CLIENT_RAND_FACTOR 1000
120 #elif (DHCP_CLIENT_RAND_FACTOR < 100)
121  #error DHCP_CLIENT_RAND_FACTOR parameter is not valid
122 #endif
123 
124 //Forward declaration of DhcpClientContext structure
125 struct _DhcpClientContext;
126 #define DhcpClientContext struct _DhcpClientContext
127 
128 //C++ guard
129 #ifdef __cplusplus
130 extern "C" {
131 #endif
132 
133 
134 /**
135  * @brief DHCP FSM states
136  **/
137 
138 typedef enum
139 {
150 
151 
152 /**
153  * @brief DHCP configuration timeout callback
154  **/
155 
156 typedef void (*DhcpTimeoutCallback)(DhcpClientContext *context,
157  NetInterface *interface);
158 
159 
160 /**
161  * @brief Link state change callback
162  **/
163 
164 typedef void (*DhcpLinkChangeCallback)(DhcpClientContext *context,
165  NetInterface *interface, bool_t linkState);
166 
167 
168 /**
169  * @brief FSM state change callback
170  **/
171 
173  NetInterface *interface, DhcpState state);
174 
175 
176 /**
177  * @brief Add DHCP options callback
178  **/
179 
180 typedef void (*DhcpAddOptionsCallback)(DhcpClientContext *context,
182 
183 
184 /**
185  * @brief Parse DHCP options callback
186  **/
187 
189  const DhcpMessage *message, size_t length, DhcpMessageType type);
190 
191 
192 /**
193  * @brief DHCP client settings
194  **/
195 
196 typedef struct
197 {
198  NetInterface *interface; ///<Network interface to configure
199  uint_t ipAddrIndex; ///<Index of the IP address to be configured
200  bool_t rapidCommit; ///<Quick configuration using rapid commit
201  bool_t manualDnsConfig; ///<Force manual DNS configuration
202  systime_t timeout; ///<DHCP configuration timeout
203  DhcpTimeoutCallback timeoutEvent; ///<DHCP configuration timeout event
204  DhcpLinkChangeCallback linkChangeEvent; ///<Link state change event
205  DhcpStateChangeCallback stateChangeEvent; ///<FSM state change event
206  DhcpAddOptionsCallback addOptionsCallback; ///<Add DHCP options callback
207  DhcpParseOptionsCallback parseOptionsCallback; ///<Parse DHCP options callback
209 
210 
211 /**
212  * @brief DHCP client context
213  **/
214 
216 {
217  DhcpClientSettings settings; ///<DHCP client settings
218  bool_t running; ///<This flag tells whether the DHCP client is running or not
219  DhcpState state; ///<Current state of the FSM
220  bool_t timeoutEventDone; ///<Timeout callback function has been called
221  systime_t timestamp; ///<Timestamp to manage retransmissions
222  systime_t timeout; ///<Timeout value
223  systime_t retransmitTimeout; ///<Retransmission timeout
224  uint_t retransmitCount; ///<Retransmission counter
225  Ipv4Addr serverIpAddr; ///<DHCP server IPv4 address
226  Ipv4Addr requestedIpAddr; ///<Requested IPv4 address
227  uint32_t transactionId; ///<Value to match requests with replies
228  systime_t configStartTime; ///<Address acquisition or renewal process start time
229  systime_t leaseStartTime; ///<Lease start time
230  uint32_t leaseTime; ///<Lease time
231  uint32_t t1; ///<Time at which the client enters the RENEWING state
232  uint32_t t2; ///<Time at which the client enters the REBINDING state
233 };
234 
235 
236 //DHCP client related functions
238 
240  const DhcpClientSettings *settings);
241 
244 
246 
247 //C++ guard
248 #ifdef __cplusplus
249 }
250 #endif
251 
252 #endif
uint8_t message[]
Definition: chap.h:152
uint8_t length
Definition: coap_common.h:191
unsigned int uint_t
Definition: compiler_port.h:45
int bool_t
Definition: compiler_port.h:48
void(* DhcpLinkChangeCallback)(DhcpClientContext *context, NetInterface *interface, bool_t linkState)
Link state change callback.
Definition: dhcp_client.h:164
error_t dhcpClientStart(DhcpClientContext *context)
Start DHCP client.
Definition: dhcp_client.c:135
void(* DhcpTimeoutCallback)(DhcpClientContext *context, NetInterface *interface)
DHCP configuration timeout callback.
Definition: dhcp_client.h:156
void(* DhcpAddOptionsCallback)(DhcpClientContext *context, DhcpMessage *message, size_t *length, DhcpMessageType type)
Add DHCP options callback.
Definition: dhcp_client.h:180
error_t dhcpClientStop(DhcpClientContext *context)
Stop DHCP client.
Definition: dhcp_client.c:194
void(* DhcpParseOptionsCallback)(DhcpClientContext *context, const DhcpMessage *message, size_t length, DhcpMessageType type)
Parse DHCP options callback.
Definition: dhcp_client.h:188
void(* DhcpStateChangeCallback)(DhcpClientContext *context, NetInterface *interface, DhcpState state)
FSM state change callback.
Definition: dhcp_client.h:172
#define DhcpClientContext
Definition: dhcp_client.h:126
DhcpState
DHCP FSM states.
Definition: dhcp_client.h:139
@ DHCP_STATE_INIT
Definition: dhcp_client.h:140
@ DHCP_STATE_BOUND
Definition: dhcp_client.h:146
@ DHCP_STATE_REBOOTING
Definition: dhcp_client.h:144
@ DHCP_STATE_SELECTING
Definition: dhcp_client.h:141
@ DHCP_STATE_INIT_REBOOT
Definition: dhcp_client.h:143
@ DHCP_STATE_REQUESTING
Definition: dhcp_client.h:142
@ DHCP_STATE_PROBING
Definition: dhcp_client.h:145
@ DHCP_STATE_RENEWING
Definition: dhcp_client.h:147
@ DHCP_STATE_REBINDING
Definition: dhcp_client.h:148
error_t dhcpClientInit(DhcpClientContext *context, const DhcpClientSettings *settings)
DHCP client initialization.
Definition: dhcp_client.c:92
DhcpState dhcpClientGetState(DhcpClientContext *context)
Retrieve current state.
Definition: dhcp_client.c:237
void dhcpClientGetDefaultSettings(DhcpClientSettings *settings)
Initialize settings with default values.
Definition: dhcp_client.c:57
Definitions common to DHCP client and server.
__start_packed struct @21 DhcpMessage
DHCP message.
DhcpMessageType
DHCP message types.
Definition: dhcp_common.h:88
error_t
Error codes.
Definition: error.h:43
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:239
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
uint32_t systime_t
System time.
char_t type
Socket API.
DHCP client context.
Definition: dhcp_client.h:216
bool_t timeoutEventDone
Timeout callback function has been called.
Definition: dhcp_client.h:220
systime_t timestamp
Timestamp to manage retransmissions.
Definition: dhcp_client.h:221
uint32_t transactionId
Value to match requests with replies.
Definition: dhcp_client.h:227
systime_t retransmitTimeout
Retransmission timeout.
Definition: dhcp_client.h:223
systime_t leaseStartTime
Lease start time.
Definition: dhcp_client.h:229
Ipv4Addr requestedIpAddr
Requested IPv4 address.
Definition: dhcp_client.h:226
uint32_t t1
Time at which the client enters the RENEWING state.
Definition: dhcp_client.h:231
Ipv4Addr serverIpAddr
DHCP server IPv4 address.
Definition: dhcp_client.h:225
bool_t running
This flag tells whether the DHCP client is running or not.
Definition: dhcp_client.h:218
uint_t retransmitCount
Retransmission counter.
Definition: dhcp_client.h:224
DhcpState state
Current state of the FSM.
Definition: dhcp_client.h:219
uint32_t leaseTime
Lease time.
Definition: dhcp_client.h:230
uint32_t t2
Time at which the client enters the REBINDING state.
Definition: dhcp_client.h:232
systime_t timeout
Timeout value.
Definition: dhcp_client.h:222
systime_t configStartTime
Address acquisition or renewal process start time.
Definition: dhcp_client.h:228
DhcpClientSettings settings
DHCP client settings.
Definition: dhcp_client.h:217
DHCP client settings.
Definition: dhcp_client.h:197
bool_t rapidCommit
Quick configuration using rapid commit.
Definition: dhcp_client.h:200
DhcpAddOptionsCallback addOptionsCallback
Add DHCP options callback.
Definition: dhcp_client.h:206
DhcpParseOptionsCallback parseOptionsCallback
Parse DHCP options callback.
Definition: dhcp_client.h:207
DhcpTimeoutCallback timeoutEvent
DHCP configuration timeout event.
Definition: dhcp_client.h:203
bool_t manualDnsConfig
Force manual DNS configuration.
Definition: dhcp_client.h:201
uint_t ipAddrIndex
Index of the IP address to be configured.
Definition: dhcp_client.h:199
DhcpStateChangeCallback stateChangeEvent
FSM state change event.
Definition: dhcp_client.h:205
DhcpLinkChangeCallback linkChangeEvent
Link state change event.
Definition: dhcp_client.h:204
systime_t timeout
DHCP configuration timeout.
Definition: dhcp_client.h:202
NetInterface * interface
Network interface to configure.
Definition: dhcp_client.h:198
UDP (User Datagram Protocol)