Webserver+3d print

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dhcp_client.h Source File

dhcp_client.h

Go to the documentation of this file.
00001 /**
00002  * @file dhcp_client.h
00003  * @brief DHCP client (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_CLIENT_H
00030 #define _DHCP_CLIENT_H
00031 
00032 //Dependencies
00033 #include "core/net.h"
00034 #include "core/socket.h"
00035 #include "core/udp.h"
00036 #include "dhcp/dhcp_common.h"
00037 
00038 //DHCP client support
00039 #ifndef DHCP_CLIENT_SUPPORT
00040    #define DHCP_CLIENT_SUPPORT ENABLED
00041 #elif (DHCP_CLIENT_SUPPORT != ENABLED && DHCP_CLIENT_SUPPORT != DISABLED)
00042    #error DHCP_CLIENT_SUPPORT parameter is not valid
00043 #endif
00044 
00045 //DHCP client tick interval
00046 #ifndef DHCP_CLIENT_TICK_INTERVAL
00047    #define DHCP_CLIENT_TICK_INTERVAL 200
00048 #elif (DHCP_CLIENT_TICK_INTERVAL < 10)
00049    #error DHCP_CLIENT_TICK_INTERVAL parameter is not valid
00050 #endif
00051 
00052 //Maximum length of host name
00053 #ifndef DHCP_CLIENT_MAX_HOSTNAME_LEN
00054    #define DHCP_CLIENT_MAX_HOSTNAME_LEN 15
00055 #elif (DHCP_CLIENT_MAX_HOSTNAME_LEN < 1)
00056    #error DHCP_CLIENT_MAX_HOSTNAME_LEN parameter is not valid
00057 #endif
00058 
00059 //Random delay before sending the first message
00060 #ifndef DHCP_CLIENT_INIT_DELAY
00061    #define DHCP_CLIENT_INIT_DELAY 2000
00062 #elif (DHCP_CLIENT_INIT_DELAY < 0)
00063    #error DHCP_CLIENT_INIT_DELAY parameter is not valid
00064 #endif
00065 
00066 //Initial retransmission timeout (DHCPDISCOVER)
00067 #ifndef DHCP_CLIENT_DISCOVER_INIT_RT
00068    #define DHCP_CLIENT_DISCOVER_INIT_RT 4000
00069 #elif (DHCP_CLIENT_DISCOVER_INIT_RT < 1000)
00070    #error DHCP_CLIENT_DISCOVER_INIT_RT parameter is not valid
00071 #endif
00072 
00073 //Maximum retransmission timeout (DHCPDISCOVER)
00074 #ifndef DHCP_CLIENT_DISCOVER_MAX_RT
00075    #define DHCP_CLIENT_DISCOVER_MAX_RT 16000
00076 #elif (DHCP_CLIENT_DISCOVER_MAX_RT < 1000)
00077    #error DHCP_CLIENT_DISCOVER_MAX_RT parameter is not valid
00078 #endif
00079 
00080 //Maximum retransmission count (DHCPREQUEST)
00081 #ifndef DHCP_CLIENT_REQUEST_MAX_RC
00082    #define DHCP_CLIENT_REQUEST_MAX_RC 4
00083 #elif (DHCP_CLIENT_REQUEST_MAX_RC < 1)
00084    #error DHCP_CLIENT_REQUEST_MAX_RC parameter is not valid
00085 #endif
00086 
00087 //Initial retransmission timeout (DHCPREQUEST)
00088 #ifndef DHCP_CLIENT_REQUEST_INIT_RT
00089    #define DHCP_CLIENT_REQUEST_INIT_RT 4000
00090 #elif (DHCP_CLIENT_REQUEST_INIT_RT < 1000)
00091    #error DHCP_CLIENT_REQUEST_INIT_RT parameter is not valid
00092 #endif
00093 
00094 //Maximum retransmission timeout (DHCPREQUEST)
00095 #ifndef DHCP_CLIENT_REQUEST_MAX_RT
00096    #define DHCP_CLIENT_REQUEST_MAX_RT 64000
00097 #elif (DHCP_CLIENT_REQUEST_MAX_RT < 1000)
00098    #error DHCP_CLIENT_REQUEST_MAX_RT parameter is not valid
00099 #endif
00100 
00101 //Minimum delay between DHCPREQUEST messages in RENEWING and REBINDING states
00102 #ifndef DHCP_CLIENT_REQUEST_MIN_DELAY
00103    #define DHCP_CLIENT_REQUEST_MIN_DELAY 60000
00104 #elif (DHCP_CLIENT_REQUEST_MIN_DELAY < 1000)
00105    #error DHCP_CLIENT_REQUEST_MIN_DELAY parameter is not valid
00106 #endif
00107 
00108 //Number of probe packets
00109 #ifndef DHCP_CLIENT_PROBE_NUM
00110    #define DHCP_CLIENT_PROBE_NUM 1
00111 #elif (DHCP_CLIENT_PROBE_NUM < 0)
00112    #error DHCP_CLIENT_PROBE_NUM parameter is not valid
00113 #endif
00114 
00115 //Delay until repeated probe
00116 #ifndef DHCP_CLIENT_PROBE_DELAY
00117    #define DHCP_CLIENT_PROBE_DELAY 1000
00118 #elif (DHCP_CLIENT_PROBE_DELAY < 100)
00119    #error DHCP_CLIENT_PROBE_DELAY parameter is not valid
00120 #endif
00121 
00122 //Random factor used to determine the delay between retransmissions
00123 #ifndef DHCP_CLIENT_RAND_FACTOR
00124    #define DHCP_CLIENT_RAND_FACTOR 1000
00125 #elif (DHCP_CLIENT_RAND_FACTOR < 100)
00126    #error DHCP_CLIENT_RAND_FACTOR parameter is not valid
00127 #endif
00128 
00129 //Forward declaration of DhcpClientContext structure
00130 struct _DhcpClientContext;
00131 #define DhcpClientContext struct _DhcpClientContext
00132 
00133 
00134 /**
00135  * @brief DHCP FSM states
00136  **/
00137 
00138 typedef enum
00139 {
00140    DHCP_STATE_INIT        = 0,
00141    DHCP_STATE_SELECTING   = 1,
00142    DHCP_STATE_REQUESTING  = 2,
00143    DHCP_STATE_INIT_REBOOT = 3,
00144    DHCP_STATE_REBOOTING   = 4,
00145    DHCP_STATE_PROBING     = 5,
00146    DHCP_STATE_BOUND       = 6,
00147    DHCP_STATE_RENEWING    = 7,
00148    DHCP_STATE_REBINDING   = 8
00149 } DhcpState;
00150 
00151 
00152 /**
00153  * @brief DHCP configuration timeout callback
00154  **/
00155 
00156 typedef void (*DhcpTimeoutCallback)(DhcpClientContext *context,
00157    NetInterface *interface);
00158 
00159 
00160 /**
00161  * @brief Link state change callback
00162  **/
00163 
00164 typedef void (*DhcpLinkChangeCallback)(DhcpClientContext *context,
00165    NetInterface *interface, bool_t linkState);
00166 
00167 
00168 /**
00169  * @brief FSM state change callback
00170  **/
00171 
00172 typedef void (*DhcpStateChangeCallback)(DhcpClientContext *context,
00173    NetInterface *interface, DhcpState state);
00174 
00175 
00176 /**
00177  * @brief DHCP client settings
00178  **/
00179 
00180 typedef struct
00181 {
00182    NetInterface *interface;                           ///<Network interface to configure
00183    char_t hostname[DHCP_CLIENT_MAX_HOSTNAME_LEN + 1]; //Host name
00184    bool_t rapidCommit;                                ///<Quick configuration using rapid commit
00185    bool_t manualDnsConfig;                            ///<Force manual DNS configuration
00186    systime_t timeout;                                 ///<DHCP configuration timeout
00187    DhcpTimeoutCallback timeoutEvent;                  ///<DHCP configuration timeout event
00188    DhcpLinkChangeCallback linkChangeEvent;            ///<Link state change event
00189    DhcpStateChangeCallback stateChangeEvent;          ///<FSM state change event
00190 } DhcpClientSettings;
00191 
00192 
00193 /**
00194  * @brief DHCP client context
00195  **/
00196 
00197 struct _DhcpClientContext
00198 {
00199    DhcpClientSettings settings; ///<DHCP client settings
00200    bool_t running;              ///<This flag tells whether the DHCP client is running or not
00201    DhcpState state;             ///<Current state of the FSM
00202    bool_t timeoutEventDone;     ///<Timeout callback function has been called
00203    systime_t timestamp;         ///<Timestamp to manage retransmissions
00204    systime_t timeout;           ///<Timeout value
00205    systime_t retransmitTimeout; ///<Retransmission timeout
00206    uint_t retransmitCount;      ///<Retransmission counter
00207    Ipv4Addr serverIpAddr;       ///<DHCP server IPv4 address
00208    Ipv4Addr requestedIpAddr;    ///<Requested IPv4 address
00209    uint32_t transactionId;      ///<Value to match requests with replies
00210    systime_t configStartTime;   ///<Address acquisition or renewal process start time
00211    systime_t leaseStartTime;    ///<Lease start time
00212    uint32_t leaseTime;          ///<Lease time
00213    uint32_t t1;                 ///<Time at which the client enters the RENEWING state
00214    uint32_t t2;                 ///<Time at which the client enters the REBINDING state
00215 };
00216 
00217 
00218 //Tick counter to handle periodic operations
00219 extern systime_t dhcpClientTickCounter;
00220 
00221 //DHCP client related functions
00222 void dhcpClientGetDefaultSettings(DhcpClientSettings *settings);
00223 error_t dhcpClientInit(DhcpClientContext *context, const DhcpClientSettings *settings);
00224 error_t dhcpClientStart(DhcpClientContext *context);
00225 error_t dhcpClientStop(DhcpClientContext *context);
00226 DhcpState dhcpClientGetState(DhcpClientContext *context);
00227 
00228 void dhcpClientTick(DhcpClientContext *context);
00229 void dhcpClientLinkChangeEvent(DhcpClientContext *context);
00230 
00231 void dhcpClientStateInit(DhcpClientContext *context);
00232 void dhcpClientStateSelecting(DhcpClientContext *context);
00233 void dhcpClientStateRequesting(DhcpClientContext *context);
00234 void dhcpClientStateInitReboot(DhcpClientContext *context);
00235 void dhcpClientStateRebooting(DhcpClientContext *context);
00236 void dhcpClientStateProbing(DhcpClientContext *context);
00237 void dhcpClientStateBound(DhcpClientContext *context);
00238 void dhcpClientStateRenewing(DhcpClientContext *context);
00239 void dhcpClientStateRebinding(DhcpClientContext *context);
00240 
00241 error_t dhcpClientSendDiscover(DhcpClientContext *context);
00242 error_t dhcpClientSendRequest(DhcpClientContext *context);
00243 error_t dhcpClientSendDecline(DhcpClientContext *context);
00244 
00245 void dhcpClientProcessMessage(NetInterface *interface,
00246    const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader,
00247    const NetBuffer *buffer, size_t offset, void *params);
00248 
00249 void dhcpClientParseOffer(DhcpClientContext *context,
00250    const DhcpMessage *message, size_t length);
00251 
00252 void dhcpClientParseAck(DhcpClientContext *context,
00253    const DhcpMessage *message, size_t length);
00254 
00255 void dhcpClientParseNak(DhcpClientContext *context,
00256    const DhcpMessage *message, size_t length);
00257 
00258 void dhcpClientCheckTimeout(DhcpClientContext *context);
00259 
00260 uint16_t dhcpClientComputeElapsedTime(DhcpClientContext *context);
00261 
00262 void dhcpClientChangeState(DhcpClientContext *context,
00263    DhcpState newState, systime_t delay);
00264 
00265 void dhcpClientDumpConfig(DhcpClientContext *context);
00266 
00267 #endif
00268