Article Figure 1 Figure 2 Listing 1 Listing 2
Listing 3 Listing 4 Listing 5 sep2006.tar

Listing 4 arp method

public byte[] arp(InetAddress ip, InetAddress srcip, \
  int networkInterfaceID) throws java.io.IOAcception{

    /*  Verify that we obtained a valid networkInterfaceID */

            if (networkInterfaceID < 0 || devices.length <= networkInterfaceID)
                    return null;

    /*  Set device to the network interface specified by the 
        networkInterfaceID */

    NetworkInterface device=devices[networkInterfaceID];

    /*  Lets obtain our MAC address and put it in an array of bytes called 
        myMac */

                byte[] myMac = myMacAddress(networkInterfaceID);

    /*  Initialize our network device and open it for sending/receiving  */

    JpcapCaptor captor=JpcapCaptor.openDevice(device,2000,false,3000);
    captor.setFilter("arp",true);

    /*  initiate our sender instance of the network  device */ 

    JpcapSender sender=captor.getJpcapSenderInstance();

    /*  We will use ff:ff:ff:ff:ff:ff as our broadcast address so everyone 
        on our network will receive the ARP request */

    byte[] broadcast=new \
      byte[]{(byte)255,(byte)255,(byte)255,(byte)255,(byte)255,(byte)255};

    /*  Initiate an ARPPacket called arp and  build the packet */

                ARPPacket arp=new ARPPacket();
    arp.hardtype=ARPPacket.HARDTYPE_ETHER; //set hardware type to Ethernet
    arp.prototype=ARPPacket.PROTOTYPE_IP;  // set the protocol type to IP
    arp.operation=ARPPacket.ARP_REQUEST;   // define the operation to be an 
                                           // ARP request
    arp.hlen=6;                               //  Hardware address length
    arp.plen=4;                               // protocol address length
    arp.sender_hardaddr=myMac;                // Set the MAC address of the 
                                              // sender to our MAC add
    arp.sender_protoaddr=srcip.getAddress();  // Set the IP address of the 
                                              // sender to our IP add
    arp.target_hardaddr=broadcast;            // Set the MAC address of the 
                                              // remote to the broadcast add
    arp.target_protoaddr=ip.getAddress();     // Set the IP address of the 
                                              // remote to the remote IP add

    /*  Initiate the EthernetPacke ether that will transport the ARP request 
        and start to build the packet */

    EthernetPacket ether=new EthernetPacket();
    ether.frametype=EthernetPacket.ETHERTYPE_ARP;  // set the Ethernet Frame 
                                                   // type to a ARP request
    ether.src_mac=myMac;   //  Set the source MAC address to our MAC address
    ether.dst_mac=broadcast;  //  Set the destination MAC address to the 
                              // broadcast ADD
    arp.datalink=ether;       // Set the Data Link Layer of the arp packet 
                              // to the Ethernet packet

    /*  send out the ARP request */

    sender.sendPacket(arp);

    /*  Captor the information coming back to our computer, and see if an 
        ARP response was sent back if not throw an Acception */

    while(true){
        ARPPacket p=(ARPPacket)captor.getPacket();  
                       // captor any packet that fits the filter
        if(p==null)    // if the filter timesout send an acception back
        {
            throw new IllegalArgumentException(ip+" did not responed to \
              ARP request");
        }
        
        /*  If the address of the captured packet equals the remote ip 
            address return the senders MAC address */

        if(Arrays.equals(p.target_protoaddr,srcip.getAddress()))
        {
            return p.sender_hardaddr;
        }
    }
}