tags:

views:

3585

answers:

5

how to get Subnet mask address of local system using java programming.?

+2  A: 

java.net.InterfaceAddress in SE6 has a getNetworkPrefixLength method that returns, as the name suggests, the network prefix length. You can calculate the subnet mask from this if you would rather have it in that format. java.net.InterfaceAddress supports both IPv4 and IPv6.

getSubnetMask() in several network application APIs returns subnet mask in java.net.InetAddress form for specified IP address (a local system may have many local IP addresses)

mas
+5  A: 

the netmask of the first address of the localhost interface:

InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength();

a more complete approach:

InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);

for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
    System.out.println(address.getNetworkPrefixLength());
}

/24 means 255.255.255.

dfa
A: 

FWIW, in the past I'd tried using InterfaceAddress.getNetworkPrefixLength() and InterfaceAddress.getBroadcast(), but they don't return accurate info (this is on Windows, with Sun JDK 1.6.0 update 10). The network prefix length is 128 (not 24, which it is on my network), and the broadcast address returned is 255.255.255.255 (not 192.168.1.255, which it is on my network).

James

Update: I just found the solution posted here:

     http://forums.sun.com/thread.jspa?threadID=5277744

You need to prevent Java from using IPv6, so that it isn't getting to IPv4 via IPv6. Adding -Djava.net.preferIPv4Stack=true to the command line fixes the results from InterfaceAddress.getNetworkPrefixLength() and InterfaceAddress.getBroadcast() for me.

A: 

Is there a way to do this in earlier versions of Java, like 1.5, or will I need to run ifconfig (or ipconfig on Windows) and parse the output?

Mikael
A: 

No, I checked the solution in the link and I doubt it will work correctly for all prefix...

I devised an IPv4 only solution that is simple enough... I needed that to generate netmask for subnetworks here in order to delegate those subnets correctly... I know I could have generated a table of the 32 possible masks, but I prefered to get it computed each time...

So here is my solution...

/*
 * Get network mask for the IP address and network prefix specified...
 * The network mask will be returned has an IP, thus you can
 * print it out with .getHostAddress()...
 */
public static InetAddress getIPv4LocalNetMask(InetAddress ip, int netPrefix) {

    try {
        // Since this is for IPv4, it's 32 bits, so set the sign value of
        // the int to "negative"...
        int shiftby = (1<<31);
        // For the number of bits of the prefix -1 (we already set the sign bit)
        for (int i=netPrefix-1; i>0; i--) {
            // Shift the sign right... Java makes the sign bit sticky on a shift...
            // So no need to "set it back up"...
            shiftby = (shiftby >> 1);
        }
        // Transform the resulting value in xxx.xxx.xxx.xxx format, like if
        /// it was a standard address...
        String maskString = Integer.toString((shiftby >> 24) & 255) + "." + Integer.toString((shiftby >> 16) & 255) + "." + Integer.toString((shiftby >> 8) & 255) + "." + Integer.toString(shiftby & 255);
        // Return the address thus created...
        return InetAddress.getByName(maskString);
    }
        catch(Exception e){e.printStackTrace();
    }
    // Something went wrong here...
    return null;
}

You just call it with the IP and the prefix you want to use, it will generate the netmask for you...

Have fun guys...

FofBorg