打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
lwip之接口netif

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         0x80Uvoid 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 */    }  }}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
LwIP移植心得
手把手教你手撸通讯协议(二)-网络的基础
Lwip之IP/MAC地址冲突检测
基于Aurix的以太网实践:TCP Client实现坑点
STM32F4+LAN8720A+STM32CubeMX+Lwip网络通讯(以太网通讯)小实例
多重网络
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服