Privacy do not check reachability#1047
Conversation
With IPv6 the standard check of Javva can send tcp/7 requests instead of standard PINGs, so this reachability check makes nodes too visible from the network level.
ea10cf5 to
f530e11
Compare
|
This class has become a bit of a mess of tangled conditions, and through all the negations, ones and minus-ones it's somewhat difficult to review whether the logic here is sound. If we define a clever helper method private final Comparator<InetAddress> innerComparator = prefer(Objects::nonNull)
.thenComparing(prefer(not(InetAddress::isAnyLocalAddress)))
.thenComparing(prefer(not(InetAddress::isLoopbackAddress)))
.thenComparing(prefer(not(InetAddress::isLinkLocalAddress)))
.thenComparing(prefer(is(InetAddress::isSiteLocalAddress).and(this::isReachable)))
// NOTE: the last 3 lines below can also be replaced by simply
// .thenComparing(InetAddressComparator.COMPARATOR);
.thenComparing(prefer(is(Inet6Address.class::isInstance)))
.thenComparingInt(Object::hashCode)
.thenComparing(InetAddress::getAddress, Fields::compareBytes);With the above defined, the actual @Override
public int compare(InetAddress arg0, InetAddress arg1) {
if (Objects.equals(arg0, arg1)) {
return 0;
}
return innerComparator.compare(arg0, arg1);
} |
That looks much nicer, yes. I’ll try to get there. Thank you! |
|
We only have |
|
@bertm could you have another look whether this is OK to be merged? |
|
Oh, snap, here’s a PR where I haven’t complained about missing tests yet! 🤣 |
bertm
left a comment
There was a problem hiding this comment.
This looks way more comprehensible already. Here's a few additional but minor remarks.
|
|
||
| // inverted prefer, because not(...) is only documented since Java 11 | ||
| private Comparator<InetAddress> preferNot(Predicate<InetAddress> pred) { | ||
| return (arg0, arg1) -> -1 * predicateToCompare(pred, arg0, arg1); |
There was a problem hiding this comment.
This can be simplified to return prefer(pred).reversed();.
| return Fields.compareBytes(bytes0, bytes1); | ||
| // Hostnames in InetAddress are merely cached, equals() only operates on the byte[]. | ||
| private Comparator<InetAddress> prefer(Predicate<InetAddress> pred) { | ||
| return (arg0, arg1) -> predicateToCompare(pred, arg0, arg1); |
There was a problem hiding this comment.
This can be simplified to return (arg0, arg1) -> Boolean.compare(!pred.test(arg0), !pred.test(arg1));
Also avoids having to test the predicates twice.
| if (pred.test(arg0) && pred.test(arg1)) { | ||
| return 0; | ||
| } |
There was a problem hiding this comment.
The indentation is off, but this code can probably be removed as per the other comments.
5ba4224 to
e22696c
Compare
e22696c to
ef74bc0
Compare
No description provided.