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-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  * @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.4.4
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  //Use default interface
63  settings->interface = netGetDefaultInterface();
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  NetInterface *interface;
106 
107  //Debug message
108  TRACE_INFO("Initializing DHCP server...\r\n");
109 
110  //Ensure the parameters are valid
111  if(context == NULL || settings == NULL)
113 
114  //Valid network interface?
115  if(settings->interface == NULL)
117 
118  //Get exclusive access
120 
121  //Point to the underlying network interface
122  interface = settings->interface;
123 
124  //Clear the DHCP server context
125  osMemset(context, 0, sizeof(DhcpServerContext));
126  //Save user settings
127  context->settings = *settings;
128 
129  //Next IP address that will be assigned by the DHCP server
130  context->nextIpAddr = settings->ipAddrRangeMin;
131  //DHCP server is currently suspended
132  context->running = FALSE;
133 
134  //Attach the DHCP server context to the network interface
135  interface->dhcpServerContext = context;
136 
137  //Release exclusive access
139 
140  //Successful initialization
141  return NO_ERROR;
142 }
143 
144 
145 /**
146  * @brief Start DHCP server
147  * @param[in] context Pointer to the DHCP server context
148  * @return Error code
149  **/
150 
152 {
153  error_t error;
154  NetInterface *interface;
155 
156  //Make sure the DHCP server context is valid
157  if(context == NULL)
159 
160  //Debug message
161  TRACE_INFO("Starting DHCP server...\r\n");
162 
163  //Get exclusive access
165 
166  //Point to the underlying network interface
167  interface = context->settings.interface;
168 
169  //Check the operational state of the DHCP client
170  if(!context->running)
171  {
172  //Register the callback function to be called whenever a UDP datagram
173  //is received on port 67
174  error = udpAttachRxCallback(interface, DHCP_SERVER_PORT,
175  dhcpServerProcessMessage, context);
176 
177  //Check status code
178  if(!error)
179  {
180  //Start DHCP server
181  context->running = TRUE;
182  }
183  }
184  else
185  {
186  //The DHCP client is already running
187  error = ERROR_ALREADY_RUNNING;
188  }
189 
190  //Release exclusive access
192 
193  //Return status code
194  return error;
195 }
196 
197 
198 /**
199  * @brief Stop DHCP server
200  * @param[in] context Pointer to the DHCP server context
201  * @return Error code
202  **/
203 
205 {
206  NetInterface *interface;
207 
208  //Make sure the DHCP server context is valid
209  if(context == NULL)
211 
212  //Debug message
213  TRACE_INFO("Stopping DHCP server...\r\n");
214 
215  //Get exclusive access
217 
218  //Point to the underlying network interface
219  interface = context->settings.interface;
220 
221  //Check whether the DHCP client is running
222  if(context->running)
223  {
224  //Unregister callback function
226 
227  //Stop DHCP server
228  context->running = FALSE;
229  }
230 
231  //Release exclusive access
233 
234  //Successful processing
235  return NO_ERROR;
236 }
237 
238 
239 /**
240  * @brief Release DHCP server context
241  * @param[in] context Pointer to the DHCP server context
242  **/
243 
245 {
246  NetInterface *interface;
247 
248  //Make sure the DHCP server context is valid
249  if(context != NULL)
250  {
251  //Get exclusive access
253 
254  //Point to the underlying network interface
255  interface = context->settings.interface;
256 
257  //Valid network interface?
258  if(interface != NULL)
259  {
260  //Detach the DHCP server context from the network interface
261  interface->dhcpServerContext = NULL;
262  }
263 
264  //Clear the DHCP server context
265  osMemset(context, 0, sizeof(DhcpServerContext));
266 
267  //Release exclusive access
269  }
270 }
271 
272 #endif
error_t dhcpServerStop(DhcpServerContext *context)
Stop DHCP server.
Definition: dhcp_server.c:204
Date and time management.
Ipv4Addr defaultGateway
Default gateway.
Definition: dhcp_server.h:133
#define netMutex
Definition: net_legacy.h:195
#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:151
Ipv4Addr subnetMask
Subnet mask.
Definition: dhcp_server.h:132
void dhcpServerDeinit(DhcpServerContext *context)
Release DHCP server context.
Definition: dhcp_server.c:244
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
error_t udpDetachRxCallback(NetInterface *interface, uint16_t port)
Unregister user callback.
Definition: udp.c:1062
#define DHCP_SERVER_DEFAULT_LEASE_TIME
Definition: dhcp_server.h:60
#define NetInterface
Definition: net.h:36
error_t udpAttachRxCallback(NetInterface *interface, uint16_t port, UdpRxCallback callback, void *param)
Register user callback.
Definition: udp.c:1021
NetInterface * netGetDefaultInterface(void)
Get default network interface.
Definition: net.c:467
#define TRACE_INFO(...)
Definition: debug.h:95
#define DHCP_SERVER_MAX_DNS_SERVERS
Definition: dhcp_server.h:67
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
#define DHCP_SERVER_PORT
Definition: dhcp_common.h:40
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
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
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
unsigned int uint_t
Definition: compiler_port.h:50
#define osMemset(p, value, length)
Definition: os_port.h:135
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:293
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define IPV4_UNSPECIFIED_ADDR
Definition: ipv4.h:117