我正在使用以下代码从套接字(AF_PACKET,SOCK_DGRAM,htons(ETH_P_ARP))读取缓冲区。我正在使用arp_frame结构来访问包含的ARP回复的组件部分。 inet_ntoa()返回IP的正确的第一个字节,但其他八位字节是0,产生172.0.0.0。这段代码为什么会产生一个不完整的IP地址?
问题1是为什么会发生这种情况? 问题2是如何在主机字节顺序中以十六进制格式打印msg缓冲区的r个字节以调试数据包?
unsigned char msg[65535];
struct ether_arp *arp_frame = (struct ether_arp *)msg;
while ((r = recv(sock, msg, sizeof(msg), 0))) {
// skip it's not an ARP REPLY
if (ntohs(arp_frame->arp_op) != ARPOP_REPLY)
continue;
for (i = 0; i < SONOS_PREFIX_NUM; i++) {
if (!memcmp(sonos_prefixes[i], arp_frame->arp_sha, 3)) {
struct in_addr addr;
addr.s_addr = *arp_frame->arp_spa;
printf("Blah: %lu\n", ntohl(*arp_frame->arp_spa));
printf("Sonos found at %s\n", inet_ntoa(addr));
}
}
}
Bawls。似乎我咨询完全错误的手册,因为它说这是一个u_long - http://www.propox.com/download/edunet_doc/all/html/structether__arp.html。谢谢:) –
它可能取决于平台......在不需要字节反转的大端系统上,它可以方便地存储为32位整数,但在小端系统中,我想它会取决于实施者选择的'ntohl()'和朋友的实现......尽管它是一个无符号的int/long,但是,将它当作指针是不正确的...... – twalberg