From ca01dc7a09cfebc9389a0b1e86917fac80ad692c Mon Sep 17 00:00:00 2001 From: kimchy Date: Thu, 7 Apr 2011 20:01:52 +0300 Subject: [PATCH] Network Settings: Allow to explicitly set ipv4 and ipv4 when using _networkInterface_ notation, closes #841. --- .../common/network/NetworkService.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/network/NetworkService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/network/NetworkService.java index 8b989c6d3f3..7818a45006c 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/network/NetworkService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/network/NetworkService.java @@ -114,6 +114,7 @@ public class NetworkService extends AbstractComponent { if (host == null) { return null; } + String origHost = host; if ((host.startsWith("#") && host.endsWith("#")) || (host.startsWith("_") && host.endsWith("_"))) { host = host.substring(1, host.length() - 1); @@ -133,17 +134,25 @@ public class NetworkService extends AbstractComponent { return NetworkUtils.getFirstNonLoopbackAddress(NetworkUtils.getIpStackType()); } } else { + NetworkUtils.StackType stackType = NetworkUtils.getIpStackType(); + if (host.toLowerCase().endsWith(":ipv4")) { + stackType = NetworkUtils.StackType.IPv4; + host = host.substring(0, host.length() - 5); + } else if (host.toLowerCase().endsWith(":ipv6")) { + stackType = NetworkUtils.StackType.IPv6; + host = host.substring(0, host.length() - 5); + } Collection allInterfs = NetworkUtils.getAllAvailableInterfaces(); for (NetworkInterface ni : allInterfs) { if (!ni.isUp() || ni.isLoopback()) { continue; } if (host.equals(ni.getName()) || host.equals(ni.getDisplayName())) { - return NetworkUtils.getFirstNonLoopbackAddress(ni, NetworkUtils.getIpStackType()); + return NetworkUtils.getFirstNonLoopbackAddress(ni, stackType); } } } - throw new IOException("Failed to find network interface for [" + host + "]"); + throw new IOException("Failed to find network interface for [" + origHost + "]"); } return InetAddress.getByName(host); }