14 Star 9 Fork 0

zhangruoxu/demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ethernet_lib.c 2.09 KB
一键复制 编辑 原始数据 按行查看 历史
zhangruoxu 提交于 2023-11-23 17:14 . 头文件守卫
#include"basetype.h"
#include"common.h"
UINT16 EthernetLib_EncodeEthernetHeader(IN UINT8* pucSrcMac, IN UINT8* pucDstMac,
IN UINT16 usNextProtocol, OUT UINT8* pucBuffer)
{
UINT8* pucPointer = pucBuffer;
memcpy(pucPointer, pucDstMac, MAC_BYTE);
pucPointer += MAC_BYTE;
memcpy(pucPointer, pucSrcMac, MAC_BYTE);
pucPointer += MAC_BYTE;
// *(UINT16*)pucPointer = usNextProtocol;
*pucPointer = (UINT8)((usNextProtocol & 0xFF00) >> 8);
pucPointer++;
*pucPointer = usNextProtocol & 0xFF;
return 14;
}
/*
* description: 解封装报文里面的以太头部分
*
* input:
* pucBuffer 报文
* usPacketLen 报文长度
* output:
* ETHERNET_S* pstEthernetHeader 报文的以太头部分
* return: NULL
* tip: IN是空宏,表示改参数是输入
*/
VOID EthernetLib_DecodeEthernetHeader(IN UINT8* pucBuffer, UINT16 usPacketLen, ETHERNET_S* pstEthernetHeader)
{
if (usPacketLen < 14)
{
printf("Ethernet header message length error\n");
exit(-1);
}
UINT8* pucPointer = pucBuffer;
memcpy(pstEthernetHeader->aucDst, pucPointer, MAC_BYTE);
pucPointer += MAC_BYTE;
memcpy(pstEthernetHeader->aucSrc, pucPointer, MAC_BYTE);
pucPointer += MAC_BYTE;
pstEthernetHeader->usNextProtocol = ((UINT16)(*pucPointer) << 8);
pucPointer++;
pstEthernetHeader->usNextProtocol += (UINT16)(*pucPointer);
pucPointer++;
return;
}
VOID EthernetLib_DecodeEthernetVlanHeader(IN UINT8* pucBuffer, UINT16 usPacketLen, ETHERNET_VLAN_S* pstEthernetHeader)
{
if (usPacketLen < 18)
{
printf("Ethernet header message length error\n");
exit(-1);
}
UINT8* pucPointer = pucBuffer;
memcpy(pstEthernetHeader->aucDst, pucPointer, MAC_BYTE);
pucPointer += MAC_BYTE;
memcpy(pstEthernetHeader->aucSrc, pucPointer, MAC_BYTE);
pucPointer += MAC_BYTE;
memcpy((VOID*)&pstEthernetHeader->usEtype, pucPointer, VLAN_ETYPE);
pucPointer += VLAN_ETYPE;
memcpy((VOID*)&pstEthernetHeader->usVlanTag, pucPointer, VLAN_ID);
pucPointer += VLAN_ID;
pstEthernetHeader->usNextProtocol = ((UINT16)(*pucPointer) << 8);
pucPointer++;
pstEthernetHeader->usNextProtocol += (UINT16)(*pucPointer);
pucPointer++;
return;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangruoxudada/demo.git
git@gitee.com:zhangruoxudada/demo.git
zhangruoxudada
demo
demo
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385