如何使用 lwIP 解析以太网数据包头(源和目标 MAC 地址)

例如,你可以在自定义数据包处理函数中使用此代码。

lwip_parse_mac.cpp
#include <lwip/prot/ethernet.h>

eth_hdr* hdr = reinterpret_cast<eth_hdr*>(buffer);
auto srcMAC = hdr->src.addr;
auto dstMAC = hdr->dest.addr;

Serial.printf("Received packet: Packet of length %d\n", len);
Serial.printf("    Src MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", srcMAC[0], srcMAC[1], srcMAC[2], srcMAC[3], srcMAC[4], srcMAC[5]);
Serial.printf("    Dst MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", dstMAC[0], dstMAC[1], dstMAC[2], dstMAC[3], dstMAC[4], dstMAC[5]);
Serial.printf("    Our MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", ourEthernetMAC[0], ourEthernetMAC[1], ourEthernetMAC[2], ourEthernetMAC[3], ourEthernetMAC[4], ourEthernetMAC[5]);

Check out similar posts by category: Embedded, Networking