14 Star 9 Fork 0

zhangruoxu/demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
routing_process.c 4.70 KB
一键复制 编辑 原始数据 按行查看 历史
fishobone 提交于 2023-07-23 17:33 . ARP请求转发逻辑
#include "basetype.h"
#include "common.h"
#include "debug_lib.h"
#include "packet_lib.h"
#include "ethernet_lib.h"
#include "arp_lib.h"
#include "switch_data.h"
#include "ipv4_lib.h"
#include "icmp_lib.h"
#include "addr_lib.h"
#include "device_data.h"
#include "route_data.h"
#include "timer_data.h"
#include "arp_data.h"
#include "route_fwdframe.h"
#include "routing_process.h"
#include "arp_pktprocess.h"
/*
函数功能:网络层三层发送:查路由和ARP表项然后调用send2
输入参数:
输出参数:
返回值: 无
注意点: 本函数被路由器或者主机的转发框架send4或者icmp send调用*/
UINT32 send3(UINT16 usDeviceId, UINT32 uiDstIPv4Addr, UINT32 uiSrcIPv4Addr, UINT8 *pucBuffer, UINT16 usPacketLen, UINT8 ucTTL, UINT8 ucProtocol)
{
DEVICE_IF_S *pstIf, *pstLinkIf;
ROUTE_DEVICE_S *pstRouteDevice;
ROUTE_DATA_S *pstRoute;
INT32 iRet = ERROR_SUCCESS;
UINT8 aucIPAddrString[IPV4_STRING_MAXLEN];
ARP_DEVICE_S *pstArpDevice;
ARP_NODE_S *pstArpNode;
UINT8 aucBroadCastMac[MAC_BYTE] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
AddrLib_InttoDotString(uiDstIPv4Addr, aucIPAddrString);
DebugLib_Printf(MODULE_PACKET, "[%d]send3 to %s\n", usDeviceId, aucIPAddrString);
pstRouteDevice = RouteData_FindDevice(usDeviceId); // 查找当前路由设备
if (NULL == pstRouteDevice)
{
return ERROR_DEVICE_NOT_EXSIT;
}
// 查最佳路由 按子网掩码从小到大的顺序
pstRoute = RouteData_BestMatch(pstRouteDevice, uiDstIPv4Addr);
if (NULL == pstRoute)
{
return ERROR_NO_ROUTE;
}
pstIf = DeviceData_GetIf(usDeviceId, pstRoute->usIfNum);
if (32 == pstRoute->usPrefixLen) // 主机路由
{
// ip封装 todo
usPacketLen += Ipv4Lib_EncodeIPv4Header(0xFF, Protocol_ICMP, uiSrcIPv4Addr, uiDstIPv4Addr, pucBuffer);
// 以太头封装
usPacketLen += EthernetLib_EncodeEthernetHeader(pstIf->aucMac, pstIf->aucMac, ETHERNET_PROTOCOL_IPV4,pucBuffer);
DeviceData_SendPacketMyself(usDeviceId, pstIf->usIfNum, pucBuffer, usPacketLen);
}
else if (ROUTE_PROTOCOL_IF == pstRoute->usProtocol) // 直连路由
{
// 查ARP表 并找到对应设备的ARP
pstArpDevice = ArpData_FindDevice(usDeviceId);
pstArpNode = ArpData_FindArp(pstArpDevice, uiDstIPv4Addr);
if (NULL != pstArpNode) // 查询arp表项为空,报文丢弃
{
/* 先封装网络层头部,再调用send2发送 */
usPacketLen += Ipv4Lib_EncodeIPv4Header(ucTTL, ucProtocol, uiSrcIPv4Addr, uiDstIPv4Addr, pucBuffer);
send2(usDeviceId, pstRoute->usIfNum, pstIf->aucMac, pstArpNode->aucMacAddr, ETHERNET_PROTOCOL_IPV4, pucBuffer, usPacketLen);
}
else
{
ARP_Send(usDeviceId, pstRoute->usIfNum, pstIf->aucMac, aucBroadCastMac, uiSrcIPv4Addr, uiDstIPv4Addr, OP_ARPREQUST);
}
}
else // 其他路由
{
// 查ARP表 并找到对应设备的ARP表项
pstArpDevice = ArpData_FindDevice(usDeviceId);
pstArpNode = ArpData_FindArp(pstArpDevice, pstRoute->uiNexthop);
if (NULL != pstArpNode) // 查询arp表项为空,报文丢弃
{
/* 先封装网络层头部,再调用send2发送 */
usPacketLen += Ipv4Lib_EncodeIPv4Header(ucTTL, ucProtocol, uiSrcIPv4Addr, uiDstIPv4Addr, pucBuffer);
send2(usDeviceId, pstRoute->usIfNum, pstIf->aucMac, pstArpNode->aucMacAddr, ETHERNET_PROTOCOL_IPV4, pucBuffer, usPacketLen);
}
else
{
ARP_Send(usDeviceId, pstRoute->usIfNum, pstIf->aucMac, aucBroadCastMac, uiSrcIPv4Addr, pstRoute->uiNexthop, OP_ARPREQUST);
}
}
return iRet;
}
VOID ARP_Send(UINT32 usDeviceId, UINT32 usOutIfNum, UINT8 *pucSrcMac, UINT8 *pucDstMac, UINT32 uiSrcIPv4Addr, UINT32 uiDstIPv4Addr, UINT8 ucOpCode)
{
UINT8 aucBuffer[PACKET_MAX_SIZE] = {0};
UINT16 usPacketLen = ArpLib_EncodeArp(pucSrcMac, pucSrcMac, uiSrcIPv4Addr, uiDstIPv4Addr, ucOpCode, &aucBuffer);
send2(usDeviceId, usOutIfNum, pucSrcMac, pucDstMac, ETHERNET_PROTOCOL_ARP, &aucBuffer, usPacketLen);
return;
}
/*
函数功能:数据链路层二层发送
输入参数:
输出参数:
返回值: 无
注意点: 本函数被路由器或者主机的转发框架send3或者arp send调用*/
VOID send2(UINT32 usDeviceId, UINT32 usOutIfNum, UINT8 *puiSrcMac, UINT8 *puiDstMac, UINT16 usNextProtocol, UINT8 *pcBuffer, UINT16 usPacketLen)
{
usPacketLen += EthernetLib_EncodeEthernetHeader(puiSrcMac, puiDstMac, usNextProtocol, pcBuffer);
DeviceData_SendPacket(usDeviceId, usOutIfNum, pcBuffer, usPacketLen);
return;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangruoxudada/demo.git
git@gitee.com:zhangruoxudada/demo.git
zhangruoxudada
demo
demo
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385