zoukankan      html  css  js  c++  java
  • winpcap ARP请求代码

    #include "network.h"
    #include <iostream>
    #include <string>
    #include <vector>
    #include "pcap.h"
    #include <ntddndis.h>
    #include "packet32.h"
    using namespace std;
    struct packet_t{
    	ethhdr eth;
    	arphdr arp;
    	u_char pad[18];
    };
    int
    	main(int argc, char **argv)
    {
    	pcap_t *fp;
    	char errbuf[PCAP_ERRBUF_SIZE];
    	string str;
    	vector<string> v;
    	packet_t packet;
    	int i = 0;
    	pcap_if_t *alldevs;
    	pcap_if_t *d;
    	/* Retrieve the device list from the local machine */
    	if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
    	{
    		fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
    		exit(1);
    	}
    	cout<<sizeof(packet_t)<<endl;
    	/* Print the list */
    	for(d= alldevs; d != NULL; d= d->next)
    	{
    		printf("%d. %s", ++i, d->name);
    		str = d->name;
    		v.push_back(str);
    		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;
    	}
    
    	/* We don't need any more the device list. Free it */
    	pcap_freealldevs(alldevs);
    	
    	printf("%s..\n",mac_to_string(get_dev_mac(v[0].c_str()),errbuf));
    	/* Open the output device */
    	if ( (fp= pcap_open(v[0].c_str(),         
    		100,                // portion of the packet to capture (only the first 100 bytes)
    		PCAP_OPENFLAG_PROMISCUOUS,  // promiscuous mode
    		1000,               // read timeout
    		NULL,               // authentication on the remote machine
    		errbuf              // error buffer
    		) ) == NULL)
    	{
    		fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", argv[1]);
    		return 1;
    	}
    	/* Send down the packet */
    	memset(&packet,0,sizeof packet_t);
    	memset(&packet.eth.h_dest,0xFF,ETH_ALEN);
    	memcpy(&packet.eth.h_source,get_dev_mac(v[0].c_str()),ETH_ALEN);
    	//上层协议为ARP协议,0x0806
    	packet.eth.h_proto = htons(ETH_P_ARP);
    
    	//硬件类型,Ethernet是0x0001
    	packet.arp.ar_hrd = htons(ARPHRD_ETHER);
    	//上层协议类型,IP为0x0800
    	packet.arp.ar_pro = htons(ETH_P_IP);
    	//硬件地址长度:MAC地址长度为0x06
    	packet.arp.ar_hln = 6;
    	//协议地址长度:IP地址长度为0x04
    	packet.arp.ar_pln = 4;
    	//操作:ARP请求为1
    	packet.arp.ar_op = htons(ARPOP_REQUEST);
    	//源端
    	memcpy(packet.arp.ar_sha,get_dev_mac(v[0].c_str()),ETH_ALEN);
    	u_long sip = inet_addr("125.217.40.219");
    	memcpy(packet.arp.ar_sip,&sip,sizeof(u_long));
    	//目标
    	memset(packet.arp.ar_tha,0,ETH_ALEN);
    	u_long tip = inet_addr("125.217.40.254");
    	memcpy(packet.arp.ar_tip,&tip,sizeof(u_long));
    	L:
    	if (pcap_sendpacket(fp, (u_char*)&packet, sizeof packet_t /* size */) != 0)
    	{
    		fprintf(stderr,"\nError sending the packet: %s\n", pcap_geterr(fp));
    		
    		return 1;
    	}
    	goto L;
    	return 0;
    }
    
  • 相关阅读:
    用SQL实现的一个自动排课机制
    如何读懂复杂的C声明
    Mingw32配置
    test
    HttpSession API
    java程序逻辑控制
    方法的定义及使用
    memcached 安装及集成(转)
    cookie和session的的区别以及使用示例?
    构造方法和普通方法的区别?
  • 原文地址:https://www.cnblogs.com/UnGeek/p/2841558.html
Copyright © 2011-2022 走看看