Skip to content

Commit

Permalink
[Java] Handle null connect/current control addresses. Flip matches ta…
Browse files Browse the repository at this point in the history
…g logic so that the incoming channel is matched against the existing channel/endpoint to simplify the code. Re-encapsulate the matching logic and pass in the overriding addresses from the endpoints.
  • Loading branch information
mikeb01 committed Jan 16, 2025
1 parent a577cd2 commit 7c1006d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -619,18 +619,7 @@ public long tag()
*/
public boolean matchesTag(final UdpChannel udpChannel)
{
final boolean udpChannelMatches = super.udpChannel.matchesTag(udpChannel);

final InetSocketAddress thisRemoteData = super.udpChannel.remoteData();
final InetSocketAddress thatRemoteData = udpChannel.remoteData();
final InetSocketAddress thatLocalData = udpChannel.localData();

final boolean addressMatches = thatRemoteData.getAddress().equals(thisRemoteData.getAddress()) &&
thatRemoteData.getPort() == thisRemoteData.getPort() &&
thatLocalData.getAddress().equals(currentControlAddress.getAddress()) &&
thatLocalData.getPort() == currentControlAddress.getPort();

return udpChannelMatches && (udpChannel.isWildcard() || addressMatches);
return udpChannel.matchesTag(super.udpChannel, currentControlAddress, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,23 +643,7 @@ private void applyChannelSendTimestamp(final ByteBuffer buffer)
*/
public boolean matchesTag(final UdpChannel udpChannel)
{
final boolean udpChannelMatches = super.udpChannel.matchesTag(udpChannel);

final InetSocketAddress thatRemoteData = udpChannel.remoteData();
final InetSocketAddress thisLocalData = super.udpChannel.localData();
final InetSocketAddress thatLocalData = udpChannel.localData();

final boolean wildcardMatches = thatRemoteData.getAddress().isAnyLocalAddress() &&
thatRemoteData.getPort() == 0 &&
thatLocalData.getAddress().isAnyLocalAddress() &&
thatLocalData.getPort() == 0;

final boolean addressMatches = thatRemoteData.getAddress().equals(connectAddress.getAddress()) &&
thatRemoteData.getPort() == connectAddress.getPort() &&
thatLocalData.getAddress().equals(thisLocalData.getAddress()) &&
thatLocalData.getPort() == thisLocalData.getPort();

return udpChannelMatches && (wildcardMatches || addressMatches);
return udpChannel.matchesTag(super.udpChannel, null, connectAddress);
}
}

Expand Down
41 changes: 30 additions & 11 deletions aeron-driver/src/main/java/io/aeron/driver/media/UdpChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -775,41 +775,60 @@ public Long nakDelayNs()
* Does this channel have a tag match to another channel having INADDR_ANY endpoints.
*
* @param udpChannel to match against.
* @param localAddress local address override to use for this channel.
* @param remoteAddress remote address override to use for this channel.
* @return true if there is a match otherwise false.
*/
public boolean matchesTag(final UdpChannel udpChannel)
public boolean matchesTag(
final UdpChannel udpChannel,
final InetSocketAddress localAddress,
final InetSocketAddress remoteAddress)
{
if (!hasTag || !udpChannel.hasTag() || tag != udpChannel.tag())
{
return false;
}

if (!matchesControlMode(udpChannel))
if (!hasMatchingControlMode(udpChannel))
{
throw new IllegalArgumentException(
"matching tag=" + tag + " has mismatched control-mode: " + uriStr + " <> " + udpChannel.uriStr);
}

if (!hasMatchingAddress(udpChannel, localAddress, remoteAddress))
{
throw new IllegalArgumentException(
"matching tag=" + tag + " has mismatched endpoint or control: " + uriStr + " <> " + udpChannel.uriStr);
}

return true;
}

/**
* Is this channel a fill wildcard, i.e. no addresses specified.
*
* @return <code>true</code> if all the address/port pairs of this channel are set to wildcard values,
* <code>false</code> otherwise.
*/
public boolean isWildcard()
private boolean isWildcard()
{
return remoteData.getAddress().isAnyLocalAddress() &&
remoteData.getPort() == 0 &&
localData.getAddress().isAnyLocalAddress() &&
localData.getPort() == 0;
}

private boolean matchesControlMode(final UdpChannel udpChannel)
private boolean hasMatchingControlMode(final UdpChannel udpChannel)
{
return udpChannel.controlMode() == ControlMode.NONE || controlMode() == udpChannel.controlMode();
return controlMode() == ControlMode.NONE || controlMode() == udpChannel.controlMode();
}

private boolean hasMatchingAddress(
final UdpChannel udpChannel,
final InetSocketAddress localAddress,
final InetSocketAddress remoteAddress)
{
final InetSocketAddress otherLocalData = localAddress != null ? localAddress : udpChannel.localData();
final InetSocketAddress otherRemoteData = remoteAddress != null ? remoteAddress : udpChannel.remoteData();

return isWildcard() || remoteData().getAddress().equals(otherRemoteData.getAddress()) &&
remoteData().getPort() == otherRemoteData.getPort() &&
localData().getAddress().equals(otherLocalData.getAddress()) &&
localData().getPort() == otherLocalData.getPort();
}

/**
Expand Down

0 comments on commit 7c1006d

Please sign in to comment.