How would I check to see whether the ip address is in private category ?
if(isPrivateIPAddress(ipAddress)) {
//do something
}
Any suggestions will be appreciated.
UPDATED ANSWER
private static boolean isPrivateIPAddress(String ipAddress) {
InetAddress ia = null;
try {
InetAddress ad = InetAddress.getByName(ipAddress);
byte[] ip = ad.getAddress();
ia = InetAddress.getByAddress(ip);
} catch (UnknownHostException e) {
e.printStackTrace();
return false;
}
return ia.isSiteLocalAddress();
}
I wrote this method and it’s working fine for me. But is there any case in which this method will not work ? I just wanted to make sure it will be working for every case.
This is a quick hack I generated to test my own address.
EDIT: This code has not been tested for the link local addresses, localhost, or address blocks reserved for documentation. The first two cases have methods that return them. The last is not referenced in the documentation of the class.