【发布时间】:2017-04-27 12:24:18
【问题描述】:
我正在使用 libpcap 来解码一些 ip 数据包。
但我发现我解码的以太网 ether_shost/ether_dhost 和 ip saddr/daddr 都是一样的。
我在哪里搞砸了?
提前致谢。
这里是回调函数的一部分:
void
got_packet(u_char *args, const struct pcap_pkthdr *header,
const u_char *packet)
{
...
eth_h = (struct ether_header *) packet;
struct ether_addr shost, dhost;
memcpy(&shost, eth_h->ether_shost, sizeof(shost));
memcpy(&dhost, eth_h->ether_dhost, sizeof(dhost));
printf("L2 DLT_EN10MB: %s -> %s\n", ether_ntoa(&shost), ether_ntoa(&dhost)); // shost == dhost?
if (ntohs(eth_h->ether_type) != ETHERTYPE_IP) {
return;
}
// only work for L2 DLT_EN10MB
ip_h = (struct iphdr *) (packet + sizeof(struct ether_header));
if (ip_h->version != 4) {
return;
}
struct in_addr saddr, daddr;
saddr.s_addr = ip_h->saddr;
daddr.s_addr = ip_h->daddr;
printf("%s -> %s\n", inet_ntoa(saddr), inet_ntoa(daddr)); // saddr == daddr?
printf("%" PRIu32 " -> %" PRIu32 "\n", ntohl(ip_h->saddr), ntohl(ip_h->daddr)); // actually not the same
...
}
【问题讨论】: