This discussion is archived
6 Replies Latest reply: Mar 22, 2012 8:06 PM by EJP RSS

IP of subnetwork

923102 Newbie
Currently Being Moderated
Hi all, I have a problem :

I was writing a little program that display all ip (all host) connected to my router and so in my same subnetwork.

Now I'll show my idea:

private boolean pingHost(InetAddress ip) throws IOException{

          if(ip.isReachable(1000)){
               System.out.println(ip.toString() +"IS REACHABLE");
               return true;
          }
          else{
               return false;
          }

I have made this to generate all ip in my range:


private ArrayList<InetAddress> generateAllSubnetIP(InetAddress localHostIP){
          ArrayList<InetAddress> allsubnetIP = new ArrayList<InetAddress>();
          byte[] localIPbyte = localHostIP.getAddress();
          for (int i = 0; i < 256; i++) {

                    byte[] tempIP={localIPbyte[0],localIPbyte[1],localIPbyte[2],(byte)i};
                    try {
                         allsubnetIP.add(InetAddress.getByAddress(tempIP));
                    } catch (UnknownHostException e) {
                         System.out.println("UnknownHost generating all ip address");
                         e.printStackTrace();
                    }
               }
          return allsubnetIP;
     }


and then I call pingHost for all the ip stored in the ArrayList.

but this doesn't work...


Any idea ?

I hope that you can understand my question...if not ask me for more details...

Thank you...
  • 1. Re: IP of subnetwork
    EJP Guru
    Currently Being Moderated
    First you would have to tell us exactly what you mean by "doesn't work".
  • 2. Re: IP of subnetwork
    923102 Newbie
    Currently Being Moderated
    Hi, thankyou for the reply...

    So the problem is that the ping doesn't work.

    for example the address 192.168.1.1 is reacable (if I go on the terminal and I ping it it respond) but from my programm the "ping" doesn't work and the method isReacable() return false for all ip.

    I hope that now the problem is more clear...

    by by
  • 3. Re: IP of subnetwork
    923102 Newbie
    Currently Being Moderated
    Hi all,

    Since no one can help me with the previous question and previous idea (using ping to know if an ip has an host and so if the ip is assigned to a pc)...

    I have found another solution.

    I try to explain the idea.

    So: We use ARP request on the ethernet broadcast address (ff:ff:ff:ff and ip 255.255.255.255).

    To do that we make an ARP packet containing: [ our IP | our MAC address ; IP we want to know if is up | FF:FF:FF:FF ] and then we put it into an
    ethernet frame with : [our MAC ; FF:FF:FF:FF] and we send it.

    If we do this for all subrange ip ( that is for example : 123.123.1.0 ....123.123.1.255) we will receive ARP response of the "ip" (computer) that are connected to the network.
    So we can make a list with all pc (ip and MAC) that are connected to our subnet.

    I hope this will help.

    I wait for some feedback...

    If you think that this is a good / bad idea please tell me...

    I'm new to network programming and I want to learn and know if I'm on the right way...
  • 4. Re: IP of subnetwork
    EJP Guru
    Currently Being Moderated
    the method isReachable() return false for all ip.
    That doesn't happen when I run it. I get the same reachable hosts that I can see in my ARP table (arp -a).

    Re your MAC address/ARP idea, you can't send ARP packets directly from Java.

    Why do you need to do this at all? It's an unusual requirement.
  • 5. Re: IP of subnetwork
    923102 Newbie
    Currently Being Moderated
    Hi,
    I don't know way is reachable for me return false for every ip... I mean if there is such method in Java it's clear that must work... no idea... :-(

    For the ARP i don't see what you mean by : you can't send ARP packets directly from Java... I actually use Jpcap library to send ARP packets.

    And finally I can say why I need this...

    1) I want to practice java programming (since I'm not an expert)
    2) I'm interested on network and how it work

    3) the program I want to create is a simple network scanner; I mean I want to find all host that are connected to my router...This can be useful to detect intrusion...If some host try to connect to my subnet then the java program (that is listening) detect a new host and can send an alert !

    Thank you for the reply...


    can you post an example of the usage of isReacable().

    by by
  • 6. Re: IP of subnetwork
    EJP Guru
    Currently Being Moderated
    For the ARP i don't see what you mean by : you can't send ARP packets directly from Java... I actually use Jpcap library to send ARP packets.
    That's what I mean. You can't do it from Java. You have to add something.
    This can be useful to detect intrusion...If some host try to connect to my subnet then the java program (that is listening) detect a new host and can send an alert !
    A new host which magically has the correct subnet:IP address? and that is magically plugged in to your router? That would be a fairly major physical intrusion. You don't want to write software for that sort of thing, you want locks on your doors. Security cameras. Access cards, etc.
    can you post an example of the usage of isReachable().
    Why? Your code works for me.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points