ArchiveOrangemail archive

Technical discussion about the development of the networking part of the the core libraries


net-dev.openjdk.java.net
(List home) (Recent threads) (60 other Java OpenJDK lists)

Subscription Options

  • RSS or Atom: Read-only subscription using a browser or aggregator. This is the recommended way if you don't need to send messages to the list. You can learn more about feed syndication and clients here.
  • Conventional: All messages are delivered to your mail address, and you can reply. To subscribe, send an email to the list's subscribe address with "subscribe" in the subject line, or visit the list's homepage here.
  • Low traffic list: less than 3 messages per day
  • This list contains about 8,088 messages, beginning May 2007
  • 4 messages added yesterday
Report the Spam
This button sends a spam report to the moderator. Please use it sparingly. For other removal requests, read this.
Are you sure? yes no

broadcast and prefix length with IPv6

Ad
Mark Thornton 1260807940Mon, 14 Dec 2009 16:25:40 +0000 (UTC)
On Vista, if you try to find the broadcast address and net prefix length
associated with an IPv4 address (InterfaceAddress.getBroadcast(),
InterfaceAddress.getNetworkPrefixLength()), you get nonsense unless IPv6
is disabled. Bug 6707289 describes the prefix length case.

Workaround: -Djava.net.preferIPv4Stack=true

Without that option, the result on my machine is
  /192.168.0.7, broadcast=/255.255.255.255, prefixLength=128

With -Djava.net.preferIPv4Stack=true
  /192.168.0.7, broadcast=/192.168.0.255, prefixLength=24

Regards,
Mark Thornton


public class TestNetworkInterfaces
{
    /**
     * Must use -Djava.net.preferIPv4Stack=true to get expected results
for broadcast address and
     * prefix length
     * @throws SocketException
     */
    @Test public void enumerateInterfaces() throws SocketException
    {
        for (Enumeration<NetworkInterface> interfaces =
NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();)
        {
            NetworkInterface ni = interfaces.nextElement();
            if (ni.isUp() && !ni.isVirtual())
                reportInterface(ni, "");
        }
    }

    private void reportInterface(NetworkInterface ni, String indent)
throws SocketException
    {
        if (ni.getInterfaceAddresses().isEmpty() &&
!ni.getSubInterfaces().hasMoreElements())
            return;    // has no addresses or child interfaces
        System.out.print(indent);
        System.out.print("\"");
        System.out.print(ni.getDisplayName());
        System.out.print("\"");
        if (ni.isLoopback())
        {
            System.out.print(" [loopback]");
        }
        if (ni.isPointToPoint())
        {
            System.out.print(" [ptp]");
        }
        byte[] mac = ni.getHardwareAddress();
        if (mac != null && mac.length > 0)
        {
            System.out.print(", hardware=");
            for (byte b: mac)
            {
                System.out.print(':');
                System.out.print(Integer.toHexString(b&255));
            }
        }
        System.out.println();
        indent = indent+"  ";
        for (InterfaceAddress addr: ni.getInterfaceAddresses())
        {
            System.out.print(indent);
            System.out.print(addr.getAddress());
            if (addr.getAddress().isLoopbackAddress())
                System.out.print(" [loopback]");
            System.out.print(", broadcast=");
            System.out.print(addr.getBroadcast());
            System.out.print(", prefixLength=");
            System.out.print(addr.getNetworkPrefixLength());
            //System.out.print(", searchedBroadcast=");

//System.out.print(BroadcastAddress.instance().getBroadcastAddress(addr.getAddress()));
            try
            {
                String name = addr.getAddress().getCanonicalHostName();
                if (name != null)
                {
                    System.out.print(", host=");
                    System.out.print(name);
                }
            }
            catch (Exception e)
            {

            }
            System.out.println();
        }
        for (Enumeration<NetworkInterface>
interfaces=ni.getSubInterfaces(); interfaces.hasMoreElements();)
        {
            NetworkInterface child = interfaces.nextElement();
            if (child.isUp())
            {
                reportInterface(child, indent);
            }
        }
    }
}
Christopher Hegarty - Sun Microsystems Ireland 1260809670Mon, 14 Dec 2009 16:54:30 +0000 (UTC)
Hi Mark,

I've actually just reproduced this myself on one of our Vista boxes. 
I've accepted the bug, 6707289, and will try to determine the root cause.

