-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathdhcp_client.c
311 lines (247 loc) · 7.8 KB
/
dhcp_client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* @file dhcp_client.c
* @brief DHCP client (Dynamic Host Configuration Protocol)
*
* @section License
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
*
* This file is part of CycloneTCP Open.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @section Description
*
* The Dynamic Host Configuration Protocol is used to provide configuration
* parameters to hosts. Refer to the following RFCs for complete details:
* - RFC 2131: Dynamic Host Configuration Protocol
* - RFC 2132: DHCP Options and BOOTP Vendor Extensions
* - RFC 4039: Rapid Commit Option for the DHCP version 4
*
* @author Oryx Embedded SARL (www.oryx-embedded.com)
* @version 2.5.0
**/
//Switch to the appropriate trace level
#define TRACE_LEVEL DHCP_TRACE_LEVEL
//Dependencies
#include "core/net.h"
#include "dhcp/dhcp_client.h"
#include "dhcp/dhcp_client_misc.h"
#include "debug.h"
//Check TCP/IP stack configuration
#if (IPV4_SUPPORT == ENABLED && DHCP_CLIENT_SUPPORT == ENABLED)
/**
* @brief Initialize settings with default values
* @param[out] settings Structure that contains DHCP client settings
**/
void dhcpClientGetDefaultSettings(DhcpClientSettings *settings)
{
//Use default interface
settings->interface = netGetDefaultInterface();
//Index of the IP address to be configured
settings->ipAddrIndex = 0;
//Support for quick configuration using rapid commit
settings->rapidCommit = FALSE;
//Use the DNS servers provided by the DHCP server
settings->manualDnsConfig = FALSE;
//DHCP configuration timeout
settings->timeout = 0;
//DHCP configuration timeout event
settings->timeoutEvent = NULL;
//Link state change event
settings->linkChangeEvent = NULL;
//FSM state change event
settings->stateChangeEvent = NULL;
//Add DHCP options callback
settings->addOptionsCallback = NULL;
//Parse DHCP options callback
settings->parseOptionsCallback = NULL;
}
/**
* @brief DHCP client initialization
* @param[in] context Pointer to the DHCP client context
* @param[in] settings DHCP client specific settings
* @return Error code
**/
error_t dhcpClientInit(DhcpClientContext *context,
const DhcpClientSettings *settings)
{
NetInterface *interface;
//Debug message
TRACE_INFO("Initializing DHCP client...\r\n");
//Ensure the parameters are valid
if(context == NULL || settings == NULL)
return ERROR_INVALID_PARAMETER;
//The DHCP client must be bound to a valid interface
if(settings->interface == NULL)
return ERROR_INVALID_PARAMETER;
//Point to the underlying network interface
interface = settings->interface;
//Clear the DHCP client context
osMemset(context, 0, sizeof(DhcpClientContext));
//Save user settings
context->settings = *settings;
//DHCP client is currently suspended
context->running = FALSE;
//Initialize state machine
context->state = DHCP_STATE_INIT;
//Attach the DHCP client context to the network interface
interface->dhcpClientContext = context;
//Successful initialization
return NO_ERROR;
}
/**
* @brief Start DHCP client
* @param[in] context Pointer to the DHCP client context
* @return Error code
**/
error_t dhcpClientStart(DhcpClientContext *context)
{
error_t error;
NetInterface *interface;
//Make sure the DHCP client context is valid
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Debug message
TRACE_INFO("Starting DHCP client...\r\n");
//Get exclusive access
osAcquireMutex(&netMutex);
//Point to the underlying network interface
interface = context->settings.interface;
//Check the operational state of the DHCP client
if(!context->running)
{
//Reset DHCP configuration
dhcpClientResetConfig(context);
//Initialize state machine
context->state = DHCP_STATE_INIT;
//Register the callback function to be called whenever a UDP datagram
//is received on port 68
error = udpAttachRxCallback(interface, DHCP_CLIENT_PORT,
dhcpClientProcessMessage, context);
//Check status code
if(!error)
{
//Start DHCP client
context->running = TRUE;
}
}
else
{
//The DHCP client is already running
error = ERROR_ALREADY_RUNNING;
}
//Release exclusive access
osReleaseMutex(&netMutex);
//Return status code
return error;
}
/**
* @brief Stop DHCP client
* @param[in] context Pointer to the DHCP client context
* @return Error code
**/
error_t dhcpClientStop(DhcpClientContext *context)
{
NetInterface *interface;
//Make sure the DHCP client context is valid
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Debug message
TRACE_INFO("Stopping DHCP client...\r\n");
//Get exclusive access
osAcquireMutex(&netMutex);
//Point to the underlying network interface
interface = context->settings.interface;
//Check whether the DHCP client is running
if(context->running)
{
//Unregister callback function
udpDetachRxCallback(interface, DHCP_CLIENT_PORT);
//Stop DHCP client
context->running = FALSE;
//Reinitialize state machine
context->state = DHCP_STATE_INIT;
}
//Release exclusive access
osReleaseMutex(&netMutex);
//Successful processing
return NO_ERROR;
}
/**
* @brief Release DHCP lease
* @param[in] context Pointer to the DHCP client context
* @return Error code
**/
error_t dhcpClientRelease(DhcpClientContext *context)
{
NetInterface *interface;
//Check parameter
if(context == NULL)
return ERROR_INVALID_PARAMETER;
//Debug message
TRACE_INFO("Releasing DHCP lease...\r\n");
//Get exclusive access
osAcquireMutex(&netMutex);
//Point to the underlying network interface
interface = context->settings.interface;
//Check whether the DHCP client is running
if(context->running)
{
//Check current state
if(context->state == DHCP_STATE_BOUND ||
context->state == DHCP_STATE_RENEWING ||
context->state == DHCP_STATE_REBINDING)
{
//Select a new transaction identifier
context->transactionId = netGenerateRand();
//The client may choose to relinquish its lease on a network address
//by sending a DHCPRELEASE message to the server
dhcpClientSendRelease(context);
//The host address is no longer valid
dhcpClientResetConfig(context);
}
//Unregister callback function
udpDetachRxCallback(interface, DHCP_CLIENT_PORT);
//Stop DHCP client
context->running = FALSE;
//Reinitialize state machine
context->state = DHCP_STATE_INIT;
}
//Release exclusive access
osReleaseMutex(&netMutex);
//Successful processing
return NO_ERROR;
}
/**
* @brief Retrieve current state
* @param[in] context Pointer to the DHCP client context
* @return Current DHCP client state
**/
DhcpState dhcpClientGetState(DhcpClientContext *context)
{
DhcpState state;
//Get exclusive access
osAcquireMutex(&netMutex);
//Get current state
state = context->state;
//Release exclusive access
osReleaseMutex(&netMutex);
//Return current state
return state;
}
#endif