Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Jpcap


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


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.