Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8158170

java.net.NetworkInterface reports wrong broadcast adresses on Linux with multiple addresses on single interface

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • None
    • core-libs
    • None

      This is a sample ip addr command output on linux:

      1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
           link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
           inet 127.0.0.1/8 scope host lo
           inet6 ::1/128 scope host
              valid_lft forever preferred_lft forever
      2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast
      state UP qlen 1000
           link/ether 08:00:27:fc:25:9f brd ff:ff:ff:ff:ff:ff
           inet 192.168.1.4/24 brd 192.168.1.255 scope global eth0
           inet6 fe80::a00:27ff:fefc:259f/64 scope link
              valid_lft forever preferred_lft forever
      3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast
      state UP qlen 1000
           link/ether 08:00:27:b1:42:d0 brd ff:ff:ff:ff:ff:ff
           inet 192.168.240.1/24 brd 192.168.240.255 scope global eth1
           inet 192.168.241.1/24 brd 192.168.241.255 scope global eth1
           inet 192.168.239.1/24 brd 192.168.239.255 scope global eth1
           inet 10.0.0.1/22 brd 10.0.3.255 scope global eth1
           inet6 fe80::a00:27ff:feb1:42d0/64 scope link
              valid_lft forever preferred_lft forever

      This is the output of a test java program that uses
      the java.net.NetworkInterface class to display all IP addresses and their
      corresponding settings:

      eth1:10.0.0.1/24 broadcast:192.168.240.255
      eth1:192.168.239.1/24 broadcast:192.168.240.255
      eth1:192.168.241.1/24 broadcast:192.168.240.255
      eth1:192.168.240.1/24 broadcast:192.168.240.255
      eth0:192.168.1.4/24 broadcast:192.168.1.255
      lo:127.0.0.1/8

      As you can see there are 2 problems visible here:

      1. On all eth1 lines same broadcast is displayed. The value is the
      broadcast of the first IP address set on that interface

      2. All addresses are with prefix length of 24 which is not correct. We
      have 10.0.0.1/22 in the "ip addr" output.

      So the problem is in NetworkInterfaces native code for Unix.

      For IPv4 addresses that code uses 2 functions (getBordacast and
      getSubnet and these 2 functions use ioctl with SIOCGIFNETMASK and
      SIOCGIFBRDADDR. Both of these requests receive as parameter interface
      name. They are not prepared to work in environments where you can
      specify more then one IP on the same interface without using virtual
      interfaces.

      The proposal is to read ip address information for interfaces with getifaddrs. getifaddrs knows how to handle this case properly. It returns all the necessary information in an array of "struct ifaddrs"

            clanger Christoph Langer
            clanger Christoph Langer
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: