From 31b23b5d49a6abf4114a5c56ad878ff6eeac145c Mon Sep 17 00:00:00 2001 From: Heiko Bornholdt Date: Mon, 4 Nov 2024 00:28:34 +0100 Subject: [PATCH] Fix behavior of TunChannel#isOpen --- src/main/java/org/drasyl/channel/tun/TunChannel.java | 12 ++++++++---- .../drasyl/channel/tun/jna/AbstractTunDevice.java | 6 ------ .../java/org/drasyl/channel/tun/jna/TunDevice.java | 7 ------- .../channel/tun/jna/darwin/DarwinTunDevice.java | 1 + .../drasyl/channel/tun/jna/linux/LinuxTunDevice.java | 1 + .../channel/tun/jna/windows/WindowsTunDevice.java | 1 + 6 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/drasyl/channel/tun/TunChannel.java b/src/main/java/org/drasyl/channel/tun/TunChannel.java index 367e023..c2bc1f1 100644 --- a/src/main/java/org/drasyl/channel/tun/TunChannel.java +++ b/src/main/java/org/drasyl/channel/tun/TunChannel.java @@ -68,6 +68,7 @@ public class TunChannel extends AbstractChannel { private boolean readPending; private final EventLoop readLoop = new DefaultEventLoop(); private TunDevice device; + private boolean closed; public TunChannel() { super(null); @@ -85,12 +86,12 @@ public TunChannelConfig config() { @Override public boolean isOpen() { - return device == null || !device.isClosed(); + return !closed; } @Override public boolean isActive() { - return device != null && isOpen(); + return !closed && device != null; } @Override @@ -128,8 +129,11 @@ protected void doDisconnect() throws Exception { @Override protected void doClose() throws Exception { - if (device != null) { - device.close(); + if (!closed) { + closed = true; + if (device != null) { + device.close(); + } } } diff --git a/src/main/java/org/drasyl/channel/tun/jna/AbstractTunDevice.java b/src/main/java/org/drasyl/channel/tun/jna/AbstractTunDevice.java index e6c4907..65b0796 100644 --- a/src/main/java/org/drasyl/channel/tun/jna/AbstractTunDevice.java +++ b/src/main/java/org/drasyl/channel/tun/jna/AbstractTunDevice.java @@ -27,7 +27,6 @@ public abstract class AbstractTunDevice implements TunDevice { protected final TunAddress localAddress; - protected boolean closed; protected AbstractTunDevice(TunAddress localAddress) { this.localAddress = requireNonNull(localAddress); @@ -37,9 +36,4 @@ protected AbstractTunDevice(TunAddress localAddress) { public TunAddress localAddress() { return localAddress; } - - @Override - public boolean isClosed() { - return closed; - } } diff --git a/src/main/java/org/drasyl/channel/tun/jna/TunDevice.java b/src/main/java/org/drasyl/channel/tun/jna/TunDevice.java index 6a3ddd5..3f3aece 100644 --- a/src/main/java/org/drasyl/channel/tun/jna/TunDevice.java +++ b/src/main/java/org/drasyl/channel/tun/jna/TunDevice.java @@ -56,11 +56,4 @@ public interface TunDevice extends Closeable { * @throws IOException if write failed */ void writePacket(final ByteBufAllocator alloc, final TunPacket msg) throws IOException; - - /** - * Returns whether the device is closed or not. - * - * @return {@code true} if the device has been closed. - */ - boolean isClosed(); } diff --git a/src/main/java/org/drasyl/channel/tun/jna/darwin/DarwinTunDevice.java b/src/main/java/org/drasyl/channel/tun/jna/darwin/DarwinTunDevice.java index d9b6912..c7c5559 100644 --- a/src/main/java/org/drasyl/channel/tun/jna/darwin/DarwinTunDevice.java +++ b/src/main/java/org/drasyl/channel/tun/jna/darwin/DarwinTunDevice.java @@ -73,6 +73,7 @@ public final class DarwinTunDevice extends AbstractTunDevice { })); private final int fd; private final NativeLong readBytes; + protected boolean closed; private DarwinTunDevice(final int fd, final int mtu, final TunAddress localAddress) { super(localAddress); diff --git a/src/main/java/org/drasyl/channel/tun/jna/linux/LinuxTunDevice.java b/src/main/java/org/drasyl/channel/tun/jna/linux/LinuxTunDevice.java index db90138..28d265b 100644 --- a/src/main/java/org/drasyl/channel/tun/jna/linux/LinuxTunDevice.java +++ b/src/main/java/org/drasyl/channel/tun/jna/linux/LinuxTunDevice.java @@ -59,6 +59,7 @@ public final class LinuxTunDevice extends AbstractTunDevice { private static final IllegalArgumentException ILLEGAL_NAME_EXCEPTION = new IllegalArgumentException("Device name must be an ASCII string shorter than 16 characters or null."); private final int fd; private final NativeLong mtu; + protected boolean closed; private LinuxTunDevice(final int fd, final int mtu, final TunAddress localAddress) { super(localAddress); diff --git a/src/main/java/org/drasyl/channel/tun/jna/windows/WindowsTunDevice.java b/src/main/java/org/drasyl/channel/tun/jna/windows/WindowsTunDevice.java index cd784bb..b38edef 100644 --- a/src/main/java/org/drasyl/channel/tun/jna/windows/WindowsTunDevice.java +++ b/src/main/java/org/drasyl/channel/tun/jna/windows/WindowsTunDevice.java @@ -62,6 +62,7 @@ public final class WindowsTunDevice extends AbstractTunDevice { public static final WString TUNNEL_TYPE = new WString("drasyl"); private final WINTUN_ADAPTER_HANDLE adapter; private final WINTUN_SESSION_HANDLE session; + protected boolean closed; private WindowsTunDevice(final WINTUN_ADAPTER_HANDLE adapter, final WINTUN_SESSION_HANDLE session,