dhcp_server.c
Go to the documentation of this file.
1 /**
2  * @file dhcp_server.c
3  * @brief DHCP server (Dynamic Host Configuration Protocol)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 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  * @section Description
28  *
29  * The Dynamic Host Configuration Protocol is used to provide configuration
30  * parameters to hosts. Refer to the following RFCs for complete details:
31  * - RFC 2131: Dynamic Host Configuration Protocol
32  * - RFC 2132: DHCP Options and BOOTP Vendor Extensions
33  * - RFC 4039: Rapid Commit Option for the DHCP version 4
34  *
35  * @author Oryx Embedded SARL (www.oryx-embedded.com)
36  * @version 2.6.0
37  **/
38 
39 //Switch to the appropriate trace level
40 #define TRACE_LEVEL DHCP_TRACE_LEVEL
41 
42 //Dependencies
43 #include "core/net.h"
44 #include "dhcp/dhcp_server.h"
45 #include "dhcp/dhcp_server_misc.h"
46 #include "date_time.h"
47 #include "debug.h"
48 
49 //Check TCP/IP stack configuration
50 #if (IPV4_SUPPORT == ENABLED && DHCP_SERVER_SUPPORT == ENABLED)
51 
52 
53 /**
54  * @brief Initialize settings with default values
55  * @param[out] settings Structure that contains DHCP server settings
56  **/
57 
59 {
60  uint_t i;
61 
62  //Underlying network interface
63  settings->interface = NULL;
64  //Index of the IP address assigned to the DHCP server
65  settings->ipAddrIndex = 0;
66 
67  //Support for quick configuration using rapid commit
68  settings->rapidCommit = FALSE;
69  //Lease time, in seconds, assigned to the DHCP clients
71 
72  //Lowest and highest IP addresses in the pool that are available
73  //for dynamic address assignment
76 
77  //Subnet mask
79  //Default gateway
81 
82  //DNS servers
83  for(i = 0; i < DHCP_SERVER_MAX_DNS_SERVERS; i++)
84  {
85  settings->dnsServer[i] = IPV4_UNSPECIFIED_ADDR;
86  }
87 
88  //Add DHCP options callback
89  settings->addOptionsCallback = NULL;
90  //Parse DHCP options callback
91  settings->parseOptionsCallback = NULL;
92 }
93 
94 
95 /**
96  * @brief DHCP server initialization
97  * @param[in] context Pointer to the DHCP server context
98  * @param[in] settings DHCP server specific settings
99  * @return Error code
100  **/
101 
103  const DhcpServerSettings *settings)
104 {
105  uint_t i;
106  NetInterface *interface;
107 
108  //Debug message
109  TRACE_INFO("Initializing DHCP server...\r\n");
110 
111  //Ensure the parameters are valid
112  if(context == NULL || settings == NULL)
114 
115  //Invalid network interface?
116  if(settings->interface == NULL)
118 
119  //Point to the underlying network interface
120  interface = settings->interface;
121 
122  //Clear the DHCP server context
123  osMemset(context, 0, sizeof(DhcpServerContext));
124 
125  //Attach TCP/IP stack context
126  context->netContext = settings->interface->netContext;
127 
128  //Save user settings
129  context->interface = settings->interface;
130  context->ipAddrIndex = settings->ipAddrIndex;
131  context->rapidCommit = settings->rapidCommit;
132  context->leaseTime = settings->leaseTime;
133  context->ipAddrRangeMin = settings->ipAddrRangeMin;
134  context->ipAddrRangeMax = settings->ipAddrRangeMax;
135  context->subnetMask = settings->subnetMask;
136  context->defaultGateway = settings->defaultGateway;
137  context->addOptionsCallback = settings->addOptionsCallback;
138  context->parseOptionsCallback = settings->parseOptionsCallback;
139 
140  //Save DNS servers
141  for(i = 0; i < DHCP_SERVER_MAX_DNS_SERVERS; i++)
142  {
143  context->dnsServer[i] = settings->dnsServer[i];
144  }
145 
146  //Next IP address that will be assigned by the DHCP server
147  context->nextIpAddr = settings->ipAddrRangeMin;
148  //DHCP server is currently suspended
149  context->running = FALSE;
150 
151  //Get exclusive access
152  netLock(context->netContext);
153  //Attach the DHCP server context to the network interface
154  interface->dhcpServerContext = context;
155  //Release exclusive access
156  netUnlock(context->netContext);
157 
158  //Successful initialization
159  return NO_ERROR;
160 }
161 
162 
163 /**
164  * @brief Start DHCP server
165  * @param[in] context Pointer to the DHCP server context
166  * @return Error code
167  **/
168 
170 {
171  error_t error;
172 
173  //Make sure the DHCP server context is valid
174  if(context == NULL)
176 
177  //Debug message
178  TRACE_INFO("Starting DHCP server...\r\n");
179 
180  //Get exclusive access
181  netLock(context->netContext);
182 
183  //Check the operational state of the DHCP client
184  if(!context->running)
185  {
186  //Register the callback function to be called whenever a UDP datagram
187  //is received on port 67
188  error = udpRegisterRxCallback(context->interface, DHCP_SERVER_PORT,
189  dhcpServerProcessMessage, context);
190 
191  //Check status code
192  if(!error)
193  {
194  //Start DHCP server
195  context->running = TRUE;
196  }
197  }
198  else
199  {
200  //The DHCP client is already running
201  error = ERROR_ALREADY_RUNNING;
202  }
203 
204  //Release exclusive access
205  netUnlock(context->netContext);
206 
207  //Return status code
208  return error;
209 }
210 
211 
212 /**
213  * @brief Stop DHCP server
214  * @param[in] context Pointer to the DHCP server context
215  * @return Error code
216  **/
217 
219 {
220  //Make sure the DHCP server context is valid
221  if(context == NULL)
223 
224  //Debug message
225  TRACE_INFO("Stopping DHCP server...\r\n");
226 
227  //Get exclusive access
228  netLock(context->netContext);
229 
230  //Check whether the DHCP client is running
231  if(context->running)
232  {
233  //Unregister callback function
234  udpUnregisterRxCallback(context->interface, DHCP_SERVER_PORT);
235 
236  //Stop DHCP server
237  context->running = FALSE;
238  }
239 
240  //Release exclusive access
241  netUnlock(context->netContext);
242 
243  //Successful processing
244  return NO_ERROR;
245 }
246 
247 
248 /**
249  * @brief Release DHCP server context
250  * @param[in] context Pointer to the DHCP server context
251  **/
252 
254 {
255  NetInterface *interface;
256 
257  //Make sure the DHCP server context is valid
258  if(context != NULL)
259  {
260  //Get exclusive access
261  netLock(context->netContext);
262 
263  //Point to the underlying network interface
264  interface = context->interface;
265  //Detach the DHCP server context from the network interface
266  interface->dhcpServerContext = NULL;
267 
268  //Release exclusive access
269  netUnlock(context->netContext);
270 
271  //Clear DHCP server context
272  osMemset(context, 0, sizeof(DhcpServerContext));
273  }
274 }
275 
276 #endif
error_t dhcpServerStop(DhcpServerContext *context)
Stop DHCP server.
Definition: dhcp_server.c:218
Date and time management.
void netUnlock(NetContext *context)
Release exclusive access to the core of the TCP/IP stack.
Definition: net.c:319
Ipv4Addr defaultGateway
Default gateway.
Definition: dhcp_server.h:133
error_t udpUnregisterRxCallback(NetInterface *interface, uint16_t port)
Unregister user callback.
Definition: udp.c:1062
#define TRUE
Definition: os_port.h:50
DHCP server settings.
Definition: dhcp_server.h:125
error_t dhcpServerStart(DhcpServerContext *context)
Start DHCP server.
Definition: dhcp_server.c:169
Ipv4Addr subnetMask
Subnet mask.
Definition: dhcp_server.h:132
void dhcpServerDeinit(DhcpServerContext *context)
Release DHCP server context.
Definition: dhcp_server.c:253
error_t dhcpServerInit(DhcpServerContext *context, const DhcpServerSettings *settings)
DHCP server initialization.
Definition: dhcp_server.c:102
Helper functions for DHCP server.
#define FALSE
Definition: os_port.h:46
Ipv4Addr dnsServer[DHCP_SERVER_MAX_DNS_SERVERS]
DNS servers.
Definition: dhcp_server.h:134
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:43
NetInterface * interface
Underlying network interface.
Definition: dhcp_server.h:126
DhcpServerAddOptionsCallback addOptionsCallback
Add DHCP options callback.
Definition: dhcp_server.h:135
Ipv4Addr ipAddrRangeMin
Lowest IP address in the pool that is available for dynamic address assignment.
Definition: dhcp_server.h:130
void dhcpServerGetDefaultSettings(DhcpServerSettings *settings)
Initialize settings with default values.
Definition: dhcp_server.c:58
#define DHCP_SERVER_DEFAULT_LEASE_TIME
Definition: dhcp_server.h:60
#define NetInterface
Definition: net.h:40
#define TRACE_INFO(...)
Definition: debug.h:105
#define DHCP_SERVER_MAX_DNS_SERVERS
Definition: dhcp_server.h:67
#define DHCP_SERVER_PORT
Definition: dhcp_common.h:40
bool_t rapidCommit
Quick configuration using rapid commit.
Definition: dhcp_server.h:128
uint_t ipAddrIndex
Index of the IP address assigned to the DHCP server.
Definition: dhcp_server.h:127
void netLock(NetContext *context)
Get exclusive access to the core of the TCP/IP stack.
Definition: net.c:307
Ipv4Addr ipAddrRangeMax
Highest IP address in the pool that is available for dynamic address assignment.
Definition: dhcp_server.h:131
uint32_t leaseTime
Lease time, in seconds, assigned to the DHCP clients.
Definition: dhcp_server.h:129
error_t udpRegisterRxCallback(NetInterface *interface, uint16_t port, UdpRxCallback callback, void *param)
Register user callback.
Definition: udp.c:1021
unsigned int uint_t
Definition: compiler_port.h:57
#define osMemset(p, value, length)
Definition: os_port.h:138
TCP/IP stack core.
void dhcpServerProcessMessage(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary, void *param)
Process incoming DHCP message.
DHCP server (Dynamic Host Configuration Protocol)
DhcpServerParseOptionsCallback parseOptionsCallback
Parse DHCP options callback.
Definition: dhcp_server.h:136
#define DhcpServerContext
Definition: dhcp_server.h:79
@ ERROR_ALREADY_RUNNING
Definition: error.h:294
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define IPV4_UNSPECIFIED_ADDR
Definition: ipv4.h:128