网站主页图片设计,网站更新提醒,wordpress怎么卸载,特色个人网站1、今天要实现lwip的多个ip配置#xff0c;本来以为需要自己修改很多核心代码 2、查阅资料才发现#xff0c;lwip已经把接口留出来了
/** Define this to 1 and define LWIP_ARP_FILTER_NETIF_FN(pbuf, netif, type) * to a filter function that returns the correct neti…1、今天要实现lwip的多个ip配置本来以为需要自己修改很多核心代码 2、查阅资料才发现lwip已经把接口留出来了
/** Define this to 1 and define LWIP_ARP_FILTER_NETIF_FN(pbuf, netif, type) * to a filter function that returns the correct netif when using multiple * netifs on one hardware interface where the netifs low-level receive * routine cannot decide for the correct netif (e.g. when mapping multiple * IP addresses to one hardware interface). */
/** 将此定义为1并将LWIP_ARP_FILTER_NETIF_FN(pbuf, netif, type)定义为一个过滤函数该函数在使用单个硬件接口上的多个netif时返回正确的netif。当netif的低级接收例程无法确定正确的netif时例如当将多个IP地址映射到单个硬件接口时需要使用此过滤函数。 */ #ifndef LWIP_ARP_FILTER_NETIF #define LWIP_ARP_FILTER_NETIF 0 #endif 3、所以我们只需要定义好宏 #define LWIP_ARP_FILTER_NETIF 1 //多ip的支持
和写好
struct netif * LWIP_ARP_FILTER_NETIF_FN(struct pbuf *p, struct netif *netifIn, u16_t type)
函数即可 4、我的操作 4.1 在lwipopt.h定义使能宏LWIP_ARP_FILTER_NETIF 以支持多ip的操作 4.2、编写LWIP_ARP_FILTER_NETIF_FN函数以能根据ip能找到对应的网卡
/*
*********************************************************************************************************
* Function Name : LWIP_ARP_FILTER_NETIF_FN
* Description : 多ip识别正确的网卡
* Input : None
* Output : None
* Return : None
*********************************************************************************************************
*/
#include lwip/prot/etharp.h
struct netif * LWIP_ARP_FILTER_NETIF_FN(struct pbuf *p, struct netif *netifIn, u16_t type)
{struct netif *netif NULL;struct etharp_hdr *hdr NULL;struct ip_hdr *iphdr NULL;ip_addr_t dest/*, src*/;switch (type){/*ARP*/case 0x0806:hdr (struct etharp_hdr *)((unsigned char*)p-payload 14);memcpy(dest, (hdr-dipaddr), sizeof(ip4_addr_t));//memcpy(src, hdr-sipaddr, sizeof (ip4_addr_t));for (netif netif_list; netif ! NULL; netif netif-next){if (netif_is_up(netif)){if (ip4_addr_cmp(dest,(netif-ip_addr))){break;}}}break;/*IP*/case 0x0800:iphdr (struct ip_hdr *) ((unsigned char*)p-payload 14);ip_addr_copy_from_ip4(dest, iphdr-dest);//ip_addr_copy_from_ip4(src, iphdr-src);for (netif netif_list; netif ! NULL; netif netif-next){if (netif_is_up(netif)){if (ip4_addr_cmp(dest, (netif-ip_addr))){break;}}}break;default:netif netif_list;break;}netifIn netif;if(netifNULL){pbuf_free(p);}return netif;}
根据ip去匹配netifarp需要单独处理因为ip通信首先都要发送arp去找物理地址不加这个arp处理会出现你ping不通ip。所以如果知道物理地址不加这个arp处理也可以直接自己填mac即可加到你自己的静态arp表中。
大家注意 这个一定要加上我刚开始没加结果刚开始通运行一段时间导致不通了就是因为这里没释放申请的内存导致无法处理新的数据
4.3、增加多ip的设置 //开始虚拟多ip--1IP4_ADDR(ipaddr_v[1], 192,168,20,48);IP4_ADDR(netmask_v[1], 255,255,255,0);IP4_ADDR(gw_v[1], 192, 168, 20,1);netif_add(g_netif_v[1], ipaddr_v[1], netmask_v[1], gw_v[1], NULL, ethernetif_init, tcpip_input); //网卡初始化和网卡输入netif_set_up(g_netif_v[1]);//开始虚拟多ip--2IP4_ADDR(ipaddr_v[2], 192,168,30,48);IP4_ADDR(netmask_v[2], 255,255,255,0);IP4_ADDR(gw_v[2], 192, 168, 30,1);netif_add(g_netif_v[2], ipaddr_v[2], netmask_v[2], gw_v[2], NULL, ethernetif_init, tcpip_input); //网卡初始化和网卡输入netif_set_up(g_netif_v[2]);
4.4、然后运行测试OK 5、测试结果
刚开始测试结果是这样我没在意以为是硬件还是那里问题ping移植有超时或者卡顿较大 有时还会出现几十上百ms的延迟特别是多个电脑同时ping时。
6、ping超时卡顿较大原因
1 原来多个neif_add时 会每次都初始化网卡这里网卡在初始化时会创建解析网络中断数据包的任务所以存在多个任务去读网卡数据造成数据的竞争从而导致可能出现超时卡顿较大
2 我们只有一个网卡所以只需初始化1次解析网络中断数据包的任务
改了之后就OK了
像下面这篇文章这样