java.net.NetworkInterfaces and Linux with multiple addresses on single interface
Doychin Bondzhev
doychin at dsoft-bg.com
Sat Dec 27 15:41:48 UTC 2014
Hi,
I want to report for a problem that I see when I have more then one IP
addresses assigned to same interface.
Here is an output from ip addr command on my linux box:
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
And this is the output of a test java program that uses
NetworkInterfaces 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.
So using virutal interfaces is the only workaround of this problem for
the moment.
I propose another method of reading ip address information for
interfaces that will use getifaddrs/freeifaddrs.
getifaddrs knows how to handle this case properly. It returns all the
necessary information in an array of "struct ifaddrs"
Any toughs on this proposition?
--
Doychin Bondzhev
dSoft-Bulgaria Ltd.
PowerPro - billing & provisioning solution for Service providers
PowerStor - Warehouse & POS
http://www.dsoft-bg.com/
Mobile: +359888243116
More information about the core-libs-dev
mailing list