我正在尝试使用名称tap0
来嗅探界面。但是,接口没有分配IP地址,因为我在虚拟网络上有它。
当我尝试下面附加的代码时,它会嗅探所有数据包,但是在错误的接口(enp30s0
)上,而没有来自tap0
接口。我正在使用wireshark捕获数据包,并且没有任何嗅探数据包来自tap0
接口。
问题是:即使没有分配IP地址,我如何仅仅嗅探tap0
?
#define DEFAULT_IF "tap0"
char ifName[IFNAMSIZ];
int broad = 1;
strcpy(ifName, DEFAULT_IF);
sockaddr_ll sAd;
struct ifreq if_idx2;
memset(&if_idx2, 0, sizeof(struct ifreq));
strcpy(if_idx2.ifr_name, ifName);
if (ioctl(sockfd, SIOCGIFINDEX, &if_idx2) < 0)
perror("SIOCGIFINDEX");
sAd.sll_family = AF_PACKET;
sAd.sll_protocol = IPPROTO_UDP;
sAd.sll_ifindex = if_idx2.ifr_ifindex;
sAd.sll_pkttype = PACKET_BROADCAST;
memset(sAd.sll_addr,0xff,8);
bind(sockfd,(sockaddr *)&sAd, sizeof(sAd));
setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST,&broad,sizeof(int));
int pocet =0;
for(int i =0 ; i<128;i++) {
unsigned char bufferik[342];
memset(bufferik, 0, sizeof(char) * 342);
socklen_t slen = sizeof(sAd);
recvfrom(sockfd, bufferik, 342, 0,(sockaddr *)&sAd,&slen);
for (int i = 0; i < 342; i++)
printf("%02x ", bufferik[i]);
cout << endl<< endl;
if (bufferik[1] == 0x10){
cout<< "success finding DHCP packet" <<endl;
for (int j = 0; j < 342; j++)
printf("%02x ", bufferik[j]);
pocet++;
}
}
cout<<pocet<<endl;