dhcp_server.h
Go to the documentation of this file.
1 /**
2  * @file dhcp_server.h
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  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 #ifndef _DHCP_SERVER_H
32 #define _DHCP_SERVER_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //DHCP server support
38 #ifndef DHCP_SERVER_SUPPORT
39  #define DHCP_SERVER_SUPPORT DISABLED
40 #elif (DHCP_SERVER_SUPPORT != ENABLED && DHCP_SERVER_SUPPORT != DISABLED)
41  #error DHCP_SERVER_SUPPORT parameter is not valid
42 #endif
43 
44 //DHCP server tick interval
45 #ifndef DHCP_SERVER_TICK_INTERVAL
46  #define DHCP_SERVER_TICK_INTERVAL 1000
47 #elif (DHCP_SERVER_TICK_INTERVAL < 10)
48  #error DHCP_SERVER_TICK_INTERVAL parameter is not valid
49 #endif
50 
51 //Maximum number of clients
52 #ifndef DHCP_SERVER_MAX_CLIENTS
53  #define DHCP_SERVER_MAX_CLIENTS 16
54 #elif (DHCP_SERVER_MAX_CLIENTS < 1)
55  #error DHCP_SERVER_MAX_CLIENTS parameter is not valid
56 #endif
57 
58 //Default lease time, in seconds
59 #ifndef DHCP_SERVER_DEFAULT_LEASE_TIME
60  #define DHCP_SERVER_DEFAULT_LEASE_TIME 86400
61 #elif (DHCP_SERVER_DEFAULT_LEASE_TIME < 1)
62  #error DHCP_SERVER_DEFAULT_LEASE_TIME parameter is not valid
63 #endif
64 
65 //Maximum number of DNS servers
66 #ifndef DHCP_SERVER_MAX_DNS_SERVERS
67  #define DHCP_SERVER_MAX_DNS_SERVERS 2
68 #elif (DHCP_SERVER_MAX_DNS_SERVERS < 1)
69  #error DHCP_SERVER_MAX_DNS_SERVERS parameter is not valid
70 #endif
71 
72 //Application specific context
73 #ifndef DHCP_SERVER_PRIVATE_CONTEXT
74  #define DHCP_SERVER_PRIVATE_CONTEXT
75 #endif
76 
77 //Forward declaration of DhcpServerContext structure
78 struct _DhcpServerContext;
79 #define DhcpServerContext struct _DhcpServerContext
80 
81 //C++ guard
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85 
86 
87 /**
88  * @brief Add DHCP options callback
89  **/
90 
93 
94 
95 /**
96  * @brief Parse DHCP options callback
97  **/
98 
100  const DhcpMessage *message, size_t length, DhcpMessageType type);
101 
102 
103 /**
104  * @brief DHCP binding
105  *
106  * A binding is a collection of configuration parameters associated
107  * with a DHCP client
108  *
109  **/
110 
111 typedef struct
112 {
113  MacAddr macAddr; ///<Client's MAC address
114  Ipv4Addr ipAddr; ///<Client's IPv4 address
115  bool_t validLease; ///<Valid lease
116  systime_t timestamp; ///<Timestamp
118 
119 
120 /**
121  * @brief DHCP server settings
122  **/
123 
124 typedef struct
125 {
126  NetInterface *interface; ///<Underlying network interface
127  uint_t ipAddrIndex; ///<Index of the IP address assigned to the DHCP server
128  bool_t rapidCommit; ///<Quick configuration using rapid commit
129  uint32_t leaseTime; ///<Lease time, in seconds, assigned to the DHCP clients
130  Ipv4Addr ipAddrRangeMin; ///<Lowest IP address in the pool that is available for dynamic address assignment
131  Ipv4Addr ipAddrRangeMax; ///<Highest IP address in the pool that is available for dynamic address assignment
132  Ipv4Addr subnetMask; ///<Subnet mask
133  Ipv4Addr defaultGateway; ///<Default gateway
134  Ipv4Addr dnsServer[DHCP_SERVER_MAX_DNS_SERVERS]; ///<DNS servers
135  DhcpServerAddOptionsCallback addOptionsCallback; ///<Add DHCP options callback
136  DhcpServerParseOptionsCallback parseOptionsCallback; ///<Parse DHCP options callback
138 
139 
140 /**
141  * @brief DHCP server context
142  **/
143 
145 {
146  DhcpServerSettings settings; ///<DHCP server settings
147  bool_t running; ///<This flag tells whether the DHCP server is running or not
148  Ipv4Addr nextIpAddr; ///<Next IP address to be assigned
150  DHCP_SERVER_PRIVATE_CONTEXT ///<Application specific context
151 };
152 
153 
154 //DHCP server related functions
156 
158  const DhcpServerSettings *settings);
159 
162 
163 void dhcpServerDeinit(DhcpServerContext *context);
164 
165 //C++ guard
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif
uint8_t message[]
Definition: chap.h:154
uint8_t type
Definition: coap_common.h:176
unsigned int uint_t
Definition: compiler_port.h:50
int bool_t
Definition: compiler_port.h:53
DhcpMessage
Definition: dhcp_common.h:226
DhcpMessageType
DHCP message types.
Definition: dhcp_common.h:88
void dhcpServerGetDefaultSettings(DhcpServerSettings *settings)
Initialize settings with default values.
Definition: dhcp_server.c:58
error_t dhcpServerStop(DhcpServerContext *context)
Stop DHCP server.
Definition: dhcp_server.c:204
#define DhcpServerContext
Definition: dhcp_server.h:79
error_t(* DhcpServerParseOptionsCallback)(DhcpServerContext *context, const DhcpMessage *message, size_t length, DhcpMessageType type)
Parse DHCP options callback.
Definition: dhcp_server.h:99
void dhcpServerDeinit(DhcpServerContext *context)
Release DHCP server context.
Definition: dhcp_server.c:244
#define DHCP_SERVER_PRIVATE_CONTEXT
Definition: dhcp_server.h:74
#define DHCP_SERVER_MAX_DNS_SERVERS
Definition: dhcp_server.h:67
error_t dhcpServerInit(DhcpServerContext *context, const DhcpServerSettings *settings)
DHCP server initialization.
Definition: dhcp_server.c:102
error_t dhcpServerStart(DhcpServerContext *context)
Start DHCP server.
Definition: dhcp_server.c:151
void(* DhcpServerAddOptionsCallback)(DhcpServerContext *context, DhcpMessage *message, size_t *length, DhcpMessageType type)
Add DHCP options callback.
Definition: dhcp_server.h:91
#define DHCP_SERVER_MAX_CLIENTS
Definition: dhcp_server.h:53
error_t
Error codes.
Definition: error.h:43
MacAddr
Definition: ethernet.h:195
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:267
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
uint32_t systime_t
System time.
DHCP server context.
Definition: dhcp_server.h:145
DhcpServerSettings settings
DHCP server settings.
Definition: dhcp_server.h:146
DhcpServerBinding clientBinding[DHCP_SERVER_MAX_CLIENTS]
List of bindings.
Definition: dhcp_server.h:149
bool_t running
This flag tells whether the DHCP server is running or not.
Definition: dhcp_server.h:147
Ipv4Addr nextIpAddr
Next IP address to be assigned.
Definition: dhcp_server.h:148
DHCP binding.
Definition: dhcp_server.h:112
systime_t timestamp
Timestamp.
Definition: dhcp_server.h:116
Ipv4Addr ipAddr
Client's IPv4 address.
Definition: dhcp_server.h:114
MacAddr macAddr
Client's MAC address.
Definition: dhcp_server.h:113
bool_t validLease
Valid lease.
Definition: dhcp_server.h:115
DHCP server settings.
Definition: dhcp_server.h:125
bool_t rapidCommit
Quick configuration using rapid commit.
Definition: dhcp_server.h:128
Ipv4Addr subnetMask
Subnet mask.
Definition: dhcp_server.h:132
DhcpServerParseOptionsCallback parseOptionsCallback
Parse DHCP options callback.
Definition: dhcp_server.h:136
Ipv4Addr ipAddrRangeMin
Lowest IP address in the pool that is available for dynamic address assignment.
Definition: dhcp_server.h:130
uint_t ipAddrIndex
Index of the IP address assigned to the DHCP server.
Definition: dhcp_server.h:127
Ipv4Addr defaultGateway
Default gateway.
Definition: dhcp_server.h:133
uint32_t leaseTime
Lease time, in seconds, assigned to the DHCP clients.
Definition: dhcp_server.h:129
DhcpServerAddOptionsCallback addOptionsCallback
Add DHCP options callback.
Definition: dhcp_server.h:135
Ipv4Addr ipAddrRangeMax
Highest IP address in the pool that is available for dynamic address assignment.
Definition: dhcp_server.h:131
NetInterface * interface
Underlying network interface.
Definition: dhcp_server.h:126
uint8_t length
Definition: tcp.h:368