Dependents:   RTnoV3 RTnoV3_LED RTnoV3_Template RTnoV3_ADC ... more

Committer:
sherckuith
Date:
Sat Dec 31 11:25:27 2011 +0000
Revision:
0:479ce5546098
Ethernet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sherckuith 0:479ce5546098 1 /** @file
sherckuith 0:479ce5546098 2 */
sherckuith 0:479ce5546098 3
sherckuith 0:479ce5546098 4 #ifndef __LWIP_DHCP_H__
sherckuith 0:479ce5546098 5 #define __LWIP_DHCP_H__
sherckuith 0:479ce5546098 6
sherckuith 0:479ce5546098 7 #include "lwip/opt.h"
sherckuith 0:479ce5546098 8
sherckuith 0:479ce5546098 9 #if LWIP_DHCP /* don't build if not configured for use in lwipopts.h */
sherckuith 0:479ce5546098 10
sherckuith 0:479ce5546098 11 #include "lwip/netif.h"
sherckuith 0:479ce5546098 12 #include "lwip/udp.h"
sherckuith 0:479ce5546098 13
sherckuith 0:479ce5546098 14 #ifdef __cplusplus
sherckuith 0:479ce5546098 15 extern "C" {
sherckuith 0:479ce5546098 16 #endif
sherckuith 0:479ce5546098 17
sherckuith 0:479ce5546098 18 /** period (in seconds) of the application calling dhcp_coarse_tmr() */
sherckuith 0:479ce5546098 19 #define DHCP_COARSE_TIMER_SECS 60
sherckuith 0:479ce5546098 20 /** period (in milliseconds) of the application calling dhcp_coarse_tmr() */
sherckuith 0:479ce5546098 21 #define DHCP_COARSE_TIMER_MSECS (DHCP_COARSE_TIMER_SECS * 1000UL)
sherckuith 0:479ce5546098 22 /** period (in milliseconds) of the application calling dhcp_fine_tmr() */
sherckuith 0:479ce5546098 23 #define DHCP_FINE_TIMER_MSECS 500
sherckuith 0:479ce5546098 24
sherckuith 0:479ce5546098 25 #define DHCP_CHADDR_LEN 16U
sherckuith 0:479ce5546098 26 #define DHCP_SNAME_LEN 64U
sherckuith 0:479ce5546098 27 #define DHCP_FILE_LEN 128U
sherckuith 0:479ce5546098 28
sherckuith 0:479ce5546098 29 struct dhcp
sherckuith 0:479ce5546098 30 {
sherckuith 0:479ce5546098 31 /** transaction identifier of last sent request */
sherckuith 0:479ce5546098 32 u32_t xid;
sherckuith 0:479ce5546098 33 /** our connection to the DHCP server */
sherckuith 0:479ce5546098 34 struct udp_pcb *pcb;
sherckuith 0:479ce5546098 35 /** incoming msg */
sherckuith 0:479ce5546098 36 struct dhcp_msg *msg_in;
sherckuith 0:479ce5546098 37 /** current DHCP state machine state */
sherckuith 0:479ce5546098 38 u8_t state;
sherckuith 0:479ce5546098 39 /** retries of current request */
sherckuith 0:479ce5546098 40 u8_t tries;
sherckuith 0:479ce5546098 41 #if LWIP_DHCP_AUTOIP_COOP
sherckuith 0:479ce5546098 42 u8_t autoip_coop_state;
sherckuith 0:479ce5546098 43 #endif
sherckuith 0:479ce5546098 44 u8_t subnet_mask_given;
sherckuith 0:479ce5546098 45
sherckuith 0:479ce5546098 46 struct pbuf *p_out; /* pbuf of outcoming msg */
sherckuith 0:479ce5546098 47 struct dhcp_msg *msg_out; /* outgoing msg */
sherckuith 0:479ce5546098 48 u16_t options_out_len; /* outgoing msg options length */
sherckuith 0:479ce5546098 49 u16_t request_timeout; /* #ticks with period DHCP_FINE_TIMER_SECS for request timeout */
sherckuith 0:479ce5546098 50 u16_t t1_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
sherckuith 0:479ce5546098 51 u16_t t2_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
sherckuith 0:479ce5546098 52 ip_addr_t server_ip_addr; /* dhcp server address that offered this lease */
sherckuith 0:479ce5546098 53 ip_addr_t offered_ip_addr;
sherckuith 0:479ce5546098 54 ip_addr_t offered_sn_mask;
sherckuith 0:479ce5546098 55 ip_addr_t offered_gw_addr;
sherckuith 0:479ce5546098 56
sherckuith 0:479ce5546098 57 u32_t offered_t0_lease; /* lease period (in seconds) */
sherckuith 0:479ce5546098 58 u32_t offered_t1_renew; /* recommended renew time (usually 50% of lease period) */
sherckuith 0:479ce5546098 59 u32_t offered_t2_rebind; /* recommended rebind time (usually 66% of lease period) */
sherckuith 0:479ce5546098 60 /* @todo: LWIP_DHCP_BOOTP_FILE configuration option?
sherckuith 0:479ce5546098 61 integrate with possible TFTP-client for booting? */
sherckuith 0:479ce5546098 62 #if LWIP_DHCP_BOOTP_FILE
sherckuith 0:479ce5546098 63 ip_addr_t offered_si_addr;
sherckuith 0:479ce5546098 64 char boot_file_name[DHCP_FILE_LEN];
sherckuith 0:479ce5546098 65 #endif /* LWIP_DHCP_BOOTPFILE */
sherckuith 0:479ce5546098 66 };
sherckuith 0:479ce5546098 67
sherckuith 0:479ce5546098 68 /* MUST be compiled with "pack structs" or equivalent! */
sherckuith 0:479ce5546098 69 #ifdef PACK_STRUCT_USE_INCLUDES
sherckuith 0:479ce5546098 70 # include "arch/bpstruct.h"
sherckuith 0:479ce5546098 71 #endif
sherckuith 0:479ce5546098 72 PACK_STRUCT_BEGIN
sherckuith 0:479ce5546098 73 /** minimum set of fields of any DHCP message */
sherckuith 0:479ce5546098 74 struct dhcp_msg
sherckuith 0:479ce5546098 75 {
sherckuith 0:479ce5546098 76 PACK_STRUCT_FIELD(u8_t op);
sherckuith 0:479ce5546098 77 PACK_STRUCT_FIELD(u8_t htype);
sherckuith 0:479ce5546098 78 PACK_STRUCT_FIELD(u8_t hlen);
sherckuith 0:479ce5546098 79 PACK_STRUCT_FIELD(u8_t hops);
sherckuith 0:479ce5546098 80 PACK_STRUCT_FIELD(u32_t xid);
sherckuith 0:479ce5546098 81 PACK_STRUCT_FIELD(u16_t secs);
sherckuith 0:479ce5546098 82 PACK_STRUCT_FIELD(u16_t flags);
sherckuith 0:479ce5546098 83 PACK_STRUCT_FIELD(ip_addr_p_t ciaddr);
sherckuith 0:479ce5546098 84 PACK_STRUCT_FIELD(ip_addr_p_t yiaddr);
sherckuith 0:479ce5546098 85 PACK_STRUCT_FIELD(ip_addr_p_t siaddr);
sherckuith 0:479ce5546098 86 PACK_STRUCT_FIELD(ip_addr_p_t giaddr);
sherckuith 0:479ce5546098 87 PACK_STRUCT_FIELD(u8_t chaddr[DHCP_CHADDR_LEN]);
sherckuith 0:479ce5546098 88 PACK_STRUCT_FIELD(u8_t sname[DHCP_SNAME_LEN]);
sherckuith 0:479ce5546098 89 PACK_STRUCT_FIELD(u8_t file[DHCP_FILE_LEN]);
sherckuith 0:479ce5546098 90 PACK_STRUCT_FIELD(u32_t cookie);
sherckuith 0:479ce5546098 91 #define DHCP_MIN_OPTIONS_LEN 68U
sherckuith 0:479ce5546098 92 /** make sure user does not configure this too small */
sherckuith 0:479ce5546098 93 #if ((defined(DHCP_OPTIONS_LEN)) && (DHCP_OPTIONS_LEN < DHCP_MIN_OPTIONS_LEN))
sherckuith 0:479ce5546098 94 # undef DHCP_OPTIONS_LEN
sherckuith 0:479ce5546098 95 #endif
sherckuith 0:479ce5546098 96 /** allow this to be configured in lwipopts.h, but not too small */
sherckuith 0:479ce5546098 97 #if (!defined(DHCP_OPTIONS_LEN))
sherckuith 0:479ce5546098 98 /** set this to be sufficient for your options in outgoing DHCP msgs */
sherckuith 0:479ce5546098 99 # define DHCP_OPTIONS_LEN DHCP_MIN_OPTIONS_LEN
sherckuith 0:479ce5546098 100 #endif
sherckuith 0:479ce5546098 101 PACK_STRUCT_FIELD(u8_t options[DHCP_OPTIONS_LEN]);
sherckuith 0:479ce5546098 102 } PACK_STRUCT_STRUCT;
sherckuith 0:479ce5546098 103 PACK_STRUCT_END
sherckuith 0:479ce5546098 104 #ifdef PACK_STRUCT_USE_INCLUDES
sherckuith 0:479ce5546098 105 # include "arch/epstruct.h"
sherckuith 0:479ce5546098 106 #endif
sherckuith 0:479ce5546098 107
sherckuith 0:479ce5546098 108 void dhcp_set_struct(struct netif *netif, struct dhcp *dhcp);
sherckuith 0:479ce5546098 109 /** start DHCP configuration */
sherckuith 0:479ce5546098 110 err_t dhcp_start(struct netif *netif);
sherckuith 0:479ce5546098 111 /** enforce early lease renewal (not needed normally)*/
sherckuith 0:479ce5546098 112 err_t dhcp_renew(struct netif *netif);
sherckuith 0:479ce5546098 113 /** release the DHCP lease, usually called before dhcp_stop()*/
sherckuith 0:479ce5546098 114 err_t dhcp_release(struct netif *netif);
sherckuith 0:479ce5546098 115 /** stop DHCP configuration */
sherckuith 0:479ce5546098 116 void dhcp_stop(struct netif *netif);
sherckuith 0:479ce5546098 117 /** inform server of our manual IP address */
sherckuith 0:479ce5546098 118 void dhcp_inform(struct netif *netif);
sherckuith 0:479ce5546098 119 /** Handle a possible change in the network configuration */
sherckuith 0:479ce5546098 120 void dhcp_network_changed(struct netif *netif);
sherckuith 0:479ce5546098 121
sherckuith 0:479ce5546098 122 /** if enabled, check whether the offered IP address is not in use, using ARP */
sherckuith 0:479ce5546098 123 #if DHCP_DOES_ARP_CHECK
sherckuith 0:479ce5546098 124 void dhcp_arp_reply(struct netif *netif, ip_addr_t *addr);
sherckuith 0:479ce5546098 125 #endif
sherckuith 0:479ce5546098 126
sherckuith 0:479ce5546098 127 /** to be called every minute */
sherckuith 0:479ce5546098 128 void dhcp_coarse_tmr(void);
sherckuith 0:479ce5546098 129 /** to be called every half second */
sherckuith 0:479ce5546098 130 void dhcp_fine_tmr(void);
sherckuith 0:479ce5546098 131
sherckuith 0:479ce5546098 132 /** DHCP message item offsets and length */
sherckuith 0:479ce5546098 133 #define DHCP_OP_OFS 0
sherckuith 0:479ce5546098 134 #define DHCP_HTYPE_OFS 1
sherckuith 0:479ce5546098 135 #define DHCP_HLEN_OFS 2
sherckuith 0:479ce5546098 136 #define DHCP_HOPS_OFS 3
sherckuith 0:479ce5546098 137 #define DHCP_XID_OFS 4
sherckuith 0:479ce5546098 138 #define DHCP_SECS_OFS 8
sherckuith 0:479ce5546098 139 #define DHCP_FLAGS_OFS 10
sherckuith 0:479ce5546098 140 #define DHCP_CIADDR_OFS 12
sherckuith 0:479ce5546098 141 #define DHCP_YIADDR_OFS 16
sherckuith 0:479ce5546098 142 #define DHCP_SIADDR_OFS 20
sherckuith 0:479ce5546098 143 #define DHCP_GIADDR_OFS 24
sherckuith 0:479ce5546098 144 #define DHCP_CHADDR_OFS 28
sherckuith 0:479ce5546098 145 #define DHCP_SNAME_OFS 44
sherckuith 0:479ce5546098 146 #define DHCP_FILE_OFS 108
sherckuith 0:479ce5546098 147 #define DHCP_MSG_LEN 236
sherckuith 0:479ce5546098 148
sherckuith 0:479ce5546098 149 #define DHCP_COOKIE_OFS DHCP_MSG_LEN
sherckuith 0:479ce5546098 150 #define DHCP_OPTIONS_OFS (DHCP_MSG_LEN + 4)
sherckuith 0:479ce5546098 151
sherckuith 0:479ce5546098 152 #define DHCP_CLIENT_PORT 68
sherckuith 0:479ce5546098 153 #define DHCP_SERVER_PORT 67
sherckuith 0:479ce5546098 154
sherckuith 0:479ce5546098 155 /** DHCP client states */
sherckuith 0:479ce5546098 156 #define DHCP_OFF 0
sherckuith 0:479ce5546098 157 #define DHCP_REQUESTING 1
sherckuith 0:479ce5546098 158 #define DHCP_INIT 2
sherckuith 0:479ce5546098 159 #define DHCP_REBOOTING 3
sherckuith 0:479ce5546098 160 #define DHCP_REBINDING 4
sherckuith 0:479ce5546098 161 #define DHCP_RENEWING 5
sherckuith 0:479ce5546098 162 #define DHCP_SELECTING 6
sherckuith 0:479ce5546098 163 #define DHCP_INFORMING 7
sherckuith 0:479ce5546098 164 #define DHCP_CHECKING 8
sherckuith 0:479ce5546098 165 #define DHCP_PERMANENT 9
sherckuith 0:479ce5546098 166 #define DHCP_BOUND 10
sherckuith 0:479ce5546098 167 /** not yet implemented #define DHCP_RELEASING 11 */
sherckuith 0:479ce5546098 168 #define DHCP_BACKING_OFF 12
sherckuith 0:479ce5546098 169
sherckuith 0:479ce5546098 170 /** AUTOIP cooperatation flags */
sherckuith 0:479ce5546098 171 #define DHCP_AUTOIP_COOP_STATE_OFF 0
sherckuith 0:479ce5546098 172 #define DHCP_AUTOIP_COOP_STATE_ON 1
sherckuith 0:479ce5546098 173
sherckuith 0:479ce5546098 174 #define DHCP_BOOTREQUEST 1
sherckuith 0:479ce5546098 175 #define DHCP_BOOTREPLY 2
sherckuith 0:479ce5546098 176
sherckuith 0:479ce5546098 177 /** DHCP message types */
sherckuith 0:479ce5546098 178 #define DHCP_DISCOVER 1
sherckuith 0:479ce5546098 179 #define DHCP_OFFER 2
sherckuith 0:479ce5546098 180 #define DHCP_REQUEST 3
sherckuith 0:479ce5546098 181 #define DHCP_DECLINE 4
sherckuith 0:479ce5546098 182 #define DHCP_ACK 5
sherckuith 0:479ce5546098 183 #define DHCP_NAK 6
sherckuith 0:479ce5546098 184 #define DHCP_RELEASE 7
sherckuith 0:479ce5546098 185 #define DHCP_INFORM 8
sherckuith 0:479ce5546098 186
sherckuith 0:479ce5546098 187 /** DHCP hardware type, currently only ethernet is supported */
sherckuith 0:479ce5546098 188 #define DHCP_HTYPE_ETH 1
sherckuith 0:479ce5546098 189
sherckuith 0:479ce5546098 190 #define DHCP_MAGIC_COOKIE 0x63825363UL
sherckuith 0:479ce5546098 191
sherckuith 0:479ce5546098 192 /* This is a list of options for BOOTP and DHCP, see RFC 2132 for descriptions */
sherckuith 0:479ce5546098 193
sherckuith 0:479ce5546098 194 /** BootP options */
sherckuith 0:479ce5546098 195 #define DHCP_OPTION_PAD 0
sherckuith 0:479ce5546098 196 #define DHCP_OPTION_SUBNET_MASK 1 /* RFC 2132 3.3 */
sherckuith 0:479ce5546098 197 #define DHCP_OPTION_ROUTER 3
sherckuith 0:479ce5546098 198 #define DHCP_OPTION_DNS_SERVER 6
sherckuith 0:479ce5546098 199 #define DHCP_OPTION_HOSTNAME 12
sherckuith 0:479ce5546098 200 #define DHCP_OPTION_IP_TTL 23
sherckuith 0:479ce5546098 201 #define DHCP_OPTION_MTU 26
sherckuith 0:479ce5546098 202 #define DHCP_OPTION_BROADCAST 28
sherckuith 0:479ce5546098 203 #define DHCP_OPTION_TCP_TTL 37
sherckuith 0:479ce5546098 204 #define DHCP_OPTION_END 255
sherckuith 0:479ce5546098 205
sherckuith 0:479ce5546098 206 /** DHCP options */
sherckuith 0:479ce5546098 207 #define DHCP_OPTION_REQUESTED_IP 50 /* RFC 2132 9.1, requested IP address */
sherckuith 0:479ce5546098 208 #define DHCP_OPTION_LEASE_TIME 51 /* RFC 2132 9.2, time in seconds, in 4 bytes */
sherckuith 0:479ce5546098 209 #define DHCP_OPTION_OVERLOAD 52 /* RFC2132 9.3, use file and/or sname field for options */
sherckuith 0:479ce5546098 210
sherckuith 0:479ce5546098 211 #define DHCP_OPTION_MESSAGE_TYPE 53 /* RFC 2132 9.6, important for DHCP */
sherckuith 0:479ce5546098 212 #define DHCP_OPTION_MESSAGE_TYPE_LEN 1
sherckuith 0:479ce5546098 213
sherckuith 0:479ce5546098 214 #define DHCP_OPTION_SERVER_ID 54 /* RFC 2132 9.7, server IP address */
sherckuith 0:479ce5546098 215 #define DHCP_OPTION_PARAMETER_REQUEST_LIST 55 /* RFC 2132 9.8, requested option types */
sherckuith 0:479ce5546098 216
sherckuith 0:479ce5546098 217 #define DHCP_OPTION_MAX_MSG_SIZE 57 /* RFC 2132 9.10, message size accepted >= 576 */
sherckuith 0:479ce5546098 218 #define DHCP_OPTION_MAX_MSG_SIZE_LEN 2
sherckuith 0:479ce5546098 219
sherckuith 0:479ce5546098 220 #define DHCP_OPTION_T1 58 /* T1 renewal time */
sherckuith 0:479ce5546098 221 #define DHCP_OPTION_T2 59 /* T2 rebinding time */
sherckuith 0:479ce5546098 222 #define DHCP_OPTION_US 60
sherckuith 0:479ce5546098 223 #define DHCP_OPTION_CLIENT_ID 61
sherckuith 0:479ce5546098 224 #define DHCP_OPTION_TFTP_SERVERNAME 66
sherckuith 0:479ce5546098 225 #define DHCP_OPTION_BOOTFILE 67
sherckuith 0:479ce5546098 226
sherckuith 0:479ce5546098 227 /** possible combinations of overloading the file and sname fields with options */
sherckuith 0:479ce5546098 228 #define DHCP_OVERLOAD_NONE 0
sherckuith 0:479ce5546098 229 #define DHCP_OVERLOAD_FILE 1
sherckuith 0:479ce5546098 230 #define DHCP_OVERLOAD_SNAME 2
sherckuith 0:479ce5546098 231 #define DHCP_OVERLOAD_SNAME_FILE 3
sherckuith 0:479ce5546098 232
sherckuith 0:479ce5546098 233 #ifdef __cplusplus
sherckuith 0:479ce5546098 234 }
sherckuith 0:479ce5546098 235 #endif
sherckuith 0:479ce5546098 236
sherckuith 0:479ce5546098 237 #endif /* LWIP_DHCP */
sherckuith 0:479ce5546098 238
sherckuith 0:479ce5546098 239 #endif /*__LWIP_DHCP_H__*/