Can I use pcap library for receiving ipv6 packets?

随声附和 提交于 2019-12-05 16:35:01

That is possible. libpcap is able to catch anything on the wire.

Example using ETHERTYPE_IPV6:

static u_int16_t ether_packet(u_char *args, const struct pcap_pkthdr *pkthdr, co
nst u_char *p) {
  struct ether_header *eptr = (struct ether_header*)p;

  assert(pkthdr->caplen <= pkthdr->len);

  assert(pkthdr->caplen >= sizeof(struct ether_header));

  return eptr->ether_type;
}


// This is the callback. assumes ethernet frame.
static void pcap_callback(u_char *args,const struct pcap_pkthdr* pkthdr,const u_
char* p)
{
  const u_int16_t type = ether_packet(args, pkthdr, p);
  switch (ntohs(type)) {
  case ETHERTYPE_IP:
    // handle IPv4 
    break;
  case ETHERTYPE_IPV6:
    // handle v6
    break;
  }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!