代码执行到这个模块之间出现越界还是什么问题不能继续运行
//获取自己主机的MAC地址
int GetSelfMac(pcap_t* adhandle, const char* ip_addr, unsigned char* ip_mac)
{
unsigned char sendbuf[42];
int i = -1;
int res;
struct ethernet_head eh;
struct arp_head ah;
struct pcap_pkthdr* pkt_header;
const u_char* pkt_data;
memset(eh.dst_mac_add, 0xff, 6); //目的地址全为广播地址
memset(eh.src_mac_add, 0x0f, 6);
memset(ah.src_mac_add, 0x0f, 6);
memset(ah.dst_mac_add, 0x00, 6);
eh.type = htons(ETH_ARP);
ah.hardware_type = htons(ARP_HARDWARE);
ah.protocol_type = htons(ETH_IP);
ah.hardware_add_len = 6;
ah.hardware_add_len = 4;
ah.src_ip_add = inet_addr("100.100.100.100"); //随便设一个请求方ip
ah.operation_field = htons(ARP_REQUEST);
ah.dst_ip_add = inet_addr(ip_addr);
memset(sendbuf, 0, sizeof(sendbuf));
memcpy(sendbuf, &eh, sizeof(eh));
memcpy(sendbuf + sizeof(eh), &ah, sizeof(ah));
if (pcap_sendpacket(adhandle, sendbuf, 42) == 0) {
printf("\nPacketSend succeed\n");
}
else {
printf("PacketSendPacket in getmine Error: %d\n",(int) GetLastError());
return 0;
}
while ((res = pcap_next_ex(adhandle, &pkt_header, &pkt_data)) >= 0) {
if (*(unsigned short *)(pkt_data + 12) == htons(ETH_ARP)
&& *(unsigned short*)(pkt_data + 20) == htons(ARP_REPLY)
&& *(unsigned long*)(pkt_data + 38)
== inet_addr("100.100.100.100")) {
for (i = 0; i < 6; i++) {
ip_mac[i] = *(unsigned char *)(pkt_data + 22 + i);
}
printf("获取自己主机的MAC地址成功!\n");
break;
}
}
if (i == 6) {
return 1;
}
else {
return 0;
}
}
//向局域网内所有可能的IP地址发送ARP请求包线程
DWORD WINAPI SendArpPacket(LPVOID lpParameter)
{
struct sparam* spara = (struct sparam*)lpParameter;
pcap_t* adhandle = spara->adhandle;
char* ip = spara->ip;
unsigned char* mac = spara->mac;
char* netmask = spara->netmask;
printf("ip_mac:%02x-%02x-%02x-%02x-%02x-%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
printf("自身的IP地址为:%s\n", ip);
printf("地址掩码NETMASK为:%s\n", netmask);
printf("\n");
unsigned char sendbuf[42];
struct ethernet_head eh;
struct arp_head ah;
memset(eh.dst_mac_add, 0xff, 6);
memcpy(eh.src_mac_add, mac, 6);
memcpy(ah.src_mac_add, mac, 6);
memset(ah.dst_mac_add, 0xff, 6);
eh.type = htons(ETH_ARP);
ah.hardware_type = htons(ARP_HARDWARE);
ah.protocol_type = htons(ETH_IP);
ah.hardware_add_len = 6;
ah.protocol_add_len = 4;
ah.src_ip_add = inet_addr(ip);
ah.operation_field = htons(ARP_REQUEST);
//向局域网内广播发arp包
unsigned long myip = inet_addr(ip);
unsigned long mynetmask = inet_addr(netmask);
unsigned long hisip = htonl((myip & mynetmask));
for (int i = 0; i < HOSTNUM; i++)
{
ah.dst_ip_add = htonl(hisip + i);
memset(sendbuf, 0, sizeof(sendbuf));
memcpy(sendbuf, &eh, sizeof(sendbuf));
memcpy(sendbuf + sizeof(eh), &ah, sizeof(ah));
if (pcap_sendpacket(adhandle, sendbuf, 42) == 0)
{
printf("\nPacketSend succeed\n");
printf("\nPacketSend succeed\n");
}
else
{
printf("PacketSendPacket in getmine Error: %d\n", (int)GetLastError());
}
Sleep(50);
}
Sleep(1000);
flag = 1;
return 0;
}
//分析截留的数据包获取活动的主机IP地址
DWORD WINAPI GetLivePC(LPVOID lpParameter)
{
struct gparam* gparam = (struct gparam*)lpParameter;
pcap_t* adhandle = gparam->adhandle;
int res;
unsigned char Mac[6];
struct pcap_pkthdr* pkt_header;
const char* pkt_data;
while (1)
{
if (flag)
{
printf("扫描完毕,按任意键退出!\n");
break;
}
if ((res = pcap_next_ex(adhandle, &pkt_header, &pkt_data)) >= 0)
{
if (*(unsigned short*)(pkt_data + 12) == htons(ETH_ARP))
{
struct arp_packet* recv = (struct arp_packet*)pkt_data;
if (*(unsigned short*)(pkt_data + 20) == htons(ARP_REPLY))
{
printf("------------------------------\n");
printf("IP地址:%d.%d.%d.%d MAC地址:", recv->ah.src_ip_add & 255, recv->ah.src_ip_add >> 8 & 255,
recv->ah.src_ip_add >> 16 & 255, recv->ah.src_ip_add >> 24 & 255);
for (int i = 0; i < 6; i++)
{
Mac[i] = *(unsigned char*)(pkt_data + 22 + i);
printf("%02x", Mac[i]);
}
printf("\n");
}
}
}
Sleep(10);
}
return 0;
}