Beispiel #1
0
 Object getValueAt(int index) {
   switch (index) {
     case 0:
       switch (arp.hardtype) {
         case ARPPacket.HARDTYPE_ETHER:
           return "Ethernet (" + arp.hardtype + ")";
         case ARPPacket.HARDTYPE_IEEE802:
           return "Token ring (" + arp.hardtype + ")";
         case ARPPacket.HARDTYPE_FRAMERELAY:
           return "Frame relay (" + arp.hardtype + ")";
         default:
           return new Integer(arp.hardtype);
       }
     case 1:
       switch (arp.prototype) {
         case ARPPacket.PROTOTYPE_IP:
           return "IP (" + arp.prototype + ")";
         default:
           return new Integer(arp.prototype);
       }
     case 2:
       return new Integer(arp.hlen);
     case 3:
       return new Integer(arp.plen);
     case 4:
       switch (arp.operation) {
         case ARPPacket.ARP_REQUEST:
           return "ARP Request";
         case ARPPacket.ARP_REPLY:
           return "ARP Reply";
         case ARPPacket.RARP_REQUEST:
           return "Reverse ARP Request";
         case ARPPacket.RARP_REPLY:
           return "Reverse ARP Reply";
         case ARPPacket.INV_REQUEST:
           return "Identify peer Request";
         case ARPPacket.INV_REPLY:
           return "Identify peer Reply";
         default:
           return new Integer(arp.operation);
       }
     case 5:
       return arp.getSenderHardwareAddress();
     case 6:
       return arp.getSenderProtocolAddress();
     case 7:
       return arp.getTargetHardwareAddress();
     case 8:
       return arp.getTargetProtocolAddress();
     default:
       return null;
   }
 }
Beispiel #2
0
  public static byte[] arp(InetAddress ip) throws java.io.IOException {
    // find network interface
    NetworkInterface[] devices = JpcapCaptor.getDeviceList();
    NetworkInterface device = null;

    loop:
    for (NetworkInterface d : devices) {
      for (NetworkInterfaceAddress addr : d.addresses) {
        if (!(addr.address instanceof Inet4Address)) continue;
        byte[] bip = ip.getAddress();
        byte[] subnet = addr.subnet.getAddress();
        byte[] bif = addr.address.getAddress();
        for (int i = 0; i < 4; i++) {
          bip[i] = (byte) (bip[i] & subnet[i]);
          bif[i] = (byte) (bif[i] & subnet[i]);
        }
        if (Arrays.equals(bip, bif)) {
          device = d;
          break loop;
        }
      }
    }

    if (device == null) throw new IllegalArgumentException(ip + " is not a local address");

    // open Jpcap
    JpcapCaptor captor = JpcapCaptor.openDevice(device, 2000, false, 3000);
    captor.setFilter("arp", true);
    JpcapSender sender = captor.getJpcapSenderInstance();

    InetAddress srcip = null;
    for (NetworkInterfaceAddress addr : device.addresses)
      if (addr.address instanceof Inet4Address) {
        srcip = addr.address;
        break;
      }

    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;

    sender.sendPacket(arp);

    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;
      }
    }
  }