关于使用libpcap的以太网/IP头主机/addr的问题?

关于使用libpcap的以太网/IP头主机/addr的问题?,c,ip,libpcap,C,Ip,Libpcap,我正在使用libpcap解码一些ip数据包 但我发现我解码的以太网以太网地址和ip地址都是一样的 我在哪里搞砸了 提前谢谢 下面是回调函数的一部分: 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, dh

我正在使用libpcap解码一些ip数据包

但我发现我解码的以太网以太网地址和ip地址都是一样的

我在哪里搞砸了

提前谢谢

下面是回调函数的一部分:

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
    ...
}

inet\u ntoa
执行以下操作:

  • 将地址的字符串形式放入缓冲区
  • 返回缓冲区的地址
关键是每次调用它时,它都使用相同的缓冲区

因此,当这条线运行时:

printf("%s -> %s\n", inet_ntoa(saddr), inet_ntoa(daddr));
首先它将把一个地址放入缓冲区,然后它将把另一个地址放入缓冲区,然后它将打印缓冲区的内容两次

您可以通过将字符串存储在自己单独的缓冲区中来解决此问题:

char saddr_str[INET_ADDRSTRLEN];
char daddr_str[INET_ADDRSTRLEN];
strcpy(saddr_str, inet_ntoa(saddr));
strcpy(daddr_str, inet_ntoa(daddr));
printf("%s -> %s\n", saddr_str, daddr_str);
或者通过调用
printf
两次:

printf("%s -> ", inet_ntoa(saddr));
printf("%s\n", inet_ntoa(daddr));

ether\u ntoa
也有同样的问题