Java如何處理ARP報文的收發(fā)

字號:

前言
    java是跨平臺語言,一般來說對網(wǎng)絡(luò)的操作都在IP層以上,也就是只能對tcp/udp進(jìn)行操作,當(dāng)然也可以設(shè)置部分tcp/udp的option,如果想再往IP層或者數(shù)據(jù)link層操作就無能為力了,必須依靠jni使用本地OS的socket部分接口。很幸運(yùn),我在知道有winpcap的時候同時也知道有人在開發(fā)jpcap,此包可以方便的操作網(wǎng)絡(luò)底層應(yīng)用協(xié)議,以下詳細(xì)描述。
    實施步驟
    下載需要的包:http://netresearch.ics.uci.edu/kfujii/jpcap/doc/index.html上可以下到最新的jpcap,你只需要把lib中的dll文件拷貝到j(luò)re的bin目錄,同時lib中的jar文件拷貝到j(luò)re中的lib/ext目錄下就安裝完整,當(dāng)然你可以使用exe安裝包進(jìn)行安裝,這樣會更加的簡單。
    編碼
    你可以使用任何你喜歡的ide工具,但是必須把jpcap.jar加到classpath中,否則無法編譯通過,以下為代碼詳細(xì)。
    import java.net.Inet4Address;
    import java.net.InetAddress;
    import java.util.Arrays;
    import jpcap.*;
    import jpcap.packet.*;
    public class ARP {
    public static byte[] arp(InetAddress ip) throws java.io.IOException{
    //發(fā)現(xiàn)本機(jī)器的網(wǎng)絡(luò)接口
    int i;
    NetworkInterface[] devices=JpcapCaptor.getDeviceList();
    NetworkInterface device=null;
    for (i = 0; i < devices.length; i++)
    {
    System.out.println(devices[i].description);
    }
    device = devices[2];//我的機(jī)器是第三個進(jìn)行網(wǎng)絡(luò)通信
    if(device==null)
    throw new IllegalArgumentException(ip+" is not a local address");
    //開啟網(wǎng)絡(luò)接口
    JpcapCaptor captor=JpcapCaptor.openDevice(device,2000,false,3000);
    captor.setFilter("arp",true);
    JpcapSender sender=captor.getJpcapSenderInstance();
    InetAddress srcip=null;
    //獲得接口地址,這里考慮到一網(wǎng)絡(luò)接口可能有多地址
    for(i = 0; i < device.addresses.length ; i++)
    if(device.addresses[i].address instanceof Inet4Address){
     srcip=device.addresses[i].address;
     break;
    }
    //填寫全1廣播mac目標(biāo)地址
    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=device.mac_address;
    arp.sender_protoaddr=srcip.getAddress();
    arp.target_hardaddr=broadcast;
    arp.target_protoaddr=ip.getAddress();
    EthernetPacket ether=new EthernetPacket();
    ether.frametype=EthernetPacket.ETHERTYPE_ARP;
    ether.src_mac=device.mac_address;
    ether.dst_mac=broadcast;
    arp.datalink=ether;
    //發(fā)送
    sender.sendPacket(arp);
    //處理回復(fù)
    while(true){
    ARPPacket p=(ARPPacket)captor.getPacket();
    if(p==null){
     throw new IllegalArgumentException(ip+" is not a local address");
    }
    if(Arrays.equals(p.target_protoaddr,srcip.getAddress())){
     return p.sender_hardaddr;
    }
    }
    }
    public static void main(String[] args) throws Exception{
    int i;
    if(args.length<1){
    System.out.println("Usage: java ARP ");
    }else{
    byte[] mac=ARP.arp(InetAddress.getByName(args[0]));
    for (i = 0;i < mac.length; i++)
     System.out.print(Integer.toHexString(mac[i]&0xff) + ":");
    System.out.println();
    System.exit(0);
    }
    }
    }
    運(yùn)行:java ARP
    本程序經(jīng)過測試,在linux和win都可以正常運(yùn)行。