成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

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

2010-08-28 10:48:52來源:西部e網(wǎng)作者:

      前言

  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 <ip address>");
  }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 <ipaddr>

  本程序經(jīng)過測試,在linux和win都可以正常運(yùn)行。
關(guān)鍵詞:JavaARP

贊助商鏈接:

主站蜘蛛池模板: 宁津县| 古丈县| 南江县| 河西区| 肇庆市| 敦煌市| 甘谷县| 乌苏市| 滕州市| 邵阳市| 永兴县| 蓬莱市| 五大连池市| 绥滨县| 大渡口区| 阿勒泰市| 巴林左旗| 马公市| 荆州市| 锡林浩特市| 刚察县| 乌苏市| 厦门市| 亳州市| 青岛市| 始兴县| 京山县| 扬中市| 中超| 朔州市| 基隆市| 济宁市| 浑源县| 柏乡县| 辰溪县| 连州市| 易门县| 商河县| 潜山县| 原阳县| 黄冈市|