From 3b93d353d6aabadff3b26f2a24f9e9b93c6b6fbf Mon Sep 17 00:00:00 2001 From: Kelven Yang Date: Thu, 23 Jun 2011 17:47:20 -0700 Subject: [PATCH] bug 9605: use CIDR from default interface --- .../cloud/server/ConfigurationServerImpl.java | 39 +---------------- utils/src/com/cloud/utils/net/NetUtils.java | 42 ++++++++++++++++++- 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/server/src/com/cloud/server/ConfigurationServerImpl.java b/server/src/com/cloud/server/ConfigurationServerImpl.java index c89bfe7ce45..bbed9d64b77 100644 --- a/server/src/com/cloud/server/ConfigurationServerImpl.java +++ b/server/src/com/cloud/server/ConfigurationServerImpl.java @@ -182,7 +182,7 @@ public class ConfigurationServerImpl implements ConfigurationServer { s_logger.debug("ConfigurationServer could not detect mount.parent."); } - String hostIpAdr = getHost(); + String hostIpAdr = NetUtils.getDefaultHostIp(); if (hostIpAdr != null) { _configDao.update("host", hostIpAdr); s_logger.debug("ConfigurationServer saved \"" + hostIpAdr + "\" as host."); @@ -242,23 +242,6 @@ public class ConfigurationServerImpl implements ConfigurationServer { _configDao.update("init", "true"); } - - - private String getEthDevice() { - String defaultRoute = Script.runSimpleBashScript("/sbin/route | grep default"); - - if (defaultRoute == null) { - return null; - } - - String[] defaultRouteList = defaultRoute.split("\\s+"); - - if (defaultRouteList.length != 8) { - return null; - } - - return defaultRouteList[7]; - } private String getMountParent() { return getEnvironmentProperty("mount.parent"); @@ -283,26 +266,6 @@ public class ConfigurationServerImpl implements ConfigurationServer { } - - @DB - protected String getHost() { - NetworkInterface nic = null; - String pubNic = getEthDevice(); - - if (pubNic == null) { - return null; - } - - try { - nic = NetworkInterface.getByName(pubNic); - } catch (final SocketException e) { - return null; - } - - String[] info = NetUtils.getNetworkParams(nic); - return info[0]; - } - @DB protected void saveUser() { // insert system account diff --git a/utils/src/com/cloud/utils/net/NetUtils.java b/utils/src/com/cloud/utils/net/NetUtils.java index 93e0a6195c0..55a145c2879 100755 --- a/utils/src/com/cloud/utils/net/NetUtils.java +++ b/utils/src/com/cloud/utils/net/NetUtils.java @@ -40,6 +40,7 @@ import org.apache.log4j.xml.DOMConfigurator; import com.cloud.utils.IteratorUtil; import com.cloud.utils.NumbersUtil; import com.cloud.utils.Pair; +import com.cloud.utils.script.Script; public class NetUtils { protected final static Logger s_logger = Logger.getLogger(NetUtils.class); @@ -113,6 +114,8 @@ public class NetUtils { } public static String[] getLocalCidrs() { + String defaultHostIp = getDefaultHostIp(); + List cidrList = new ArrayList(); try { for (NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) { @@ -122,7 +125,8 @@ public class NetUtils { int prefixLength = address.getNetworkPrefixLength(); if (prefixLength < 32 && prefixLength > 0) { String ip = ipFromInetAddress(addr); - cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength))); + if(ip.equalsIgnoreCase(defaultHostIp)) + cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength))); } } } @@ -134,6 +138,40 @@ public class NetUtils { return cidrList.toArray(new String[0]); } + public static String getDefaultHostIp() { + NetworkInterface nic = null; + String pubNic = getDefaultEthDevice(); + + if (pubNic == null) { + return null; + } + + try { + nic = NetworkInterface.getByName(pubNic); + } catch (final SocketException e) { + return null; + } + + String[] info = NetUtils.getNetworkParams(nic); + return info[0]; + } + + public static String getDefaultEthDevice() { + String defaultRoute = Script.runSimpleBashScript("/sbin/route | grep default"); + + if (defaultRoute == null) { + return null; + } + + String[] defaultRouteList = defaultRoute.split("\\s+"); + + if (defaultRouteList.length != 8) { + return null; + } + + return defaultRouteList[7]; + } + public static InetAddress getFirstNonLoopbackLocalInetAddress() { InetAddress[] addrs = getAllLocalInetAddresses(); if (addrs != null) { @@ -851,7 +889,7 @@ public class NetUtils { public static void main(String[] args) { configLog4j(); - + if (args.length == 0) { System.out.println("Must specify at least one parameter"); }