From 047003315be799e6b12a8c3eeaf7f21da13f767e Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Tue, 2 Jul 2019 09:46:18 +0200 Subject: [PATCH] utils: reverse ip addresses of a nic returned by java to get the first ip address (#3449) Java methods getInterfaceAddresses() returns ip addresses in reverse order as "ip addr show" If there are multiple IPs assigned to a management interface, the last ip will be used as management ip in cloudstack. We need to reverse the ip addresses to get the first ip that makes more sense. Fixes #3311 --- utils/src/main/java/com/cloud/utils/net/NetUtils.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/utils/src/main/java/com/cloud/utils/net/NetUtils.java b/utils/src/main/java/com/cloud/utils/net/NetUtils.java index dce6c257a1f..baddad2908c 100644 --- a/utils/src/main/java/com/cloud/utils/net/NetUtils.java +++ b/utils/src/main/java/com/cloud/utils/net/NetUtils.java @@ -34,6 +34,7 @@ import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Formatter; import java.util.List; +import java.util.Collections; import java.util.Random; import java.util.Set; import java.util.SortedSet; @@ -410,10 +411,11 @@ public class NetUtils { } public static String[] getNetworkParams(final NetworkInterface nic) { - final List addrs = nic.getInterfaceAddresses(); + List addrs = nic.getInterfaceAddresses(); if (addrs == null || addrs.size() == 0) { return null; } + Collections.reverse(addrs); // reverse addresses because it has reverse order as "ip addr show" InterfaceAddress addr = null; for (final InterfaceAddress iaddr : addrs) { final InetAddress inet = iaddr.getAddress();