📄 dhcp.c
字号:
{ struct dhcp *dhcp = netif->dhcp; err_t result; u16_t msecs;#if LWIP_NETIF_HOSTNAME const char *p;#endif /* LWIP_NETIF_HOSTNAME */ LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind()\n")); dhcp_set_state(dhcp, DHCP_REBINDING); /* create and initialize the DHCP message header */ result = dhcp_create_request(netif); if (result == ERR_OK) { dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN); dhcp_option_byte(dhcp, DHCP_REQUEST); dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));#if LWIP_NETIF_HOSTNAME p = (const char*)netif->hostname; if (p != NULL) { dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, strlen(p)); while (*p) { dhcp_option_byte(dhcp, *p++); } }#endif /* LWIP_NETIF_HOSTNAME */#if 0 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4); dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr)); dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4); dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));#endif dhcp_option_trailer(dhcp); pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len); /* broadcast to server */ udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif); dhcp_delete_request(netif); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind: REBINDING\n")); } else { LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_rebind: could not allocate DHCP request\n")); } dhcp->tries++; msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000; dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind(): set request timeout %"U16_F" msecs\n", msecs)); return result;}/** * Enter REBOOTING state to verify an existing lease * * @param netif network interface which must reboot */static err_tdhcp_reboot(struct netif *netif){ struct dhcp *dhcp = netif->dhcp; err_t result; u16_t msecs; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot()\n")); dhcp_set_state(dhcp, DHCP_REBOOTING); /* create and initialize the DHCP message header */ result = dhcp_create_request(netif); if (result == ERR_OK) { dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN); dhcp_option_byte(dhcp, DHCP_REQUEST); dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); dhcp_option_short(dhcp, 576); dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4); dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr)); dhcp_option_trailer(dhcp); pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len); /* broadcast to server */ udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif); dhcp_delete_request(netif); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot: REBOOTING\n")); } else { LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_reboot: could not allocate DHCP request\n")); } dhcp->tries++; msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000; dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot(): set request timeout %"U16_F" msecs\n", msecs)); return result;}/** * Release a DHCP lease. * * @param netif network interface which must release its lease */err_tdhcp_release(struct netif *netif){ struct dhcp *dhcp = netif->dhcp; err_t result; u16_t msecs; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_release()\n")); /* idle DHCP client */ dhcp_set_state(dhcp, DHCP_OFF); /* clean old DHCP offer */ dhcp->server_ip_addr.addr = 0; dhcp->offered_ip_addr.addr = dhcp->offered_sn_mask.addr = 0; dhcp->offered_gw_addr.addr = dhcp->offered_bc_addr.addr = 0; dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0; dhcp->dns_count = 0; /* create and initialize the DHCP message header */ result = dhcp_create_request(netif); if (result == ERR_OK) { dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN); dhcp_option_byte(dhcp, DHCP_RELEASE); dhcp_option_trailer(dhcp); pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len); udp_sendto_if(dhcp->pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif); dhcp_delete_request(netif); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release: RELEASED, DHCP_OFF\n")); } else { LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_release: could not allocate DHCP request\n")); } dhcp->tries++; msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000; dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release(): set request timeout %"U16_F" msecs\n", msecs)); /* bring the interface down */ netif_set_down(netif); /* remove IP address from interface */ netif_set_ipaddr(netif, IP_ADDR_ANY); netif_set_gw(netif, IP_ADDR_ANY); netif_set_netmask(netif, IP_ADDR_ANY); /* TODO: netif_down(netif); */ return result;}/** * Remove the DHCP client from the interface. * * @param netif The network interface to stop DHCP on */voiddhcp_stop(struct netif *netif){ struct dhcp *dhcp = netif->dhcp; LWIP_ERROR("dhcp_stop: netif != NULL", (netif != NULL), return;); /* Remove the flag that says this netif is handled by DHCP. */ netif->flags &= ~NETIF_FLAG_DHCP; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_stop()\n")); /* netif is DHCP configured? */ if (dhcp != NULL) {#if LWIP_DHCP_AUTOIP_COOP if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) { autoip_stop(netif); dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF; }#endif /* LWIP_DHCP_AUTOIP_COOP */ if (dhcp->pcb != NULL) { udp_remove(dhcp->pcb); dhcp->pcb = NULL; } LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL && dhcp->options_in == NULL && dhcp->options_in_len == 0); mem_free((void *)dhcp); netif->dhcp = NULL; }}/* * Set the DHCP state of a DHCP client. * * If the state changed, reset the number of tries. * * TODO: we might also want to reset the timeout here? */static voiddhcp_set_state(struct dhcp *dhcp, u8_t new_state){ if (new_state != dhcp->state) { dhcp->state = new_state; dhcp->tries = 0; }}/* * Concatenate an option type and length field to the outgoing * DHCP message. * */static voiddhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len){ LWIP_ASSERT("dhcp_option: dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U + option_len <= DHCP_OPTIONS_LEN); dhcp->msg_out->options[dhcp->options_out_len++] = option_type; dhcp->msg_out->options[dhcp->options_out_len++] = option_len;}/* * Concatenate a single byte to the outgoing DHCP message. * */static voiddhcp_option_byte(struct dhcp *dhcp, u8_t value){ LWIP_ASSERT("dhcp_option_byte: dhcp->options_out_len < DHCP_OPTIONS_LEN", dhcp->options_out_len < DHCP_OPTIONS_LEN); dhcp->msg_out->options[dhcp->options_out_len++] = value;}static voiddhcp_option_short(struct dhcp *dhcp, u16_t value){ LWIP_ASSERT("dhcp_option_short: dhcp->options_out_len + 2 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U <= DHCP_OPTIONS_LEN); dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff00U) >> 8); dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t) (value & 0x00ffU);}static voiddhcp_option_long(struct dhcp *dhcp, u32_t value){ LWIP_ASSERT("dhcp_option_long: dhcp->options_out_len + 4 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 4U <= DHCP_OPTIONS_LEN); dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff000000UL) >> 24); dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x00ff0000UL) >> 16); dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x0000ff00UL) >> 8); dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x000000ffUL));}/** * Extract the DHCP message and the DHCP options. * * Extract the DHCP message and the DHCP options, each into a contiguous * piece of memory. As a DHCP message is variable sized by its options, * and also allows overriding some fields for options, the easy approach * is to first unfold the options into a conitguous piece of memory, and * use that further on. * */static err_tdhcp_unfold_reply(struct dhcp *dhcp, struct pbuf *p){ u16_t ret; LWIP_ERROR("dhcp != NULL", (dhcp != NULL), return ERR_ARG;); /* free any left-overs from previous unfolds */ dhcp_free_reply(dhcp); /* options present? */ if (p->tot_len > (sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN)) { dhcp->options_in_len = p->tot_len - (sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN); dhcp->options_in = mem_malloc(dhcp->options_in_len); if (dhcp->options_in == NULL) { LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_unfold_reply(): could not allocate dhcp->options\n")); dhcp->options_in_len = 0; return ERR_MEM; } } dhcp->msg_in = mem_malloc(sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN); if (dhcp->msg_in == NULL) { LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_unfold_reply(): could not allocate dhcp->msg_in\n")); if (dhcp->options_in != NULL) { mem_free(dhcp->options_in); dhcp->options_in = NULL; dhcp->options_in_len = 0; } return ERR_MEM; } /** copy the DHCP message without options */ ret = pbuf_copy_partial(p, dhcp->msg_in, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN, 0); LWIP_ASSERT("ret == sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN", ret == sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_unfold_reply(): copied %"U16_F" bytes into dhcp->msg_in[]\n", sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN)); if (dhcp->options_in != NULL) { /** copy the DHCP options */ ret = pbuf_copy_partial(p, dhcp->options_in, dhcp->options_in_len, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN); LWIP_ASSERT("ret == dhcp->options_in_len", ret == dhcp->options_in_len); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_unfold_reply(): copied %"U16_F" bytes to dhcp->options_in[]\n", dhcp->options_in_len)); } LWIP_UNUSED_ARG(ret); return ERR_OK;}/** * Free the incoming DHCP message including contiguous copy of * its DHCP options. */static void dhcp_free_reply(struct dhcp *dhcp){ if (dhcp->msg_in != NULL) { mem_free((void *)dhcp->msg_in); dhcp->msg_in = NULL; } if (dhcp->options_in) { mem_free(dhcp->options_in); dhcp->options_in = NULL; dhcp->options_in_len = 0; } LWIP_DEBUGF(DHCP_DEBUG, ("dhcp_free_reply(): free'd\n"));}/** * If an incoming DHCP message is in response to us, then trigger the state machine */static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port){ struct netif *netif = (struct netif *)arg; struct dhcp *dhcp = netif->dhcp; struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload; u8_t *options_ptr; u8_t msg_type; u8_t i; 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, (u16_t)(ntohl(addr->addr) >> 24 & 0xff), (u16_t)(ntohl(addr->addr) >> 16 & 0xff), (u16_t)(ntohl(addr->addr) >> 8 & 0xff), (u16_t)(ntohl(addr->addr) & 0xff), port)); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->len = %"U16_F"\n", p->len)); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->tot_len = %"U16_F"\n", p->tot_len)); /* prevent warnings about unused arguments */ LWIP_UNUSED_ARG(pcb); LWIP_UNUSED_ARG(addr); LWIP_UNUSED_ARG(port); LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL && dhcp->options_in == NULL && dhcp->options_in_len == 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -