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.