Github user anmolnar commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/448#discussion_r165844805
  
    --- Diff: src/java/main/org/apache/zookeeper/client/StaticHostProvider.java 
---
    @@ -107,16 +112,43 @@ public 
StaticHostProvider(Collection<InetSocketAddress> serverAddresses,
             lastIndex = -1;              
         }
     
    +    private Set<Class<? extends InetAddress>> supportedAddressTypes() {
    +        Set<Class<? extends InetAddress>> types = new HashSet<>();
    +        try {
    +            Enumeration<NetworkInterface> inets = 
NetworkInterface.getNetworkInterfaces();
    +            while (inets.hasMoreElements()) {
    +                NetworkInterface ne = inets.nextElement();
    +
    +                Enumeration<InetAddress> addrs = ne.getInetAddresses();
    +                while (addrs.hasMoreElements()) {
    +                    InetAddress addr = addrs.nextElement();
    +                    if (addr.isLinkLocalAddress() || 
addr.isLoopbackAddress()) {
    +                        continue;
    +                    }
    +                    types.add(addr.getClass());
    +                }
    +            }
    +        } catch (SocketException e) {
    +            LOG.error("Failed to resolve supported address types: ", e);
    +        }
    +        return types;
    +    }
    +
         private List<InetSocketAddress> 
resolveAndShuffle(Collection<InetSocketAddress> serverAddresses) {
    -        List<InetSocketAddress> tmpList = new 
ArrayList<InetSocketAddress>(serverAddresses.size());       
    +        List<InetSocketAddress> tmpList = new 
ArrayList<InetSocketAddress>(serverAddresses.size());
    +        Set<Class<? extends InetAddress>> supprtedInetTypes = 
supportedAddressTypes();
    +
             for (InetSocketAddress address : serverAddresses) {
                 try {
                     InetAddress ia = address.getAddress();
                     String addr = (ia != null) ? ia.getHostAddress() : 
address.getHostString();
                     InetAddress resolvedAddresses[] = 
InetAddress.getAllByName(addr);
                     for (InetAddress resolvedAddress : resolvedAddresses) {
                         InetAddress taddr = 
InetAddress.getByAddress(address.getHostString(), resolvedAddress.getAddress());
    -                    tmpList.add(new InetSocketAddress(taddr, 
address.getPort()));
    +                    // try to use IP address only if it's supported by our 
network stack
    +                    if (!supprtedInetTypes.isEmpty() && 
supprtedInetTypes.contains(taddr.getClass())) {
    --- End diff --
    
    It'd also work to skip the validation if supportedInetTypes is empty.


---

Reply via email to