LwIP网络接口结构体---netif

 在lwIP中,是通过结构体netif来描述一个硬件网络接口的,在单网卡中,这个结构体只有一个,多网卡中可有何网卡数目相同的netif结构体,它们构成一个数据链。下面的代码选自netif.h,是netif结构体的代码描述:
  1. /** Generic data structure used for all lwIP network interfaces. 
  2.  *  The following fields should be filled in by the initialization 
  3.  *  function for the device driver: hwaddr_len, hwaddr[], mtu, flags  
  4.  *  用于所有lwIP网络接口的通用数据结构体 
  5.  *  设备驱动函数在初始化时应该填充以下数据域:hwaddr_len,hwaddr[],mtu,flags.*/  
  6.   
  7. struct netif {  
  8.   /** pointer to next in linked list */  
  9.   struct netif *next;  
  10.   
  11.   /** IP address configuration in network byte order */  
  12.   struct ip_addr ip_addr;       //IP地址   
  13.   struct ip_addr netmask;       //子网掩码   
  14.   struct ip_addr gw;            //默认网关   
  15.   
  16.   /** This function is called by the network device driver 
  17.    *  to pass a packet up the TCP/IP stack.  
  18.       这个函数由网络设备驱动所调用,从网卡中接收数据包并传递给TCP/IP协议栈*/  
  19.   err_t (* input)(struct pbuf *p, struct netif *inp);      //从网卡中接收一包数据   
  20.   
  21.   /** This function is called by the IP module when it wants 
  22.    *  to send a packet on the interface. This function typically 
  23.    *  first resolves the hardware address, then sends the packet.  
  24.        当IP模块向接口发送一个数据包时调用此函数。这个函数通常首先解析 
  25.        硬件地址,然后发送数据包*/  
  26.   err_t (* output)(struct netif *netif, struct pbuf *p,    //IP层调用此函数向网卡发送一包数据   
  27.        struct ip_addr *ipaddr);  
  28.   
  29.   /** This function is called by the ARP module when it wants 
  30.    *  to send a packet on the interface. This function outputs 
  31.    *  the pbuf as-is on the link medium.  
  32.       当ARP模块向接口发送一个数据包时调用此函数。这个函数向链路输出一个pbuf。*/  
  33.   err_t (* linkoutput)(struct netif *netif, struct pbuf *p);     //ARP模块调用这个函数向网卡发送一包数据   
  34.   
  35. #if LWIP_NETIF_STATUS_CALLBACK    //netif状态被改变时该函数被调用,未使用   
  36.   /** This function is called when the netif state is set to up or down 
  37.    */  
  38.   void (* status_callback)(struct netif *netif);  
  39. #endif /* LWIP_NETIF_STATUS_CALLBACK */   
  40.   
  41. #if LWIP_NETIF_LINK_CALLBACK        //未使用   
  42.   /** This function is called when the netif link is set to up or down 
  43.       netif连接改变时调用 
  44.    */  
  45.   void (* link_callback)(struct netif *netif);  
  46. #endif /* LWIP_NETIF_LINK_CALLBACK */   
  47.   
  48.   /** This field can be set by the device driver and could point 
  49.    *  to state information for the device.  
  50.    *  这个域能够通过设备驱动设置并且能够指向设备的信息状态,供用户自行使用*/  
  51.   void *state;  
  52.   
  53. #if LWIP_DHCP     //使能DHCP   
  54.   /** the DHCP client state information for this netif */  
  55.   struct dhcp *dhcp;  
  56. #endif /* LWIP_DHCP */   
  57.   
  58. #if LWIP_AUTOIP   
  59.   /** the AutoIP client state information for this netif */  
  60.   struct autoip *autoip;  
  61. #endif   
  62.   
  63. #if LWIP_NETIF_HOSTNAME   
  64.   /* the hostname for this netif, NULL is a valid value NULL也是一个有效值*/  
  65.   char*  hostname;  
  66. #endif /* LWIP_NETIF_HOSTNAME */   
  67.   
  68.   /** maximum transfer unit (in bytes) 最大发送单元(单位:字节)以太网为1500*/  
  69.   u16_t mtu;  
  70.   /** number of bytes used in hwaddr 硬件地址长度*/  
  71.   u8_t hwaddr_len;  
  72.   /** link level hardware address of this interface 接口的连接层硬件地址*/  
  73.   u8_t hwaddr[NETIF_MAX_HWADDR_LEN];       //=6   
  74.   /** flags (see NETIF_FLAG_ above) 标志位*/  
  75.   u8_t flags;  
  76.   /** descriptive abbreviation 描述缩写*/  
  77.   char name[2];  
  78.   /** number of this interface 接口号,如果两个网络接口具有相同的描述缩写(即上面的name字段),就用num字段来区分相同类型的不同网络接口*/  
  79.   u8_t num;  
  80.   
  81. #if LWIP_SNMP   
  82.   /** link type (from "snmp_ifType" enum from snmp.h) 连接类型*/  
  83.   u8_t link_type;  
  84.   /** (estimate) link speed (预计)连接速度*/  
  85.   u32_t link_speed;  
  86.   /** timestamp at last change made (up/down) 最后up/down变化时间戳*/  
  87.   u32_t ts;  
  88.   /** counters 计数器*/  
  89.   u32_t ifinoctets;  
  90.   u32_t ifinucastpkts;  
  91.   u32_t ifinnucastpkts;  
  92.   u32_t ifindiscards;  
  93.   u32_t ifoutoctets;  
  94.   u32_t ifoutucastpkts;  
  95.   u32_t ifoutnucastpkts;  
  96.   u32_t ifoutdiscards;  
  97. #endif /* LWIP_SNMP */   
  98.   
  99. #if LWIP_IGMP   
  100.   /* This function could be called to add or delete a entry in the multicast filter table of the ethernet MAC.*/  
  101.   err_t (*igmp_mac_filter)( struct netif *netif, struct ip_addr *group, u8_t action);  
  102. #endif /* LWIP_IGMP */   
  103. #if LWIP_NETIF_HWADDRHINT   
  104.   u8_t *addr_hint;  
  105. #endif /* LWIP_NETIF_HWADDRHINT */   
  106. #if ENABLE_LOOPBACK   
  107.   /* List of packets to be queued for ourselves. */  
  108.   struct pbuf *loop_first;  
  109.   struct pbuf *loop_last;  
  110. #if LWIP_LOOPBACK_MAX_PBUFS   
  111.   u16_t loop_cnt_current;  
  112. #endif /* LWIP_LOOPBACK_MAX_PBUFS */   
  113. #endif /* ENABLE_LOOPBACK */   
  114. }; // END struct netif  


