我有这段代码,它应该从我正在监听的套接字打印每个数据包的目标和源 MAC 地址:
sock_raw = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
if(sock_raw < 0)
{
printf("Socket Error\n");
return 1;
}
while(1)
{
saddr_size = sizeof saddr;
//Receive a packet
data_size = recvfrom(sock_raw , buffer , 65536 , 0 , &saddr , (socklen_t*)&saddr_size);
if(data_size <0 )
{
printf("Recvfrom error , failed to get packets\n");
return 1;
}
//Now process the packet
ProcessPacket(buffer , data_size);
}
close(sock_raw);
............
fprintf(logfile,"\n###########################################################");
struct ether_header *ethh;
ethh = (struct ether_header *)Buffer;
fprintf(logfile,"\n\n***********************ETHERNET Packet*************************\n");
fprintf(logfile,"\n");
fprintf(logfile,"Ethernet Header\n");
fprintf(logfile," |-Source Address : %s\n", ether_ntoa((struct ether_addr *)ðh->ether_dhost));
fprintf(logfile," |-Destination Address : %s\n", ether_ntoa((struct ether_addr *)ðh->ether_shost));
fprintf(logfile,"\n");
fprintf(logfile,"\n###########################################################");
问题是当我运行 ping 或其他操作时,我希望在我的 ether_addr 结构的 ether_dhost 变量中看到我正在监听的 PC 的 MAC。但是,从该变量打印的 MAC 不是我的。此外,源 MAC 也不是我所期望的。
有人可以向我解释发生了什么并可能为我的代码提供修复吗?
谢谢!