Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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