1、miscellaneous声明
(1)结构体声明
struct netif {
struct netif *next;
ip_addr_t ip_addr;
ip_addr_t netmask;
ip_addr_t gw;
// 接口接收数据函数,调用系统函数ethernet_input
netif_input_fn input;
//接口输出数据函数,调用系统函数etharp_output
netif_output_fn output;
//底层硬件输出数据函数,调用自定义函数low_level_output
netif_linkoutput_fn linkoutput;
void *state;
u16_t mtu;
u8_t hwaddr_len;
u8_t hwaddr[NETIF_MAX_HWADDR_LEN];
u8_t flags;
char name[2];
u8_t num;
#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 */
};
结构体中的typedef定义
typedef err_t (*netif_init_fn)(struct netif *netif);
typedef err_t (*netif_input_fn)(struct pbuf *p, struct netif *inp);
typedef err_t (*netif_output_fn)(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr);
typedef err_t (*netif_linkoutput_fn)(struct netif *netif, struct pbuf *p);
(2)全局变量声明
struct netif *netif_list; //网络接口链表指针
struct netif *netif_default; //default 接口
static u8_t netif_num;
static struct netif loop_netif;
2、loopback interface 介绍
以loopback 接口初始化自定义的接口。
void netif_init(void)
{
#if LWIP_HAVE_LOOPIF
ip_addr_t loop_ipaddr, loop_netmask, loop_gw;
IP4_ADDR(&loop_gw, 127,0,0,1);
IP4_ADDR(&loop_ipaddr, 127,0,0,1);
IP4_ADDR(&loop_netmask, 255,0,0,0);
// 无操作系统input函数使用ip_input,若使用操作系统,则使用tcpip_input,不经过底层网卡数据
#if NO_SYS
netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, ip_input);
#else /* NO_SYS */
netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, tcpip_input);
#endif /* NO_SYS */
netif_set_up(&loop_netif);
#endif /* LWIP_HAVE_LOOPIF */
}
netif_loopif_init 和netif_add实现对struct netif成员变量的初始化
static err_t netif_loopif_init(struct netif *netif)
{
netif->name[0] = 'l';
netif->name[1] = 'o';
netif->output = netif_loop_output; //使用函数net_poll传送至netif->input函数
return ERR_OK;
}
struct netif *netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input)
{
ip_addr_set_zero(&netif->ip_addr);
ip_addr_set_zero(&netif->netmask);
ip_addr_set_zero(&netif->gw);
netif->flags = 0;
#if ENABLE_LOOPBACK
netif->loop_first = NULL;
netif->loop_last = NULL;
#endif /* ENABLE_LOOPBACK */
/* remember netif specific state information data */
netif->state = state;
netif->num = netif_num++;
netif->input = input;
netif_set_addr(netif, ipaddr, netmask, gw);
/* call user specified initialization function for netif */
if (init(netif) != ERR_OK) {
return NULL;
}
/* add this netif to the list */
netif->next = netif_list;
netif_list = netif;
return netif;
}
net_set_up实现对struct netif 成员变量flags的设置
#define NETIF_FLAG_UP 0x01U
#define NETIF_FLAG_BROADCAST 0x02U
#define NETIF_FLAG_POINTTOPOINT 0x04U
#define NETIF_FLAG_DHCP 0x08U
#define NETIF_FLAG_LINK_UP 0x10U
#define NETIF_FLAG_ETHARP 0x20U
#define NETIF_FLAG_ETHERNET 0x40U
#define NETIF_FLAG_IGMP 0x80U
void netif_set_up(struct netif *netif)
{
if (!(netif->flags & NETIF_FLAG_UP)) {
netif->flags |= NETIF_FLAG_UP;
NETIF_STATUS_CALLBACK(netif);
if (netif->flags & NETIF_FLAG_LINK_UP) {
#if LWIP_ARP
/* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
if (netif->flags & (NETIF_FLAG_ETHARP)) {
etharp_gratuitous(netif);
}
#endif /* LWIP_ARP */
}
}
}
|