Example self-announcing webserver which controls a servo through a smallHTML userinterface.

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:56:01 2010 +0000
Revision:
0:a259777c45a3

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dirkx 0:a259777c45a3 1 /**
dirkx 0:a259777c45a3 2 * @file
dirkx 0:a259777c45a3 3 * Dynamic Host Configuration Protocol client
dirkx 0:a259777c45a3 4 *
dirkx 0:a259777c45a3 5 */
dirkx 0:a259777c45a3 6
dirkx 0:a259777c45a3 7 /*
dirkx 0:a259777c45a3 8 *
dirkx 0:a259777c45a3 9 * Copyright (c) 2001-2004 Leon Woestenberg <leon.woestenberg@gmx.net>
dirkx 0:a259777c45a3 10 * Copyright (c) 2001-2004 Axon Digital Design B.V., The Netherlands.
dirkx 0:a259777c45a3 11 * All rights reserved.
dirkx 0:a259777c45a3 12 *
dirkx 0:a259777c45a3 13 * Redistribution and use in source and binary forms, with or without modification,
dirkx 0:a259777c45a3 14 * are permitted provided that the following conditions are met:
dirkx 0:a259777c45a3 15 *
dirkx 0:a259777c45a3 16 * 1. Redistributions of source code must retain the above copyright notice,
dirkx 0:a259777c45a3 17 * this list of conditions and the following disclaimer.
dirkx 0:a259777c45a3 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
dirkx 0:a259777c45a3 19 * this list of conditions and the following disclaimer in the documentation
dirkx 0:a259777c45a3 20 * and/or other materials provided with the distribution.
dirkx 0:a259777c45a3 21 * 3. The name of the author may not be used to endorse or promote products
dirkx 0:a259777c45a3 22 * derived from this software without specific prior written permission.
dirkx 0:a259777c45a3 23 *
dirkx 0:a259777c45a3 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
dirkx 0:a259777c45a3 25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
dirkx 0:a259777c45a3 26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
dirkx 0:a259777c45a3 27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
dirkx 0:a259777c45a3 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
dirkx 0:a259777c45a3 29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dirkx 0:a259777c45a3 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dirkx 0:a259777c45a3 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
dirkx 0:a259777c45a3 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
dirkx 0:a259777c45a3 33 * OF SUCH DAMAGE.
dirkx 0:a259777c45a3 34 *
dirkx 0:a259777c45a3 35 * This file is a contribution to the lwIP TCP/IP stack.
dirkx 0:a259777c45a3 36 * The Swedish Institute of Computer Science and Adam Dunkels
dirkx 0:a259777c45a3 37 * are specifically granted permission to redistribute this
dirkx 0:a259777c45a3 38 * source code.
dirkx 0:a259777c45a3 39 *
dirkx 0:a259777c45a3 40 * Author: Leon Woestenberg <leon.woestenberg@gmx.net>
dirkx 0:a259777c45a3 41 *
dirkx 0:a259777c45a3 42 * This is a DHCP client for the lwIP TCP/IP stack. It aims to conform
dirkx 0:a259777c45a3 43 * with RFC 2131 and RFC 2132.
dirkx 0:a259777c45a3 44 *
dirkx 0:a259777c45a3 45 * TODO:
dirkx 0:a259777c45a3 46 * - Support for interfaces other than Ethernet (SLIP, PPP, ...)
dirkx 0:a259777c45a3 47 *
dirkx 0:a259777c45a3 48 * Please coordinate changes and requests with Leon Woestenberg
dirkx 0:a259777c45a3 49 * <leon.woestenberg@gmx.net>
dirkx 0:a259777c45a3 50 *
dirkx 0:a259777c45a3 51 * Integration with your code:
dirkx 0:a259777c45a3 52 *
dirkx 0:a259777c45a3 53 * In lwip/dhcp.h
dirkx 0:a259777c45a3 54 * #define DHCP_COARSE_TIMER_SECS (recommended 60 which is a minute)
dirkx 0:a259777c45a3 55 * #define DHCP_FINE_TIMER_MSECS (recommended 500 which equals TCP coarse timer)
dirkx 0:a259777c45a3 56 *
dirkx 0:a259777c45a3 57 * Then have your application call dhcp_coarse_tmr() and
dirkx 0:a259777c45a3 58 * dhcp_fine_tmr() on the defined intervals.
dirkx 0:a259777c45a3 59 *
dirkx 0:a259777c45a3 60 * dhcp_start(struct netif *netif);
dirkx 0:a259777c45a3 61 * starts a DHCP client instance which configures the interface by
dirkx 0:a259777c45a3 62 * obtaining an IP address lease and maintaining it.
dirkx 0:a259777c45a3 63 *
dirkx 0:a259777c45a3 64 * Use dhcp_release(netif) to end the lease and use dhcp_stop(netif)
dirkx 0:a259777c45a3 65 * to remove the DHCP client.
dirkx 0:a259777c45a3 66 *
dirkx 0:a259777c45a3 67 */
dirkx 0:a259777c45a3 68
dirkx 0:a259777c45a3 69 #include "lwip/opt.h"
dirkx 0:a259777c45a3 70
dirkx 0:a259777c45a3 71 #if LWIP_DHCP /* don't build if not configured for use in lwipopts.h */
dirkx 0:a259777c45a3 72
dirkx 0:a259777c45a3 73 #include "lwip/stats.h"
dirkx 0:a259777c45a3 74 #include "lwip/mem.h"
dirkx 0:a259777c45a3 75 #include "lwip/udp.h"
dirkx 0:a259777c45a3 76 #include "lwip/ip_addr.h"
dirkx 0:a259777c45a3 77 #include "lwip/netif.h"
dirkx 0:a259777c45a3 78 #include "lwip/def.h"
dirkx 0:a259777c45a3 79 #include "lwip/sys.h"
dirkx 0:a259777c45a3 80 #include "lwip/dhcp.h"
dirkx 0:a259777c45a3 81 #include "lwip/autoip.h"
dirkx 0:a259777c45a3 82 #include "lwip/dns.h"
dirkx 0:a259777c45a3 83 #include "netif/etharp.h"
dirkx 0:a259777c45a3 84
dirkx 0:a259777c45a3 85 #include <string.h>
dirkx 0:a259777c45a3 86
dirkx 0:a259777c45a3 87 /** Default for DHCP_GLOBAL_XID is 0xABCD0000
dirkx 0:a259777c45a3 88 * This can be changed by defining DHCP_GLOBAL_XID and DHCP_GLOBAL_XID_HEADER, e.g.
dirkx 0:a259777c45a3 89 * #define DHCP_GLOBAL_XID_HEADER "stdlib.h"
dirkx 0:a259777c45a3 90 * #define DHCP_GLOBAL_XID rand()
dirkx 0:a259777c45a3 91 */
dirkx 0:a259777c45a3 92 #ifdef DHCP_GLOBAL_XID_HEADER
dirkx 0:a259777c45a3 93 #include DHCP_GLOBAL_XID_HEADER /* include optional starting XID generation prototypes */
dirkx 0:a259777c45a3 94 #endif
dirkx 0:a259777c45a3 95
dirkx 0:a259777c45a3 96 /** DHCP_OPTION_MAX_MSG_SIZE is set to the MTU
dirkx 0:a259777c45a3 97 * MTU is checked to be big enough in dhcp_start */
dirkx 0:a259777c45a3 98 #define DHCP_MAX_MSG_LEN(netif) (netif->mtu)
dirkx 0:a259777c45a3 99 #define DHCP_MAX_MSG_LEN_MIN_REQUIRED 576
dirkx 0:a259777c45a3 100 /** Minimum length for reply before packet is parsed */
dirkx 0:a259777c45a3 101 #define DHCP_MIN_REPLY_LEN 44
dirkx 0:a259777c45a3 102
dirkx 0:a259777c45a3 103 #define REBOOT_TRIES 2
dirkx 0:a259777c45a3 104
dirkx 0:a259777c45a3 105 /** Option handling: options are parsed in dhcp_parse_reply
dirkx 0:a259777c45a3 106 * and saved in an array where other functions can load them from.
dirkx 0:a259777c45a3 107 * This might be moved into the struct dhcp (not necessarily since
dirkx 0:a259777c45a3 108 * lwIP is single-threaded and the array is only used while in recv
dirkx 0:a259777c45a3 109 * callback). */
dirkx 0:a259777c45a3 110 #define DHCP_OPTION_IDX_OVERLOAD 0
dirkx 0:a259777c45a3 111 #define DHCP_OPTION_IDX_MSG_TYPE 1
dirkx 0:a259777c45a3 112 #define DHCP_OPTION_IDX_SERVER_ID 2
dirkx 0:a259777c45a3 113 #define DHCP_OPTION_IDX_LEASE_TIME 3
dirkx 0:a259777c45a3 114 #define DHCP_OPTION_IDX_T1 4
dirkx 0:a259777c45a3 115 #define DHCP_OPTION_IDX_T2 5
dirkx 0:a259777c45a3 116 #define DHCP_OPTION_IDX_SUBNET_MASK 6
dirkx 0:a259777c45a3 117 #define DHCP_OPTION_IDX_ROUTER 7
dirkx 0:a259777c45a3 118 #define DHCP_OPTION_IDX_DNS_SERVER 8
dirkx 0:a259777c45a3 119 #define DHCP_OPTION_IDX_MAX (DHCP_OPTION_IDX_DNS_SERVER + DNS_MAX_SERVERS)
dirkx 0:a259777c45a3 120
dirkx 0:a259777c45a3 121 /** Holds the decoded option values, only valid while in dhcp_recv.
dirkx 0:a259777c45a3 122 @todo: move this into struct dhcp? */
dirkx 0:a259777c45a3 123 u32_t dhcp_rx_options_val[DHCP_OPTION_IDX_MAX];
dirkx 0:a259777c45a3 124 /** Holds a flag which option was received and is contained in dhcp_rx_options_val,
dirkx 0:a259777c45a3 125 only valid while in dhcp_recv.
dirkx 0:a259777c45a3 126 @todo: move this into struct dhcp? */
dirkx 0:a259777c45a3 127 u8_t dhcp_rx_options_given[DHCP_OPTION_IDX_MAX];
dirkx 0:a259777c45a3 128
dirkx 0:a259777c45a3 129 #define dhcp_option_given(dhcp, idx) (dhcp_rx_options_given[idx] != 0)
dirkx 0:a259777c45a3 130 #define dhcp_got_option(dhcp, idx) (dhcp_rx_options_given[idx] = 1)
dirkx 0:a259777c45a3 131 #define dhcp_clear_option(dhcp, idx) (dhcp_rx_options_given[idx] = 0)
dirkx 0:a259777c45a3 132 #define dhcp_clear_all_options(dhcp) (memset(dhcp_rx_options_given, 0, sizeof(dhcp_rx_options_given)))
dirkx 0:a259777c45a3 133 #define dhcp_get_option_value(dhcp, idx) (dhcp_rx_options_val[idx])
dirkx 0:a259777c45a3 134 #define dhcp_set_option_value(dhcp, idx, val) (dhcp_rx_options_val[idx] = (val))
dirkx 0:a259777c45a3 135
dirkx 0:a259777c45a3 136
dirkx 0:a259777c45a3 137 /* DHCP client state machine functions */
dirkx 0:a259777c45a3 138 static err_t dhcp_discover(struct netif *netif);
dirkx 0:a259777c45a3 139 static err_t dhcp_select(struct netif *netif);
dirkx 0:a259777c45a3 140 static void dhcp_bind(struct netif *netif);
dirkx 0:a259777c45a3 141 #if DHCP_DOES_ARP_CHECK
dirkx 0:a259777c45a3 142 static err_t dhcp_decline(struct netif *netif);
dirkx 0:a259777c45a3 143 #endif /* DHCP_DOES_ARP_CHECK */
dirkx 0:a259777c45a3 144 static err_t dhcp_rebind(struct netif *netif);
dirkx 0:a259777c45a3 145 static err_t dhcp_reboot(struct netif *netif);
dirkx 0:a259777c45a3 146 static void dhcp_set_state(struct dhcp *dhcp, u8_t new_state);
dirkx 0:a259777c45a3 147
dirkx 0:a259777c45a3 148 /* receive, unfold, parse and free incoming messages */
dirkx 0:a259777c45a3 149 static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port);
dirkx 0:a259777c45a3 150
dirkx 0:a259777c45a3 151 /* set the DHCP timers */
dirkx 0:a259777c45a3 152 static void dhcp_timeout(struct netif *netif);
dirkx 0:a259777c45a3 153 static void dhcp_t1_timeout(struct netif *netif);
dirkx 0:a259777c45a3 154 static void dhcp_t2_timeout(struct netif *netif);
dirkx 0:a259777c45a3 155
dirkx 0:a259777c45a3 156 /* build outgoing messages */
dirkx 0:a259777c45a3 157 /* create a DHCP message, fill in common headers */
dirkx 0:a259777c45a3 158 static err_t dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type);
dirkx 0:a259777c45a3 159 /* free a DHCP request */
dirkx 0:a259777c45a3 160 static void dhcp_delete_msg(struct dhcp *dhcp);
dirkx 0:a259777c45a3 161 /* add a DHCP option (type, then length in bytes) */
dirkx 0:a259777c45a3 162 static void dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len);
dirkx 0:a259777c45a3 163 /* add option values */
dirkx 0:a259777c45a3 164 static void dhcp_option_byte(struct dhcp *dhcp, u8_t value);
dirkx 0:a259777c45a3 165 static void dhcp_option_short(struct dhcp *dhcp, u16_t value);
dirkx 0:a259777c45a3 166 static void dhcp_option_long(struct dhcp *dhcp, u32_t value);
dirkx 0:a259777c45a3 167 /* always add the DHCP options trailer to end and pad */
dirkx 0:a259777c45a3 168 static void dhcp_option_trailer(struct dhcp *dhcp);
dirkx 0:a259777c45a3 169
dirkx 0:a259777c45a3 170 /**
dirkx 0:a259777c45a3 171 * Back-off the DHCP client (because of a received NAK response).
dirkx 0:a259777c45a3 172 *
dirkx 0:a259777c45a3 173 * Back-off the DHCP client because of a received NAK. Receiving a
dirkx 0:a259777c45a3 174 * NAK means the client asked for something non-sensible, for
dirkx 0:a259777c45a3 175 * example when it tries to renew a lease obtained on another network.
dirkx 0:a259777c45a3 176 *
dirkx 0:a259777c45a3 177 * We clear any existing set IP address and restart DHCP negotiation
dirkx 0:a259777c45a3 178 * afresh (as per RFC2131 3.2.3).
dirkx 0:a259777c45a3 179 *
dirkx 0:a259777c45a3 180 * @param netif the netif under DHCP control
dirkx 0:a259777c45a3 181 */
dirkx 0:a259777c45a3 182 static void
dirkx 0:a259777c45a3 183 dhcp_handle_nak(struct netif *netif)
dirkx 0:a259777c45a3 184 {
dirkx 0:a259777c45a3 185 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 186 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_handle_nak(netif=%p) %c%c%"U16_F"\n",
dirkx 0:a259777c45a3 187 (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
dirkx 0:a259777c45a3 188 /* Set the interface down since the address must no longer be used, as per RFC2131 */
dirkx 0:a259777c45a3 189 netif_set_down(netif);
dirkx 0:a259777c45a3 190 /* remove IP address from interface */
dirkx 0:a259777c45a3 191 netif_set_ipaddr(netif, IP_ADDR_ANY);
dirkx 0:a259777c45a3 192 netif_set_gw(netif, IP_ADDR_ANY);
dirkx 0:a259777c45a3 193 netif_set_netmask(netif, IP_ADDR_ANY);
dirkx 0:a259777c45a3 194 /* Change to a defined state */
dirkx 0:a259777c45a3 195 dhcp_set_state(dhcp, DHCP_BACKING_OFF);
dirkx 0:a259777c45a3 196 /* We can immediately restart discovery */
dirkx 0:a259777c45a3 197 dhcp_discover(netif);
dirkx 0:a259777c45a3 198 }
dirkx 0:a259777c45a3 199
dirkx 0:a259777c45a3 200 #if DHCP_DOES_ARP_CHECK
dirkx 0:a259777c45a3 201 /**
dirkx 0:a259777c45a3 202 * Checks if the offered IP address is already in use.
dirkx 0:a259777c45a3 203 *
dirkx 0:a259777c45a3 204 * It does so by sending an ARP request for the offered address and
dirkx 0:a259777c45a3 205 * entering CHECKING state. If no ARP reply is received within a small
dirkx 0:a259777c45a3 206 * interval, the address is assumed to be free for use by us.
dirkx 0:a259777c45a3 207 *
dirkx 0:a259777c45a3 208 * @param netif the netif under DHCP control
dirkx 0:a259777c45a3 209 */
dirkx 0:a259777c45a3 210 static void
dirkx 0:a259777c45a3 211 dhcp_check(struct netif *netif)
dirkx 0:a259777c45a3 212 {
dirkx 0:a259777c45a3 213 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 214 err_t result;
dirkx 0:a259777c45a3 215 u16_t msecs;
dirkx 0:a259777c45a3 216 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_check(netif=%p) %c%c\n", (void *)netif, (s16_t)netif->name[0],
dirkx 0:a259777c45a3 217 (s16_t)netif->name[1]));
dirkx 0:a259777c45a3 218 dhcp_set_state(dhcp, DHCP_CHECKING);
dirkx 0:a259777c45a3 219 /* create an ARP query for the offered IP address, expecting that no host
dirkx 0:a259777c45a3 220 responds, as the IP address should not be in use. */
dirkx 0:a259777c45a3 221 result = etharp_query(netif, &dhcp->offered_ip_addr, NULL);
dirkx 0:a259777c45a3 222 if (result != ERR_OK) {
dirkx 0:a259777c45a3 223 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("dhcp_check: could not perform ARP query\n"));
dirkx 0:a259777c45a3 224 }
dirkx 0:a259777c45a3 225 dhcp->tries++;
dirkx 0:a259777c45a3 226 msecs = 500;
dirkx 0:a259777c45a3 227 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
dirkx 0:a259777c45a3 228 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_check(): set request timeout %"U16_F" msecs\n", msecs));
dirkx 0:a259777c45a3 229 }
dirkx 0:a259777c45a3 230 #endif /* DHCP_DOES_ARP_CHECK */
dirkx 0:a259777c45a3 231
dirkx 0:a259777c45a3 232 /**
dirkx 0:a259777c45a3 233 * Remember the configuration offered by a DHCP server.
dirkx 0:a259777c45a3 234 *
dirkx 0:a259777c45a3 235 * @param netif the netif under DHCP control
dirkx 0:a259777c45a3 236 */
dirkx 0:a259777c45a3 237 static void
dirkx 0:a259777c45a3 238 dhcp_handle_offer(struct netif *netif)
dirkx 0:a259777c45a3 239 {
dirkx 0:a259777c45a3 240 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 241 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_handle_offer(netif=%p) %c%c%"U16_F"\n",
dirkx 0:a259777c45a3 242 (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
dirkx 0:a259777c45a3 243 /* obtain the server address */
dirkx 0:a259777c45a3 244 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SERVER_ID)) {
dirkx 0:a259777c45a3 245 ip4_addr_set_u32(&dhcp->server_ip_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SERVER_ID)));
dirkx 0:a259777c45a3 246 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): server 0x%08"X32_F"\n",
dirkx 0:a259777c45a3 247 ip4_addr_get_u32(&dhcp->server_ip_addr)));
dirkx 0:a259777c45a3 248 /* remember offered address */
dirkx 0:a259777c45a3 249 ip_addr_copy(dhcp->offered_ip_addr, dhcp->msg_in->yiaddr);
dirkx 0:a259777c45a3 250 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): offer for 0x%08"X32_F"\n",
dirkx 0:a259777c45a3 251 ip4_addr_get_u32(&dhcp->offered_ip_addr)));
dirkx 0:a259777c45a3 252
dirkx 0:a259777c45a3 253 dhcp_select(netif);
dirkx 0:a259777c45a3 254 } else {
dirkx 0:a259777c45a3 255 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
dirkx 0:a259777c45a3 256 ("dhcp_handle_offer(netif=%p) did not get server ID!\n", (void*)netif));
dirkx 0:a259777c45a3 257 }
dirkx 0:a259777c45a3 258 }
dirkx 0:a259777c45a3 259
dirkx 0:a259777c45a3 260 /**
dirkx 0:a259777c45a3 261 * Select a DHCP server offer out of all offers.
dirkx 0:a259777c45a3 262 *
dirkx 0:a259777c45a3 263 * Simply select the first offer received.
dirkx 0:a259777c45a3 264 *
dirkx 0:a259777c45a3 265 * @param netif the netif under DHCP control
dirkx 0:a259777c45a3 266 * @return lwIP specific error (see error.h)
dirkx 0:a259777c45a3 267 */
dirkx 0:a259777c45a3 268 static err_t
dirkx 0:a259777c45a3 269 dhcp_select(struct netif *netif)
dirkx 0:a259777c45a3 270 {
dirkx 0:a259777c45a3 271 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 272 err_t result;
dirkx 0:a259777c45a3 273 u16_t msecs;
dirkx 0:a259777c45a3 274
dirkx 0:a259777c45a3 275 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_select(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
dirkx 0:a259777c45a3 276 dhcp_set_state(dhcp, DHCP_REQUESTING);
dirkx 0:a259777c45a3 277
dirkx 0:a259777c45a3 278 /* create and initialize the DHCP message header */
dirkx 0:a259777c45a3 279 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
dirkx 0:a259777c45a3 280 if (result == ERR_OK) {
dirkx 0:a259777c45a3 281 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
dirkx 0:a259777c45a3 282 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
dirkx 0:a259777c45a3 283
dirkx 0:a259777c45a3 284 /* MUST request the offered IP address */
dirkx 0:a259777c45a3 285 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
dirkx 0:a259777c45a3 286 dhcp_option_long(dhcp, ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
dirkx 0:a259777c45a3 287
dirkx 0:a259777c45a3 288 dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
dirkx 0:a259777c45a3 289 dhcp_option_long(dhcp, ntohl(ip4_addr_get_u32(&dhcp->server_ip_addr)));
dirkx 0:a259777c45a3 290
dirkx 0:a259777c45a3 291 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 4/*num options*/);
dirkx 0:a259777c45a3 292 dhcp_option_byte(dhcp, DHCP_OPTION_SUBNET_MASK);
dirkx 0:a259777c45a3 293 dhcp_option_byte(dhcp, DHCP_OPTION_ROUTER);
dirkx 0:a259777c45a3 294 dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
dirkx 0:a259777c45a3 295 dhcp_option_byte(dhcp, DHCP_OPTION_DNS_SERVER);
dirkx 0:a259777c45a3 296
dirkx 0:a259777c45a3 297 #if LWIP_NETIF_HOSTNAME
dirkx 0:a259777c45a3 298 if (netif->hostname != NULL) {
dirkx 0:a259777c45a3 299 const char *p = (const char*)netif->hostname;
dirkx 0:a259777c45a3 300 u8_t namelen = (u8_t)strlen(p);
dirkx 0:a259777c45a3 301 if (namelen > 0) {
dirkx 0:a259777c45a3 302 LWIP_ASSERT("DHCP: hostname is too long!", namelen < 255);
dirkx 0:a259777c45a3 303 dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, namelen);
dirkx 0:a259777c45a3 304 while (*p) {
dirkx 0:a259777c45a3 305 dhcp_option_byte(dhcp, *p++);
dirkx 0:a259777c45a3 306 }
dirkx 0:a259777c45a3 307 }
dirkx 0:a259777c45a3 308 }
dirkx 0:a259777c45a3 309 #endif /* LWIP_NETIF_HOSTNAME */
dirkx 0:a259777c45a3 310
dirkx 0:a259777c45a3 311 dhcp_option_trailer(dhcp);
dirkx 0:a259777c45a3 312 /* shrink the pbuf to the actual content length */
dirkx 0:a259777c45a3 313 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
dirkx 0:a259777c45a3 314
dirkx 0:a259777c45a3 315 /* send broadcast to any DHCP server */
dirkx 0:a259777c45a3 316 udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
dirkx 0:a259777c45a3 317 dhcp_delete_msg(dhcp);
dirkx 0:a259777c45a3 318 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_select: REQUESTING\n"));
dirkx 0:a259777c45a3 319 } else {
dirkx 0:a259777c45a3 320 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("dhcp_select: could not allocate DHCP request\n"));
dirkx 0:a259777c45a3 321 }
dirkx 0:a259777c45a3 322 dhcp->tries++;
dirkx 0:a259777c45a3 323 msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
dirkx 0:a259777c45a3 324 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
dirkx 0:a259777c45a3 325 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_select(): set request timeout %"U16_F" msecs\n", msecs));
dirkx 0:a259777c45a3 326 return result;
dirkx 0:a259777c45a3 327 }
dirkx 0:a259777c45a3 328
dirkx 0:a259777c45a3 329 /**
dirkx 0:a259777c45a3 330 * The DHCP timer that checks for lease renewal/rebind timeouts.
dirkx 0:a259777c45a3 331 */
dirkx 0:a259777c45a3 332 void
dirkx 0:a259777c45a3 333 dhcp_coarse_tmr()
dirkx 0:a259777c45a3 334 {
dirkx 0:a259777c45a3 335 struct netif *netif = netif_list;
dirkx 0:a259777c45a3 336 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_coarse_tmr()\n"));
dirkx 0:a259777c45a3 337 /* iterate through all network interfaces */
dirkx 0:a259777c45a3 338 while (netif != NULL) {
dirkx 0:a259777c45a3 339 /* only act on DHCP configured interfaces */
dirkx 0:a259777c45a3 340 if (netif->dhcp != NULL) {
dirkx 0:a259777c45a3 341 /* timer is active (non zero), and triggers (zeroes) now? */
dirkx 0:a259777c45a3 342 if (netif->dhcp->t2_timeout-- == 1) {
dirkx 0:a259777c45a3 343 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t2 timeout\n"));
dirkx 0:a259777c45a3 344 /* this clients' rebind timeout triggered */
dirkx 0:a259777c45a3 345 dhcp_t2_timeout(netif);
dirkx 0:a259777c45a3 346 /* timer is active (non zero), and triggers (zeroes) now */
dirkx 0:a259777c45a3 347 } else if (netif->dhcp->t1_timeout-- == 1) {
dirkx 0:a259777c45a3 348 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t1 timeout\n"));
dirkx 0:a259777c45a3 349 /* this clients' renewal timeout triggered */
dirkx 0:a259777c45a3 350 dhcp_t1_timeout(netif);
dirkx 0:a259777c45a3 351 }
dirkx 0:a259777c45a3 352 }
dirkx 0:a259777c45a3 353 /* proceed to next netif */
dirkx 0:a259777c45a3 354 netif = netif->next;
dirkx 0:a259777c45a3 355 }
dirkx 0:a259777c45a3 356 }
dirkx 0:a259777c45a3 357
dirkx 0:a259777c45a3 358 /**
dirkx 0:a259777c45a3 359 * DHCP transaction timeout handling
dirkx 0:a259777c45a3 360 *
dirkx 0:a259777c45a3 361 * A DHCP server is expected to respond within a short period of time.
dirkx 0:a259777c45a3 362 * This timer checks whether an outstanding DHCP request is timed out.
dirkx 0:a259777c45a3 363 */
dirkx 0:a259777c45a3 364 void
dirkx 0:a259777c45a3 365 dhcp_fine_tmr()
dirkx 0:a259777c45a3 366 {
dirkx 0:a259777c45a3 367 struct netif *netif = netif_list;
dirkx 0:a259777c45a3 368 /* loop through netif's */
dirkx 0:a259777c45a3 369 while (netif != NULL) {
dirkx 0:a259777c45a3 370 /* only act on DHCP configured interfaces */
dirkx 0:a259777c45a3 371 if (netif->dhcp != NULL) {
dirkx 0:a259777c45a3 372 /* timer is active (non zero), and is about to trigger now */
dirkx 0:a259777c45a3 373 if (netif->dhcp->request_timeout > 1) {
dirkx 0:a259777c45a3 374 netif->dhcp->request_timeout--;
dirkx 0:a259777c45a3 375 }
dirkx 0:a259777c45a3 376 else if (netif->dhcp->request_timeout == 1) {
dirkx 0:a259777c45a3 377 netif->dhcp->request_timeout--;
dirkx 0:a259777c45a3 378 /* { netif->dhcp->request_timeout == 0 } */
dirkx 0:a259777c45a3 379 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_fine_tmr(): request timeout\n"));
dirkx 0:a259777c45a3 380 /* this client's request timeout triggered */
dirkx 0:a259777c45a3 381 dhcp_timeout(netif);
dirkx 0:a259777c45a3 382 }
dirkx 0:a259777c45a3 383 }
dirkx 0:a259777c45a3 384 /* proceed to next network interface */
dirkx 0:a259777c45a3 385 netif = netif->next;
dirkx 0:a259777c45a3 386 }
dirkx 0:a259777c45a3 387 }
dirkx 0:a259777c45a3 388
dirkx 0:a259777c45a3 389 /**
dirkx 0:a259777c45a3 390 * A DHCP negotiation transaction, or ARP request, has timed out.
dirkx 0:a259777c45a3 391 *
dirkx 0:a259777c45a3 392 * The timer that was started with the DHCP or ARP request has
dirkx 0:a259777c45a3 393 * timed out, indicating no response was received in time.
dirkx 0:a259777c45a3 394 *
dirkx 0:a259777c45a3 395 * @param netif the netif under DHCP control
dirkx 0:a259777c45a3 396 */
dirkx 0:a259777c45a3 397 static void
dirkx 0:a259777c45a3 398 dhcp_timeout(struct netif *netif)
dirkx 0:a259777c45a3 399 {
dirkx 0:a259777c45a3 400 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 401 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout()\n"));
dirkx 0:a259777c45a3 402 /* back-off period has passed, or server selection timed out */
dirkx 0:a259777c45a3 403 if ((dhcp->state == DHCP_BACKING_OFF) || (dhcp->state == DHCP_SELECTING)) {
dirkx 0:a259777c45a3 404 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout(): restarting discovery\n"));
dirkx 0:a259777c45a3 405 dhcp_discover(netif);
dirkx 0:a259777c45a3 406 /* receiving the requested lease timed out */
dirkx 0:a259777c45a3 407 } else if (dhcp->state == DHCP_REQUESTING) {
dirkx 0:a259777c45a3 408 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, DHCP request timed out\n"));
dirkx 0:a259777c45a3 409 if (dhcp->tries <= 5) {
dirkx 0:a259777c45a3 410 dhcp_select(netif);
dirkx 0:a259777c45a3 411 } else {
dirkx 0:a259777c45a3 412 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, releasing, restarting\n"));
dirkx 0:a259777c45a3 413 dhcp_release(netif);
dirkx 0:a259777c45a3 414 dhcp_discover(netif);
dirkx 0:a259777c45a3 415 }
dirkx 0:a259777c45a3 416 #if DHCP_DOES_ARP_CHECK
dirkx 0:a259777c45a3 417 /* received no ARP reply for the offered address (which is good) */
dirkx 0:a259777c45a3 418 } else if (dhcp->state == DHCP_CHECKING) {
dirkx 0:a259777c45a3 419 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): CHECKING, ARP request timed out\n"));
dirkx 0:a259777c45a3 420 if (dhcp->tries <= 1) {
dirkx 0:a259777c45a3 421 dhcp_check(netif);
dirkx 0:a259777c45a3 422 /* no ARP replies on the offered address,
dirkx 0:a259777c45a3 423 looks like the IP address is indeed free */
dirkx 0:a259777c45a3 424 } else {
dirkx 0:a259777c45a3 425 /* bind the interface to the offered address */
dirkx 0:a259777c45a3 426 dhcp_bind(netif);
dirkx 0:a259777c45a3 427 }
dirkx 0:a259777c45a3 428 #endif /* DHCP_DOES_ARP_CHECK */
dirkx 0:a259777c45a3 429 }
dirkx 0:a259777c45a3 430 /* did not get response to renew request? */
dirkx 0:a259777c45a3 431 else if (dhcp->state == DHCP_RENEWING) {
dirkx 0:a259777c45a3 432 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): RENEWING, DHCP request timed out\n"));
dirkx 0:a259777c45a3 433 /* just retry renewal */
dirkx 0:a259777c45a3 434 /* note that the rebind timer will eventually time-out if renew does not work */
dirkx 0:a259777c45a3 435 dhcp_renew(netif);
dirkx 0:a259777c45a3 436 /* did not get response to rebind request? */
dirkx 0:a259777c45a3 437 } else if (dhcp->state == DHCP_REBINDING) {
dirkx 0:a259777c45a3 438 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REBINDING, DHCP request timed out\n"));
dirkx 0:a259777c45a3 439 if (dhcp->tries <= 8) {
dirkx 0:a259777c45a3 440 dhcp_rebind(netif);
dirkx 0:a259777c45a3 441 } else {
dirkx 0:a259777c45a3 442 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): RELEASING, DISCOVERING\n"));
dirkx 0:a259777c45a3 443 dhcp_release(netif);
dirkx 0:a259777c45a3 444 dhcp_discover(netif);
dirkx 0:a259777c45a3 445 }
dirkx 0:a259777c45a3 446 } else if (dhcp->state == DHCP_REBOOTING) {
dirkx 0:a259777c45a3 447 if (dhcp->tries < REBOOT_TRIES) {
dirkx 0:a259777c45a3 448 dhcp_reboot(netif);
dirkx 0:a259777c45a3 449 } else {
dirkx 0:a259777c45a3 450 dhcp_discover(netif);
dirkx 0:a259777c45a3 451 }
dirkx 0:a259777c45a3 452 }
dirkx 0:a259777c45a3 453 }
dirkx 0:a259777c45a3 454
dirkx 0:a259777c45a3 455 /**
dirkx 0:a259777c45a3 456 * The renewal period has timed out.
dirkx 0:a259777c45a3 457 *
dirkx 0:a259777c45a3 458 * @param netif the netif under DHCP control
dirkx 0:a259777c45a3 459 */
dirkx 0:a259777c45a3 460 static void
dirkx 0:a259777c45a3 461 dhcp_t1_timeout(struct netif *netif)
dirkx 0:a259777c45a3 462 {
dirkx 0:a259777c45a3 463 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 464 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_t1_timeout()\n"));
dirkx 0:a259777c45a3 465 if ((dhcp->state == DHCP_REQUESTING) || (dhcp->state == DHCP_BOUND) ||
dirkx 0:a259777c45a3 466 (dhcp->state == DHCP_RENEWING)) {
dirkx 0:a259777c45a3 467 /* just retry to renew - note that the rebind timer (t2) will
dirkx 0:a259777c45a3 468 * eventually time-out if renew tries fail. */
dirkx 0:a259777c45a3 469 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
dirkx 0:a259777c45a3 470 ("dhcp_t1_timeout(): must renew\n"));
dirkx 0:a259777c45a3 471 /* This slightly different to RFC2131: DHCPREQUEST will be sent from state
dirkx 0:a259777c45a3 472 DHCP_RENEWING, not DHCP_BOUND */
dirkx 0:a259777c45a3 473 dhcp_renew(netif);
dirkx 0:a259777c45a3 474 }
dirkx 0:a259777c45a3 475 }
dirkx 0:a259777c45a3 476
dirkx 0:a259777c45a3 477 /**
dirkx 0:a259777c45a3 478 * The rebind period has timed out.
dirkx 0:a259777c45a3 479 *
dirkx 0:a259777c45a3 480 * @param netif the netif under DHCP control
dirkx 0:a259777c45a3 481 */
dirkx 0:a259777c45a3 482 static void
dirkx 0:a259777c45a3 483 dhcp_t2_timeout(struct netif *netif)
dirkx 0:a259777c45a3 484 {
dirkx 0:a259777c45a3 485 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 486 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t2_timeout()\n"));
dirkx 0:a259777c45a3 487 if ((dhcp->state == DHCP_REQUESTING) || (dhcp->state == DHCP_BOUND) ||
dirkx 0:a259777c45a3 488 (dhcp->state == DHCP_RENEWING)) {
dirkx 0:a259777c45a3 489 /* just retry to rebind */
dirkx 0:a259777c45a3 490 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
dirkx 0:a259777c45a3 491 ("dhcp_t2_timeout(): must rebind\n"));
dirkx 0:a259777c45a3 492 /* This slightly different to RFC2131: DHCPREQUEST will be sent from state
dirkx 0:a259777c45a3 493 DHCP_REBINDING, not DHCP_BOUND */
dirkx 0:a259777c45a3 494 dhcp_rebind(netif);
dirkx 0:a259777c45a3 495 }
dirkx 0:a259777c45a3 496 }
dirkx 0:a259777c45a3 497
dirkx 0:a259777c45a3 498 /**
dirkx 0:a259777c45a3 499 * Handle a DHCP ACK packet
dirkx 0:a259777c45a3 500 *
dirkx 0:a259777c45a3 501 * @param netif the netif under DHCP control
dirkx 0:a259777c45a3 502 */
dirkx 0:a259777c45a3 503 static void
dirkx 0:a259777c45a3 504 dhcp_handle_ack(struct netif *netif)
dirkx 0:a259777c45a3 505 {
dirkx 0:a259777c45a3 506 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 507 #if LWIP_DNS
dirkx 0:a259777c45a3 508 u8_t n;
dirkx 0:a259777c45a3 509 #endif /* LWIP_DNS */
dirkx 0:a259777c45a3 510
dirkx 0:a259777c45a3 511 /* clear options we might not get from the ACK */
dirkx 0:a259777c45a3 512 ip_addr_set_zero(&dhcp->offered_sn_mask);
dirkx 0:a259777c45a3 513 ip_addr_set_zero(&dhcp->offered_gw_addr);
dirkx 0:a259777c45a3 514 #if LWIP_DHCP_BOOTP_FILE
dirkx 0:a259777c45a3 515 ip_addr_set_zero(&dhcp->offered_si_addr);
dirkx 0:a259777c45a3 516 #endif /* LWIP_DHCP_BOOTP_FILE */
dirkx 0:a259777c45a3 517
dirkx 0:a259777c45a3 518 /* lease time given? */
dirkx 0:a259777c45a3 519 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_LEASE_TIME)) {
dirkx 0:a259777c45a3 520 /* remember offered lease time */
dirkx 0:a259777c45a3 521 dhcp->offered_t0_lease = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_LEASE_TIME);
dirkx 0:a259777c45a3 522 }
dirkx 0:a259777c45a3 523 /* renewal period given? */
dirkx 0:a259777c45a3 524 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T1)) {
dirkx 0:a259777c45a3 525 /* remember given renewal period */
dirkx 0:a259777c45a3 526 dhcp->offered_t1_renew = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T1);
dirkx 0:a259777c45a3 527 } else {
dirkx 0:a259777c45a3 528 /* calculate safe periods for renewal */
dirkx 0:a259777c45a3 529 dhcp->offered_t1_renew = dhcp->offered_t0_lease / 2;
dirkx 0:a259777c45a3 530 }
dirkx 0:a259777c45a3 531
dirkx 0:a259777c45a3 532 /* renewal period given? */
dirkx 0:a259777c45a3 533 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T2)) {
dirkx 0:a259777c45a3 534 /* remember given rebind period */
dirkx 0:a259777c45a3 535 dhcp->offered_t2_rebind = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T2);
dirkx 0:a259777c45a3 536 } else {
dirkx 0:a259777c45a3 537 /* calculate safe periods for rebinding */
dirkx 0:a259777c45a3 538 dhcp->offered_t2_rebind = dhcp->offered_t0_lease;
dirkx 0:a259777c45a3 539 }
dirkx 0:a259777c45a3 540
dirkx 0:a259777c45a3 541 /* (y)our internet address */
dirkx 0:a259777c45a3 542 ip_addr_copy(dhcp->offered_ip_addr, dhcp->msg_in->yiaddr);
dirkx 0:a259777c45a3 543
dirkx 0:a259777c45a3 544 #if LWIP_DHCP_BOOTP_FILE
dirkx 0:a259777c45a3 545 /* copy boot server address,
dirkx 0:a259777c45a3 546 boot file name copied in dhcp_parse_reply if not overloaded */
dirkx 0:a259777c45a3 547 ip_addr_copy(dhcp->offered_si_addr, dhcp->msg_in->siaddr);
dirkx 0:a259777c45a3 548 #endif /* LWIP_DHCP_BOOTP_FILE */
dirkx 0:a259777c45a3 549
dirkx 0:a259777c45a3 550 /* subnet mask given? */
dirkx 0:a259777c45a3 551 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)) {
dirkx 0:a259777c45a3 552 /* remember given subnet mask */
dirkx 0:a259777c45a3 553 ip4_addr_set_u32(&dhcp->offered_sn_mask, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)));
dirkx 0:a259777c45a3 554 dhcp->subnet_mask_given = 1;
dirkx 0:a259777c45a3 555 } else {
dirkx 0:a259777c45a3 556 dhcp->subnet_mask_given = 0;
dirkx 0:a259777c45a3 557 }
dirkx 0:a259777c45a3 558
dirkx 0:a259777c45a3 559 /* gateway router */
dirkx 0:a259777c45a3 560 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_ROUTER)) {
dirkx 0:a259777c45a3 561 ip4_addr_set_u32(&dhcp->offered_gw_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_ROUTER)));
dirkx 0:a259777c45a3 562 }
dirkx 0:a259777c45a3 563
dirkx 0:a259777c45a3 564 #if LWIP_DNS
dirkx 0:a259777c45a3 565 /* DNS servers */
dirkx 0:a259777c45a3 566 n = 0;
dirkx 0:a259777c45a3 567 while(dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n) && (n < DNS_MAX_SERVERS)) {
dirkx 0:a259777c45a3 568 ip_addr_t dns_addr;
dirkx 0:a259777c45a3 569 ip4_addr_set_u32(&dns_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n)));
dirkx 0:a259777c45a3 570 dns_setserver(n, &dns_addr);
dirkx 0:a259777c45a3 571 n++;
dirkx 0:a259777c45a3 572 }
dirkx 0:a259777c45a3 573 #endif /* LWIP_DNS */
dirkx 0:a259777c45a3 574 }
dirkx 0:a259777c45a3 575
dirkx 0:a259777c45a3 576 /** Set a statically allocated struct dhcp to work with.
dirkx 0:a259777c45a3 577 * Using this prevents dhcp_start to allocate it using mem_malloc.
dirkx 0:a259777c45a3 578 *
dirkx 0:a259777c45a3 579 * @param netif the netif for which to set the struct dhcp
dirkx 0:a259777c45a3 580 * @param dhcp (uninitialised) dhcp struct allocated by the application
dirkx 0:a259777c45a3 581 */
dirkx 0:a259777c45a3 582 void
dirkx 0:a259777c45a3 583 dhcp_set_struct(struct netif *netif, struct dhcp *dhcp)
dirkx 0:a259777c45a3 584 {
dirkx 0:a259777c45a3 585 LWIP_ASSERT("netif != NULL", netif != NULL);
dirkx 0:a259777c45a3 586 LWIP_ASSERT("dhcp != NULL", dhcp != NULL);
dirkx 0:a259777c45a3 587 LWIP_ASSERT("netif already has a struct dhcp set", netif->dhcp == NULL);
dirkx 0:a259777c45a3 588
dirkx 0:a259777c45a3 589 /* clear data structure */
dirkx 0:a259777c45a3 590 memset(dhcp, 0, sizeof(struct dhcp));
dirkx 0:a259777c45a3 591 /* dhcp_set_state(&dhcp, DHCP_OFF); */
dirkx 0:a259777c45a3 592 netif->dhcp = dhcp;
dirkx 0:a259777c45a3 593 }
dirkx 0:a259777c45a3 594
dirkx 0:a259777c45a3 595 /**
dirkx 0:a259777c45a3 596 * Start DHCP negotiation for a network interface.
dirkx 0:a259777c45a3 597 *
dirkx 0:a259777c45a3 598 * If no DHCP client instance was attached to this interface,
dirkx 0:a259777c45a3 599 * a new client is created first. If a DHCP client instance
dirkx 0:a259777c45a3 600 * was already present, it restarts negotiation.
dirkx 0:a259777c45a3 601 *
dirkx 0:a259777c45a3 602 * @param netif The lwIP network interface
dirkx 0:a259777c45a3 603 * @return lwIP error code
dirkx 0:a259777c45a3 604 * - ERR_OK - No error
dirkx 0:a259777c45a3 605 * - ERR_MEM - Out of memory
dirkx 0:a259777c45a3 606 */
dirkx 0:a259777c45a3 607 err_t
dirkx 0:a259777c45a3 608 dhcp_start(struct netif *netif)
dirkx 0:a259777c45a3 609 {
dirkx 0:a259777c45a3 610 struct dhcp *dhcp;
dirkx 0:a259777c45a3 611 err_t result = ERR_OK;
dirkx 0:a259777c45a3 612
dirkx 0:a259777c45a3 613 LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG;);
dirkx 0:a259777c45a3 614 dhcp = netif->dhcp;
dirkx 0:a259777c45a3 615 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_start(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
dirkx 0:a259777c45a3 616 /* Remove the flag that says this netif is handled by DHCP,
dirkx 0:a259777c45a3 617 it is set when we succeeded starting. */
dirkx 0:a259777c45a3 618 netif->flags &= ~NETIF_FLAG_DHCP;
dirkx 0:a259777c45a3 619
dirkx 0:a259777c45a3 620 /* check hwtype of the netif */
dirkx 0:a259777c45a3 621 if ((netif->flags & NETIF_FLAG_ETHARP) == 0) {
dirkx 0:a259777c45a3 622 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): No ETHARP netif\n"));
dirkx 0:a259777c45a3 623 return ERR_ARG;
dirkx 0:a259777c45a3 624 }
dirkx 0:a259777c45a3 625
dirkx 0:a259777c45a3 626 /* check MTU of the netif */
dirkx 0:a259777c45a3 627 if (netif->mtu < DHCP_MAX_MSG_LEN_MIN_REQUIRED) {
dirkx 0:a259777c45a3 628 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): Cannot use this netif with DHCP: MTU is too small\n"));
dirkx 0:a259777c45a3 629 return ERR_MEM;
dirkx 0:a259777c45a3 630 }
dirkx 0:a259777c45a3 631
dirkx 0:a259777c45a3 632 /* no DHCP client attached yet? */
dirkx 0:a259777c45a3 633 if (dhcp == NULL) {
dirkx 0:a259777c45a3 634 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting new DHCP client\n"));
dirkx 0:a259777c45a3 635 dhcp = (struct dhcp *)mem_malloc(sizeof(struct dhcp));
dirkx 0:a259777c45a3 636 if (dhcp == NULL) {
dirkx 0:a259777c45a3 637 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): could not allocate dhcp\n"));
dirkx 0:a259777c45a3 638 return ERR_MEM;
dirkx 0:a259777c45a3 639 }
dirkx 0:a259777c45a3 640 /* store this dhcp client in the netif */
dirkx 0:a259777c45a3 641 netif->dhcp = dhcp;
dirkx 0:a259777c45a3 642 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): allocated dhcp"));
dirkx 0:a259777c45a3 643 /* already has DHCP client attached */
dirkx 0:a259777c45a3 644 } else {
dirkx 0:a259777c45a3 645 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_start(): restarting DHCP configuration\n"));
dirkx 0:a259777c45a3 646 if (dhcp->pcb != NULL) {
dirkx 0:a259777c45a3 647 udp_remove(dhcp->pcb);
dirkx 0:a259777c45a3 648 }
dirkx 0:a259777c45a3 649 LWIP_ASSERT("pbuf p_out wasn't freed", dhcp->p_out == NULL);
dirkx 0:a259777c45a3 650 LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL );
dirkx 0:a259777c45a3 651 }
dirkx 0:a259777c45a3 652
dirkx 0:a259777c45a3 653 /* clear data structure */
dirkx 0:a259777c45a3 654 memset(dhcp, 0, sizeof(struct dhcp));
dirkx 0:a259777c45a3 655 /* dhcp_set_state(&dhcp, DHCP_OFF); */
dirkx 0:a259777c45a3 656 /* allocate UDP PCB */
dirkx 0:a259777c45a3 657 dhcp->pcb = udp_new();
dirkx 0:a259777c45a3 658 if (dhcp->pcb == NULL) {
dirkx 0:a259777c45a3 659 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): could not obtain pcb\n"));
dirkx 0:a259777c45a3 660 return ERR_MEM;
dirkx 0:a259777c45a3 661 }
dirkx 0:a259777c45a3 662 dhcp->pcb->so_options |= SOF_BROADCAST;
dirkx 0:a259777c45a3 663 /* set up local and remote port for the pcb */
dirkx 0:a259777c45a3 664 udp_bind(dhcp->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
dirkx 0:a259777c45a3 665 udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
dirkx 0:a259777c45a3 666 /* set up the recv callback and argument */
dirkx 0:a259777c45a3 667 udp_recv(dhcp->pcb, dhcp_recv, netif);
dirkx 0:a259777c45a3 668 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting DHCP configuration\n"));
dirkx 0:a259777c45a3 669 /* (re)start the DHCP negotiation */
dirkx 0:a259777c45a3 670 result = dhcp_discover(netif);
dirkx 0:a259777c45a3 671 if (result != ERR_OK) {
dirkx 0:a259777c45a3 672 /* free resources allocated above */
dirkx 0:a259777c45a3 673 dhcp_stop(netif);
dirkx 0:a259777c45a3 674 return ERR_MEM;
dirkx 0:a259777c45a3 675 }
dirkx 0:a259777c45a3 676 /* Set the flag that says this netif is handled by DHCP. */
dirkx 0:a259777c45a3 677 netif->flags |= NETIF_FLAG_DHCP;
dirkx 0:a259777c45a3 678 return result;
dirkx 0:a259777c45a3 679 }
dirkx 0:a259777c45a3 680
dirkx 0:a259777c45a3 681 /**
dirkx 0:a259777c45a3 682 * Inform a DHCP server of our manual configuration.
dirkx 0:a259777c45a3 683 *
dirkx 0:a259777c45a3 684 * This informs DHCP servers of our fixed IP address configuration
dirkx 0:a259777c45a3 685 * by sending an INFORM message. It does not involve DHCP address
dirkx 0:a259777c45a3 686 * configuration, it is just here to be nice to the network.
dirkx 0:a259777c45a3 687 *
dirkx 0:a259777c45a3 688 * @param netif The lwIP network interface
dirkx 0:a259777c45a3 689 */
dirkx 0:a259777c45a3 690 void
dirkx 0:a259777c45a3 691 dhcp_inform(struct netif *netif)
dirkx 0:a259777c45a3 692 {
dirkx 0:a259777c45a3 693 struct dhcp dhcp;
dirkx 0:a259777c45a3 694 err_t result = ERR_OK;
dirkx 0:a259777c45a3 695 struct udp_pcb *pcb;
dirkx 0:a259777c45a3 696
dirkx 0:a259777c45a3 697 LWIP_ERROR("netif != NULL", (netif != NULL), return;);
dirkx 0:a259777c45a3 698
dirkx 0:a259777c45a3 699 memset(&dhcp, 0, sizeof(struct dhcp));
dirkx 0:a259777c45a3 700 dhcp_set_state(&dhcp, DHCP_INFORM);
dirkx 0:a259777c45a3 701
dirkx 0:a259777c45a3 702 if ((netif->dhcp != NULL) && (netif->dhcp->pcb != NULL)) {
dirkx 0:a259777c45a3 703 /* re-use existing pcb */
dirkx 0:a259777c45a3 704 pcb = netif->dhcp->pcb;
dirkx 0:a259777c45a3 705 } else {
dirkx 0:a259777c45a3 706 pcb = udp_new();
dirkx 0:a259777c45a3 707 if (pcb == NULL) {
dirkx 0:a259777c45a3 708 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_inform(): could not obtain pcb"));
dirkx 0:a259777c45a3 709 return;
dirkx 0:a259777c45a3 710 }
dirkx 0:a259777c45a3 711 dhcp.pcb = pcb;
dirkx 0:a259777c45a3 712 dhcp.pcb->so_options |= SOF_BROADCAST;
dirkx 0:a259777c45a3 713 udp_bind(dhcp.pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
dirkx 0:a259777c45a3 714 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_inform(): created new udp pcb\n"));
dirkx 0:a259777c45a3 715 }
dirkx 0:a259777c45a3 716 /* create and initialize the DHCP message header */
dirkx 0:a259777c45a3 717 result = dhcp_create_msg(netif, &dhcp, DHCP_INFORM);
dirkx 0:a259777c45a3 718 if (result == ERR_OK) {
dirkx 0:a259777c45a3 719 dhcp_option(&dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
dirkx 0:a259777c45a3 720 dhcp_option_short(&dhcp, DHCP_MAX_MSG_LEN(netif));
dirkx 0:a259777c45a3 721
dirkx 0:a259777c45a3 722 dhcp_option_trailer(&dhcp);
dirkx 0:a259777c45a3 723
dirkx 0:a259777c45a3 724 pbuf_realloc(dhcp.p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp.options_out_len);
dirkx 0:a259777c45a3 725
dirkx 0:a259777c45a3 726 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_inform: INFORMING\n"));
dirkx 0:a259777c45a3 727 udp_sendto_if(pcb, dhcp.p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
dirkx 0:a259777c45a3 728 dhcp_delete_msg(&dhcp);
dirkx 0:a259777c45a3 729 } else {
dirkx 0:a259777c45a3 730 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_inform: could not allocate DHCP request\n"));
dirkx 0:a259777c45a3 731 }
dirkx 0:a259777c45a3 732
dirkx 0:a259777c45a3 733 if (dhcp.pcb != NULL) {
dirkx 0:a259777c45a3 734 /* otherwise, the existing pcb was used */
dirkx 0:a259777c45a3 735 udp_remove(dhcp.pcb);
dirkx 0:a259777c45a3 736 }
dirkx 0:a259777c45a3 737 }
dirkx 0:a259777c45a3 738
dirkx 0:a259777c45a3 739 /** Handle a possible change in the network configuration.
dirkx 0:a259777c45a3 740 *
dirkx 0:a259777c45a3 741 * This enters the REBOOTING state to verify that the currently bound
dirkx 0:a259777c45a3 742 * address is still valid.
dirkx 0:a259777c45a3 743 */
dirkx 0:a259777c45a3 744 void
dirkx 0:a259777c45a3 745 dhcp_network_changed(struct netif *netif)
dirkx 0:a259777c45a3 746 {
dirkx 0:a259777c45a3 747 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 748 if (!dhcp)
dirkx 0:a259777c45a3 749 return;
dirkx 0:a259777c45a3 750 switch (dhcp->state) {
dirkx 0:a259777c45a3 751 case DHCP_REBINDING:
dirkx 0:a259777c45a3 752 case DHCP_RENEWING:
dirkx 0:a259777c45a3 753 case DHCP_BOUND:
dirkx 0:a259777c45a3 754 case DHCP_REBOOTING:
dirkx 0:a259777c45a3 755 netif_set_down(netif);
dirkx 0:a259777c45a3 756 dhcp->tries = 0;
dirkx 0:a259777c45a3 757 dhcp_reboot(netif);
dirkx 0:a259777c45a3 758 break;
dirkx 0:a259777c45a3 759 case DHCP_OFF:
dirkx 0:a259777c45a3 760 /* stay off */
dirkx 0:a259777c45a3 761 break;
dirkx 0:a259777c45a3 762 default:
dirkx 0:a259777c45a3 763 dhcp->tries = 0;
dirkx 0:a259777c45a3 764 dhcp_discover(netif);
dirkx 0:a259777c45a3 765 break;
dirkx 0:a259777c45a3 766 }
dirkx 0:a259777c45a3 767 }
dirkx 0:a259777c45a3 768
dirkx 0:a259777c45a3 769 #if DHCP_DOES_ARP_CHECK
dirkx 0:a259777c45a3 770 /**
dirkx 0:a259777c45a3 771 * Match an ARP reply with the offered IP address.
dirkx 0:a259777c45a3 772 *
dirkx 0:a259777c45a3 773 * @param netif the network interface on which the reply was received
dirkx 0:a259777c45a3 774 * @param addr The IP address we received a reply from
dirkx 0:a259777c45a3 775 */
dirkx 0:a259777c45a3 776 void dhcp_arp_reply(struct netif *netif, ip_addr_t *addr)
dirkx 0:a259777c45a3 777 {
dirkx 0:a259777c45a3 778 LWIP_ERROR("netif != NULL", (netif != NULL), return;);
dirkx 0:a259777c45a3 779 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_arp_reply()\n"));
dirkx 0:a259777c45a3 780 /* is a DHCP client doing an ARP check? */
dirkx 0:a259777c45a3 781 if ((netif->dhcp != NULL) && (netif->dhcp->state == DHCP_CHECKING)) {
dirkx 0:a259777c45a3 782 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_arp_reply(): CHECKING, arp reply for 0x%08"X32_F"\n",
dirkx 0:a259777c45a3 783 ip4_addr_get_u32(addr)));
dirkx 0:a259777c45a3 784 /* did a host respond with the address we
dirkx 0:a259777c45a3 785 were offered by the DHCP server? */
dirkx 0:a259777c45a3 786 if (ip_addr_cmp(addr, &netif->dhcp->offered_ip_addr)) {
dirkx 0:a259777c45a3 787 /* we will not accept the offered address */
dirkx 0:a259777c45a3 788 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING,
dirkx 0:a259777c45a3 789 ("dhcp_arp_reply(): arp reply matched with offered address, declining\n"));
dirkx 0:a259777c45a3 790 dhcp_decline(netif);
dirkx 0:a259777c45a3 791 }
dirkx 0:a259777c45a3 792 }
dirkx 0:a259777c45a3 793 }
dirkx 0:a259777c45a3 794
dirkx 0:a259777c45a3 795 /**
dirkx 0:a259777c45a3 796 * Decline an offered lease.
dirkx 0:a259777c45a3 797 *
dirkx 0:a259777c45a3 798 * Tell the DHCP server we do not accept the offered address.
dirkx 0:a259777c45a3 799 * One reason to decline the lease is when we find out the address
dirkx 0:a259777c45a3 800 * is already in use by another host (through ARP).
dirkx 0:a259777c45a3 801 *
dirkx 0:a259777c45a3 802 * @param netif the netif under DHCP control
dirkx 0:a259777c45a3 803 */
dirkx 0:a259777c45a3 804 static err_t
dirkx 0:a259777c45a3 805 dhcp_decline(struct netif *netif)
dirkx 0:a259777c45a3 806 {
dirkx 0:a259777c45a3 807 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 808 err_t result = ERR_OK;
dirkx 0:a259777c45a3 809 u16_t msecs;
dirkx 0:a259777c45a3 810 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline()\n"));
dirkx 0:a259777c45a3 811 dhcp_set_state(dhcp, DHCP_BACKING_OFF);
dirkx 0:a259777c45a3 812 /* create and initialize the DHCP message header */
dirkx 0:a259777c45a3 813 result = dhcp_create_msg(netif, dhcp, DHCP_DECLINE);
dirkx 0:a259777c45a3 814 if (result == ERR_OK) {
dirkx 0:a259777c45a3 815 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
dirkx 0:a259777c45a3 816 dhcp_option_long(dhcp, ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
dirkx 0:a259777c45a3 817
dirkx 0:a259777c45a3 818 dhcp_option_trailer(dhcp);
dirkx 0:a259777c45a3 819 /* resize pbuf to reflect true size of options */
dirkx 0:a259777c45a3 820 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
dirkx 0:a259777c45a3 821
dirkx 0:a259777c45a3 822 /* per section 4.4.4, broadcast DECLINE messages */
dirkx 0:a259777c45a3 823 udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
dirkx 0:a259777c45a3 824 dhcp_delete_msg(dhcp);
dirkx 0:a259777c45a3 825 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_decline: BACKING OFF\n"));
dirkx 0:a259777c45a3 826 } else {
dirkx 0:a259777c45a3 827 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
dirkx 0:a259777c45a3 828 ("dhcp_decline: could not allocate DHCP request\n"));
dirkx 0:a259777c45a3 829 }
dirkx 0:a259777c45a3 830 dhcp->tries++;
dirkx 0:a259777c45a3 831 msecs = 10*1000;
dirkx 0:a259777c45a3 832 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
dirkx 0:a259777c45a3 833 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline(): set request timeout %"U16_F" msecs\n", msecs));
dirkx 0:a259777c45a3 834 return result;
dirkx 0:a259777c45a3 835 }
dirkx 0:a259777c45a3 836 #endif /* DHCP_DOES_ARP_CHECK */
dirkx 0:a259777c45a3 837
dirkx 0:a259777c45a3 838
dirkx 0:a259777c45a3 839 /**
dirkx 0:a259777c45a3 840 * Start the DHCP process, discover a DHCP server.
dirkx 0:a259777c45a3 841 *
dirkx 0:a259777c45a3 842 * @param netif the netif under DHCP control
dirkx 0:a259777c45a3 843 */
dirkx 0:a259777c45a3 844 static err_t
dirkx 0:a259777c45a3 845 dhcp_discover(struct netif *netif)
dirkx 0:a259777c45a3 846 {
dirkx 0:a259777c45a3 847 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 848 err_t result = ERR_OK;
dirkx 0:a259777c45a3 849 u16_t msecs;
dirkx 0:a259777c45a3 850 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover()\n"));
dirkx 0:a259777c45a3 851 ip_addr_set_any(&dhcp->offered_ip_addr);
dirkx 0:a259777c45a3 852 dhcp_set_state(dhcp, DHCP_SELECTING);
dirkx 0:a259777c45a3 853 /* create and initialize the DHCP message header */
dirkx 0:a259777c45a3 854 result = dhcp_create_msg(netif, dhcp, DHCP_DISCOVER);
dirkx 0:a259777c45a3 855 if (result == ERR_OK) {
dirkx 0:a259777c45a3 856 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: making request\n"));
dirkx 0:a259777c45a3 857
dirkx 0:a259777c45a3 858 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
dirkx 0:a259777c45a3 859 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
dirkx 0:a259777c45a3 860
dirkx 0:a259777c45a3 861 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 4/*num options*/);
dirkx 0:a259777c45a3 862 dhcp_option_byte(dhcp, DHCP_OPTION_SUBNET_MASK);
dirkx 0:a259777c45a3 863 dhcp_option_byte(dhcp, DHCP_OPTION_ROUTER);
dirkx 0:a259777c45a3 864 dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
dirkx 0:a259777c45a3 865 dhcp_option_byte(dhcp, DHCP_OPTION_DNS_SERVER);
dirkx 0:a259777c45a3 866
dirkx 0:a259777c45a3 867 dhcp_option_trailer(dhcp);
dirkx 0:a259777c45a3 868
dirkx 0:a259777c45a3 869 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: realloc()ing\n"));
dirkx 0:a259777c45a3 870 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
dirkx 0:a259777c45a3 871
dirkx 0:a259777c45a3 872 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: sendto(DISCOVER, IP_ADDR_BROADCAST, DHCP_SERVER_PORT)\n"));
dirkx 0:a259777c45a3 873 udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
dirkx 0:a259777c45a3 874 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: deleting()ing\n"));
dirkx 0:a259777c45a3 875 dhcp_delete_msg(dhcp);
dirkx 0:a259777c45a3 876 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover: SELECTING\n"));
dirkx 0:a259777c45a3 877 } else {
dirkx 0:a259777c45a3 878 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_discover: could not allocate DHCP request\n"));
dirkx 0:a259777c45a3 879 }
dirkx 0:a259777c45a3 880 dhcp->tries++;
dirkx 0:a259777c45a3 881 #if LWIP_DHCP_AUTOIP_COOP
dirkx 0:a259777c45a3 882 if(dhcp->tries >= LWIP_DHCP_AUTOIP_COOP_TRIES && dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_OFF) {
dirkx 0:a259777c45a3 883 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_ON;
dirkx 0:a259777c45a3 884 autoip_start(netif);
dirkx 0:a259777c45a3 885 }
dirkx 0:a259777c45a3 886 #endif /* LWIP_DHCP_AUTOIP_COOP */
dirkx 0:a259777c45a3 887 msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
dirkx 0:a259777c45a3 888 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
dirkx 0:a259777c45a3 889 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover(): set request timeout %"U16_F" msecs\n", msecs));
dirkx 0:a259777c45a3 890 return result;
dirkx 0:a259777c45a3 891 }
dirkx 0:a259777c45a3 892
dirkx 0:a259777c45a3 893
dirkx 0:a259777c45a3 894 /**
dirkx 0:a259777c45a3 895 * Bind the interface to the offered IP address.
dirkx 0:a259777c45a3 896 *
dirkx 0:a259777c45a3 897 * @param netif network interface to bind to the offered address
dirkx 0:a259777c45a3 898 */
dirkx 0:a259777c45a3 899 static void
dirkx 0:a259777c45a3 900 dhcp_bind(struct netif *netif)
dirkx 0:a259777c45a3 901 {
dirkx 0:a259777c45a3 902 u32_t timeout;
dirkx 0:a259777c45a3 903 struct dhcp *dhcp;
dirkx 0:a259777c45a3 904 ip_addr_t sn_mask, gw_addr;
dirkx 0:a259777c45a3 905 LWIP_ERROR("dhcp_bind: netif != NULL", (netif != NULL), return;);
dirkx 0:a259777c45a3 906 dhcp = netif->dhcp;
dirkx 0:a259777c45a3 907 LWIP_ERROR("dhcp_bind: dhcp != NULL", (dhcp != NULL), return;);
dirkx 0:a259777c45a3 908 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
dirkx 0:a259777c45a3 909
dirkx 0:a259777c45a3 910 /* temporary DHCP lease? */
dirkx 0:a259777c45a3 911 if (dhcp->offered_t1_renew != 0xffffffffUL) {
dirkx 0:a259777c45a3 912 /* set renewal period timer */
dirkx 0:a259777c45a3 913 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t1 renewal timer %"U32_F" secs\n", dhcp->offered_t1_renew));
dirkx 0:a259777c45a3 914 timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
dirkx 0:a259777c45a3 915 if(timeout > 0xffff) {
dirkx 0:a259777c45a3 916 timeout = 0xffff;
dirkx 0:a259777c45a3 917 }
dirkx 0:a259777c45a3 918 dhcp->t1_timeout = (u16_t)timeout;
dirkx 0:a259777c45a3 919 if (dhcp->t1_timeout == 0) {
dirkx 0:a259777c45a3 920 dhcp->t1_timeout = 1;
dirkx 0:a259777c45a3 921 }
dirkx 0:a259777c45a3 922 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t1_renew*1000));
dirkx 0:a259777c45a3 923 }
dirkx 0:a259777c45a3 924 /* set renewal period timer */
dirkx 0:a259777c45a3 925 if (dhcp->offered_t2_rebind != 0xffffffffUL) {
dirkx 0:a259777c45a3 926 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t2 rebind timer %"U32_F" secs\n", dhcp->offered_t2_rebind));
dirkx 0:a259777c45a3 927 timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
dirkx 0:a259777c45a3 928 if(timeout > 0xffff) {
dirkx 0:a259777c45a3 929 timeout = 0xffff;
dirkx 0:a259777c45a3 930 }
dirkx 0:a259777c45a3 931 dhcp->t2_timeout = (u16_t)timeout;
dirkx 0:a259777c45a3 932 if (dhcp->t2_timeout == 0) {
dirkx 0:a259777c45a3 933 dhcp->t2_timeout = 1;
dirkx 0:a259777c45a3 934 }
dirkx 0:a259777c45a3 935 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t2_rebind*1000));
dirkx 0:a259777c45a3 936 }
dirkx 0:a259777c45a3 937
dirkx 0:a259777c45a3 938 if (dhcp->subnet_mask_given) {
dirkx 0:a259777c45a3 939 /* copy offered network mask */
dirkx 0:a259777c45a3 940 ip_addr_copy(sn_mask, dhcp->offered_sn_mask);
dirkx 0:a259777c45a3 941 } else {
dirkx 0:a259777c45a3 942 /* subnet mask not given, choose a safe subnet mask given the network class */
dirkx 0:a259777c45a3 943 u8_t first_octet = ip4_addr1(&dhcp->offered_ip_addr);
dirkx 0:a259777c45a3 944 if (first_octet <= 127) {
dirkx 0:a259777c45a3 945 ip4_addr_set_u32(&sn_mask, htonl(0xff000000));
dirkx 0:a259777c45a3 946 } else if (first_octet >= 192) {
dirkx 0:a259777c45a3 947 ip4_addr_set_u32(&sn_mask, htonl(0xffffff00));
dirkx 0:a259777c45a3 948 } else {
dirkx 0:a259777c45a3 949 ip4_addr_set_u32(&sn_mask, htonl(0xffff0000));
dirkx 0:a259777c45a3 950 }
dirkx 0:a259777c45a3 951 }
dirkx 0:a259777c45a3 952
dirkx 0:a259777c45a3 953 ip_addr_copy(gw_addr, dhcp->offered_gw_addr);
dirkx 0:a259777c45a3 954 /* gateway address not given? */
dirkx 0:a259777c45a3 955 if (ip_addr_isany(&gw_addr)) {
dirkx 0:a259777c45a3 956 /* copy network address */
dirkx 0:a259777c45a3 957 ip_addr_get_network(&gw_addr, &dhcp->offered_ip_addr, &sn_mask);
dirkx 0:a259777c45a3 958 /* use first host address on network as gateway */
dirkx 0:a259777c45a3 959 ip4_addr_set_u32(&gw_addr, ip4_addr_get_u32(&gw_addr) | htonl(0x00000001));
dirkx 0:a259777c45a3 960 }
dirkx 0:a259777c45a3 961
dirkx 0:a259777c45a3 962 #if LWIP_DHCP_AUTOIP_COOP
dirkx 0:a259777c45a3 963 if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
dirkx 0:a259777c45a3 964 autoip_stop(netif);
dirkx 0:a259777c45a3 965 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
dirkx 0:a259777c45a3 966 }
dirkx 0:a259777c45a3 967 #endif /* LWIP_DHCP_AUTOIP_COOP */
dirkx 0:a259777c45a3 968
dirkx 0:a259777c45a3 969 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): IP: 0x%08"X32_F"\n",
dirkx 0:a259777c45a3 970 ip4_addr_get_u32(&dhcp->offered_ip_addr)));
dirkx 0:a259777c45a3 971 netif_set_ipaddr(netif, &dhcp->offered_ip_addr);
dirkx 0:a259777c45a3 972 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): SN: 0x%08"X32_F"\n",
dirkx 0:a259777c45a3 973 ip4_addr_get_u32(&sn_mask)));
dirkx 0:a259777c45a3 974 netif_set_netmask(netif, &sn_mask);
dirkx 0:a259777c45a3 975 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): GW: 0x%08"X32_F"\n",
dirkx 0:a259777c45a3 976 ip4_addr_get_u32(&gw_addr)));
dirkx 0:a259777c45a3 977 netif_set_gw(netif, &gw_addr);
dirkx 0:a259777c45a3 978 /* bring the interface up */
dirkx 0:a259777c45a3 979 netif_set_up(netif);
dirkx 0:a259777c45a3 980 /* netif is now bound to DHCP leased address */
dirkx 0:a259777c45a3 981 dhcp_set_state(dhcp, DHCP_BOUND);
dirkx 0:a259777c45a3 982 }
dirkx 0:a259777c45a3 983
dirkx 0:a259777c45a3 984 /**
dirkx 0:a259777c45a3 985 * Renew an existing DHCP lease at the involved DHCP server.
dirkx 0:a259777c45a3 986 *
dirkx 0:a259777c45a3 987 * @param netif network interface which must renew its lease
dirkx 0:a259777c45a3 988 */
dirkx 0:a259777c45a3 989 err_t
dirkx 0:a259777c45a3 990 dhcp_renew(struct netif *netif)
dirkx 0:a259777c45a3 991 {
dirkx 0:a259777c45a3 992 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 993 err_t result;
dirkx 0:a259777c45a3 994 u16_t msecs;
dirkx 0:a259777c45a3 995 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_renew()\n"));
dirkx 0:a259777c45a3 996 dhcp_set_state(dhcp, DHCP_RENEWING);
dirkx 0:a259777c45a3 997
dirkx 0:a259777c45a3 998 /* create and initialize the DHCP message header */
dirkx 0:a259777c45a3 999 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
dirkx 0:a259777c45a3 1000 if (result == ERR_OK) {
dirkx 0:a259777c45a3 1001 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
dirkx 0:a259777c45a3 1002 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
dirkx 0:a259777c45a3 1003
dirkx 0:a259777c45a3 1004 #if LWIP_NETIF_HOSTNAME
dirkx 0:a259777c45a3 1005 if (netif->hostname != NULL) {
dirkx 0:a259777c45a3 1006 const char *p = (const char*)netif->hostname;
dirkx 0:a259777c45a3 1007 u8_t namelen = (u8_t)strlen(p);
dirkx 0:a259777c45a3 1008 if (namelen > 0) {
dirkx 0:a259777c45a3 1009 LWIP_ASSERT("DHCP: hostname is too long!", namelen < 255);
dirkx 0:a259777c45a3 1010 dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, namelen);
dirkx 0:a259777c45a3 1011 while (*p) {
dirkx 0:a259777c45a3 1012 dhcp_option_byte(dhcp, *p++);
dirkx 0:a259777c45a3 1013 }
dirkx 0:a259777c45a3 1014 }
dirkx 0:a259777c45a3 1015 }
dirkx 0:a259777c45a3 1016 #endif /* LWIP_NETIF_HOSTNAME */
dirkx 0:a259777c45a3 1017
dirkx 0:a259777c45a3 1018 #if 0
dirkx 0:a259777c45a3 1019 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
dirkx 0:a259777c45a3 1020 dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
dirkx 0:a259777c45a3 1021 #endif
dirkx 0:a259777c45a3 1022
dirkx 0:a259777c45a3 1023 #if 0
dirkx 0:a259777c45a3 1024 dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
dirkx 0:a259777c45a3 1025 dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
dirkx 0:a259777c45a3 1026 #endif
dirkx 0:a259777c45a3 1027 /* append DHCP message trailer */
dirkx 0:a259777c45a3 1028 dhcp_option_trailer(dhcp);
dirkx 0:a259777c45a3 1029
dirkx 0:a259777c45a3 1030 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
dirkx 0:a259777c45a3 1031
dirkx 0:a259777c45a3 1032 udp_sendto_if(dhcp->pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif);
dirkx 0:a259777c45a3 1033 dhcp_delete_msg(dhcp);
dirkx 0:a259777c45a3 1034
dirkx 0:a259777c45a3 1035 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew: RENEWING\n"));
dirkx 0:a259777c45a3 1036 } else {
dirkx 0:a259777c45a3 1037 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_renew: could not allocate DHCP request\n"));
dirkx 0:a259777c45a3 1038 }
dirkx 0:a259777c45a3 1039 dhcp->tries++;
dirkx 0:a259777c45a3 1040 /* back-off on retries, but to a maximum of 20 seconds */
dirkx 0:a259777c45a3 1041 msecs = dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000;
dirkx 0:a259777c45a3 1042 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
dirkx 0:a259777c45a3 1043 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew(): set request timeout %"U16_F" msecs\n", msecs));
dirkx 0:a259777c45a3 1044 return result;
dirkx 0:a259777c45a3 1045 }
dirkx 0:a259777c45a3 1046
dirkx 0:a259777c45a3 1047 /**
dirkx 0:a259777c45a3 1048 * Rebind with a DHCP server for an existing DHCP lease.
dirkx 0:a259777c45a3 1049 *
dirkx 0:a259777c45a3 1050 * @param netif network interface which must rebind with a DHCP server
dirkx 0:a259777c45a3 1051 */
dirkx 0:a259777c45a3 1052 static err_t
dirkx 0:a259777c45a3 1053 dhcp_rebind(struct netif *netif)
dirkx 0:a259777c45a3 1054 {
dirkx 0:a259777c45a3 1055 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 1056 err_t result;
dirkx 0:a259777c45a3 1057 u16_t msecs;
dirkx 0:a259777c45a3 1058 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind()\n"));
dirkx 0:a259777c45a3 1059 dhcp_set_state(dhcp, DHCP_REBINDING);
dirkx 0:a259777c45a3 1060
dirkx 0:a259777c45a3 1061 /* create and initialize the DHCP message header */
dirkx 0:a259777c45a3 1062 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
dirkx 0:a259777c45a3 1063 if (result == ERR_OK) {
dirkx 0:a259777c45a3 1064 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
dirkx 0:a259777c45a3 1065 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
dirkx 0:a259777c45a3 1066
dirkx 0:a259777c45a3 1067 #if LWIP_NETIF_HOSTNAME
dirkx 0:a259777c45a3 1068 if (netif->hostname != NULL) {
dirkx 0:a259777c45a3 1069 const char *p = (const char*)netif->hostname;
dirkx 0:a259777c45a3 1070 u8_t namelen = (u8_t)strlen(p);
dirkx 0:a259777c45a3 1071 if (namelen > 0) {
dirkx 0:a259777c45a3 1072 LWIP_ASSERT("DHCP: hostname is too long!", namelen < 255);
dirkx 0:a259777c45a3 1073 dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, namelen);
dirkx 0:a259777c45a3 1074 while (*p) {
dirkx 0:a259777c45a3 1075 dhcp_option_byte(dhcp, *p++);
dirkx 0:a259777c45a3 1076 }
dirkx 0:a259777c45a3 1077 }
dirkx 0:a259777c45a3 1078 }
dirkx 0:a259777c45a3 1079 #endif /* LWIP_NETIF_HOSTNAME */
dirkx 0:a259777c45a3 1080
dirkx 0:a259777c45a3 1081 #if 0
dirkx 0:a259777c45a3 1082 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
dirkx 0:a259777c45a3 1083 dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
dirkx 0:a259777c45a3 1084
dirkx 0:a259777c45a3 1085 dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
dirkx 0:a259777c45a3 1086 dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
dirkx 0:a259777c45a3 1087 #endif
dirkx 0:a259777c45a3 1088
dirkx 0:a259777c45a3 1089 dhcp_option_trailer(dhcp);
dirkx 0:a259777c45a3 1090
dirkx 0:a259777c45a3 1091 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
dirkx 0:a259777c45a3 1092
dirkx 0:a259777c45a3 1093 /* broadcast to server */
dirkx 0:a259777c45a3 1094 udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
dirkx 0:a259777c45a3 1095 dhcp_delete_msg(dhcp);
dirkx 0:a259777c45a3 1096 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind: REBINDING\n"));
dirkx 0:a259777c45a3 1097 } else {
dirkx 0:a259777c45a3 1098 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_rebind: could not allocate DHCP request\n"));
dirkx 0:a259777c45a3 1099 }
dirkx 0:a259777c45a3 1100 dhcp->tries++;
dirkx 0:a259777c45a3 1101 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
dirkx 0:a259777c45a3 1102 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
dirkx 0:a259777c45a3 1103 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind(): set request timeout %"U16_F" msecs\n", msecs));
dirkx 0:a259777c45a3 1104 return result;
dirkx 0:a259777c45a3 1105 }
dirkx 0:a259777c45a3 1106
dirkx 0:a259777c45a3 1107 /**
dirkx 0:a259777c45a3 1108 * Enter REBOOTING state to verify an existing lease
dirkx 0:a259777c45a3 1109 *
dirkx 0:a259777c45a3 1110 * @param netif network interface which must reboot
dirkx 0:a259777c45a3 1111 */
dirkx 0:a259777c45a3 1112 static err_t
dirkx 0:a259777c45a3 1113 dhcp_reboot(struct netif *netif)
dirkx 0:a259777c45a3 1114 {
dirkx 0:a259777c45a3 1115 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 1116 err_t result;
dirkx 0:a259777c45a3 1117 u16_t msecs;
dirkx 0:a259777c45a3 1118 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot()\n"));
dirkx 0:a259777c45a3 1119 dhcp_set_state(dhcp, DHCP_REBOOTING);
dirkx 0:a259777c45a3 1120
dirkx 0:a259777c45a3 1121 /* create and initialize the DHCP message header */
dirkx 0:a259777c45a3 1122 result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
dirkx 0:a259777c45a3 1123 if (result == ERR_OK) {
dirkx 0:a259777c45a3 1124 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
dirkx 0:a259777c45a3 1125 dhcp_option_short(dhcp, 576);
dirkx 0:a259777c45a3 1126
dirkx 0:a259777c45a3 1127 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
dirkx 0:a259777c45a3 1128 dhcp_option_long(dhcp, ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
dirkx 0:a259777c45a3 1129
dirkx 0:a259777c45a3 1130 dhcp_option_trailer(dhcp);
dirkx 0:a259777c45a3 1131
dirkx 0:a259777c45a3 1132 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
dirkx 0:a259777c45a3 1133
dirkx 0:a259777c45a3 1134 /* broadcast to server */
dirkx 0:a259777c45a3 1135 udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
dirkx 0:a259777c45a3 1136 dhcp_delete_msg(dhcp);
dirkx 0:a259777c45a3 1137 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot: REBOOTING\n"));
dirkx 0:a259777c45a3 1138 } else {
dirkx 0:a259777c45a3 1139 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_reboot: could not allocate DHCP request\n"));
dirkx 0:a259777c45a3 1140 }
dirkx 0:a259777c45a3 1141 dhcp->tries++;
dirkx 0:a259777c45a3 1142 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
dirkx 0:a259777c45a3 1143 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
dirkx 0:a259777c45a3 1144 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot(): set request timeout %"U16_F" msecs\n", msecs));
dirkx 0:a259777c45a3 1145 return result;
dirkx 0:a259777c45a3 1146 }
dirkx 0:a259777c45a3 1147
dirkx 0:a259777c45a3 1148
dirkx 0:a259777c45a3 1149 /**
dirkx 0:a259777c45a3 1150 * Release a DHCP lease.
dirkx 0:a259777c45a3 1151 *
dirkx 0:a259777c45a3 1152 * @param netif network interface which must release its lease
dirkx 0:a259777c45a3 1153 */
dirkx 0:a259777c45a3 1154 err_t
dirkx 0:a259777c45a3 1155 dhcp_release(struct netif *netif)
dirkx 0:a259777c45a3 1156 {
dirkx 0:a259777c45a3 1157 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 1158 err_t result;
dirkx 0:a259777c45a3 1159 u16_t msecs;
dirkx 0:a259777c45a3 1160 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_release()\n"));
dirkx 0:a259777c45a3 1161
dirkx 0:a259777c45a3 1162 /* idle DHCP client */
dirkx 0:a259777c45a3 1163 dhcp_set_state(dhcp, DHCP_OFF);
dirkx 0:a259777c45a3 1164 /* clean old DHCP offer */
dirkx 0:a259777c45a3 1165 ip_addr_set_zero(&dhcp->server_ip_addr);
dirkx 0:a259777c45a3 1166 ip_addr_set_zero(&dhcp->offered_ip_addr);
dirkx 0:a259777c45a3 1167 ip_addr_set_zero(&dhcp->offered_sn_mask);
dirkx 0:a259777c45a3 1168 ip_addr_set_zero(&dhcp->offered_gw_addr);
dirkx 0:a259777c45a3 1169 #if LWIP_DHCP_BOOTP_FILE
dirkx 0:a259777c45a3 1170 ip_addr_set_zero(&dhcp->offered_si_addr);
dirkx 0:a259777c45a3 1171 #endif /* LWIP_DHCP_BOOTP_FILE */
dirkx 0:a259777c45a3 1172 dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0;
dirkx 0:a259777c45a3 1173
dirkx 0:a259777c45a3 1174 /* create and initialize the DHCP message header */
dirkx 0:a259777c45a3 1175 result = dhcp_create_msg(netif, dhcp, DHCP_RELEASE);
dirkx 0:a259777c45a3 1176 if (result == ERR_OK) {
dirkx 0:a259777c45a3 1177 dhcp_option_trailer(dhcp);
dirkx 0:a259777c45a3 1178
dirkx 0:a259777c45a3 1179 pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
dirkx 0:a259777c45a3 1180
dirkx 0:a259777c45a3 1181 udp_sendto_if(dhcp->pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif);
dirkx 0:a259777c45a3 1182 dhcp_delete_msg(dhcp);
dirkx 0:a259777c45a3 1183 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release: RELEASED, DHCP_OFF\n"));
dirkx 0:a259777c45a3 1184 } else {
dirkx 0:a259777c45a3 1185 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_release: could not allocate DHCP request\n"));
dirkx 0:a259777c45a3 1186 }
dirkx 0:a259777c45a3 1187 dhcp->tries++;
dirkx 0:a259777c45a3 1188 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
dirkx 0:a259777c45a3 1189 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
dirkx 0:a259777c45a3 1190 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release(): set request timeout %"U16_F" msecs\n", msecs));
dirkx 0:a259777c45a3 1191 /* bring the interface down */
dirkx 0:a259777c45a3 1192 netif_set_down(netif);
dirkx 0:a259777c45a3 1193 /* remove IP address from interface */
dirkx 0:a259777c45a3 1194 netif_set_ipaddr(netif, IP_ADDR_ANY);
dirkx 0:a259777c45a3 1195 netif_set_gw(netif, IP_ADDR_ANY);
dirkx 0:a259777c45a3 1196 netif_set_netmask(netif, IP_ADDR_ANY);
dirkx 0:a259777c45a3 1197
dirkx 0:a259777c45a3 1198 return result;
dirkx 0:a259777c45a3 1199 }
dirkx 0:a259777c45a3 1200
dirkx 0:a259777c45a3 1201 /**
dirkx 0:a259777c45a3 1202 * Remove the DHCP client from the interface.
dirkx 0:a259777c45a3 1203 *
dirkx 0:a259777c45a3 1204 * @param netif The network interface to stop DHCP on
dirkx 0:a259777c45a3 1205 */
dirkx 0:a259777c45a3 1206 void
dirkx 0:a259777c45a3 1207 dhcp_stop(struct netif *netif)
dirkx 0:a259777c45a3 1208 {
dirkx 0:a259777c45a3 1209 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 1210 LWIP_ERROR("dhcp_stop: netif != NULL", (netif != NULL), return;);
dirkx 0:a259777c45a3 1211 /* Remove the flag that says this netif is handled by DHCP. */
dirkx 0:a259777c45a3 1212 netif->flags &= ~NETIF_FLAG_DHCP;
dirkx 0:a259777c45a3 1213
dirkx 0:a259777c45a3 1214 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_stop()\n"));
dirkx 0:a259777c45a3 1215 /* netif is DHCP configured? */
dirkx 0:a259777c45a3 1216 if (dhcp != NULL) {
dirkx 0:a259777c45a3 1217 #if LWIP_DHCP_AUTOIP_COOP
dirkx 0:a259777c45a3 1218 if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
dirkx 0:a259777c45a3 1219 autoip_stop(netif);
dirkx 0:a259777c45a3 1220 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
dirkx 0:a259777c45a3 1221 }
dirkx 0:a259777c45a3 1222 #endif /* LWIP_DHCP_AUTOIP_COOP */
dirkx 0:a259777c45a3 1223
dirkx 0:a259777c45a3 1224 if (dhcp->pcb != NULL) {
dirkx 0:a259777c45a3 1225 udp_remove(dhcp->pcb);
dirkx 0:a259777c45a3 1226 dhcp->pcb = NULL;
dirkx 0:a259777c45a3 1227 }
dirkx 0:a259777c45a3 1228 LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL);
dirkx 0:a259777c45a3 1229 dhcp_set_state(dhcp, DHCP_OFF);
dirkx 0:a259777c45a3 1230 }
dirkx 0:a259777c45a3 1231 }
dirkx 0:a259777c45a3 1232
dirkx 0:a259777c45a3 1233 /*
dirkx 0:a259777c45a3 1234 * Set the DHCP state of a DHCP client.
dirkx 0:a259777c45a3 1235 *
dirkx 0:a259777c45a3 1236 * If the state changed, reset the number of tries.
dirkx 0:a259777c45a3 1237 */
dirkx 0:a259777c45a3 1238 static void
dirkx 0:a259777c45a3 1239 dhcp_set_state(struct dhcp *dhcp, u8_t new_state)
dirkx 0:a259777c45a3 1240 {
dirkx 0:a259777c45a3 1241 if (new_state != dhcp->state) {
dirkx 0:a259777c45a3 1242 dhcp->state = new_state;
dirkx 0:a259777c45a3 1243 dhcp->tries = 0;
dirkx 0:a259777c45a3 1244 dhcp->request_timeout = 0;
dirkx 0:a259777c45a3 1245 }
dirkx 0:a259777c45a3 1246 }
dirkx 0:a259777c45a3 1247
dirkx 0:a259777c45a3 1248 /*
dirkx 0:a259777c45a3 1249 * Concatenate an option type and length field to the outgoing
dirkx 0:a259777c45a3 1250 * DHCP message.
dirkx 0:a259777c45a3 1251 *
dirkx 0:a259777c45a3 1252 */
dirkx 0:a259777c45a3 1253 static void
dirkx 0:a259777c45a3 1254 dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len)
dirkx 0:a259777c45a3 1255 {
dirkx 0:a259777c45a3 1256 LWIP_ASSERT("dhcp_option: dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U + option_len <= DHCP_OPTIONS_LEN);
dirkx 0:a259777c45a3 1257 dhcp->msg_out->options[dhcp->options_out_len++] = option_type;
dirkx 0:a259777c45a3 1258 dhcp->msg_out->options[dhcp->options_out_len++] = option_len;
dirkx 0:a259777c45a3 1259 }
dirkx 0:a259777c45a3 1260 /*
dirkx 0:a259777c45a3 1261 * Concatenate a single byte to the outgoing DHCP message.
dirkx 0:a259777c45a3 1262 *
dirkx 0:a259777c45a3 1263 */
dirkx 0:a259777c45a3 1264 static void
dirkx 0:a259777c45a3 1265 dhcp_option_byte(struct dhcp *dhcp, u8_t value)
dirkx 0:a259777c45a3 1266 {
dirkx 0:a259777c45a3 1267 LWIP_ASSERT("dhcp_option_byte: dhcp->options_out_len < DHCP_OPTIONS_LEN", dhcp->options_out_len < DHCP_OPTIONS_LEN);
dirkx 0:a259777c45a3 1268 dhcp->msg_out->options[dhcp->options_out_len++] = value;
dirkx 0:a259777c45a3 1269 }
dirkx 0:a259777c45a3 1270
dirkx 0:a259777c45a3 1271 static void
dirkx 0:a259777c45a3 1272 dhcp_option_short(struct dhcp *dhcp, u16_t value)
dirkx 0:a259777c45a3 1273 {
dirkx 0:a259777c45a3 1274 LWIP_ASSERT("dhcp_option_short: dhcp->options_out_len + 2 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U <= DHCP_OPTIONS_LEN);
dirkx 0:a259777c45a3 1275 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff00U) >> 8);
dirkx 0:a259777c45a3 1276 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t) (value & 0x00ffU);
dirkx 0:a259777c45a3 1277 }
dirkx 0:a259777c45a3 1278
dirkx 0:a259777c45a3 1279 static void
dirkx 0:a259777c45a3 1280 dhcp_option_long(struct dhcp *dhcp, u32_t value)
dirkx 0:a259777c45a3 1281 {
dirkx 0:a259777c45a3 1282 LWIP_ASSERT("dhcp_option_long: dhcp->options_out_len + 4 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 4U <= DHCP_OPTIONS_LEN);
dirkx 0:a259777c45a3 1283 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff000000UL) >> 24);
dirkx 0:a259777c45a3 1284 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x00ff0000UL) >> 16);
dirkx 0:a259777c45a3 1285 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x0000ff00UL) >> 8);
dirkx 0:a259777c45a3 1286 dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x000000ffUL));
dirkx 0:a259777c45a3 1287 }
dirkx 0:a259777c45a3 1288
dirkx 0:a259777c45a3 1289 /**
dirkx 0:a259777c45a3 1290 * Extract the DHCP message and the DHCP options.
dirkx 0:a259777c45a3 1291 *
dirkx 0:a259777c45a3 1292 * Extract the DHCP message and the DHCP options, each into a contiguous
dirkx 0:a259777c45a3 1293 * piece of memory. As a DHCP message is variable sized by its options,
dirkx 0:a259777c45a3 1294 * and also allows overriding some fields for options, the easy approach
dirkx 0:a259777c45a3 1295 * is to first unfold the options into a conitguous piece of memory, and
dirkx 0:a259777c45a3 1296 * use that further on.
dirkx 0:a259777c45a3 1297 *
dirkx 0:a259777c45a3 1298 */
dirkx 0:a259777c45a3 1299 static err_t
dirkx 0:a259777c45a3 1300 dhcp_parse_reply(struct dhcp *dhcp, struct pbuf *p)
dirkx 0:a259777c45a3 1301 {
dirkx 0:a259777c45a3 1302 u8_t *options;
dirkx 0:a259777c45a3 1303 u16_t offset;
dirkx 0:a259777c45a3 1304 u16_t offset_max;
dirkx 0:a259777c45a3 1305 u16_t options_idx;
dirkx 0:a259777c45a3 1306 u16_t options_idx_max;
dirkx 0:a259777c45a3 1307 struct pbuf *q;
dirkx 0:a259777c45a3 1308 int parse_file_as_options = 0;
dirkx 0:a259777c45a3 1309 int parse_sname_as_options = 0;
dirkx 0:a259777c45a3 1310
dirkx 0:a259777c45a3 1311 /* clear received options */
dirkx 0:a259777c45a3 1312 dhcp_clear_all_options(dhcp);
dirkx 0:a259777c45a3 1313 /* check that beginning of dhcp_msg (up to and including chaddr) is in first pbuf */
dirkx 0:a259777c45a3 1314 if (p->len < DHCP_SNAME_OFS) {
dirkx 0:a259777c45a3 1315 return ERR_BUF;
dirkx 0:a259777c45a3 1316 }
dirkx 0:a259777c45a3 1317 dhcp->msg_in = (struct dhcp_msg *)p->payload;
dirkx 0:a259777c45a3 1318 #if LWIP_DHCP_BOOTP_FILE
dirkx 0:a259777c45a3 1319 /* clear boot file name */
dirkx 0:a259777c45a3 1320 dhcp->boot_file_name[0] = 0;
dirkx 0:a259777c45a3 1321 #endif /* LWIP_DHCP_BOOTP_FILE */
dirkx 0:a259777c45a3 1322
dirkx 0:a259777c45a3 1323 /* parse options */
dirkx 0:a259777c45a3 1324
dirkx 0:a259777c45a3 1325 /* start with options field */
dirkx 0:a259777c45a3 1326 options_idx = DHCP_OPTIONS_OFS;
dirkx 0:a259777c45a3 1327 /* parse options to the end of the received packet */
dirkx 0:a259777c45a3 1328 options_idx_max = p->tot_len;
dirkx 0:a259777c45a3 1329 again:
dirkx 0:a259777c45a3 1330 q = p;
dirkx 0:a259777c45a3 1331 while((q != NULL) && (options_idx >= q->len)) {
dirkx 0:a259777c45a3 1332 options_idx -= q->len;
dirkx 0:a259777c45a3 1333 options_idx_max -= q->len;
dirkx 0:a259777c45a3 1334 q = q->next;
dirkx 0:a259777c45a3 1335 }
dirkx 0:a259777c45a3 1336 if (q == NULL) {
dirkx 0:a259777c45a3 1337 return ERR_BUF;
dirkx 0:a259777c45a3 1338 }
dirkx 0:a259777c45a3 1339 offset = options_idx;
dirkx 0:a259777c45a3 1340 offset_max = options_idx_max;
dirkx 0:a259777c45a3 1341 options = (u8_t*)q->payload;
dirkx 0:a259777c45a3 1342 /* at least 1 byte to read and no end marker, then at least 3 bytes to read? */
dirkx 0:a259777c45a3 1343 while((q != NULL) && (options[offset] != DHCP_OPTION_END) && (offset < offset_max)) {
dirkx 0:a259777c45a3 1344 u8_t op = options[offset];
dirkx 0:a259777c45a3 1345 u8_t len;
dirkx 0:a259777c45a3 1346 u8_t decode_len = 0;
dirkx 0:a259777c45a3 1347 int decode_idx = -1;
dirkx 0:a259777c45a3 1348 u16_t val_offset = offset + 2;
dirkx 0:a259777c45a3 1349 /* len byte might be in the next pbuf */
dirkx 0:a259777c45a3 1350 if (offset + 1 < q->len) {
dirkx 0:a259777c45a3 1351 len = options[offset + 1];
dirkx 0:a259777c45a3 1352 } else {
dirkx 0:a259777c45a3 1353 len = (q->next != NULL ? ((u8_t*)q->next->payload)[0] : 0);
dirkx 0:a259777c45a3 1354 }
dirkx 0:a259777c45a3 1355 /* LWIP_DEBUGF(DHCP_DEBUG, ("msg_offset=%"U16_F", q->len=%"U16_F, msg_offset, q->len)); */
dirkx 0:a259777c45a3 1356 decode_len = len;
dirkx 0:a259777c45a3 1357 switch(op) {
dirkx 0:a259777c45a3 1358 /* case(DHCP_OPTION_END): handled above */
dirkx 0:a259777c45a3 1359 case(DHCP_OPTION_PAD):
dirkx 0:a259777c45a3 1360 /* special option: no len encoded */
dirkx 0:a259777c45a3 1361 decode_len = len = 0;
dirkx 0:a259777c45a3 1362 /* will be increased below */
dirkx 0:a259777c45a3 1363 offset--;
dirkx 0:a259777c45a3 1364 break;
dirkx 0:a259777c45a3 1365 case(DHCP_OPTION_SUBNET_MASK):
dirkx 0:a259777c45a3 1366 LWIP_ASSERT("len == 4", len == 4);
dirkx 0:a259777c45a3 1367 decode_idx = DHCP_OPTION_IDX_SUBNET_MASK;
dirkx 0:a259777c45a3 1368 break;
dirkx 0:a259777c45a3 1369 case(DHCP_OPTION_ROUTER):
dirkx 0:a259777c45a3 1370 decode_len = 4; /* only copy the first given router */
dirkx 0:a259777c45a3 1371 LWIP_ASSERT("len >= decode_len", len >= decode_len);
dirkx 0:a259777c45a3 1372 decode_idx = DHCP_OPTION_IDX_ROUTER;
dirkx 0:a259777c45a3 1373 break;
dirkx 0:a259777c45a3 1374 case(DHCP_OPTION_DNS_SERVER):
dirkx 0:a259777c45a3 1375 /* special case: there might be more than one server */
dirkx 0:a259777c45a3 1376 LWIP_ASSERT("len % 4 == 0", len % 4 == 0);
dirkx 0:a259777c45a3 1377 /* limit number of DNS servers */
dirkx 0:a259777c45a3 1378 decode_len = LWIP_MIN(len, 4 * DNS_MAX_SERVERS);
dirkx 0:a259777c45a3 1379 LWIP_ASSERT("len >= decode_len", len >= decode_len);
dirkx 0:a259777c45a3 1380 decode_idx = DHCP_OPTION_IDX_DNS_SERVER;
dirkx 0:a259777c45a3 1381 break;
dirkx 0:a259777c45a3 1382 case(DHCP_OPTION_LEASE_TIME):
dirkx 0:a259777c45a3 1383 LWIP_ASSERT("len == 4", len == 4);
dirkx 0:a259777c45a3 1384 decode_idx = DHCP_OPTION_IDX_LEASE_TIME;
dirkx 0:a259777c45a3 1385 break;
dirkx 0:a259777c45a3 1386 case(DHCP_OPTION_OVERLOAD):
dirkx 0:a259777c45a3 1387 LWIP_ASSERT("len == 1", len == 1);
dirkx 0:a259777c45a3 1388 decode_idx = DHCP_OPTION_IDX_OVERLOAD;
dirkx 0:a259777c45a3 1389 break;
dirkx 0:a259777c45a3 1390 case(DHCP_OPTION_MESSAGE_TYPE):
dirkx 0:a259777c45a3 1391 LWIP_ASSERT("len == 1", len == 1);
dirkx 0:a259777c45a3 1392 decode_idx = DHCP_OPTION_IDX_MSG_TYPE;
dirkx 0:a259777c45a3 1393 break;
dirkx 0:a259777c45a3 1394 case(DHCP_OPTION_SERVER_ID):
dirkx 0:a259777c45a3 1395 LWIP_ASSERT("len == 4", len == 4);
dirkx 0:a259777c45a3 1396 decode_idx = DHCP_OPTION_IDX_SERVER_ID;
dirkx 0:a259777c45a3 1397 break;
dirkx 0:a259777c45a3 1398 case(DHCP_OPTION_T1):
dirkx 0:a259777c45a3 1399 LWIP_ASSERT("len == 4", len == 4);
dirkx 0:a259777c45a3 1400 decode_idx = DHCP_OPTION_IDX_T1;
dirkx 0:a259777c45a3 1401 break;
dirkx 0:a259777c45a3 1402 case(DHCP_OPTION_T2):
dirkx 0:a259777c45a3 1403 LWIP_ASSERT("len == 4", len == 4);
dirkx 0:a259777c45a3 1404 decode_idx = DHCP_OPTION_IDX_T2;
dirkx 0:a259777c45a3 1405 break;
dirkx 0:a259777c45a3 1406 default:
dirkx 0:a259777c45a3 1407 decode_len = 0;
dirkx 0:a259777c45a3 1408 LWIP_DEBUGF(DHCP_DEBUG, ("skipping option %"U16_F" in options\n", op));
dirkx 0:a259777c45a3 1409 break;
dirkx 0:a259777c45a3 1410 }
dirkx 0:a259777c45a3 1411 offset += len + 2;
dirkx 0:a259777c45a3 1412 if (decode_len > 0) {
dirkx 0:a259777c45a3 1413 u32_t value = 0;
dirkx 0:a259777c45a3 1414 u16_t copy_len;
dirkx 0:a259777c45a3 1415 decode_next:
dirkx 0:a259777c45a3 1416 LWIP_ASSERT("check decode_idx", decode_idx >= 0 && decode_idx < DHCP_OPTION_IDX_MAX);
dirkx 0:a259777c45a3 1417 LWIP_ASSERT("option already decoded", !dhcp_option_given(dhcp, decode_idx));
dirkx 0:a259777c45a3 1418 copy_len = LWIP_MIN(decode_len, 4);
dirkx 0:a259777c45a3 1419 pbuf_copy_partial(q, &value, copy_len, val_offset);
dirkx 0:a259777c45a3 1420 if (decode_len > 4) {
dirkx 0:a259777c45a3 1421 /* decode more than one u32_t */
dirkx 0:a259777c45a3 1422 LWIP_ASSERT("decode_len % 4 == 0", decode_len % 4 == 0);
dirkx 0:a259777c45a3 1423 dhcp_got_option(dhcp, decode_idx);
dirkx 0:a259777c45a3 1424 dhcp_set_option_value(dhcp, decode_idx, htonl(value));
dirkx 0:a259777c45a3 1425 decode_len -= 4;
dirkx 0:a259777c45a3 1426 val_offset += 4;
dirkx 0:a259777c45a3 1427 decode_idx++;
dirkx 0:a259777c45a3 1428 goto decode_next;
dirkx 0:a259777c45a3 1429 } else if (decode_len == 4) {
dirkx 0:a259777c45a3 1430 value = ntohl(value);
dirkx 0:a259777c45a3 1431 } else {
dirkx 0:a259777c45a3 1432 LWIP_ASSERT("invalid decode_len", decode_len == 1);
dirkx 0:a259777c45a3 1433 }
dirkx 0:a259777c45a3 1434 dhcp_got_option(dhcp, decode_idx);
dirkx 0:a259777c45a3 1435 dhcp_set_option_value(dhcp, decode_idx, value);
dirkx 0:a259777c45a3 1436 }
dirkx 0:a259777c45a3 1437 if (offset >= q->len) {
dirkx 0:a259777c45a3 1438 offset -= q->len;
dirkx 0:a259777c45a3 1439 offset_max -= q->len;
dirkx 0:a259777c45a3 1440 q = q->next;
dirkx 0:a259777c45a3 1441 options = (u8_t*)q->payload;
dirkx 0:a259777c45a3 1442 }
dirkx 0:a259777c45a3 1443 }
dirkx 0:a259777c45a3 1444 /* is this an overloaded message? */
dirkx 0:a259777c45a3 1445 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_OVERLOAD)) {
dirkx 0:a259777c45a3 1446 u32_t overload = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_OVERLOAD);
dirkx 0:a259777c45a3 1447 dhcp_clear_option(dhcp, DHCP_OPTION_IDX_OVERLOAD);
dirkx 0:a259777c45a3 1448 if (overload == DHCP_OVERLOAD_FILE) {
dirkx 0:a259777c45a3 1449 parse_file_as_options = 1;
dirkx 0:a259777c45a3 1450 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded file field\n"));
dirkx 0:a259777c45a3 1451 } else if (overload == DHCP_OVERLOAD_SNAME) {
dirkx 0:a259777c45a3 1452 parse_sname_as_options = 1;
dirkx 0:a259777c45a3 1453 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded sname field\n"));
dirkx 0:a259777c45a3 1454 } else if (overload == DHCP_OVERLOAD_SNAME_FILE) {
dirkx 0:a259777c45a3 1455 parse_sname_as_options = 1;
dirkx 0:a259777c45a3 1456 parse_file_as_options = 1;
dirkx 0:a259777c45a3 1457 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded sname and file field\n"));
dirkx 0:a259777c45a3 1458 } else {
dirkx 0:a259777c45a3 1459 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("invalid overload option: %d\n", (int)overload));
dirkx 0:a259777c45a3 1460 }
dirkx 0:a259777c45a3 1461 #if LWIP_DHCP_BOOTP_FILE
dirkx 0:a259777c45a3 1462 if (!parse_file_as_options) {
dirkx 0:a259777c45a3 1463 /* only do this for ACK messages */
dirkx 0:a259777c45a3 1464 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE) &&
dirkx 0:a259777c45a3 1465 (dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE) == DHCP_ACK))
dirkx 0:a259777c45a3 1466 /* copy bootp file name, don't care for sname (server hostname) */
dirkx 0:a259777c45a3 1467 pbuf_copy_partial(p, dhcp->boot_file_name, DHCP_FILE_LEN-1, DHCP_FILE_OFS);
dirkx 0:a259777c45a3 1468 /* make sure the string is really NULL-terminated */
dirkx 0:a259777c45a3 1469 dhcp->boot_file_name[DHCP_FILE_LEN-1] = 0;
dirkx 0:a259777c45a3 1470 }
dirkx 0:a259777c45a3 1471 #endif /* LWIP_DHCP_BOOTP_FILE */
dirkx 0:a259777c45a3 1472 }
dirkx 0:a259777c45a3 1473 if (parse_file_as_options) {
dirkx 0:a259777c45a3 1474 /* if both are overloaded, parse file first and then sname (RFC 2131 ch. 4.1) */
dirkx 0:a259777c45a3 1475 parse_file_as_options = 0;
dirkx 0:a259777c45a3 1476 options_idx = DHCP_FILE_OFS;
dirkx 0:a259777c45a3 1477 options_idx_max = DHCP_FILE_OFS + DHCP_FILE_LEN;
dirkx 0:a259777c45a3 1478 goto again;
dirkx 0:a259777c45a3 1479 } else if (parse_sname_as_options) {
dirkx 0:a259777c45a3 1480 parse_sname_as_options = 0;
dirkx 0:a259777c45a3 1481 options_idx = DHCP_SNAME_OFS;
dirkx 0:a259777c45a3 1482 options_idx_max = DHCP_SNAME_OFS + DHCP_SNAME_LEN;
dirkx 0:a259777c45a3 1483 goto again;
dirkx 0:a259777c45a3 1484 }
dirkx 0:a259777c45a3 1485 return ERR_OK;
dirkx 0:a259777c45a3 1486 }
dirkx 0:a259777c45a3 1487
dirkx 0:a259777c45a3 1488 /**
dirkx 0:a259777c45a3 1489 * If an incoming DHCP message is in response to us, then trigger the state machine
dirkx 0:a259777c45a3 1490 */
dirkx 0:a259777c45a3 1491 static void
dirkx 0:a259777c45a3 1492 dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
dirkx 0:a259777c45a3 1493 {
dirkx 0:a259777c45a3 1494 struct netif *netif = (struct netif *)arg;
dirkx 0:a259777c45a3 1495 struct dhcp *dhcp = netif->dhcp;
dirkx 0:a259777c45a3 1496 struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload;
dirkx 0:a259777c45a3 1497 u8_t msg_type;
dirkx 0:a259777c45a3 1498 u8_t i;
dirkx 0:a259777c45a3 1499 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_recv(pbuf = %p) from DHCP server %"U16_F".%"U16_F".%"U16_F".%"U16_F" port %"U16_F"\n", (void*)p,
dirkx 0:a259777c45a3 1500 ip4_addr1_16(addr), ip4_addr2_16(addr), ip4_addr3_16(addr), ip4_addr4_16(addr), port));
dirkx 0:a259777c45a3 1501 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->len = %"U16_F"\n", p->len));
dirkx 0:a259777c45a3 1502 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->tot_len = %"U16_F"\n", p->tot_len));
dirkx 0:a259777c45a3 1503 /* prevent warnings about unused arguments */
dirkx 0:a259777c45a3 1504 LWIP_UNUSED_ARG(pcb);
dirkx 0:a259777c45a3 1505 LWIP_UNUSED_ARG(addr);
dirkx 0:a259777c45a3 1506 LWIP_UNUSED_ARG(port);
dirkx 0:a259777c45a3 1507
dirkx 0:a259777c45a3 1508 LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL);
dirkx 0:a259777c45a3 1509
dirkx 0:a259777c45a3 1510 if (p->len < DHCP_MIN_REPLY_LEN) {
dirkx 0:a259777c45a3 1511 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("DHCP reply message or pbuf too short\n"));
dirkx 0:a259777c45a3 1512 goto free_pbuf_and_return;
dirkx 0:a259777c45a3 1513 }
dirkx 0:a259777c45a3 1514
dirkx 0:a259777c45a3 1515 if (reply_msg->op != DHCP_BOOTREPLY) {
dirkx 0:a259777c45a3 1516 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("not a DHCP reply message, but type %"U16_F"\n", (u16_t)reply_msg->op));
dirkx 0:a259777c45a3 1517 goto free_pbuf_and_return;
dirkx 0:a259777c45a3 1518 }
dirkx 0:a259777c45a3 1519 /* iterate through hardware address and match against DHCP message */
dirkx 0:a259777c45a3 1520 for (i = 0; i < netif->hwaddr_len; i++) {
dirkx 0:a259777c45a3 1521 if (netif->hwaddr[i] != reply_msg->chaddr[i]) {
dirkx 0:a259777c45a3 1522 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
dirkx 0:a259777c45a3 1523 ("netif->hwaddr[%"U16_F"]==%02"X16_F" != reply_msg->chaddr[%"U16_F"]==%02"X16_F"\n",
dirkx 0:a259777c45a3 1524 (u16_t)i, (u16_t)netif->hwaddr[i], (u16_t)i, (u16_t)reply_msg->chaddr[i]));
dirkx 0:a259777c45a3 1525 goto free_pbuf_and_return;
dirkx 0:a259777c45a3 1526 }
dirkx 0:a259777c45a3 1527 }
dirkx 0:a259777c45a3 1528 /* match transaction ID against what we expected */
dirkx 0:a259777c45a3 1529 if (ntohl(reply_msg->xid) != dhcp->xid) {
dirkx 0:a259777c45a3 1530 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
dirkx 0:a259777c45a3 1531 ("transaction id mismatch reply_msg->xid(%"X32_F")!=dhcp->xid(%"X32_F")\n",ntohl(reply_msg->xid),dhcp->xid));
dirkx 0:a259777c45a3 1532 goto free_pbuf_and_return;
dirkx 0:a259777c45a3 1533 }
dirkx 0:a259777c45a3 1534 /* option fields could be unfold? */
dirkx 0:a259777c45a3 1535 if (dhcp_parse_reply(dhcp, p) != ERR_OK) {
dirkx 0:a259777c45a3 1536 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
dirkx 0:a259777c45a3 1537 ("problem unfolding DHCP message - too short on memory?\n"));
dirkx 0:a259777c45a3 1538 goto free_pbuf_and_return;
dirkx 0:a259777c45a3 1539 }
dirkx 0:a259777c45a3 1540
dirkx 0:a259777c45a3 1541 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("searching DHCP_OPTION_MESSAGE_TYPE\n"));
dirkx 0:a259777c45a3 1542 /* obtain pointer to DHCP message type */
dirkx 0:a259777c45a3 1543 if (!dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE)) {
dirkx 0:a259777c45a3 1544 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("DHCP_OPTION_MESSAGE_TYPE option not found\n"));
dirkx 0:a259777c45a3 1545 goto free_pbuf_and_return;
dirkx 0:a259777c45a3 1546 }
dirkx 0:a259777c45a3 1547
dirkx 0:a259777c45a3 1548 /* read DHCP message type */
dirkx 0:a259777c45a3 1549 msg_type = (u8_t)dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE);
dirkx 0:a259777c45a3 1550 /* message type is DHCP ACK? */
dirkx 0:a259777c45a3 1551 if (msg_type == DHCP_ACK) {
dirkx 0:a259777c45a3 1552 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_ACK received\n"));
dirkx 0:a259777c45a3 1553 /* in requesting state? */
dirkx 0:a259777c45a3 1554 if (dhcp->state == DHCP_REQUESTING) {
dirkx 0:a259777c45a3 1555 dhcp_handle_ack(netif);
dirkx 0:a259777c45a3 1556 #if DHCP_DOES_ARP_CHECK
dirkx 0:a259777c45a3 1557 /* check if the acknowledged lease address is already in use */
dirkx 0:a259777c45a3 1558 dhcp_check(netif);
dirkx 0:a259777c45a3 1559 #else
dirkx 0:a259777c45a3 1560 /* bind interface to the acknowledged lease address */
dirkx 0:a259777c45a3 1561 dhcp_bind(netif);
dirkx 0:a259777c45a3 1562 #endif
dirkx 0:a259777c45a3 1563 }
dirkx 0:a259777c45a3 1564 /* already bound to the given lease address? */
dirkx 0:a259777c45a3 1565 else if ((dhcp->state == DHCP_REBOOTING) || (dhcp->state == DHCP_REBINDING) || (dhcp->state == DHCP_RENEWING)) {
dirkx 0:a259777c45a3 1566 dhcp_bind(netif);
dirkx 0:a259777c45a3 1567 }
dirkx 0:a259777c45a3 1568 }
dirkx 0:a259777c45a3 1569 /* received a DHCP_NAK in appropriate state? */
dirkx 0:a259777c45a3 1570 else if ((msg_type == DHCP_NAK) &&
dirkx 0:a259777c45a3 1571 ((dhcp->state == DHCP_REBOOTING) || (dhcp->state == DHCP_REQUESTING) ||
dirkx 0:a259777c45a3 1572 (dhcp->state == DHCP_REBINDING) || (dhcp->state == DHCP_RENEWING ))) {
dirkx 0:a259777c45a3 1573 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_NAK received\n"));
dirkx 0:a259777c45a3 1574 dhcp_handle_nak(netif);
dirkx 0:a259777c45a3 1575 }
dirkx 0:a259777c45a3 1576 /* received a DHCP_OFFER in DHCP_SELECTING state? */
dirkx 0:a259777c45a3 1577 else if ((msg_type == DHCP_OFFER) && (dhcp->state == DHCP_SELECTING)) {
dirkx 0:a259777c45a3 1578 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_OFFER received in DHCP_SELECTING state\n"));
dirkx 0:a259777c45a3 1579 dhcp->request_timeout = 0;
dirkx 0:a259777c45a3 1580 /* remember offered lease */
dirkx 0:a259777c45a3 1581 dhcp_handle_offer(netif);
dirkx 0:a259777c45a3 1582 }
dirkx 0:a259777c45a3 1583 free_pbuf_and_return:
dirkx 0:a259777c45a3 1584 dhcp->msg_in = NULL;
dirkx 0:a259777c45a3 1585 pbuf_free(p);
dirkx 0:a259777c45a3 1586 }
dirkx 0:a259777c45a3 1587
dirkx 0:a259777c45a3 1588 /**
dirkx 0:a259777c45a3 1589 * Create a DHCP request, fill in common headers
dirkx 0:a259777c45a3 1590 *
dirkx 0:a259777c45a3 1591 * @param netif the netif under DHCP control
dirkx 0:a259777c45a3 1592 * @param dhcp dhcp control struct
dirkx 0:a259777c45a3 1593 * @param message_type message type of the request
dirkx 0:a259777c45a3 1594 */
dirkx 0:a259777c45a3 1595 static err_t
dirkx 0:a259777c45a3 1596 dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type)
dirkx 0:a259777c45a3 1597 {
dirkx 0:a259777c45a3 1598 u16_t i;
dirkx 0:a259777c45a3 1599 #ifndef DHCP_GLOBAL_XID
dirkx 0:a259777c45a3 1600 /** default global transaction identifier starting value (easy to match
dirkx 0:a259777c45a3 1601 * with a packet analyser). We simply increment for each new request.
dirkx 0:a259777c45a3 1602 * Predefine DHCP_GLOBAL_XID to a better value or a function call to generate one
dirkx 0:a259777c45a3 1603 * at runtime, any supporting function prototypes can be defined in DHCP_GLOBAL_XID_HEADER */
dirkx 0:a259777c45a3 1604 static u32_t xid = 0xABCD0000;
dirkx 0:a259777c45a3 1605 #else
dirkx 0:a259777c45a3 1606 static u32_t xid;
dirkx 0:a259777c45a3 1607 static u8_t xid_initialised = 0;
dirkx 0:a259777c45a3 1608 if (!xid_initialised) {
dirkx 0:a259777c45a3 1609 xid = DHCP_GLOBAL_XID;
dirkx 0:a259777c45a3 1610 xid_initialised = !xid_initialised;
dirkx 0:a259777c45a3 1611 }
dirkx 0:a259777c45a3 1612 #endif
dirkx 0:a259777c45a3 1613 LWIP_ERROR("dhcp_create_msg: netif != NULL", (netif != NULL), return ERR_ARG;);
dirkx 0:a259777c45a3 1614 LWIP_ERROR("dhcp_create_msg: dhcp != NULL", (dhcp != NULL), return ERR_VAL;);
dirkx 0:a259777c45a3 1615 LWIP_ASSERT("dhcp_create_msg: dhcp->p_out == NULL", dhcp->p_out == NULL);
dirkx 0:a259777c45a3 1616 LWIP_ASSERT("dhcp_create_msg: dhcp->msg_out == NULL", dhcp->msg_out == NULL);
dirkx 0:a259777c45a3 1617 dhcp->p_out = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcp_msg), PBUF_RAM);
dirkx 0:a259777c45a3 1618 if (dhcp->p_out == NULL) {
dirkx 0:a259777c45a3 1619 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
dirkx 0:a259777c45a3 1620 ("dhcp_create_msg(): could not allocate pbuf\n"));
dirkx 0:a259777c45a3 1621 return ERR_MEM;
dirkx 0:a259777c45a3 1622 }
dirkx 0:a259777c45a3 1623 LWIP_ASSERT("dhcp_create_msg: check that first pbuf can hold struct dhcp_msg",
dirkx 0:a259777c45a3 1624 (dhcp->p_out->len >= sizeof(struct dhcp_msg)));
dirkx 0:a259777c45a3 1625
dirkx 0:a259777c45a3 1626 /* reuse transaction identifier in retransmissions */
dirkx 0:a259777c45a3 1627 if (dhcp->tries == 0) {
dirkx 0:a259777c45a3 1628 xid++;
dirkx 0:a259777c45a3 1629 }
dirkx 0:a259777c45a3 1630 dhcp->xid = xid;
dirkx 0:a259777c45a3 1631 LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE,
dirkx 0:a259777c45a3 1632 ("transaction id xid(%"X32_F")\n", xid));
dirkx 0:a259777c45a3 1633
dirkx 0:a259777c45a3 1634 dhcp->msg_out = (struct dhcp_msg *)dhcp->p_out->payload;
dirkx 0:a259777c45a3 1635
dirkx 0:a259777c45a3 1636 dhcp->msg_out->op = DHCP_BOOTREQUEST;
dirkx 0:a259777c45a3 1637 /* TODO: make link layer independent */
dirkx 0:a259777c45a3 1638 dhcp->msg_out->htype = DHCP_HTYPE_ETH;
dirkx 0:a259777c45a3 1639 dhcp->msg_out->hlen = netif->hwaddr_len;
dirkx 0:a259777c45a3 1640 dhcp->msg_out->hops = 0;
dirkx 0:a259777c45a3 1641 dhcp->msg_out->xid = htonl(dhcp->xid);
dirkx 0:a259777c45a3 1642 dhcp->msg_out->secs = 0;
dirkx 0:a259777c45a3 1643 /* we don't need the broadcast flag since we can receive unicast traffic
dirkx 0:a259777c45a3 1644 before being fully configured! */
dirkx 0:a259777c45a3 1645 dhcp->msg_out->flags = 0;
dirkx 0:a259777c45a3 1646 ip_addr_set_zero(&dhcp->msg_out->ciaddr);
dirkx 0:a259777c45a3 1647 /* set ciaddr to netif->ip_addr based on message_type and state */
dirkx 0:a259777c45a3 1648 if ((message_type == DHCP_INFORM) || (message_type == DHCP_DECLINE) ||
dirkx 0:a259777c45a3 1649 ((message_type == DHCP_REQUEST) && /* DHCP_BOUND not used for sending! */
dirkx 0:a259777c45a3 1650 ((dhcp->state==DHCP_RENEWING) || dhcp->state==DHCP_REBINDING))) {
dirkx 0:a259777c45a3 1651 ip_addr_copy(dhcp->msg_out->ciaddr, netif->ip_addr);
dirkx 0:a259777c45a3 1652 }
dirkx 0:a259777c45a3 1653 ip_addr_set_zero(&dhcp->msg_out->yiaddr);
dirkx 0:a259777c45a3 1654 ip_addr_set_zero(&dhcp->msg_out->siaddr);
dirkx 0:a259777c45a3 1655 ip_addr_set_zero(&dhcp->msg_out->giaddr);
dirkx 0:a259777c45a3 1656 for (i = 0; i < DHCP_CHADDR_LEN; i++) {
dirkx 0:a259777c45a3 1657 /* copy netif hardware address, pad with zeroes */
dirkx 0:a259777c45a3 1658 dhcp->msg_out->chaddr[i] = (i < netif->hwaddr_len) ? netif->hwaddr[i] : 0/* pad byte*/;
dirkx 0:a259777c45a3 1659 }
dirkx 0:a259777c45a3 1660 for (i = 0; i < DHCP_SNAME_LEN; i++) {
dirkx 0:a259777c45a3 1661 dhcp->msg_out->sname[i] = 0;
dirkx 0:a259777c45a3 1662 }
dirkx 0:a259777c45a3 1663 for (i = 0; i < DHCP_FILE_LEN; i++) {
dirkx 0:a259777c45a3 1664 dhcp->msg_out->file[i] = 0;
dirkx 0:a259777c45a3 1665 }
dirkx 0:a259777c45a3 1666 dhcp->msg_out->cookie = htonl(DHCP_MAGIC_COOKIE);
dirkx 0:a259777c45a3 1667 dhcp->options_out_len = 0;
dirkx 0:a259777c45a3 1668 /* fill options field with an incrementing array (for debugging purposes) */
dirkx 0:a259777c45a3 1669 for (i = 0; i < DHCP_OPTIONS_LEN; i++) {
dirkx 0:a259777c45a3 1670 dhcp->msg_out->options[i] = (u8_t)i; /* for debugging only, no matter if truncated */
dirkx 0:a259777c45a3 1671 }
dirkx 0:a259777c45a3 1672 /* Add option MESSAGE_TYPE */
dirkx 0:a259777c45a3 1673 dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
dirkx 0:a259777c45a3 1674 dhcp_option_byte(dhcp, message_type);
dirkx 0:a259777c45a3 1675 return ERR_OK;
dirkx 0:a259777c45a3 1676 }
dirkx 0:a259777c45a3 1677
dirkx 0:a259777c45a3 1678 /**
dirkx 0:a259777c45a3 1679 * Free previously allocated memory used to send a DHCP request.
dirkx 0:a259777c45a3 1680 *
dirkx 0:a259777c45a3 1681 * @param dhcp the dhcp struct to free the request from
dirkx 0:a259777c45a3 1682 */
dirkx 0:a259777c45a3 1683 static void
dirkx 0:a259777c45a3 1684 dhcp_delete_msg(struct dhcp *dhcp)
dirkx 0:a259777c45a3 1685 {
dirkx 0:a259777c45a3 1686 LWIP_ERROR("dhcp_delete_msg: dhcp != NULL", (dhcp != NULL), return;);
dirkx 0:a259777c45a3 1687 LWIP_ASSERT("dhcp_delete_msg: dhcp->p_out != NULL", dhcp->p_out != NULL);
dirkx 0:a259777c45a3 1688 LWIP_ASSERT("dhcp_delete_msg: dhcp->msg_out != NULL", dhcp->msg_out != NULL);
dirkx 0:a259777c45a3 1689 if (dhcp->p_out != NULL) {
dirkx 0:a259777c45a3 1690 pbuf_free(dhcp->p_out);
dirkx 0:a259777c45a3 1691 }
dirkx 0:a259777c45a3 1692 dhcp->p_out = NULL;
dirkx 0:a259777c45a3 1693 dhcp->msg_out = NULL;
dirkx 0:a259777c45a3 1694 }
dirkx 0:a259777c45a3 1695
dirkx 0:a259777c45a3 1696 /**
dirkx 0:a259777c45a3 1697 * Add a DHCP message trailer
dirkx 0:a259777c45a3 1698 *
dirkx 0:a259777c45a3 1699 * Adds the END option to the DHCP message, and if
dirkx 0:a259777c45a3 1700 * necessary, up to three padding bytes.
dirkx 0:a259777c45a3 1701 *
dirkx 0:a259777c45a3 1702 * @param dhcp DHCP state structure
dirkx 0:a259777c45a3 1703 */
dirkx 0:a259777c45a3 1704 static void
dirkx 0:a259777c45a3 1705 dhcp_option_trailer(struct dhcp *dhcp)
dirkx 0:a259777c45a3 1706 {
dirkx 0:a259777c45a3 1707 LWIP_ERROR("dhcp_option_trailer: dhcp != NULL", (dhcp != NULL), return;);
dirkx 0:a259777c45a3 1708 LWIP_ASSERT("dhcp_option_trailer: dhcp->msg_out != NULL\n", dhcp->msg_out != NULL);
dirkx 0:a259777c45a3 1709 LWIP_ASSERT("dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
dirkx 0:a259777c45a3 1710 dhcp->msg_out->options[dhcp->options_out_len++] = DHCP_OPTION_END;
dirkx 0:a259777c45a3 1711 /* packet is too small, or not 4 byte aligned? */
dirkx 0:a259777c45a3 1712 while ((dhcp->options_out_len < DHCP_MIN_OPTIONS_LEN) || (dhcp->options_out_len & 3)) {
dirkx 0:a259777c45a3 1713 /* LWIP_DEBUGF(DHCP_DEBUG,("dhcp_option_trailer:dhcp->options_out_len=%"U16_F", DHCP_OPTIONS_LEN=%"U16_F, dhcp->options_out_len, DHCP_OPTIONS_LEN)); */
dirkx 0:a259777c45a3 1714 LWIP_ASSERT("dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
dirkx 0:a259777c45a3 1715 /* add a fill/padding byte */
dirkx 0:a259777c45a3 1716 dhcp->msg_out->options[dhcp->options_out_len++] = 0;
dirkx 0:a259777c45a3 1717 }
dirkx 0:a259777c45a3 1718 }
dirkx 0:a259777c45a3 1719
dirkx 0:a259777c45a3 1720 #endif /* LWIP_DHCP */