Used in Live Traffic Update Nokia LCD Display Project

Fork of NetServices by Segundo Equipo

Committer:
rrajan8
Date:
Wed Mar 06 19:07:23 2013 +0000
Revision:
8:92b57208ab99
Parent:
0:ac1725ba162c
This project utilizes mbed's networking features to display live traffic updates on the Nokia LCD using the MapQuest API's Traffic Web Service.

Who changed what in which revision?

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