0

I am trying to fetch all the nodes connected to a router and try to control the bandwidth of the router

   int depth = 10;          //Where depth is the range of the ip address
        int subDepth = 255;    // subdepth is the subnet mask of the network 
        int TIMEOUT = 500;    // timeout is the maximum wait for another host for the device 
        String ip = InetAddress.getLocalHost().toString().split("/")[1];
        String tmp = ip.substring(0,
                ip.lastIndexOf(".", ip.lastIndexOf(".") - 1))
                + ".";
        ArrayList<InetAddress> lanMachines = new ArrayList<>();
        for (int j = 1; j < depth; j++) {
            for (int i = 1; i < subDepth; i++) {
                InetAddress a = InetAddress.getByName(tmp + j + "." + i);
                if (a.isReachable(TIMEOUT))
                    lanMachines.add(a);
            }
        }

After getting all the details i need to control the bandwidth of the nodes..

2 Answers2

1

No machine will be added because of this:

for (int j = 1; j < depth; j++) {

and

int depth=1;

at first case 1<1 is false and for cycle body won't be executed, declare cycle condition as folows:

for (int j = 0; j < depth; j++) {

EDIT

Your IP address is with prefix 127.0.xxx.xxx, you should ask user for IP adress space like 192.168.xxx.xxx:

int depth = 1;          //Where depth is the range of the ip address
int subDepth = 255;    // subdepth is the subnet mask of the network 
int TIMEOUT = 500;    // timeout is the maximum wait for another host for the device 
/*
 * User input will be better
 * There exists a way to get LAN ip, but you used
 * localhost address 127.0.0.1
 */
String ip = fromUser;
String tmp = ip.substring(0,
            ip.lastIndexOf(".", ip.lastIndexOf(".") - 1))
            + ".";
ArrayList<InetAddress> lanMachines = new ArrayList<>();
for (int j = 0; j < depth; j++) {
    for (int i = 1; i < subDepth; i++) {
        InetAddress a = InetAddress.getByName(tmp + j + "." + i);
        System.out.println(tmp + j + "." + i);
        if (a.isReachable(TIMEOUT))
            lanMachines.add(a);
    }
}
for(InetAddress ina: lanMachines){
    System.out.println("Found: "+ina.toString());
}
maskacovnik
  • 3,080
  • 5
  • 20
  • 26
  • lets say int depth=10 ; still no machine is adding – tayyab techventor Jul 13 '15 at 10:47
  • brother i dont have to take input from user you have added one field String ip=fromUser; I have to get all the nodes which are connected to the same router which i am accessing right now .. i think you have not understand my senerio – tayyab techventor Jul 13 '15 at 11:54
  • You have to know your LAN IP, not localhost (127.0.0.1) - this is not IP in same network as router, this is your loopback adress, maybe look at this: http://stackoverflow.com/questions/17252018/getting-my-lan-ip-address-192-168-xxxx-ipv4 @tayyabtechventor – maskacovnik Jul 13 '15 at 11:56
  • this is my lan ip 192.168.0.108 and its going again infinite as the previous state was further no change ... – tayyab techventor Jul 13 '15 at 12:53
  • this shouldnt be infinite, it only takes some time, if you use my code you can see I print test string in inner loop while adding, look into the console if it is printing, Reachable is very long operation, dont forget you have timeout 500ms which causes 2 hosts per second only (almost) @tayyabtechventor – maskacovnik Jul 13 '15 at 12:56
  • ok, we should start at 192.168. **0** .xxx, I have edited my answer, loop start with 0, set depth to 1. Let the program end and then it should print whole array of found hosts – maskacovnik Jul 13 '15 at 13:01
  • What is in console: nothing, 192.168.0.1 - 192.168.0.255? @tayyabtechventor – maskacovnik Jul 13 '15 at 13:12
  • Stack overflow is for solving this question, I wont provide or answer via e-mail, be sure you enter the loop statement, use prints for help which parts of code is executing, to see variable values etc. @tayyabtechventor – maskacovnik Jul 13 '15 at 13:37
  • actually it is not giving any response and i am working on this code from 6 hours – tayyab techventor Jul 13 '15 at 13:39
  • if you use configuration: depth=1, fromUser="192.168.0.0", for cycle header: `for (int j = 0; j < depth; j++) {` you have to get some lines to console, just to let it go to the end, maybe you can try to decrease timeout to 100 to make it faster @tayyabtechventor – maskacovnik Jul 13 '15 at 13:41
1

There's no definitive way to know what machines use the same router as you just from their IP address. You'd have to inspect the network settings of each machine (which will depend on OS configuration). You certainly won't be able to limit their bandwidth from this kind of program. You'd need to modify the router and / or firewall's rules in order to do that.

Machines don't maintain a "connection" to the router constantly. The DHCP server (which may or may not be the router) may maintain a list of machines it's given leases to, but machines with static IP addresses won't be in that list.

It's also possible (though uncommon) that more than one machine on a subnet may be acting as a router, depending on the destination of the packet.

For instance, machines on the network 192.168.0.0/24 may communicate to the network 192.168.8.0/24 through a router that has two addresses (192.168.0.254 and 192.168.8.254), but communicate to the internet via the router at 192.168.0.1. There is no way to detect this behavior without inspecting each machine's network configuration. You can't tell just from the IP address.

Now, if you just want to know which machines are up and living on the same subnet you are on, then you can look at your IP Address and Netmask together to figure out addresses to check. This WILL NOT tell you for certain if they use your router, but there's a decent chance they will.

The addresses you'll want to check, are those between your network address (which is your IP range with all the host bits set to 0), and your broadcast address (which is the IP range you're in, with all the host bits set to 1).

WARNING: Depending on your environment, the following code may get you in a large heap of very deep trouble. Many network administrators will be greatly displeased with you if you do this, because it looks like a precursor to an attack. Use this at your own risk, and make sure to ask your administrator's permission before you do this.

This will get you your network address and netmask (Note: this code should be IPv6 safe. I have not tested it in an IPv6 environment.)

InetAddress localhost = InetAddress.getLocalHost();
NetworkInterface iFace = NetworkInterface.getByInetAddress(localhost);
int prefix = iFace.getInterfaceAddresses().get(0).getNetworkPrefixLength();
InetAddress bcastAddress = iFace.getInterfaceAddresses().get(0).getBroadcast();

int addrSize = localhost.getAddress().length * 8;

byte[] maskBytes = new byte[localhost.getAddress().length];
for (int i = 0; i < prefix; ++i) {
    maskBytes[i / 8] |= 1 << (7 - i % 8);
}

byte[] net = Arrays.copyOf(localhost.getAddress(), localhost.getAddress().length);
for (int i = 0; i < net.length; ++i) {
    net[i] = (byte)(net[i] & maskBytes[i]);
}
InetAddress networkAddress = InetAddress.getByAddress(net);

Having done that, you can then loop between those two addresses. Everything between them is a potential host on the same subnet.

List<InetAddress> addresses = new ArrayList<InetAddress>();
BigInteger bcastNum = new BigInteger(bcastAddress.getAddress());
BigInteger workNum = new BigInteger(net).add(BigInteger.ONE);
while (workNum.compareTo(bcastNum) < 0) {
    InetAddress toCheck = InetAddress.getByAddress(workNum.toByteArray());
    if (toCheck.isReachable(20)) {
        addresses.add(toCheck);
    }
    workNum = workNum.add(BigInteger.ONE);
}

You'll probably want to increase that timeout from 20 msec, depending on your needs.

Ian McLaird
  • 5,507
  • 2
  • 22
  • 31