lwip_DHCP

/** DHCPDISCOVER:发现阶段(DHCP客户端在网络中广播发送DHCP DISCOVER请求报文,发现DHCP服务器,请求IP地址租约)*/
/** DHCPOFFER:提供阶段(DHCP服务器通过DHCPOFFER报文向DHCP客户端提供IP地址预分配)*/
/** DHCPREQUEST:选择阶段(DHCP客户端通过DHCPREQUEST报文确认选择第一个DHCP服务器为他提供IP地址自动分配服务)*/
/** DHCPACK:确认阶段(被选择的DHCP服务器通过DHCPACK报文把在DHCPOFFER报文中准备的IP地址租约给对应DHCP客户端)*/
//netif链表
struct netif{
        /** pointer to next in linked list */
      struct netif *next;

      /** IP address configuration in network byte order */
      struct ip_addr ip_addr;
      struct ip_addr netmask;
      struct ip_addr gw;

      /** This function is called by the network device driver
       *  to pass a packet up the TCP/IP stack. */
      err_t (* input)(struct pbuf *p, struct netif *inp);
      /** This function is called by the IP module when it wants
       *  to send a packet on the interface. This function typically
       *  first resolves the hardware address, then sends the packet. */
      err_t (* output)(struct netif *netif, struct pbuf *p,
           struct ip_addr *ipaddr);
      /** This function is called by the ARP module when it wants
       *  to send a packet on the interface. This function outputs
       *  the pbuf as-is on the link medium. */
      err_t (* linkoutput)(struct netif *netif, struct pbuf *p);
      /** This function is called when the netif link is set to up or down
       */
      void (* link_callback)(struct netif *netif);
      /** This field can be set by the device driver and could point
       *  to state information for the device. */
      void *state;
      /** maximum transfer unit (in bytes) */
      u16_t mtu;
      /** number of bytes used in hwaddr */
      u8_t hwaddr_len;
      /** link level hardware address of this interface */
      u8_t hwaddr[NETIF_MAX_HWADDR_LEN];
      /** flags (see NETIF_FLAG_ above) */
      u8_t flags;
      /** descriptive abbreviation */
      char name[2];
      /** number of this interface */
      u8_t num;
}

struct dhcp
{
  /** transaction identifier of last sent request */ 
  u32_t xid;
  /** our connection to the DHCP server */ 
  struct udp_pcb *pcb;
  /** incoming msg */
  struct dhcp_msg *msg_in;
  /** incoming msg options */
  void *options_in; 
  /** ingoing msg options length */
  u16_t options_in_len;
  /** current DHCP state machine state */
  u8_t state;
  /** retries of current request */
  u8_t tries;

  struct pbuf *p_out; /* pbuf of outcoming msg */
  struct dhcp_msg *msg_out; /* outgoing msg */
  u16_t options_out_len; /* outgoing msg options length */
  u16_t request_timeout; /* #ticks with period DHCP_FINE_TIMER_SECS for request timeout */
  u16_t t1_timeout;  /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
  u16_t t2_timeout;  /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
  struct ip_addr server_ip_addr; /* dhcp server address that offered this lease */
  struct ip_addr offered_ip_addr;
  struct ip_addr offered_sn_mask;
  struct ip_addr offered_gw_addr;
  struct ip_addr offered_bc_addr;
#define DHCP_MAX_DNS 2
  u32_t dns_count; /* actual number of DNS servers obtained */
  struct ip_addr offered_dns_addr[DHCP_MAX_DNS]; /* DNS server addresses */

  u32_t offered_t0_lease; /* lease period (in seconds) */
  u32_t offered_t1_renew; /* recommended renew time (usually 50% of lease period) */
  u32_t offered_t2_rebind; /* recommended rebind time (usually 66% of lease period)  */
#if LWIP_DHCP_AUTOIP_COOP
  u8_t autoip_coop_state;
#endif
/** Patch #1308
 *  TODO: See dhcp.c "TODO"s
 */
#if 0
  struct ip_addr offered_si_addr;
  u8_t *boot_file_name;
#endif
}


lwIPPrivateInit(lwiplib.c) 
            dhcp_start();

dhcp_start(struct netif *netif)
{
    //判断netif是否为空
    //清空netif->flags标识位
    //是否有dhcp客户端连接
        //如果没有,创建一个新的dhcp
        //否则,重新建立连接
    //建立一个udp连接  recv回调dhcp_recv函数
    //执行dhcp_discover,/*******发现阶段********/
    goto dhcp_discover;
    //判断discover结果
        //如果失败,释放资源dhcp_stop
        //如果成功,置位netif->flags标志位
}
dhcp_discover:
dhcp_discover(struct netif *netif)
{
    //设置dhcp状态
    //创建一个dhcp请求        /*********选择阶段********/
    //dhcp->tries++
    //如果定义了LWIP_DHCP_AUTOIP_COOP
    //如果尝试次数大于5次,仍然失败
        //执行autoip_start(netif);

}

dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
{
    //判断返回DHCP消息类型
        //如果是ACK
            //执行dhcp_handle_ack   /********确认阶段********/
            //dhcp_check
            //dhcp_bind
        //如果是NAK
            //执行dhcp_handle_nak
        //如果是OFFER | SELECTING
            //执行dhcp_handle_offer
}

你可能感兴趣的:(lwip)