Reputation: 9
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..
Upvotes: 0
Views: 1313
Reputation: 5585
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.
Upvotes: 1
Reputation: 3084
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());
}
Upvotes: 1