I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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