我正在尝试构建一个在以太网级别(使用原始套接字)工作的简单回显服务器/客户端。服务器端自己工作并显示 eth0 上的所有传入数据包。客户端工作并在 eth0 上发送以太网数据包(我用 wireshark 检查了这个,可以看到数据包出去了。)我现在想做一个过滤器,只查看我感兴趣的数据包。(这基于目标/源地址。)
在下面的代码中,有人可以向我解释为什么 strncmp 返回零(意味着字符串匹配)但是“if(ethernet_header->h_dest == mac)”无法执行(不匹配)。变量“mac”和“ethernet_header->h_dest”的类型和长度相同。
更多背景知识: - 这是在 linux 64 位(ubuntu)上完成的 - 我在同一台机器上使用 eth0 来发送/接收......我认为这应该不是问题吗?
我只是不明白为什么 strcmp 会返回匹配项,而如果没有。我错过了什么??
void ParseEthernetHeader(unsigned char *packet, int len) {
struct ethhdr *ethernet_header;
unsigned char mac[ETH_ALEN] = {0x01, 0x55, 0x56, 0x88, 0x32, 0x7c};
if (len > sizeof(struct ethhdr)) {
ethernet_header = (struct ethhdr *) packet;
int result = strncmp(ethernet_header->h_dest, mac, ETH_ALEN);
printf("Result: %d\n", result);
if(ethernet_header->h_dest == mac) {
/* First set of 6 bytes are Destination MAC */
PrintInHex("Destination MAC: ", ethernet_header->h_dest, 6);
printf("\n");
/* Second set of 6 bytes are Source MAC */
PrintInHex("Source MAC: ", ethernet_header->h_source, 6);
printf("\n");
/* Last 2 bytes in the Ethernet header are the protocol it carries */
PrintInHex("Protocol: ", (void *) ðernet_header->h_proto, 2);
printf("\n\n");
printf("Length: %d\n",len);
}
} else {
printf("Packet size too small (length: %d)!\n",len);
}
}