dhcp.c

来自「lwip-1.4.0」· C语言 代码 · 共 1,746 行 · 第 1/5 页

C
1,746
字号
    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_msg(dhcp);    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){  struct dhcp *dhcp = netif->dhcp;  err_t result;  u16_t msecs;  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_msg(netif, dhcp, DHCP_REQUEST);  if (result == ERR_OK) {    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    if (netif->hostname != NULL) {      const char *p = (const char*)netif->hostname;      u8_t namelen = (u8_t)strlen(p);      if (namelen > 0) {        LWIP_ASSERT("DHCP: hostname is too long!", namelen < 255);        dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, namelen);        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_msg(dhcp);    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_msg(netif, dhcp, DHCP_REQUEST);  if (result == ERR_OK) {    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(ip4_addr_get_u32(&dhcp->offered_ip_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_msg(dhcp);    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 */  ip_addr_set_zero(&dhcp->server_ip_addr);  ip_addr_set_zero(&dhcp->offered_ip_addr);  ip_addr_set_zero(&dhcp->offered_sn_mask);  ip_addr_set_zero(&dhcp->offered_gw_addr);#if LWIP_DHCP_BOOTP_FILE  ip_addr_set_zero(&dhcp->offered_si_addr);#endif /* LWIP_DHCP_BOOTP_FILE */  dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0;    /* create and initialize the DHCP message header */  result = dhcp_create_msg(netif, dhcp, DHCP_RELEASE);  if (result == ERR_OK) {    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_msg(dhcp);    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);    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;  LWIP_ERROR("dhcp_stop: netif != NULL", (netif != NULL), return;);  dhcp = netif->dhcp;  /* 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_set_state(dhcp, DHCP_OFF);  }}/* * Set the DHCP state of a DHCP client. * * If the state changed, reset the number of tries. */static voiddhcp_set_state(struct dhcp *dhcp, u8_t new_state){  if (new_state != dhcp->state) {    dhcp->state = new_state;    dhcp->tries = 0;    dhcp->request_timeout = 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_parse_reply(struct dhcp *dhcp, struct pbuf *p){  u8_t *options;  u16_t offset;  u16_t offset_max;  u16_t options_idx;  u16_t options_idx_max;  struct pbuf *q;  int parse_file_as_options = 0;  int parse_sname_as_options = 0;  /* clear received options */  dhcp_clear_all_options(dhcp);  /* check that beginning of dhcp_msg (up to and including chaddr) is in first pbuf */  if (p->len < DHCP_SNAME_OFS) {    return ERR_BUF;  }  dhcp->msg_in = (struct dhcp_msg *)p->payload;#if LWIP_DHCP_BOOTP_FILE  /* clear boot file name */  dhcp->boot_file_name[0] = 0;#endif /* LWIP_DHCP_BOOTP_FILE */  /* parse options */  /* start with options field */  options_idx = DHCP_OPTIONS_OFS;  /* parse options to the end of the received packet */  options_idx_max = p->tot_len;again:  q = p;  while((q != NULL) && (options_idx >= q->len)) {    options_idx -= q->len;    options_idx_max -= q->len;    q = q->next;  }  if (q == NULL) {    return ERR_BUF;  }  offset = options_idx;  offset_max = options_idx_max;  options = (u8_t*)q->payload;  /* at least 1 byte to read and no end marker, then at least 3 bytes to read? */  while((q != NULL) && (options[offset] != DHCP_OPTION_END) && (offset < offset_max)) {    u8_t op = options[offset];    u8_t len;    u8_t decode_len = 0;    int decode_idx = -1;    u16_t val_offset = offset + 2;    /* len byte might be in the next pbuf */    if (offset + 1 < q->len) {      len = options[offset + 1];    } else {      len = (q->next != NULL ? ((u8_t*)q->next->payload)[0] : 0);    }    /* LWIP_DEBUGF(DHCP_DEBUG, ("msg_offset=%"U16_F", q->len=%"U16_F, msg_offset, q->len)); */    decode_len = len;    switch(op) {      /* case(DHCP_OPTION_END): handled above */      case(DHCP_OPTION_PAD):        /* special option: no len encoded */        decode_len = len = 0;        /* will be increased below */        offset--;        break;      case(DHCP_OPTION_SUBNET_MASK):        LWIP_ASSERT("len == 4", len == 4);        decode_idx = DHCP_OPTION_IDX_SUBNET_MASK;        break;      case(DHCP_OPTION_ROUTER):        decode_len = 4; /* only copy the first given router */        LWIP_ASSERT("len >= decode_len", len >= decode_len);        decode_idx = DHCP_OPTION_IDX_ROUTER;        break;      case(DHCP_OPTION_DNS_SERVER):        /* special case: there might be more than one server */        LWIP_ASSERT("len % 4 == 0", len % 4 == 0);

⌨️ 快捷键说明

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