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-2019 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 1.9.6
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 //Tick counter to handle periodic operations
128 
129 //DHCP server related functions
131 
133  const DhcpServerSettings *settings);
134 
135 void dhcpServerDeinit(DhcpServerContext *context);
136 
139 
140 void dhcpServerTick(DhcpServerContext *context);
141 
142 void dhcpServerProcessMessage(NetInterface *interface,
143  const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader,
144  const NetBuffer *buffer, size_t offset, void *param);
145 
147  const DhcpMessage *message, size_t length);
148 
150  const DhcpMessage *message, size_t length);
151 
153  const DhcpMessage *message, size_t length);
154 
156  const DhcpMessage *message, size_t length);
157 
159  const DhcpMessage *message, size_t length);
160 
162  Ipv4Addr yourIpAddr, const DhcpMessage *request, size_t length);
163 
165 
167  const MacAddr *macAddr);
168 
170  Ipv4Addr ipAddr);
171 
173 
174 //C++ guard
175 #ifdef __cplusplus
176 }
177 #endif
178 
179 #endif
uint8_t length
Definition: dtls_misc.h:149
void dhcpServerParseRelease(DhcpServerContext *context, const DhcpMessage *message, size_t length)
Parse DHCPRELEASE message.
Definition: dhcp_server.c:686
Ipv4Addr defaultGateway
Default gateway.
Definition: dhcp_server.h:108
int bool_t
Definition: compiler_port.h:49
void dhcpServerTick(DhcpServerContext *context)
DHCP server timer handler.
Definition: dhcp_server.c:251
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
void dhcpServerParseDecline(DhcpServerContext *context, const DhcpMessage *message, size_t length)
Parse DHCPDECLINE message.
Definition: dhcp_server.c:646
DHCP server settings.
Definition: dhcp_server.h:99
DHCP binding.
Definition: dhcp_server.h:86
Ipv4Addr subnetMask
Subnet mask.
Definition: dhcp_server.h:107
error_t dhcpServerStop(DhcpServerContext *context)
Stop DHCP server.
Definition: dhcp_server.c:221
MacAddr macAddr
Client's MAC address.
Definition: dhcp_server.h:88
DhcpServerSettings settings
DHCP server settings.
Definition: dhcp_server.h:119
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:239
DhcpServerBinding * dhcpServerCreateBinding(DhcpServerContext *context)
Create a new binding.
Definition: dhcp_server.c:930
__start_packed struct @123 DhcpMessage
DHCP message.
IP pseudo header.
Definition: ip.h:90
error_t dhcpServerGetNextIpAddr(DhcpServerContext *context, Ipv4Addr *ipAddr)
Retrieve the next IP address to be used.
Definition: dhcp_server.c:1066
systime_t timestamp
Timestamp.
Definition: dhcp_server.h:91
char_t type
error_t
Error codes.
Definition: error.h:42
NetInterface * interface
Underlying network interface.
Definition: dhcp_server.h:101
systime_t dhcpServerTickCounter
Definition: dhcp_server.c:54
Ipv4Addr ipAddr
Client's IPv4 address.
Definition: dhcp_server.h:89
void dhcpServerParseRequest(DhcpServerContext *context, const DhcpMessage *message, size_t length)
Parse DHCPREQUEST message.
Definition: dhcp_server.c:526
Ipv4Addr ipAddrRangeMin
Lowest IP address in the pool that is available for dynamic address assignment.
Definition: dhcp_server.h:105
bool_t running
This flag tells whether the DHCP server is running or not.
Definition: dhcp_server.h:120
void dhcpServerParseDiscover(DhcpServerContext *context, const DhcpMessage *message, size_t length)
Parse DHCPDISCOVER message.
Definition: dhcp_server.c:396
#define NetInterface
Definition: net.h:36
void dhcpServerDeinit(DhcpServerContext *context)
Release DHCP server context.
Definition: dhcp_server.c:156
__start_packed struct @122 UdpHeader
UDP header.
error_t dhcpServerStart(DhcpServerContext *context)
Start DHCP server.
Definition: dhcp_server.c:194
void dhcpServerParseInform(DhcpServerContext *context, const DhcpMessage *message, size_t length)
Parse DHCPINFORM message.
Definition: dhcp_server.c:714
DhcpServerBinding * dhcpServerFindBindingByIpAddr(DhcpServerContext *context, Ipv4Addr ipAddr)
Search the list of bindings for a given IP address.
Definition: dhcp_server.c:1030
DhcpServerBinding * dhcpServerFindBindingByMacAddr(DhcpServerContext *context, const MacAddr *macAddr)
Search the list of bindings for a given MAC address.
Definition: dhcp_server.c:994
error_t dhcpServerSendReply(DhcpServerContext *context, uint8_t type, Ipv4Addr yourIpAddr, const DhcpMessage *request, size_t length)
Send DHCP reply message.
Definition: dhcp_server.c:738
error_t dhcpServerInit(DhcpServerContext *context, const DhcpServerSettings *settings)
DHCP server initialization.
Definition: dhcp_server.c:99
#define DHCP_SERVER_MAX_DNS_SERVERS
Definition: dhcp_server.h:67
#define DHCP_SERVER_MAX_CLIENTS
Definition: dhcp_server.h:53
bool_t rapidCommit
Quick configuration using rapid commit.
Definition: dhcp_server.h:103
uint_t ipAddrIndex
Index of the IP address assigned to the DHCP server.
Definition: dhcp_server.h:102
uint8_t message[]
Definition: chap.h:152
DHCP server context.
Definition: dhcp_server.h:117
Ipv4Addr ipAddrRangeMax
Highest IP address in the pool that is available for dynamic address assignment.
Definition: dhcp_server.h:106
bool_t validLease
Valid lease.
Definition: dhcp_server.h:90
uint32_t leaseTime
Lease time, in seconds, assigned to the DHCP clients.
Definition: dhcp_server.h:104
void dhcpServerProcessMessage(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader, const NetBuffer *buffer, size_t offset, void *param)
Process incoming DHCP message.
Definition: dhcp_server.c:305
void dhcpServerGetDefaultSettings(DhcpServerSettings *settings)
Initialize settings with default values.
Definition: dhcp_server.c:62
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
uint32_t systime_t
Definition: compiler_port.h:46
uint8_t ipAddr[4]
Definition: mib_common.h:187
Ipv4Addr nextIpAddr
Next IP address to be assigned.
Definition: dhcp_server.h:121
__start_packed struct @108 MacAddr
MAC address.