JDK-7158636 : InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_7
  • CPU: x86
  • Submitted: 2012-04-03
  • Updated: 2015-02-02
  • Resolved: 2012-07-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8
7u76Fixed 8 b38Fixed
Description
FULL PRODUCT VERSION :
java version "1.7.0_02"
Java(TM) SE Runtime Environment (build 1.7.0_02-b13)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [versie 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
For an UDP application I want to send a packet to the network broadcast address.

java.net.InterfaceAddress has the method getBroadcast() to retrieve this information. However, on a wireless connection this functionality is broken.

getBroadcast() always returns 0.0.0.0 on the wireless interface, although it works correctly for the wired interface.

I can reproduce this problem on multiple laptops.

Trying to work around this by applying the subnetmask to the ip myself is impossible, because getNetworkPrefixLength() returns -1 on a WLAN connection.

If I connect to laptop to the same network using a cable instead of using WiFi, Java returns the correct information about the broadcast address on the wired interface.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Execute source code attached as "Source code for an executable test case" on a laptop connected to WiFi / WLAN.
2. Output shows that the broadcast address is wrong

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The names of the network interfaces and the ip can be different:

{{net3}}
145.89.X.X: 145.89.131.255 | 22

{{eth3}}
145.89.X.X: 145.89.131.255 | 22
ACTUAL -
Instead of the expected result java.net.InterfaceAddress.getBroadcast() returns 0.0.0.0 and getNetworkPrefixLength() returns -1 for the WiFi adapter only. As seen here, the wired interface returns the correct information.

{{net3}}
145.89.X.X: 0.0.0.0 | -1

{{eth3}}
145.89.X.X: 145.89.131.255 | 22

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.net.Inet6Address;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class BugDemonstrator
{

    public static void main(String[] args) throws Exception
    {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while(interfaces.hasMoreElements())
        {
            NetworkInterface networkInterface = interfaces.nextElement();
            if (networkInterface.isLoopback() || (!networkInterface.isUp()))
                continue; // Ignore loopback and interfaces that are down
            
            System.out.println("== "+networkInterface.getName()+" ==");
            for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses())
            {
                if(interfaceAddress.getAddress() instanceof Inet6Address)
                    continue; //IPv6 doesn't have broadcast adresses

                String ipAddress = interfaceAddress.getAddress().getHostAddress();
                String broadcastAddress = interfaceAddress.getBroadcast().getHostAddress();
                short networkPrefixLength = interfaceAddress.getNetworkPrefixLength();

                System.out.println(ipAddress+": "+broadcastAddress+" | "+networkPrefixLength);
            }
            System.out.println();
        }
    }

}
---------- END SOURCE ----------

Comments
Nigthly results are green for the binaries with the fix. SQE OK to take it in PSU15_01
08-09-2014

Fix Summary Template - Fix for Release : JDK7 PSU 15_01 - Risk Analysis : Low. Its been in 8 since fcs. - Testing (done/to-be-done) : Existing network tests to make sure it doesn't break anything. Its dependent on special hardware however so noreg-hard - JavaFX Impact : No
03-09-2014

EVALUATION Changeset: 715f50872ae7 Author: khazra Date: 2012-04-19 18:11 -0700 URL: http://hg.openjdk.java.net/jdk8/tl/jdk/rev/715f50872ae7 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN Summary: Update Windows native code to infer WLAN interface type in Windows Vista and later Reviewed-by: chegar, alanb ! src/windows/native/java/net/NetworkInterface.c ! src/windows/native/java/net/NetworkInterface.h
20-04-2012

EVALUATION In the native windows code, enumAddresses_win() misses case for WLAN interface for Windows Vista and above, when retrieving broadcast addresses.
11-04-2012