Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dhcp_server.h Source File

dhcp_server.h

Go to the documentation of this file.
00001 /**
00002  * @file dhcp_server.h
00003  * @brief DHCP server (Dynamic Host Configuration Protocol)
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 #ifndef _DHCP_SERVER_H
00030 #define _DHCP_SERVER_H
00031 
00032 //Dependencies
00033 #include "core/net.h"
00034 
00035 //DHCP server support
00036 #ifndef DHCP_SERVER_SUPPORT
00037    #define DHCP_SERVER_SUPPORT DISABLED
00038 #elif (DHCP_SERVER_SUPPORT != ENABLED && DHCP_SERVER_SUPPORT != DISABLED)
00039    #error DHCP_SERVER_SUPPORT parameter is not valid
00040 #endif
00041 
00042 //DHCP server tick interval
00043 #ifndef DHCP_SERVER_TICK_INTERVAL
00044    #define DHCP_SERVER_TICK_INTERVAL 1000
00045 #elif (DHCP_SERVER_TICK_INTERVAL < 10)
00046    #error DHCP_SERVER_TICK_INTERVAL parameter is not valid
00047 #endif
00048 
00049 //Maximum number of clients
00050 #ifndef DHCP_SERVER_MAX_CLIENTS
00051    #define DHCP_SERVER_MAX_CLIENTS 16
00052 #elif (DHCP_SERVER_MAX_CLIENTS < 1)
00053    #error DHCP_SERVER_MAX_CLIENTS parameter is not valid
00054 #endif
00055 
00056 //Default lease time, in seconds
00057 #ifndef DHCP_SERVER_DEFAULT_LEASE_TIME
00058    #define DHCP_SERVER_DEFAULT_LEASE_TIME 86400
00059 #elif (DHCP_SERVER_DEFAULT_LEASE_TIME < 1)
00060    #error DHCP_SERVER_DEFAULT_LEASE_TIME parameter is not valid
00061 #endif
00062 
00063 //Maximum number of DNS servers
00064 #ifndef DHCP_SERVER_MAX_DNS_SERVERS
00065    #define DHCP_SERVER_MAX_DNS_SERVERS 2
00066 #elif (DHCP_SERVER_MAX_DNS_SERVERS < 1)
00067    #error DHCP_SERVER_MAX_DNS_SERVERS parameter is not valid
00068 #endif
00069 
00070 
00071 /**
00072  * @brief DHCP binding
00073  *
00074  * A binding is a collection of configuration parameters associated
00075  * with a DHCP client
00076  *
00077  **/
00078 
00079 typedef struct
00080 {
00081    MacAddr macAddr;     ///<Client's MAC address
00082    Ipv4Addr ipAddr;     ///<Client's IPv4 address
00083    bool_t validLease;   ///<Valid lease
00084    systime_t timestamp; ///<Timestamp
00085 } DhcpServerBinding;
00086 
00087 
00088 /**
00089  * @brief DHCP server settings
00090  **/
00091 
00092 typedef struct
00093 {
00094    NetInterface *interface;                         ///<Underlying network interface
00095    bool_t rapidCommit;                              ///<Quick configuration using rapid commit
00096    uint32_t leaseTime;                              ///<Lease time, in seconds, assigned to the DHCP clients
00097    Ipv4Addr ipAddrRangeMin;                         ///<Lowest IP address in the pool that is available for dynamic address assignment
00098    Ipv4Addr ipAddrRangeMax;                         ///<Highest IP address in the pool that is available for dynamic address assignment
00099    Ipv4Addr subnetMask;                             ///<Subnet mask
00100    Ipv4Addr defaultGateway;                         ///<Default gateway
00101    Ipv4Addr dnsServer[DHCP_SERVER_MAX_DNS_SERVERS]; ///<DNS servers
00102 } DhcpServerSettings;
00103 
00104 
00105 /**
00106  * @brief DHCP server context
00107  **/
00108 
00109 typedef struct
00110 {
00111    DhcpServerSettings settings;                              ///<DHCP server settings
00112    bool_t running;                                           ///<This flag tells whether the DHCP server is running or not
00113    Ipv4Addr nextIpAddr;                                      ///<Next IP address to be assigned
00114    DhcpServerBinding clientBinding[DHCP_SERVER_MAX_CLIENTS]; ///<List of bindings
00115 } DhcpServerContext;
00116 
00117 
00118 //Tick counter to handle periodic operations
00119 extern systime_t dhcpServerTickCounter;
00120 
00121 //DHCP server related functions
00122 void dhcpServerGetDefaultSettings(DhcpServerSettings *settings);
00123 error_t dhcpServerInit(DhcpServerContext *context, const DhcpServerSettings *settings);
00124 error_t dhcpServerStart(DhcpServerContext *context);
00125 error_t dhcpServerStop(DhcpServerContext *context);
00126 
00127 void dhcpServerTick(DhcpServerContext *context);
00128 
00129 void dhcpServerProcessMessage(NetInterface *interface,
00130    const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader,
00131    const NetBuffer *buffer, size_t offset, void *params);
00132 
00133 void dhcpServerParseDiscover(DhcpServerContext *context,
00134    const DhcpMessage *message, size_t length);
00135 
00136 void dhcpServerParseRequest(DhcpServerContext *context,
00137    const DhcpMessage *message, size_t length);
00138 
00139 void dhcpServerParseDecline(DhcpServerContext *context,
00140    const DhcpMessage *message, size_t length);
00141 
00142 void dhcpServerParseRelease(DhcpServerContext *context,
00143    const DhcpMessage *message, size_t length);
00144 
00145 void dhcpServerParseInform(DhcpServerContext *context,
00146    const DhcpMessage *message, size_t length);
00147 
00148 error_t dhcpServerSendReply(DhcpServerContext *context, uint8_t type,
00149    Ipv4Addr yourIpAddr, const DhcpMessage *request, size_t length);
00150 
00151 DhcpServerBinding *dhcpServerCreateBinding(DhcpServerContext *context);
00152 
00153 DhcpServerBinding *dhcpServerFindBindingByMacAddr(DhcpServerContext *context,
00154    const MacAddr* macAddr);
00155 
00156 DhcpServerBinding *dhcpServerFindBindingByIpAddr(DhcpServerContext *context,
00157    Ipv4Addr ipAddr);
00158 
00159 error_t dhcpServerGetNextIpAddr(DhcpServerContext *context, Ipv4Addr *ipAddr);
00160 
00161 #endif
00162