Skip to content

Commit

Permalink
Send packets from correct local address
Browse files Browse the repository at this point in the history
  • Loading branch information
SupremeMortal committed Aug 18, 2024
1 parent b7522a8 commit 78d16f7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ public class RakChildChannel extends AbstractChannel implements RakChannel {

private final RakChannelConfig config;
private final InetSocketAddress remoteAddress;
private final InetSocketAddress localAddress;
private final DefaultChannelPipeline rakPipeline;
private volatile boolean open = true;
private volatile boolean active;

RakChildChannel(InetSocketAddress remoteAddress, RakServerChannel parent, long guid, int version, int mtu, Consumer<RakChannel> childConsumer) {
RakChildChannel(InetSocketAddress remoteAddress, InetSocketAddress localAddress, RakServerChannel parent, long guid, int version, int mtu, Consumer<RakChannel> childConsumer) {
super(parent);
this.remoteAddress = remoteAddress;
this.localAddress = localAddress;
this.config = new DefaultRakSessionConfig(this);
this.config.setGuid(guid);
this.config.setProtocolVersion(version);
Expand Down Expand Up @@ -80,7 +82,7 @@ public ChannelPipeline rakPipeline() {

@Override
public SocketAddress localAddress0() {
return this.parent().localAddress();
return this.localAddress;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public RakServerChannel(DatagramChannel channel, Consumer<RakChannel> childConsu
* @param address remote address of new connection.
* @return RakChildChannel instance of new channel.
*/
public RakChildChannel createChildChannel(InetSocketAddress address, long clientGuid, int protocolVersion, int mtu) {
public RakChildChannel createChildChannel(InetSocketAddress address, InetSocketAddress localAddress,
long clientGuid, int protocolVersion, int mtu) {
RakChildChannel existingChannel = this.childChannelMap.get(address);
if (this.config().getSendCookie() && existingChannel != null) {
// We know this player is coming from this IP address due to the cookie, so we can safely close the existing channel.
Expand All @@ -84,7 +85,7 @@ public RakChildChannel createChildChannel(InetSocketAddress address, long client
return null;
}

RakChildChannel channel = new RakChildChannel(address, this, clientGuid, protocolVersion, mtu, childConsumer);
RakChildChannel channel = new RakChildChannel(address, localAddress, this, clientGuid, protocolVersion, mtu, childConsumer);
channel.closeFuture().addListener((GenericFutureListener<ChannelFuture>) this::onChildClosed);
// Fire channel thought ServerBootstrap,
// register to eventLoop, assign default options and attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)

this.canFlush = true;
promise.trySuccess();
DatagramPacket datagram = isDatagram ? (DatagramPacket) msg : new DatagramPacket((ByteBuf) msg, this.channel.remoteAddress());
DatagramPacket datagram = isDatagram ? (DatagramPacket) msg :
new DatagramPacket((ByteBuf) msg, this.channel.remoteAddress(), this.channel.localAddress());

RakChannelMetrics metrics = this.channel.config().getMetrics();
if (metrics != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void onUnconnectedPing(ChannelHandlerContext ctx, DatagramPacket packet,
out.writeBytes(advertisement, advertisement.readerIndex(), advertisement.readableBytes());
}

ctx.writeAndFlush(new DatagramPacket(out, packet.sender()));
ctx.writeAndFlush(RakUtils.datagramReply(out, packet));
}

private void onOpenConnectionRequest1(ChannelHandlerContext ctx, DatagramPacket packet, ByteBuf magicBuf, long guid) {
Expand All @@ -166,7 +166,7 @@ private void onOpenConnectionRequest1(ChannelHandlerContext ctx, DatagramPacket
int[] supportedProtocols = ((RakServerChannelConfig) ctx.channel().config()).getSupportedProtocols();
if (supportedProtocols != null && Arrays.binarySearch(supportedProtocols, protocolVersion) < 0) {
int latestVersion = supportedProtocols[supportedProtocols.length - 1];
this.sendIncompatibleVersion(ctx, packet.sender(), latestVersion, magicBuf, guid);
this.sendIncompatibleVersion(ctx, packet, latestVersion, magicBuf, guid);
return;
}

Expand Down Expand Up @@ -200,7 +200,7 @@ private void onOpenConnectionRequest1(ChannelHandlerContext ctx, DatagramPacket
}
replyBuffer.writeShort(RakUtils.clamp(mtu, ((RakServerChannelConfig) ctx.channel().config()).getMinMtu(),
((RakServerChannelConfig) ctx.channel().config()).getMaxMtu()));
ctx.writeAndFlush(new DatagramPacket(replyBuffer, sender));
ctx.writeAndFlush(RakUtils.datagramReply(replyBuffer, packet));
}

private void onOpenConnectionRequest2(ChannelHandlerContext ctx, DatagramPacket packet, ByteBuf magicBuf, long guid) {
Expand Down Expand Up @@ -248,19 +248,19 @@ private void onOpenConnectionRequest2(ChannelHandlerContext ctx, DatagramPacket
sender, mtu, minMtu, maxMtu);
}
// The client should have already negotiated a valid MTU
this.sendAlreadyConnected(ctx, sender, magicBuf, guid);
this.sendAlreadyConnected(ctx, packet, magicBuf, guid);
return;
}

RakServerChannel serverChannel = (RakServerChannel) ctx.channel();
RakChildChannel channel = serverChannel.createChildChannel(sender, clientGuid, connection.protocolVersion, mtu);
RakChildChannel channel = serverChannel.createChildChannel(sender, packet.recipient(), clientGuid, connection.protocolVersion, mtu);
if (channel == null) {
if (log.isTraceEnabled()) {
log.trace("[{}] Received ID_OPEN_CONNECTION_REQUEST_2, but a channel already exists for this socket address",
sender);
}
// Already connected
this.sendAlreadyConnected(ctx, sender, magicBuf, guid);
this.sendAlreadyConnected(ctx, packet, magicBuf, guid);
return;
}

Expand All @@ -271,24 +271,24 @@ private void onOpenConnectionRequest2(ChannelHandlerContext ctx, DatagramPacket
RakUtils.writeAddress(replyBuffer, packet.sender());
replyBuffer.writeShort(mtu);
replyBuffer.writeBoolean(false); // Security
ctx.writeAndFlush(new DatagramPacket(replyBuffer, packet.sender()));
ctx.writeAndFlush(RakUtils.datagramReply(replyBuffer, packet));
}

private void sendIncompatibleVersion(ChannelHandlerContext ctx, InetSocketAddress sender, int protocolVersion, ByteBuf magicBuf, long guid) {
private void sendIncompatibleVersion(ChannelHandlerContext ctx, DatagramPacket request, int protocolVersion, ByteBuf magicBuf, long guid) {
ByteBuf buffer = ctx.alloc().ioBuffer(26, 26);
buffer.writeByte(ID_INCOMPATIBLE_PROTOCOL_VERSION);
buffer.writeByte(protocolVersion);
buffer.writeBytes(magicBuf, magicBuf.readerIndex(), magicBuf.readableBytes());
buffer.writeLong(guid);
ctx.writeAndFlush(new DatagramPacket(buffer, sender));
ctx.writeAndFlush(RakUtils.datagramReply(buffer, request));
}

private void sendAlreadyConnected(ChannelHandlerContext ctx, InetSocketAddress sender, ByteBuf magicBuf, long guid) {
private void sendAlreadyConnected(ChannelHandlerContext ctx, DatagramPacket request, ByteBuf magicBuf, long guid) {
ByteBuf buffer = ctx.alloc().ioBuffer(25, 25);
buffer.writeByte(ID_ALREADY_CONNECTED);
buffer.writeBytes(magicBuf, magicBuf.readerIndex(), magicBuf.readableBytes());
buffer.writeLong(guid);
ctx.writeAndFlush(new DatagramPacket(buffer, sender));
ctx.writeAndFlush(RakUtils.datagramReply(buffer, request));
}

private static class PendingConnection {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.DefaultChannelPipeline;
import io.netty.channel.socket.DatagramPacket;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -186,4 +187,8 @@ public static int powerOfTwoCeiling(int value) {
value++;
return value;
}

public static DatagramPacket datagramReply(ByteBuf buf, DatagramPacket request) {
return new DatagramPacket(buf, request.sender(), request.recipient());
}
}

0 comments on commit 78d16f7

Please sign in to comment.