⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhcp.c

📁 freescale k40/k60 freertos-lwip例程
💻 C
📖 第 1 页 / 共 5 页
字号:
    dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);    dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));    dhcp_option_trailer(dhcp);    pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);#if IP_SOF_BROADCAST    dhcp->pcb->so_options|=SOF_BROADCAST;#endif /* IP_SOF_BROADCAST */    udp_bind(dhcp->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_inform: INFORMING\n"));    udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);    dhcp_delete_request(netif);  } else {    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_inform: could not allocate DHCP request\n"));  }  udp_remove(dhcp->pcb);  dhcp->pcb = NULL;  netif->dhcp = old_dhcp;free_dhcp_and_return:  mem_free((void *)dhcp);}/** Handle a possible change in the network configuration. * * This enters the REBOOTING state to verify that the currently bound * address is still valid. */voiddhcp_network_changed(struct netif *netif){  struct dhcp *dhcp = netif->dhcp;  if (!dhcp)    return;  switch (dhcp->state) {  case DHCP_REBINDING:  case DHCP_RENEWING:  case DHCP_BOUND:  case DHCP_REBOOTING:    netif_set_down(netif);    dhcp->tries = 0;    dhcp_reboot(netif);    break;  case DHCP_OFF:    /* stay off */    break;  default:    dhcp->tries = 0;    dhcp_discover(netif);    break;  }}#if DHCP_DOES_ARP_CHECK/** * Match an ARP reply with the offered IP address. * * @param netif the network interface on which the reply was received * @param addr The IP address we received a reply from */void dhcp_arp_reply(struct netif *netif, struct ip_addr *addr){  LWIP_ERROR("netif != NULL", (netif != NULL), return;);  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_arp_reply()\n"));  /* is a DHCP client doing an ARP check? */  if ((netif->dhcp != NULL) && (netif->dhcp->state == DHCP_CHECKING)) {    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_arp_reply(): CHECKING, arp reply for 0x%08"X32_F"\n", addr->addr));    /* did a host respond with the address we       were offered by the DHCP server? */    if (ip_addr_cmp(addr, &netif->dhcp->offered_ip_addr)) {      /* we will not accept the offered address */      LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING,        ("dhcp_arp_reply(): arp reply matched with offered address, declining\n"));      dhcp_decline(netif);    }  }}/** * Decline an offered lease. * * Tell the DHCP server we do not accept the offered address. * One reason to decline the lease is when we find out the address * is already in use by another host (through ARP). * * @param netif the netif under DHCP control */static err_tdhcp_decline(struct netif *netif){  struct dhcp *dhcp = netif->dhcp;  err_t result = ERR_OK;  u16_t msecs;  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline()\n"));  dhcp_set_state(dhcp, DHCP_BACKING_OFF);  /* 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_DECLINE);    dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);    dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));    dhcp_option_trailer(dhcp);    /* resize pbuf to reflect true size of options */    pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);    /* per section 4.4.4, broadcast DECLINE messages */    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_decline: BACKING OFF\n"));  } else {    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,      ("dhcp_decline: could not allocate DHCP request\n"));  }  dhcp->tries++;  msecs = 10*1000;  dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline(): set request timeout %"U16_F" msecs\n", msecs));  return result;}#endif/** * Start the DHCP process, discover a DHCP server. * * @param netif the netif under DHCP control */static err_tdhcp_discover(struct netif *netif){  struct dhcp *dhcp = netif->dhcp;  err_t result = ERR_OK;  u16_t msecs;  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover()\n"));  ip_addr_set(&dhcp->offered_ip_addr, IP_ADDR_ANY);  dhcp_set_state(dhcp, DHCP_SELECTING);  /* create and initialize the DHCP message header */  result = dhcp_create_request(netif);  if (result == ERR_OK) {    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: making request\n"));    dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);    dhcp_option_byte(dhcp, DHCP_DISCOVER);    dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);    dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));    dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 4/*num options*/);    dhcp_option_byte(dhcp, DHCP_OPTION_SUBNET_MASK);    dhcp_option_byte(dhcp, DHCP_OPTION_ROUTER);    dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);    dhcp_option_byte(dhcp, DHCP_OPTION_DNS_SERVER);    dhcp_option_trailer(dhcp);    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: realloc()ing\n"));    pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: sendto(DISCOVER, IP_ADDR_BROADCAST, DHCP_SERVER_PORT)\n"));    udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: deleting()ing\n"));    dhcp_delete_request(netif);    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover: SELECTING\n"));  } else {    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_discover: could not allocate DHCP request\n"));  }  dhcp->tries++;#if LWIP_DHCP_AUTOIP_COOP  if(dhcp->tries >= LWIP_DHCP_AUTOIP_COOP_TRIES && dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_OFF) {    dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_ON;    autoip_start(netif);  }#endif /* LWIP_DHCP_AUTOIP_COOP */  msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 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_discover(): set request timeout %"U16_F" msecs\n", msecs));  return result;}/** * Bind the interface to the offered IP address. * * @param netif network interface to bind to the offered address */static voiddhcp_bind(struct netif *netif){  u32_t timeout;  struct dhcp *dhcp;  struct ip_addr sn_mask, gw_addr;  LWIP_ERROR("dhcp_bind: netif != NULL", (netif != NULL), return;);  dhcp = netif->dhcp;  LWIP_ERROR("dhcp_bind: dhcp != NULL", (dhcp != NULL), return;);  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));  /* temporary DHCP lease? */  if (dhcp->offered_t1_renew != 0xffffffffUL) {    /* set renewal period timer */    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t1 renewal timer %"U32_F" secs\n", dhcp->offered_t1_renew));    timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;    if(timeout > 0xffff) {      timeout = 0xffff;    }    dhcp->t1_timeout = (u16_t)timeout;    if (dhcp->t1_timeout == 0) {      dhcp->t1_timeout = 1;    }    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t1_renew*1000));  }  /* set renewal period timer */  if (dhcp->offered_t2_rebind != 0xffffffffUL) {    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t2 rebind timer %"U32_F" secs\n", dhcp->offered_t2_rebind));    timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;    if(timeout > 0xffff) {      timeout = 0xffff;    }    dhcp->t2_timeout = (u16_t)timeout;    if (dhcp->t2_timeout == 0) {      dhcp->t2_timeout = 1;    }    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t2_rebind*1000));  }  /* copy offered network mask */  ip_addr_set(&sn_mask, &dhcp->offered_sn_mask);  /* subnet mask not given? */  /* TODO: this is not a valid check. what if the network mask is 0? */  if (sn_mask.addr == 0) {    /* choose a safe subnet mask given the network class */    u8_t first_octet = ip4_addr1(&sn_mask);    if (first_octet <= 127) {      sn_mask.addr = htonl(0xff000000);    } else if (first_octet >= 192) {      sn_mask.addr = htonl(0xffffff00);    } else {      sn_mask.addr = htonl(0xffff0000);    }  }  ip_addr_set(&gw_addr, &dhcp->offered_gw_addr);  /* gateway address not given? */  if (gw_addr.addr == 0) {    /* copy network address */    gw_addr.addr = (dhcp->offered_ip_addr.addr & sn_mask.addr);    /* use first host address on network as gateway */    gw_addr.addr |= htonl(0x00000001);  }#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 */  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): IP: 0x%08"X32_F"\n", dhcp->offered_ip_addr.addr));  netif_set_ipaddr(netif, &dhcp->offered_ip_addr);  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): SN: 0x%08"X32_F"\n", sn_mask.addr));  netif_set_netmask(netif, &sn_mask);  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): GW: 0x%08"X32_F"\n", gw_addr.addr));  netif_set_gw(netif, &gw_addr);  /* bring the interface up */  netif_set_up(netif);  /* netif is now bound to DHCP leased address */  dhcp_set_state(dhcp, DHCP_BOUND);}/** * Renew an existing DHCP lease at the involved DHCP server. * * @param netif network interface which must renew its lease */err_tdhcp_renew(struct netif *netif){  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, ("dhcp_renew()\n"));  dhcp_set_state(dhcp, DHCP_RENEWING);  /* 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));#endif#if 0    dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);    dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));#endif    /* append DHCP message trailer */    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_renew: RENEWING\n"));  } else {    LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_renew: could not allocate DHCP request\n"));  }  dhcp->tries++;  /* back-off on retries, but to a maximum of 20 seconds */  msecs = dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 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_renew(): set request timeout %"U16_F" msecs\n", msecs));  return result;}/** * Rebind with a DHCP server for an existing DHCP lease. * * @param netif network interface which must rebind with a DHCP server */static err_tdhcp_rebind(struct netif *netif)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -