我正在尝试使用Windows中的npcap SDK (https://nmap.org/npcap/)获取所有数据包的源地址和目的地址。它适用于IPv4,但它为IPv6地址的源地址和目的地址返回相同的地址。下面是我的packet_handler回调函数的代码:
void packet_handler(u_char* param, const struct pcap_pkthdr* header, const u_char* pkt_data)
{
u_int ip_len;
u_short eth_type;
const sniff_ip* iph;
const in6_addr* orig_saddr6;
const in6_addr* orig_daddr6;
in6_addr swapped_saddr;
in6_addr swapped_daddr;
const struct sniff_ethernet* ethernet; /* The ethernet header */
ip_len = header->len;
ethernet = (struct sniff_ethernet*)(pkt_data);
eth_type = ntohs(ethernet->ether_type);
iph = (sniff_ip*)(pkt_data +
14); //length of ethernet header
if (eth_type == 0x0800) {
char str_saddr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(iph->ip_src), str_saddr, INET_ADDRSTRLEN);
char str_daddr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(iph->ip_dst), str_daddr, INET_ADDRSTRLEN);
printf("%s %s\n", str_saddr, str_daddr);
}
else if (eth_type == 0x86DD)
{
char str_saddr[INET6_ADDRSTRLEN];
orig_saddr6 = (const in6_addr*)&(iph->ip_src);
ipv6_sbyteswap(orig_saddr6, &swapped_saddr);
inet_ntop(AF_INET6, &swapped_saddr, str_saddr, INET6_ADDRSTRLEN);
char str_daddr[INET6_ADDRSTRLEN];
orig_daddr6 = (const in6_addr*)&(iph->ip_dst);
ipv6_dbyteswap(orig_daddr6, &swapped_daddr);
inet_ntop(AF_INET6, &swapped_daddr, str_daddr, INET6_ADDRSTRLEN);
printf("%s %s\n", str_saddr, str_daddr);
}
}我看到的问题是,当eth_type用于IPv6数据包(例如eth_type == 0x86DD)时,saddr和daddr是相同的IP地址,只是字节的顺序不同。我已经对代码进行了两倍和三倍的检查,但是当我检查iph->ip_src和iph->ip_dst时,我看到了相同的类型,所以看起来npcap库返回了相同的地址。我看不出我能做什么来改变这种行为。有没有人碰到过这个?
发布于 2021-03-25 20:29:56
要解决此问题,您必须使用适当的IPv6结构转换IP报头。以下是工作代码:
/* IPv4 header */
typedef struct ip4_header {
u_char ver_ihl; // Version (4 bits) + Internet header length (4 bits)
u_char tos; // Type of service
u_short tlen; // Total length
u_short identification; // Identification
u_short flags_fo; // Flags (3 bits) + Fragment offset (13 bits)
u_char ttl; // Time to live
u_char proto; // Protocol
u_short crc; // Header checksum
ip_address saddr; // Source address
ip_address daddr; // Destination address
u_int op_pad; // Option + Padding
}ip4_header;
/* IPv6 header */
typedef struct ipv6_header
{
unsigned int
version : 4,
traffic_class : 8,
flow_label : 20;
uint16_t length;
uint8_t next_header;
uint8_t hop_limit;
struct in6_addr saddr;
struct in6_addr daddr;
} ipv6_header;
/* Process IPv6 packets*/
void ipv6_handler(const u_char* pkt_data) {
const ipv6_header* iph;
iph = (ipv6_header*)(pkt_data + ETHERNET_HEADER_LEN);
char str_saddr[INET6_ADDRSTRLEN];
memset(str_saddr, 0, sizeof(str_saddr));
inet_ntop(AF_INET6, &iph->saddr, str_saddr, INET6_ADDRSTRLEN);
char str_daddr[INET6_ADDRSTRLEN];
memset(str_daddr, 0, sizeof(str_saddr));
inet_ntop(AF_INET6, &iph->daddr, str_daddr, INET6_ADDRSTRLEN);
printf("%s %s\n", str_saddr, str_daddr);
}
void packet_handler2(u_char* param, const struct pcap_pkthdr* header, const u_char* pkt_data)
{
const struct sniff_ethernet* ethernet; /* The ethernet header */
u_short eth_type;
ethernet = (struct sniff_ethernet*)(pkt_data);
eth_type = ntohs(ethernet->ether_type);
if (eth_type == ETHERNET_TYPE_IPv4) {
ipv4_handler(pkt_data);
}
else if (eth_type == ETHERNET_TYPE_IPv6)
{
ipv6_handler(pkt_data);
}
}https://stackoverflow.com/questions/66784119
复制相似问题