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-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_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 //C++ guard
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
76 
77 
78 /**
79  * @brief DHCP binding
80  *
81  * A binding is a collection of configuration parameters associated
82  * with a DHCP client
83  *
84  **/
85 
86 typedef struct
87 {
88  MacAddr macAddr; ///<Client's MAC address
89  Ipv4Addr ipAddr; ///<Client's IPv4 address
90  bool_t validLease; ///<Valid lease
91  systime_t timestamp; ///<Timestamp
93 
94 
95 /**
96  * @brief DHCP server settings
97  **/
98 
99 typedef struct
100 {
101  NetInterface *interface; ///<Underlying network interface
102  uint_t ipAddrIndex; ///<Index of the IP address assigned to the DHCP server
103  bool_t rapidCommit; ///<Quick configuration using rapid commit
104  uint32_t leaseTime; ///<Lease time, in seconds, assigned to the DHCP clients
105  Ipv4Addr ipAddrRangeMin; ///<Lowest IP address in the pool that is available for dynamic address assignment
106  Ipv4Addr ipAddrRangeMax; ///<Highest IP address in the pool that is available for dynamic address assignment
107  Ipv4Addr subnetMask; ///<Subnet mask
108  Ipv4Addr defaultGateway; ///<Default gateway
109  Ipv4Addr dnsServer[DHCP_SERVER_MAX_DNS_SERVERS]; ///<DNS servers
111 
112 
113 /**
114  * @brief DHCP server context
115  **/
116 
117 typedef struct
118 {
119  DhcpServerSettings settings; ///<DHCP server settings
120  bool_t running; ///<This flag tells whether the DHCP server is running or not
121  Ipv4Addr nextIpAddr; ///<Next IP address to be assigned
122  DhcpServerBinding clientBinding[DHCP_SERVER_MAX_CLIENTS]; ///<List of bindings
124 
125 
126 //DHCP server related functions
128 
130  const DhcpServerSettings *settings);
131 
134 
135 void dhcpServerDeinit(DhcpServerContext *context);
136 
137 //C++ guard
138 #ifdef __cplusplus
139 }
140 #endif
141 
142 #endif
unsigned int uint_t
Definition: compiler_port.h:45
int bool_t
Definition: compiler_port.h:48
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:197
void dhcpServerDeinit(DhcpServerContext *context)
Release DHCP server context.
Definition: dhcp_server.c:237
#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:95
error_t dhcpServerStart(DhcpServerContext *context)
Start DHCP server.
Definition: dhcp_server.c:144
#define DHCP_SERVER_MAX_CLIENTS
Definition: dhcp_server.h:53
error_t
Error codes.
Definition: error.h:43
__start_packed struct @5 MacAddr
MAC address.
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.
DHCP binding.
Definition: dhcp_server.h:87
systime_t timestamp
Timestamp.
Definition: dhcp_server.h:91
Ipv4Addr ipAddr
Client's IPv4 address.
Definition: dhcp_server.h:89
MacAddr macAddr
Client's MAC address.
Definition: dhcp_server.h:88
bool_t validLease
Valid lease.
Definition: dhcp_server.h:90
DHCP server context.
Definition: dhcp_server.h:118
DhcpServerSettings settings
DHCP server settings.
Definition: dhcp_server.h:119
bool_t running
This flag tells whether the DHCP server is running or not.
Definition: dhcp_server.h:120
Ipv4Addr nextIpAddr
Next IP address to be assigned.
Definition: dhcp_server.h:121
DHCP server settings.
Definition: dhcp_server.h:100
bool_t rapidCommit
Quick configuration using rapid commit.
Definition: dhcp_server.h:103
Ipv4Addr subnetMask
Subnet mask.
Definition: dhcp_server.h:107
Ipv4Addr ipAddrRangeMin
Lowest IP address in the pool that is available for dynamic address assignment.
Definition: dhcp_server.h:105
uint_t ipAddrIndex
Index of the IP address assigned to the DHCP server.
Definition: dhcp_server.h:102
Ipv4Addr defaultGateway
Default gateway.
Definition: dhcp_server.h:108
uint32_t leaseTime
Lease time, in seconds, assigned to the DHCP clients.
Definition: dhcp_server.h:104
Ipv4Addr ipAddrRangeMax
Highest IP address in the pool that is available for dynamic address assignment.
Definition: dhcp_server.h:106
NetInterface * interface
Underlying network interface.
Definition: dhcp_server.h:101