Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:54:31 2010 +0000
Revision:
5:8e53abda9900
Parent:
0:355018f44c9f

        

Who changed what in which revision?

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