1. next字段指向下一个netif结构体指针.只有一个产品有多个网卡时,才使用该字段.lwIP会把所有 网卡的结构体组成一个链表来进行管理. 

2. ip_addr、netmask、gw分别表示IP地址、子网掩码和网关地址。前两个在发送和处理数据时有重要作用,第三个字段目前保留。在同一个局域网内通信时也不用使用,它是一个网络与另一个网络进行通信的交换点。不懂的可查看有关计算机网络书籍。

3. input字段指向一个函数,该函数将网卡设备接收到的数据提交给IP层。使用时,将input指针指向该函数即可。参数为pbuf和netif类型,其中pbuf为接收到的数据包。

4. output字段指向一个函数,该函数和具体的网络接口设备密切相关,它用于IP层将一包数据发送到网络接口上。用户需要编写此函数并使output指向它。参数为pbuf、netif和ip_addr类型,其中,ipaddr代表要将该数据包发送到的地址,但不一定是数据包最终到到达的IP地址。比如,要发送IP数据包到一个并不在本网络的主机上,该数据包要被发送到一个路由器上,这里的ipaddr就是路由器IP地址。

5. linkoutput字段和output类似,但只有两个参数,它是由ARP模块调用的,用于将一包数据发送到网络接口上。实际上output最终还是调用linkoutput字段函数完成数据包的发送。

6. flag字段是网卡状态标志位,是很重要的字段,包括网卡功能使能,广播使能,ARP使能等等

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值