Simon Cooksey / mbed-os
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dhcp.h Source File

dhcp.h

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * DHCP client API
00004  */
00005 
00006 /*
00007  * Copyright (c) 2001-2004 Leon Woestenberg <leon.woestenberg@gmx.net>
00008  * Copyright (c) 2001-2004 Axon Digital Design B.V., The Netherlands.
00009  * All rights reserved.
00010  *
00011  * Redistribution and use in source and binary forms, with or without modification,
00012  * are permitted provided that the following conditions are met:
00013  *
00014  * 1. Redistributions of source code must retain the above copyright notice,
00015  *    this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright notice,
00017  *    this list of conditions and the following disclaimer in the documentation
00018  *    and/or other materials provided with the distribution.
00019  * 3. The name of the author may not be used to endorse or promote products
00020  *    derived from this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00025  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00027  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00030  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00031  * OF SUCH DAMAGE.
00032  *
00033  * This file is part of the lwIP TCP/IP stack.
00034  *
00035  * Author: Leon Woestenberg <leon.woestenberg@gmx.net>
00036  *
00037  */
00038 #ifndef LWIP_HDR_DHCP_H
00039 #define LWIP_HDR_DHCP_H
00040 
00041 #include "lwip/opt.h"
00042 
00043 #if LWIP_DHCP /* don't build if not configured for use in lwipopts.h */
00044 
00045 #include "lwip/netif.h"
00046 #include "lwip/udp.h"
00047 
00048 #ifdef __cplusplus
00049 extern "C" {
00050 #endif
00051 
00052 /** period (in seconds) of the application calling dhcp_coarse_tmr() */
00053 #define DHCP_COARSE_TIMER_SECS 60
00054 /** period (in milliseconds) of the application calling dhcp_coarse_tmr() */
00055 #define DHCP_COARSE_TIMER_MSECS (DHCP_COARSE_TIMER_SECS * 1000UL)
00056 /** period (in milliseconds) of the application calling dhcp_fine_tmr() */
00057 #define DHCP_FINE_TIMER_MSECS 500
00058 
00059 #define DHCP_CHADDR_LEN 16U
00060 #define DHCP_SNAME_LEN  64U
00061 #define DHCP_FILE_LEN   128U
00062 
00063 struct dhcp
00064 {
00065   /** transaction identifier of last sent request */
00066   u32_t xid;
00067   /** incoming msg */
00068   struct dhcp_msg *msg_in;
00069   /** track PCB allocation state */
00070   u8_t pcb_allocated;
00071   /** current DHCP state machine state */
00072   u8_t state;
00073   /** retries of current request */
00074   u8_t tries;
00075 #if LWIP_DHCP_AUTOIP_COOP
00076   u8_t autoip_coop_state;
00077 #endif
00078   u8_t subnet_mask_given;
00079 
00080   struct pbuf *p_out; /* pbuf of outcoming msg */
00081   struct dhcp_msg *msg_out; /* outgoing msg */
00082   u16_t options_out_len; /* outgoing msg options length */
00083   u16_t request_timeout; /* #ticks with period DHCP_FINE_TIMER_SECS for request timeout */
00084   u16_t t1_timeout;  /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
00085   u16_t t2_timeout;  /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
00086   u16_t t1_renew_time;  /* #ticks with period DHCP_COARSE_TIMER_SECS until next renew try */
00087   u16_t t2_rebind_time; /* #ticks with period DHCP_COARSE_TIMER_SECS until next rebind try */
00088   u16_t lease_used; /* #ticks with period DHCP_COARSE_TIMER_SECS since last received DHCP ack */
00089   u16_t t0_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for lease time */
00090   ip_addr_t server_ip_addr; /* dhcp server address that offered this lease (ip_addr_t because passed to UDP) */
00091   ip4_addr_t offered_ip_addr;
00092   ip4_addr_t offered_sn_mask;
00093   ip4_addr_t offered_gw_addr;
00094 
00095   u32_t offered_t0_lease; /* lease period (in seconds) */
00096   u32_t offered_t1_renew; /* recommended renew time (usually 50% of lease period) */
00097   u32_t offered_t2_rebind; /* recommended rebind time (usually 87.5 of lease period)  */
00098 #if LWIP_DHCP_BOOTP_FILE
00099   ip_addr_t offered_si_addr;
00100   char boot_file_name[DHCP_FILE_LEN];
00101 #endif /* LWIP_DHCP_BOOTPFILE */
00102 };
00103 
00104 /* MUST be compiled with "pack structs" or equivalent! */
00105 #ifdef PACK_STRUCT_USE_INCLUDES
00106 #  include "arch/bpstruct.h"
00107 #endif
00108 PACK_STRUCT_BEGIN
00109 /** minimum set of fields of any DHCP message */
00110 struct dhcp_msg
00111 {
00112   PACK_STRUCT_FLD_8(u8_t op);
00113   PACK_STRUCT_FLD_8(u8_t htype);
00114   PACK_STRUCT_FLD_8(u8_t hlen);
00115   PACK_STRUCT_FLD_8(u8_t hops);
00116   PACK_STRUCT_FIELD(u32_t xid);
00117   PACK_STRUCT_FIELD(u16_t secs);
00118   PACK_STRUCT_FIELD(u16_t flags);
00119   PACK_STRUCT_FLD_S(ip4_addr_p_t ciaddr);
00120   PACK_STRUCT_FLD_S(ip4_addr_p_t yiaddr);
00121   PACK_STRUCT_FLD_S(ip4_addr_p_t siaddr);
00122   PACK_STRUCT_FLD_S(ip4_addr_p_t giaddr);
00123   PACK_STRUCT_FLD_8(u8_t chaddr[DHCP_CHADDR_LEN]);
00124   PACK_STRUCT_FLD_8(u8_t sname[DHCP_SNAME_LEN]);
00125   PACK_STRUCT_FLD_8(u8_t file[DHCP_FILE_LEN]);
00126   PACK_STRUCT_FIELD(u32_t cookie);
00127 #define DHCP_MIN_OPTIONS_LEN 68U
00128 /** make sure user does not configure this too small */
00129 #if ((defined(DHCP_OPTIONS_LEN)) && (DHCP_OPTIONS_LEN < DHCP_MIN_OPTIONS_LEN))
00130 #  undef DHCP_OPTIONS_LEN
00131 #endif
00132 /** allow this to be configured in lwipopts.h, but not too small */
00133 #if (!defined(DHCP_OPTIONS_LEN))
00134 /** set this to be sufficient for your options in outgoing DHCP msgs */
00135 #  define DHCP_OPTIONS_LEN DHCP_MIN_OPTIONS_LEN
00136 #endif
00137   PACK_STRUCT_FLD_8(u8_t options[DHCP_OPTIONS_LEN]);
00138 } PACK_STRUCT_STRUCT;
00139 PACK_STRUCT_END
00140 #ifdef PACK_STRUCT_USE_INCLUDES
00141 #  include "arch/epstruct.h"
00142 #endif
00143 
00144 void dhcp_set_struct(struct netif *netif, struct dhcp *dhcp);
00145 /** Remove a struct dhcp previously set to the netif using dhcp_set_struct() */
00146 #define dhcp_remove_struct(netif) do { (netif)->dhcp = NULL; } while(0)
00147 void dhcp_cleanup(struct netif *netif);
00148 /** start DHCP configuration */
00149 err_t dhcp_start(struct netif *netif);
00150 /** enforce early lease renewal (not needed normally)*/
00151 err_t dhcp_renew(struct netif *netif);
00152 /** release the DHCP lease, usually called before dhcp_stop()*/
00153 err_t dhcp_release(struct netif *netif);
00154 /** stop DHCP configuration */
00155 void dhcp_stop(struct netif *netif);
00156 /** inform server of our manual IP address */
00157 void dhcp_inform(struct netif *netif);
00158 /** Handle a possible change in the network configuration */
00159 void dhcp_network_changed(struct netif *netif);
00160 
00161 /** if enabled, check whether the offered IP address is not in use, using ARP */
00162 #if DHCP_DOES_ARP_CHECK
00163 void dhcp_arp_reply(struct netif *netif, const ip4_addr_t *addr);
00164 #endif
00165 
00166 /** check if DHCP supplied netif->ip_addr */
00167 u8_t dhcp_supplied_address(const struct netif *netif);
00168 
00169 /** to be called every minute */
00170 void dhcp_coarse_tmr(void);
00171 /** to be called every half second */
00172 void dhcp_fine_tmr(void);
00173 
00174 /** DHCP message item offsets and length */
00175 #define DHCP_OP_OFS       0
00176 #define DHCP_HTYPE_OFS    1
00177 #define DHCP_HLEN_OFS     2
00178 #define DHCP_HOPS_OFS     3
00179 #define DHCP_XID_OFS      4
00180 #define DHCP_SECS_OFS     8
00181 #define DHCP_FLAGS_OFS    10
00182 #define DHCP_CIADDR_OFS   12
00183 #define DHCP_YIADDR_OFS   16
00184 #define DHCP_SIADDR_OFS   20
00185 #define DHCP_GIADDR_OFS   24
00186 #define DHCP_CHADDR_OFS   28
00187 #define DHCP_SNAME_OFS    44
00188 #define DHCP_FILE_OFS     108
00189 #define DHCP_MSG_LEN      236
00190 
00191 #define DHCP_COOKIE_OFS   DHCP_MSG_LEN
00192 #define DHCP_OPTIONS_OFS  (DHCP_MSG_LEN + 4)
00193 
00194 #define DHCP_CLIENT_PORT  68
00195 #define DHCP_SERVER_PORT  67
00196 
00197 /** DHCP client states */
00198 #define DHCP_STATE_OFF          0
00199 #define DHCP_STATE_REQUESTING   1
00200 #define DHCP_STATE_INIT         2
00201 #define DHCP_STATE_REBOOTING    3
00202 #define DHCP_STATE_REBINDING    4
00203 #define DHCP_STATE_RENEWING     5
00204 #define DHCP_STATE_SELECTING    6
00205 #define DHCP_STATE_INFORMING    7
00206 #define DHCP_STATE_CHECKING     8
00207 /** not yet implemented \#define DHCP_STATE_PERMANENT 9 */
00208 #define DHCP_STATE_BOUND        10
00209 /** not yet implemented \#define DHCP_STATE_RELEASING 11 */
00210 #define DHCP_STATE_BACKING_OFF  12
00211 
00212 /** AUTOIP cooperation flags */
00213 #define DHCP_AUTOIP_COOP_STATE_OFF  0
00214 #define DHCP_AUTOIP_COOP_STATE_ON   1
00215 
00216 #define DHCP_BOOTREQUEST  1
00217 #define DHCP_BOOTREPLY    2
00218 
00219 /** DHCP message types */
00220 #define DHCP_DISCOVER 1
00221 #define DHCP_OFFER    2
00222 #define DHCP_REQUEST  3
00223 #define DHCP_DECLINE  4
00224 #define DHCP_ACK      5
00225 #define DHCP_NAK      6
00226 #define DHCP_RELEASE  7
00227 #define DHCP_INFORM   8
00228 
00229 /** DHCP hardware type, currently only ethernet is supported */
00230 #define DHCP_HTYPE_ETH 1
00231 
00232 #define DHCP_MAGIC_COOKIE 0x63825363UL
00233 
00234 /* This is a list of options for BOOTP and DHCP, see RFC 2132 for descriptions */
00235 
00236 /** BootP options */
00237 #define DHCP_OPTION_PAD 0
00238 #define DHCP_OPTION_SUBNET_MASK 1 /* RFC 2132 3.3 */
00239 #define DHCP_OPTION_ROUTER 3
00240 #define DHCP_OPTION_DNS_SERVER 6
00241 #define DHCP_OPTION_HOSTNAME 12
00242 #define DHCP_OPTION_IP_TTL 23
00243 #define DHCP_OPTION_MTU 26
00244 #define DHCP_OPTION_BROADCAST 28
00245 #define DHCP_OPTION_TCP_TTL 37
00246 #define DHCP_OPTION_NTP 42
00247 #define DHCP_OPTION_END 255
00248 
00249 /** DHCP options */
00250 #define DHCP_OPTION_REQUESTED_IP 50 /* RFC 2132 9.1, requested IP address */
00251 #define DHCP_OPTION_LEASE_TIME 51 /* RFC 2132 9.2, time in seconds, in 4 bytes */
00252 #define DHCP_OPTION_OVERLOAD 52 /* RFC2132 9.3, use file and/or sname field for options */
00253 
00254 #define DHCP_OPTION_MESSAGE_TYPE 53 /* RFC 2132 9.6, important for DHCP */
00255 #define DHCP_OPTION_MESSAGE_TYPE_LEN 1
00256 
00257 #define DHCP_OPTION_SERVER_ID 54 /* RFC 2132 9.7, server IP address */
00258 #define DHCP_OPTION_PARAMETER_REQUEST_LIST 55 /* RFC 2132 9.8, requested option types */
00259 
00260 #define DHCP_OPTION_MAX_MSG_SIZE 57 /* RFC 2132 9.10, message size accepted >= 576 */
00261 #define DHCP_OPTION_MAX_MSG_SIZE_LEN 2
00262 
00263 #define DHCP_OPTION_T1 58 /* T1 renewal time */
00264 #define DHCP_OPTION_T2 59 /* T2 rebinding time */
00265 #define DHCP_OPTION_US 60
00266 #define DHCP_OPTION_CLIENT_ID 61
00267 #define DHCP_OPTION_TFTP_SERVERNAME 66
00268 #define DHCP_OPTION_BOOTFILE 67
00269 
00270 /** possible combinations of overloading the file and sname fields with options */
00271 #define DHCP_OVERLOAD_NONE 0
00272 #define DHCP_OVERLOAD_FILE 1
00273 #define DHCP_OVERLOAD_SNAME  2
00274 #define DHCP_OVERLOAD_SNAME_FILE 3
00275 
00276 #if LWIP_DHCP_GET_NTP_SRV
00277 /** This function must exist, in other to add offered NTP servers to
00278  * the NTP (or SNTP) engine.
00279  * See LWIP_DHCP_MAX_NTP_SERVERS */
00280 extern void dhcp_set_ntp_servers(u8_t num_ntp_servers, const ip4_addr_t* ntp_server_addrs);
00281 #endif /* LWIP_DHCP_GET_NTP_SRV */
00282 
00283 #ifdef __cplusplus
00284 }
00285 #endif
00286 
00287 #endif /* LWIP_DHCP */
00288 
00289 #endif /*LWIP_HDR_DHCP_H*/