嵌入式C语言实现以太网ARP协议栈:无操作系统MCU也能实现网络通信
无操作系统跑网络,协议栈的第一层并不是 IP,而是 ARP。IP 包要想发出去,必须先把目的 IP 换成一个 MAC 地址;这一步没有做对,后面的 UDP、TCP 一条都发不出去。但 ARP 本身很窄,只需一个缓存表、两个处理分支、一个超时老化,全套代码不到百行就能完成。
帧头与 ARP 报文结构
以太网帧头 14 字节,其中类型字段 0x0806 表示 ARP。ARP 报文固定 28 字节,硬件类型 1、协议类型 0x0800、长度 6 和 4:
typedef struct __attribute__((packed)) {
uint8_t dst[6];
uint8_t src[6];
uint16_t type; /* 0x0806 */
} eth_hdr_t;
typedef struct __attribute__((packed)) {
uint16_t htype; /* 1 = Ethernet */
uint16_t ptype; /* 0x0800 */
uint8_t hlen; /* 6 */
uint8_t plen; /* 4 */
uint16_t oper; /* 1 请求, 2 应答 */
uint8_t sha[6]; /* 发送方 MAC */
uint8_t spa[4]; /* 发送方 IP */
uint8_t tha[6]; /* 目标 MAC, 请求时全0 */
uint8_t tpa[4]; /* 目标 IP */
} arp_pkt_t;
结构体必须加 packed,否则编译器会按 4 字节对齐填充,后面的字段全部网段偏移错位。
缓存表:小容量带时间戳
MCU 的 RAM 有限,缓存表不用太大,8 条就够一个典型工控网段使用:
#define ARP_SLOTS 8
#define ARP_TTL_MS 120000UL /* 2 分钟 */
typedef struct {
uint8_t ip[4];
uint8_t mac[6];
uint32_t stamp;
uint8_t used;
} arp_entry_t;
static arp_entry_t g_arp[ARP_SLOTS];
static arp_entry_t *arp_find(const uint8_t *ip)
{
for (int i = 0; i < ARP_SLOTS; i++)
if (g_arp[i].used && !memcmp(g_arp[i].ip, ip, 4))
return &g_arp[i];
return NULL;
}
static void arp_learn(const uint8_t *ip, const uint8_t *mac)
{
arp_entry_t *e = arp_find(ip);
if (!e) { /* 找空位或淘汰最旧 */
e = &g_arp[0];
for (int i = 0; i < ARP_SLOTS; i++) {
if (!g_arp[i].used) { e = &g_arp[i]; break; }
if (g_arp[i].stamp < e->stamp) e = &g_arp[i];
}
}
memcpy(e->ip, ip, 4);
memcpy(e->mac, mac, 6);
e->stamp = now_ms();
e->used = 1;
}
arp_learn 在两个地方被调用:收到 ARP 请求时顺便记录对方的 IP 与 MAC,这是免费获取的映射;收到应答时也记录。把发送方信息也学下来,能省掉很多不必要的请求。
收到请求就回,收到应答就更新
主循环从 MAC 取一帧,类型是 0x0806 就交给 ARP 处理:
void arp_input(uint8_t *buf, uint16_t len)
{
if (len < sizeof(eth_hdr_t) + sizeof(arp_pkt_t)) return;
eth_hdr_t *eth = (eth_hdr_t *)buf;
arp_pkt_t *a = (arp_pkt_t *)(buf + sizeof(eth_hdr_t));
if (a->htype != 1 || a->ptype != 0x0800) return;
if (a->hlen != 6 || a->plen != 4) return;
arp_learn(a->spa, a->sha); /* 先学, 请求应答都学 */
if (a->oper != 1) return; /* 只处理请求 */
if (memcmp(a->tpa, g_my_ip, 4)) return; /* 问的不是我 */
arp_send(2, a->sha, a->spa); /* 回应答 */
}
组包发送:应答单播,请求广播
static void arp_send(uint16_t oper, const uint8_t *tha, const uint8_t *tpa)
{
static uint8_t tx[sizeof(eth_hdr_t) + sizeof(arp_pkt_t)];
eth_hdr_t *eth = (eth_hdr_t *)tx;
arp_pkt_t *a = (arp_pkt_t *)(tx + sizeof(eth_hdr_t));
const uint8_t bcast[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
memcpy(eth->dst, oper == 1 ? bcast : tha, 6); /* 请求必广播 */
memcpy(eth->src, g_my_mac, 6);
eth->type = htons(0x0806);
a->htype = htons(1); a->ptype = htons(0x0800);
a->hlen = 6; a->plen = 4; a->oper = htons(oper);
memcpy(a->sha, g_my_mac, 6);
memcpy(a->spa, g_my_ip, 4);
memcpy(a->tha, oper == 1 ? bcast : tha, 6);
memcpy(a->tpa, tpa, 4);
eth_send(tx, sizeof(tx)); /* 不足60字节由驱动补pad */
}
发送前补一个要点:以太网最小帧长 60 字节(不含 FCS),ARP 帧总长 42 字节,必须由驱动补 18 字节填充,否则很多交换机会直接丢掉。
发 IP 包前先查表,查不到先挂起
int ip_need_mac(const uint8_t *dip, uint8_t *dmac)
{
arp_entry_t *e = arp_find(dip);
if (e && now_ms() - e->stamp < ARP_TTL_MS) {
memcpy(dmac, e->mac, 6);
return 0; /* 命中 */
}
arp_send(1, NULL, dip); /* 发请求 */
return -1; /* 调用方挂起该包 */
}
挂起策略要明确:只挂一份待发包,不排队。ARP 应答通常几十毫秒内就回来,请求重发间隔取 1 秒,连发三次没回应则丢弃该包并返回主机不可达。
三个实际踩过的坑
只处理请求,不处理应答。 结果每次发包都在重新发请求,网段里 ARP 广播增多,交换机 MAC 表翻动。应答必须进缓存。
缓存不过期。 对端换了网卡或 IP 被复用后,设备一直往旧 MAC 发,抓包看是“数据发出去了但没人回”,误判成上层协议问题。TTL 老化必须做,到点清标记,下次自然重查。
多网口共用一张缓存表。 双网口设备把两个网段的映射混在一张表里,同一个 IP 在两侧对应不同 MAC,来回覆盖导致随机不通。缓存表要按端口分开索引。
无 OS 的 MCU 跑 ARP,靠的是紧凑结构体加 packed、请求与应答都学习、应答单播请求广播、发 IP 前先查表并挂起一份待发包、TTL 老化定期清。协议本身十几行逻辑,能不能通,全看帧长补位和缓存老化这两处细节有没有做对。





