moltak
2010. 3. 18. 22:20
2010. 3. 18. 22:20
- #include "stdafx.h"
- #include <pcap.h>
- #define ETHER_ADDR_LEN 6
- typedefstruct ether_header {
- unsigned char ether_dhost[ETHER_ADDR_LEN];
- unsigned char ether_shost[ETHER_ADDR_LEN];
- unsigned short ether_type;
- } ETHER_HDR;
- #define ETHERTYPE_IP 0x0800 /* IP Protocol */
- #define ETHERTYPE_ARP 0x0806 /* Address Resolution Protocol */
- #define ETHERTYPE_REVARP 0x8035 /* reverse Address Resolution Protocol */
- typedefstruct ip_hdr
- {
- unsigned char ip_header_len:4;
- unsigned char ip_version :4;
- unsigned char ip_tos;
- unsigned short ip_total_length;
- unsigned short ip_id;
- unsigned char ip_frag_offset :5;
- unsigned char ip_more_fragment :1;
- unsigned char ip_dont_fragment :1;
- unsigned char ip_reserved_zero :1;
- unsigned char ip_frag_offset1;
- unsigned char ip_ttl;
- unsigned char ip_protocol;
- unsigned short ip_checksum;
- unsigned int ip_srcaddr;
- unsigned int ip_destaddr;
- } IPV4_HDR;
- int _tmain(int argc, _TCHAR* argv[])
- {
- pcap_if_t *alldevs;
- pcap_if_t *d;
- int inum;
- int i=0;
- pcap_t *fp;
- char errbuf[PCAP_ERRBUF_SIZE];
- unsigned char packet[1500];
-
- if (pcap_findalldevs(&alldevs, errbuf) == -1)
- {
- fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
- exit(1);
- }
-
- 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);
- if(inum < 1 || inum > i)
- {
- printf("\nInterface number out of range.\n");
-
- pcap_freealldevs(alldevs);
- return -1;
- }
-
- for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
-
- if ( (fp= pcap_open_live(d->name,
- 65536,
- 0,
- 1000,
- errbuf
- ) ) == NULL)
- {
- fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", argv[1]);
- return -1;
- }
-
- memset(packet, 0, sizeof(packet));
- ETHER_HDR eth;
- IPV4_HDR ip;
- int length = 0;
-
-
-
-
-
-
- eth.ether_dhost[0] = 0x00;
- eth.ether_dhost[1] = 0x01;
- eth.ether_dhost[2] = 0x02;
- eth.ether_dhost[3] = 0x03;
- eth.ether_dhost[4] = 0x04;
- eth.ether_dhost[5] = 0x05;
- eth.ether_shost[0] = 0x10;
- eth.ether_shost[1] = 0x11;
- eth.ether_shost[2] = 0x12;
- eth.ether_shost[3] = 0x13;
- eth.ether_shost[4] = 0x14;
- eth.ether_shost[5] = 0x15;
- eth.ether_type = htons(ETHERTYPE_IP);
- memcpy(packet, eth, sizeof(eth));
- length += sizeof(eth);
- memset(&ip,0x01,sizeof(ip));
- ip.ip_header_len = sizeof(ip)/4;
-
-
- memcpy(packet+length, &ip, sizeof(ip));
- length += sizeof(ip);
- if ( length < 64 ) {
- for( i = length ; i < 64 ;i++)
- {
- packet[i]=0;
- }
- }
-
-
- if (pcap_sendpacket(fp, packet, length ) != 0)
- {
- fprintf(stderr,"\nError sending the packet: \n", pcap_geterr(fp));
- return -1;
- }
-
- if (pcap_sendpacket(fp, packet, length ) != 0)
- {
- fprintf(stderr,"\nError sending the packet: \n", pcap_geterr(fp));
- return -1;
- }
- return 0;
- }