lwip单网卡多ip的实现

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 netif's 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_NETIF1 //多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能找到对应的网卡

cpp 复制代码
/*
*********************************************************************************************************
* 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(netif==NULL)
    {
        pbuf_free(p);
    }
	return netif;
    
}

根据ip去匹配netif,arp需要单独处理,因为ip通信首先都要发送arp去找物理地址,不加这个arp处理,会出现你ping不通ip。所以如果知道物理地址,不加这个arp处理也可以,直接自己填mac即可,加到你自己的静态arp表中。

大家注意:

这个一定要加上,我刚开始没加,结果刚开始通,运行一段时间导致不通了,就是因为这里没释放申请的内存,导致无法处理新的数据

4.3、增加多ip的设置

cpp 复制代码
	//开始虚拟多ip--1
	IP4_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--2
	IP4_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了

像下面这篇文章这样:

相关推荐
义一4 小时前
华为eNSP示例说明网关地址和终端IP地址不在同一网段能正常通信吗
网络
minji...7 小时前
Linux 基础IO(一) (C语言文件接口、系统调用文件调用接口open,write,close、文件fd)
linux·运维·服务器·网络·数据结构·c++
真正的醒悟8 小时前
202503-经验之道
服务器·网络·php
qq_251616199 小时前
ubuntu nginx文件服务器
linux·服务器·网络
云计算练习生9 小时前
渗透测试行业术语—— 网络攻击方式与漏洞利用
服务器·网络·安全·渗透测试术语·网络安全术语
乾元9 小时前
SDN 与 AI 协同:控制面策略自动化与策略一致性校验
运维·网络·人工智能·网络协议·华为·系统架构·ansible
锡兰_CC9 小时前
无缝触达,卓越体验:开启openEuler世界的任意门
服务器·网络·数据库·c++·图像处理·qt·nginx
橘子真甜~9 小时前
C/C++ Linux网络编程10 - http协议
linux·服务器·网络·c++·网络协议·http
十五年专注C++开发10 小时前
async_simple:一个轻量级C++异步协程框架
开发语言·网络·c++·boost·asio
hnlq11 小时前
基于dpdk的用户态协议栈的实现(三)—— TCP的三次握手实现
网络·网络协议·tcp/ip