在lwIP中,是經過結構體netif來描述一個硬件網絡接口的,在單網卡中,這個結構體只有一個,多網卡中可有何網卡數目相同的netif結構體,它們構成一個數據鏈。網絡
/** Generic data structure used for all lwIP network interfaces. * The following fields should be filled in by the initialization * function for the device driver: hwaddr_len, hwaddr[], mtu, flags * 用於全部lwIP網絡接口的通用數據結構體 * 設備驅動函數在初始化時應該填充如下數據域:hwaddr_len,hwaddr[],mtu,flags.*/ struct netif { /** pointer to next in linked list */ struct netif *next; /** IP address configuration in network byte order */ struct ip_addr ip_addr; //IP地址 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. 這個函數由網絡設備驅動所調用,從網卡中接收數據包並傳遞給TCP/IP協議棧*/ 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. 當IP模塊向接口發送一個數據包時調用此函數。這個函數一般首先解析 硬件地址,而後發送數據包*/ err_t (* output)(struct netif *netif, struct pbuf *p, //IP層調用此函數向網卡發送一包數據 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. 當ARP模塊向接口發送一個數據包時調用此函數。這個函數向鏈路輸出一個pbuf。*/ err_t (* linkoutput)(struct netif *netif, struct pbuf *p); //ARP模塊調用這個函數向網卡發送一包數據 #if LWIP_NETIF_STATUS_CALLBACK //netif狀態被改變時該函數被調用,未使用 /** This function is called when the netif state is set to up or down */ void (* status_callback)(struct netif *netif); #endif /* LWIP_NETIF_STATUS_CALLBACK */ #if LWIP_NETIF_LINK_CALLBACK //未使用 /** This function is called when the netif link is set to up or down netif鏈接改變時調用 */ void (* link_callback)(struct netif *netif); #endif /* LWIP_NETIF_LINK_CALLBACK */ /** This field can be set by the device driver and could point * to state information for the device. * 這個域可以經過設備驅動設置而且可以指向設備的信息狀態,供用戶自行使用*/ void *state; #if LWIP_DHCP //使能DHCP /** the DHCP client state information for this netif */ struct dhcp *dhcp; #endif /* LWIP_DHCP */ #if LWIP_AUTOIP /** the AutoIP client state information for this netif */ struct autoip *autoip; #endif #if LWIP_NETIF_HOSTNAME /* the hostname for this netif, NULL is a valid value NULL也是一個有效值*/ char* hostname; #endif /* LWIP_NETIF_HOSTNAME */ /** maximum transfer unit (in bytes) 最大發送單元(單位:字節)以太網爲1500*/ 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]; //=6 /** flags (see NETIF_FLAG_ above) 網卡狀態信息標誌位,網卡功能使能、廣播使能、ARP 使能等等*/ u8_t flags; /** descriptive abbreviation 描述縮寫*/ char name[2]; /** number of this interface 接口號,若是兩個網絡接口具備相同的描述縮寫(即上面的name字段),就用num字段來區分相同類型的不一樣網絡接口*/ u8_t num; #if LWIP_SNMP /** link type (from "snmp_ifType" enum from snmp.h) 鏈接類型*/ u8_t link_type; /** (estimate) link speed (預計)鏈接速度*/ u32_t link_speed; /** timestamp at last change made (up/down) 最後up/down變化時間戳*/ u32_t ts; /** counters 計數器*/ u32_t ifinoctets; u32_t ifinucastpkts; u32_t ifinnucastpkts; u32_t ifindiscards; u32_t ifoutoctets; u32_t ifoutucastpkts; u32_t ifoutnucastpkts; u32_t ifoutdiscards; #endif /* LWIP_SNMP */ #if LWIP_IGMP /* This function could be called to add or delete a entry in the multicast filter table of the ethernet MAC.*/ err_t (*igmp_mac_filter)( struct netif *netif, struct ip_addr *group, u8_t action); #endif /* LWIP_IGMP */ #if LWIP_NETIF_HWADDRHINT u8_t *addr_hint; #endif /* LWIP_NETIF_HWADDRHINT */ #if ENABLE_LOOPBACK /* List of packets to be queued for ourselves. */ struct pbuf *loop_first; struct pbuf *loop_last; #if LWIP_LOOPBACK_MAX_PBUFS u16_t loop_cnt_current; #endif /* LWIP_LOOPBACK_MAX_PBUFS */ #endif /* ENABLE_LOOPBACK */ }; // END struct netif
1. next字段指向下一個netif結構體指針.只有一個產品有多個網卡時,才使用該字段.lwIP會把全部 網卡的結構體組成一個鏈表來進行管理. 數據結構
2. ip_addr、netmask、gw分別表示IP地址、子網掩碼和網關地址。前兩個在發送和處理數據時有重要做用,第三個字段目前保留。在同一個局域網內通訊時也不用使用,它是一個網絡與另外一個網絡進行通訊的交換點。不懂的可查看有關計算機網絡書籍。函數
3. input字段指向一個函數,該函數將網卡設備接收到的數據提交給IP層。使用時,將input指針指向該函數便可。參數爲pbuf和netif類型,其中pbuf爲接收到的數據包。oop
4. output字段指向一個函數,該函數和具體的網絡接口設備密切相關,它用於IP層將一包數據發送到網絡接口上。用戶須要編寫此函數並使output指向它。參數爲pbuf、netif和ip_addr類型,其中,ipaddr表明要將該數據包發送到的地址,但不必定是數據包最終到到達的IP地址。好比,要發送IP數據包到一個並不在本網絡的主機上,該數據包要被髮送到一個路由器上,這裏的ipaddr就是路由器IP地址。this
5. linkoutput字段和output相似,但只有兩個參數,它是由ARP模塊調用的,用於將一包數據發送到網絡接口上。實際上output最終仍是調用linkoutput字段函數完成數據包的發送。spa
6. flag字段是網卡狀態標誌位,是很重要的字段,包括網卡功能使能,廣播使能,ARP使能等等。計算機網絡