-Chris.
On 14/12/2009 16:27, Mark Thornton wrote: > On Vista, if you try to find the broadcast address and net prefix length > associated with an IPv4 address (InterfaceAddress.getBroadcast(), > InterfaceAddress.getNetworkPrefixLength()), you get nonsense unless IPv6 > is disabled. Bug 6707289 describes the prefix length case. > > Workaround: -Djava.net.preferIPv4Stack=true > > Without that option, the result on my machine is > /192.168.0.7, broadcast=/255.255.255.255, prefixLength=128 > > With -Djava.net.preferIPv4Stack=true > /192.168.0.7, broadcast=/192.168.0.255, prefixLength=24 > > Regards, > Mark Thornton > > > public class TestNetworkInterfaces > { > /** > * Must use -Djava.net.preferIPv4Stack=true to get expected results > for broadcast address and > * prefix length > * @throws SocketException > */ > @Test public void enumerateInterfaces() throws SocketException > { > for (Enumeration<NetworkInterface> interfaces = > NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) > { > NetworkInterface ni = interfaces.nextElement(); > if (ni.isUp() && !ni.isVirtual()) > reportInterface(ni, ""); > } > } > > private void reportInterface(NetworkInterface ni, String indent) > throws SocketException > { > if (ni.getInterfaceAddresses().isEmpty() && > !ni.getSubInterfaces().hasMoreElements()) > return; // has no addresses or child interfaces > System.out.print(indent); > System.out.print("\""); > System.out.print(ni.getDisplayName()); > System.out.print("\""); > if (ni.isLoopback()) > { > System.out.print(" [loopback]"); > } > if (ni.isPointToPoint()) > { > System.out.print(" [ptp]"); > } > byte[] mac = ni.getHardwareAddress(); > if (mac != null && mac.length > 0) > { > System.out.print(", hardware="); > for (byte b: mac) > { > System.out.print(':'); > System.out.print(Integer.toHexString(b&255)); > } > } > System.out.println(); > indent = indent+" "; > for (InterfaceAddress addr: ni.getInterfaceAddresses()) > { > System.out.print(indent); > System.out.print(addr.getAddress()); > if (addr.getAddress().isLoopbackAddress()) > System.out.print(" [loopback]"); > System.out.print(", broadcast="); > System.out.print(addr.getBroadcast()); > System.out.print(", prefixLength="); > System.out.print(addr.getNetworkPrefixLength()); > //System.out.print(", searchedBroadcast="); > > //System.out.print(BroadcastAddress.instance().getBroadcastAddress(addr.getAddress())); > > try > { > String name = addr.getAddress().getCanonicalHostName(); > if (name != null) > { > System.out.print(", host="); > System.out.print(name); > } > } > catch (Exception e) > { > > } > System.out.println(); > } > for (Enumeration<NetworkInterface> > interfaces=ni.getSubInterfaces(); interfaces.hasMoreElements();) > { > NetworkInterface child = interfaces.nextElement(); > if (child.isUp()) > { > reportInterface(child, indent); > } > } > } > } >
Christopher Hegarty - Sun Microsystems Ireland 1264437341Mon, 25 Jan 2010 16:35:41 +0000 (UTC)
Hi Mark,

I finally got around to this one. It will be fixed in the next JDK7 
promotion, b83.

For reference,
  Changeset:
   http://hg.openjdk.java.net/jdk7/tl/jdk/rev/e6...
  Review:
   http://mail.openjdk.java.net/pipermail/net-de...

-Chris.
On 14/12/2009 16:27, Mark Thornton wrote: > On Vista, if you try to find the broadcast address and net prefix length > associated with an IPv4 address (InterfaceAddress.getBroadcast(), > InterfaceAddress.getNetworkPrefixLength()), you get nonsense unless IPv6 > is disabled. Bug 6707289 describes the prefix length case. > > Workaround: -Djava.net.preferIPv4Stack=true > > Without that option, the result on my machine is > /192.168.0.7, broadcast=/255.255.255.255, prefixLength=128 > > With -Djava.net.preferIPv4Stack=true > /192.168.0.7, broadcast=/192.168.0.255, prefixLength=24 > > Regards, > Mark Thornton > > > public class TestNetworkInterfaces > { > /** > * Must use -Djava.net.preferIPv4Stack=true to get expected results > for broadcast address and > * prefix length > * @throws SocketException > */ > @Test public void enumerateInterfaces() throws SocketException > { > for (Enumeration<NetworkInterface> interfaces = > NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) > { > NetworkInterface ni = interfaces.nextElement(); > if (ni.isUp() && !ni.isVirtual()) > reportInterface(ni, ""); > } > } > > private void reportInterface(NetworkInterface ni, String indent) > throws SocketException > { > if (ni.getInterfaceAddresses().isEmpty() && > !ni.getSubInterfaces().hasMoreElements()) > return; // has no addresses or child interfaces > System.out.print(indent); > System.out.print("\""); > System.out.print(ni.getDisplayName()); > System.out.print("\""); > if (ni.isLoopback()) > { > System.out.print(" [loopback]"); > } > if (ni.isPointToPoint()) > { > System.out.print(" [ptp]"); > } > byte[] mac = ni.getHardwareAddress(); > if (mac != null && mac.length > 0) > { > System.out.print(", hardware="); > for (byte b: mac) > { > System.out.print(':'); > System.out.print(Integer.toHexString(b&255)); > } > } > System.out.println(); > indent = indent+" "; > for (InterfaceAddress addr: ni.getInterfaceAddresses()) > { > System.out.print(indent); > System.out.print(addr.getAddress()); > if (addr.getAddress().isLoopbackAddress()) > System.out.print(" [loopback]"); > System.out.print(", broadcast="); > System.out.print(addr.getBroadcast()); > System.out.print(", prefixLength="); > System.out.print(addr.getNetworkPrefixLength()); > //System.out.print(", searchedBroadcast="); > > //System.out.print(BroadcastAddress.instance().getBroadcastAddress(addr.getAddress())); > > try > { > String name = addr.getAddress().getCanonicalHostName(); > if (name != null) > { > System.out.print(", host="); > System.out.print(name); > } > } > catch (Exception e) > { > > } > System.out.println(); > } > for (Enumeration<NetworkInterface> > interfaces=ni.getSubInterfaces(); interfaces.hasMoreElements();) > { > NetworkInterface child = interfaces.nextElement(); > if (child.isUp()) > { > reportInterface(child, indent); > } > } > } > } >
Home | About | Privacy