main.cpp--------------------------------#i nclude " main.h " pcap_t * driver;u_char * buff ; void * thread_send( void * ); int main( int argc, char ** argv) ... {pcap_if_t *all,*current;u_char packet[100];struct pcap_pkthdr *head;char *eth_dst = "00-02-3f-03-3f-26";char *errbuff;char *data;char in;int intin;int i = 0;if(pcap_findalldevs(&all,errbuff) == -1)...{ fprintf(stderr,"error in open interface the reason is: %s",errbuff); exit(-1);}i = 1;for(current = all;current;current = current->next)...{ fprintf(stdout,"%d.%s(%s) ",i,current->name,current->description); i++;}fprintf(stdout,"input the number of the interface:");scanf("%c",&in);intin = atoi(&in);for(current = all,i = 1;current;current = current->next,i++)...{ if(i == intin) ...{ break; }}fprintf(stdout,"you choice driver is %s(%s)",current->name,current->description);char buff_t[strlen(current->name)];strcpy(buff_t,current->name);pcap_freealldevs(all);if((driver = pcap_open_live(buff_t,65535,PCAP_OPENFLAG_PROMISCUOUS,1000,errbuff)) == NULL)...{ fprintf(stdout,"open a driver error ,reason:%s",errbuff); return -1;} /**//* Send down the packet */ if (pcap_sendpacket(driver, packet, 100 ) != 0) ...{ fprintf(stderr," Error sending the packet: ", pcap_geterr(driver)); return 1; } buff = (u_char*)malloc(sizeof(struct ether_header) + sizeof(struct ARP_HEADER)); struct ether_header *eh = (struct ether_header*)buff; char tmp[ETHER_ADDR_LEN]; //dst-host-eth-addr memset(tmp,0,ETHER_ADDR_LEN); eth_addr_parse("ff-ff-ff-ff-ff-ff",tmp); memcpy(&(eh->ether_dhost),tmp,ETHER_ADDR_LEN); //src-host-eth-addr memset(tmp,0,ETHER_ADDR_LEN); eth_addr_parse(eth_dst,tmp); memcpy(&(eh->ether_shost),tmp,ETHER_ADDR_LEN); eh->ether_type=htons(0x0806); struct ARP_HEADER *arp; arp = (struct ARP_HEADER*)(buff + sizeof(struct ether_header)); arp->arp_hdr = htons(1); arp->arp_pro = htons(0x0800); arp->arp_hln = 0x0006; arp->arp_pln = 0x0004; arp->arp_opt = htons(0x0002); memset(tmp,0,ETHER_ADDR_LEN); eth_addr_parse(eth_dst,tmp); memcpy(&(arp->arp_sha),tmp,ETHER_ADDR_LEN); inet_aton("10.100.0.61",(struct in_addr*)&(arp->arp_spa)); memset(tmp,0,ETHER_ADDR_LEN); eth_addr_parse("ff-ff-ff-ff-ff-ff",tmp); memcpy(&(arp->arp_tha),tmp,ETHER_ADDR_LEN); inet_aton("0.0.0.0",(struct in_addr*)&(arp->arp_tpa)); thread_create(thread_send); wait_thread(); return 0;} void * thread_send( void * ) ... {while(true) ...{ if (pcap_sendpacket(driver, buff, sizeof(struct ether_header) + sizeof(struct ARP_HEADER)) != 0) ...{ fprintf(stderr," Error sending the packet: ", pcap_geterr(driver)); return NULL; } sleep(30); }} main.h -------------------------------------------------------------- #i nclude " pcap.h " #i nclude < stdlib.h > #i nclude < stdio.h > #i nclude < string .h > #i nclude " util.h " #i nclude < sys / socket.h > #i nclude < netinet / in .h > #i nclude < arpa / inet.h > #i nclude < unistd.h > #i nclude " thread.h " #define SIOCGARP 0x00008951 using namespace std;#ifndef ETHER_ADDR_LEN #define ETHER_ADDR_LEN 6 #endif #ifndef PCAP_OPENFLAG_PROMISCUOUS #define PCAP_OPENFLAG_PROMISCUOUS 1 #endif struct ARP_HEADER // ARP头部 ... { unsigned short arp_hdr; unsigned short arp_pro; unsigned char arp_hln; unsigned char arp_pln; unsigned short arp_opt; unsigned char arp_sha[6]; unsigned char arp_spa[4]; unsigned char arp_tha[6]; unsigned long arp_tpa[4];} ; struct ether_header ... {u_char ether_dhost[ETHER_ADDR_LEN];u_char ether_shost[ETHER_ADDR_LEN];u_short ether_type;} ;thread.cpp -------------------------------------------------------------------- #i nclude < pthread.h > #i nclude < iostream > #i nclude " thread.h " pthread_t t; void thread_create( void * ( * methd)( void * )) ... {using namespace std;if(pthread_create(&t,NULL,methd,NULL))...{cout << "error create thread" << endl;exit(0);}} void wait_thread() ... {pthread_join(t,NULL); } thread.h---------------------- void thread_create( void * ( * methd)( void * )); void wait_thread();util.cpp ----------------------------------------------------- #i nclude < string > #i nclude < iostream > #i nclude " util.h " #i nclude < math.h > int hex_to_int( const char , int ); extern " C " void eth_addr_parse( const char * s, char * d) ... { using std::string; string str(s); int index = 0; int beg = 0; int li = 0; while(beg <= str.length() && (index = str.find("-",beg)) != str.npos) ...{ string sub = str.substr(beg,index - beg); int sum = 0; int i = 1; for(int j = sub.length(); j > 0;j--) ...{ sum += hex_to_int(sub[j - 1],i++); } d[li++] = sum; beg = index + 1; } if(beg < str.length()) ...{ string sub = str.substr(beg,str.length() - beg); int sum = 0; int i = 1; for(int j = sub.length(); j > 0;j--) ...{ sum += hex_to_int(sub[j - 1],i++); } d[li++] = sum; }} int hex_to_int( const char c, int i) ... { for(int j = 0; j < 6; j++)...{ if(c == (char)(97+j)) ...{ return (10 + c - 97)*(int)pow(16,i-1); }}char buf[1] ;buf[0] = c;return atoi(buf)*(int)pow(16,i-1);} util.h extern " C " void eth_addr_parse( const char * , char * );