Skip to content

Commit

Permalink
Allow setting IpDontFragment to false
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Castle <[email protected]>
  • Loading branch information
Kas-tle committed Jul 4, 2024
1 parent b7ee213 commit a891953
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public boolean isIpDontFragment() {
}

public void setIpDontFragment(boolean enable) {
this.ipDontFragment = enable ? IpDontFragmentProvider.trySet(this.channel) : false;
this.ipDontFragment = IpDontFragmentProvider.trySet(this.channel, enable);
}

public int getClientInternalAddresses() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public RakServerMetrics getMetrics() {

@Override
public void setIpDontFragment(boolean ipDontFragment) {
this.ipDontFragment = ipDontFragment ? IpDontFragmentProvider.trySet(this.channel.parent()) : false;
this.ipDontFragment = IpDontFragmentProvider.trySet(this.channel.parent(), ipDontFragment);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

public class IpDontFragmentProvider {
private static final ChannelOption<?> IP_DONT_FRAGMENT_OPTION;
private static final Object IP_DONT_FRAGMENT_VALUE;
private static final Object IP_DONT_FRAGMENT_TRUE_VALUE;
private static final Object IP_DONT_FRAGMENT_FALSE_VALUE;

static {
ChannelOption<?> ipDontFragmentOption = null;
Object ipDontFragmentValue = null;
Object ipDontFragmentTrueValue = null;
Object ipDontFragmentFalseValue = null;

setterBlock: {
// Windows and Linux Compatible (Java 19+)
Expand All @@ -23,25 +25,28 @@ public class IpDontFragmentProvider {
Field f = c.getField("IP_DONTFRAGMENT");

ipDontFragmentOption = NioChannelOption.of((SocketOption<?>) f.get(null));
ipDontFragmentValue = true;
ipDontFragmentTrueValue = true;
ipDontFragmentFalseValue = false;
break setterBlock;
} catch (ClassNotFoundException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {

}

// Unix Compatible (Java 8+)
ipDontFragmentOption = new IntegerUnixChannelOption("IP_DONTFRAG", 0 /* IPPROTO_IP */, 10 /* IP_MTU_DISCOVER */);
ipDontFragmentValue = 2 /* IP_PMTUDISC_DO */;
ipDontFragmentTrueValue = 2 /* IP_PMTUDISC_DO */;
ipDontFragmentFalseValue = 0 /* IP_PMTUDISC_DONT */;
break setterBlock;
}

IP_DONT_FRAGMENT_OPTION = ipDontFragmentOption;
IP_DONT_FRAGMENT_VALUE = ipDontFragmentValue;
IP_DONT_FRAGMENT_TRUE_VALUE = ipDontFragmentTrueValue;
IP_DONT_FRAGMENT_FALSE_VALUE = ipDontFragmentFalseValue;
}

@SuppressWarnings("unchecked")
public static <T> boolean trySet(Channel channel) {
public static <T> boolean trySet(Channel channel, boolean value) {
if (IP_DONT_FRAGMENT_OPTION == null) return false;
return channel.config().setOption((ChannelOption<T>) IP_DONT_FRAGMENT_OPTION, (T) IP_DONT_FRAGMENT_VALUE);
return channel.config().setOption((ChannelOption<T>) IP_DONT_FRAGMENT_OPTION, (T) (value ? IP_DONT_FRAGMENT_TRUE_VALUE : IP_DONT_FRAGMENT_FALSE_VALUE));
}
}

0 comments on commit a891953

Please sign in to comment.