Skip to content

Commit

Permalink
pool: Partially fix interface selection for FTP mover
Browse files Browse the repository at this point in the history
The FTP mover tries to select an interface with the correct
protocol family and falls back to proxy mode if not possible.
Unfortunately the NetworkUtils utility class fails to return
null in case no suitable interface could be found.

On the other hand, the original protocol family less version
of the NetworkUtils method fails to handle null values.

This patch fixes the former to return null if the protocol
family could not be match, and fixes the latter by falling
back to ignoring the protocol family. The latter is likely
wrong, but it restore the previous functionality and resolves
the problem that existing callers of that method do not
handle a null return value.

There are plenty of issues with this piece of code. This
patch only sets out to fix the one.

Target: trunk
Require-notes: yes
Require-book: no
Request: 2.11
Request: 2.10
Request: 2.9
Acked-by: Paul Millar <paul.millar@desy.de>
Acked-by: Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
Patch: https://rb.dcache.org/r/7597/
(cherry picked from commit b3b24bc)
  • Loading branch information
gbehrmann committed Dec 15, 2014
1 parent e8a2c30 commit 9c416b2
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions modules/common/src/main/java/org/dcache/util/NetworkUtils.java
Expand Up @@ -153,12 +153,39 @@ public static URL toURL(URI uri)
public static InetAddress getLocalAddress(InetAddress intendedDestination)
throws SocketException
{
return getLocalAddress(intendedDestination, getProtocolFamily(intendedDestination));
InetAddress localAddress = getLocalAddress(intendedDestination, getProtocolFamily(intendedDestination));
if (localAddress == null) {
if (FAKED_ADDRESS) {
localAddress = LOCAL_INET_ADDRESSES.get(0);
} else {
try (DatagramSocket socket = new DatagramSocket()) {
socket.connect(intendedDestination, RANDOM_PORT);
localAddress = socket.getLocalAddress();

/* The following is a workaround for Java bugs on Mac OS X and
* Windows XP, see eg http://goo.gl/ENXkD
*/
if (localAddress.isAnyLocalAddress()) {
if (intendedDestination.isLoopbackAddress()) {
localAddress = InetAddress.getLoopbackAddress();
} else {
try {
localAddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
localAddress = LOCAL_INET_ADDRESSES.get(0);
}
}
}
}
}
}
return localAddress;
}

/**
* Like getLocalAddress(InetAddress), but return an addresses from the given protocolFamily on
* the network interface that would be used to reach the destination.
* the network interface that would be used to reach the destination. Returns null if the
* interface doesn't support the protocol family.
*/
public static InetAddress getLocalAddress(InetAddress intendedDestination, ProtocolFamily protocolFamily)
throws SocketException
Expand Down Expand Up @@ -198,10 +225,10 @@ public static InetAddress getLocalAddress(InetAddress intendedDestination, Proto
(!address.isLoopbackAddress() || intendedDestination.isLoopbackAddress()) &&
(!address.isSiteLocalAddress() || intendedDestination.isSiteLocalAddress()) &&
(!address.isLinkLocalAddress() || intendedDestination.isLinkLocalAddress())) {
localAddress = address;
break;
return address;
}
}
return null;
}
return localAddress;
}
Expand Down

0 comments on commit 9c416b2

Please sign in to comment.