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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _DHCP_SERVER_H
30 #define _DHCP_SERVER_H
31 
32 //Dependencies
33 #include "core/net.h"
34 
35 //DHCP server support
36 #ifndef DHCP_SERVER_SUPPORT
37  #define DHCP_SERVER_SUPPORT DISABLED
38 #elif (DHCP_SERVER_SUPPORT != ENABLED && DHCP_SERVER_SUPPORT != DISABLED)
39  #error DHCP_SERVER_SUPPORT parameter is not valid
40 #endif
41 
42 //DHCP server tick interval
43 #ifndef DHCP_SERVER_TICK_INTERVAL
44  #define DHCP_SERVER_TICK_INTERVAL 1000
45 #elif (DHCP_SERVER_TICK_INTERVAL < 10)
46  #error DHCP_SERVER_TICK_INTERVAL parameter is not valid
47 #endif
48 
49 //Maximum number of clients
50 #ifndef DHCP_SERVER_MAX_CLIENTS
51  #define DHCP_SERVER_MAX_CLIENTS 16
52 #elif (DHCP_SERVER_MAX_CLIENTS < 1)
53  #error DHCP_SERVER_MAX_CLIENTS parameter is not valid
54 #endif
55 
56 //Default lease time, in seconds
57 #ifndef DHCP_SERVER_DEFAULT_LEASE_TIME
58  #define DHCP_SERVER_DEFAULT_LEASE_TIME 86400
59 #elif (DHCP_SERVER_DEFAULT_LEASE_TIME < 1)
60  #error DHCP_SERVER_DEFAULT_LEASE_TIME parameter is not valid
61 #endif
62 
63 //Maximum number of DNS servers
64 #ifndef DHCP_SERVER_MAX_DNS_SERVERS
65  #define DHCP_SERVER_MAX_DNS_SERVERS 2
66 #elif (DHCP_SERVER_MAX_DNS_SERVERS < 1)
67  #error DHCP_SERVER_MAX_DNS_SERVERS parameter is not valid
68 #endif
69 
70 //C++ guard
71 #ifdef __cplusplus
72  extern "C" {
73 #endif
74 
75 
76 /**
77  * @brief DHCP binding
78  *
79  * A binding is a collection of configuration parameters associated
80  * with a DHCP client
81  *
82  **/
83 
84 typedef struct
85 {
86  MacAddr macAddr; ///<Client's MAC address
87  Ipv4Addr ipAddr; ///<Client's IPv4 address
88  bool_t validLease; ///<Valid lease
89  systime_t timestamp; ///<Timestamp
91 
92 
93 /**
94  * @brief DHCP server settings
95  **/
96 
97 typedef struct
98 {
99  NetInterface *interface; ///<Underlying network interface
100  bool_t rapidCommit; ///<Quick configuration using rapid commit
101  uint32_t leaseTime; ///<Lease time, in seconds, assigned to the DHCP clients
102  Ipv4Addr ipAddrRangeMin; ///<Lowest IP address in the pool that is available for dynamic address assignment
103  Ipv4Addr ipAddrRangeMax; ///<Highest IP address in the pool that is available for dynamic address assignment
104  Ipv4Addr subnetMask; ///<Subnet mask
105  Ipv4Addr defaultGateway; ///<Default gateway
106  Ipv4Addr dnsServer[DHCP_SERVER_MAX_DNS_SERVERS]; ///<DNS servers
108 
109 
110 /**
111  * @brief DHCP server context
112  **/
113 
114 typedef struct
115 {
116  DhcpServerSettings settings; ///<DHCP server settings
117  bool_t running; ///<This flag tells whether the DHCP server is running or not
118  Ipv4Addr nextIpAddr; ///<Next IP address to be assigned
119  DhcpServerBinding clientBinding[DHCP_SERVER_MAX_CLIENTS]; ///<List of bindings
121 
122 
123 //Tick counter to handle periodic operations
125 
126 //DHCP server related functions
131 
132 void dhcpServerTick(DhcpServerContext *context);
133 
134 void dhcpServerProcessMessage(NetInterface *interface,
135  const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader,
136  const NetBuffer *buffer, size_t offset, void *param);
137 
139  const DhcpMessage *message, size_t length);
140 
142  const DhcpMessage *message, size_t length);
143 
145  const DhcpMessage *message, size_t length);
146 
148  const DhcpMessage *message, size_t length);
149 
151  const DhcpMessage *message, size_t length);
152 
154  Ipv4Addr yourIpAddr, const DhcpMessage *request, size_t length);
155 
157 
159  const MacAddr *macAddr);
160 
162  Ipv4Addr ipAddr);
163 
165 
166 //C++ guard
167 #ifdef __cplusplus
168  }
169 #endif
170 
171 #endif
DhcpServerBinding * dhcpServerFindBindingByMacAddr(DhcpServerContext *context, const MacAddr *macAddr)
Search the list of bindings for a given MAC address.
Definition: dhcp_server.c:936
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:232
uint32_t systime_t
Definition: compiler_port.h:44
systime_t timestamp
Timestamp.
Definition: dhcp_server.h:89
Ipv4Addr nextIpAddr
Next IP address to be assigned.
Definition: dhcp_server.h:118
#define DHCP_SERVER_MAX_CLIENTS
Definition: dhcp_server.h:51
TCP/IP stack core.
DHCP server settings.
Definition: dhcp_server.h:97
error_t dhcpServerStop(DhcpServerContext *context)
Stop DHCP server.
Definition: dhcp_server.c:179
uint8_t message[]
Definition: chap.h:150
void dhcpServerParseRelease(DhcpServerContext *context, const DhcpMessage *message, size_t length)
Parse DHCPRELEASE message.
Definition: dhcp_server.c:638
char_t type
Ipv4Addr ipAddrRangeMin
Lowest IP address in the pool that is available for dynamic address assignment.
Definition: dhcp_server.h:102
void dhcpServerParseRequest(DhcpServerContext *context, const DhcpMessage *message, size_t length)
Parse DHCPREQUEST message.
Definition: dhcp_server.c:481
error_t dhcpServerGetNextIpAddr(DhcpServerContext *context, Ipv4Addr *ipAddr)
Retrieve the next IP address to be used.
Definition: dhcp_server.c:1008
bool_t validLease
Valid lease.
Definition: dhcp_server.h:88
error_t dhcpServerInit(DhcpServerContext *context, const DhcpServerSettings *settings)
DHCP server initialization.
Definition: dhcp_server.c:95
uint8_t ipAddr[4]
Definition: mib_common.h:185
systime_t dhcpServerTickCounter
Definition: dhcp_server.c:52
error_t dhcpServerSendReply(DhcpServerContext *context, uint8_t type, Ipv4Addr yourIpAddr, const DhcpMessage *request, size_t length)
Send DHCP reply message.
Definition: dhcp_server.c:690
Ipv4Addr subnetMask
Subnet mask.
Definition: dhcp_server.h:104
__start_packed struct @126 UdpHeader
UDP header.
DhcpServerSettings settings
DHCP server settings.
Definition: dhcp_server.h:116
DhcpServerBinding * dhcpServerCreateBinding(DhcpServerContext *context)
Create a new binding.
Definition: dhcp_server.c:872
bool_t running
This flag tells whether the DHCP server is running or not.
Definition: dhcp_server.h:117
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:86
void dhcpServerParseDiscover(DhcpServerContext *context, const DhcpMessage *message, size_t length)
Parse DHCPDISCOVER message.
Definition: dhcp_server.c:354
void dhcpServerParseInform(DhcpServerContext *context, const DhcpMessage *message, size_t length)
Parse DHCPINFORM message.
Definition: dhcp_server.c:666
void dhcpServerGetDefaultSettings(DhcpServerSettings *settings)
Initialize settings with default values.
Definition: dhcp_server.c:60
NetInterface * interface
Underlying network interface.
Definition: dhcp_server.h:99
DHCP server context.
Definition: dhcp_server.h:114
error_t
Error codes.
Definition: error.h:40
bool_t rapidCommit
Quick configuration using rapid commit.
Definition: dhcp_server.h:100
__start_packed struct @112 MacAddr
MAC address.
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:263
Ipv4Addr ipAddrRangeMax
Highest IP address in the pool that is available for dynamic address assignment.
Definition: dhcp_server.h:103
DhcpServerBinding * dhcpServerFindBindingByIpAddr(DhcpServerContext *context, Ipv4Addr ipAddr)
Search the list of bindings for a given IP address.
Definition: dhcp_server.c:972
#define NetInterface
Definition: net.h:34
MacAddr macAddr
Client&#39;s MAC address.
Definition: dhcp_server.h:86
IP pseudo header.
Definition: ip.h:76
void dhcpServerParseDecline(DhcpServerContext *context, const DhcpMessage *message, size_t length)
Parse DHCPDECLINE message.
Definition: dhcp_server.c:598
__start_packed struct @127 DhcpMessage
DHCP message.
void dhcpServerTick(DhcpServerContext *context)
DHCP server timer handler.
Definition: dhcp_server.c:209
uint32_t leaseTime
Lease time, in seconds, assigned to the DHCP clients.
Definition: dhcp_server.h:101
Ipv4Addr defaultGateway
Default gateway.
Definition: dhcp_server.h:105
error_t dhcpServerStart(DhcpServerContext *context)
Start DHCP server.
Definition: dhcp_server.c:152
DHCP binding.
Definition: dhcp_server.h:84
uint8_t length
Definition: dtls_misc.h:140
Ipv4Addr ipAddr
Client&#39;s IPv4 address.
Definition: dhcp_server.h:87
int bool_t
Definition: compiler_port.h:47
#define DHCP_SERVER_MAX_DNS_SERVERS
Definition: dhcp_server.h:65