Lower SetMtu minimum from 50 to IKCP_OVERHEAD+1 (25) (#337)

* Lower SetMtu minimum from 50 to IKCP_OVERHEAD+1 (25)

The hardcoded minimum of 50 bytes is an arbitrary safety margin.
The real minimum is IKCP_OVERHEAD (24 bytes for the KCP header)
plus at least 1 byte of payload data. This allows use of smaller
MTUs in transport scenarios where capacity is constrained.

* Address review: use mtu <= IKCP_OVERHEAD and update boundary tests

- Changed condition from mtu < IKCP_OVERHEAD+1 to mtu <= IKCP_OVERHEAD
  for readability.
- Updated TestSetMTU to verify the new boundary: MTU at or below
  IKCP_OVERHEAD is rejected, MTU just above total overhead is accepted.
This commit is contained in:
selfishblackberry177
2026-03-26 20:25:55 +08:00
committed by GitHub
parent 9781b44b33
commit 8a84a05b1a
2 changed files with 9 additions and 5 deletions
+1 -1
View File
@@ -1078,7 +1078,7 @@ func (kcp *KCP) Check() uint32 {
// SetMtu changes MTU size, default is 1400
func (kcp *KCP) SetMtu(mtu int) int {
if mtu < 50 || mtu < IKCP_OVERHEAD {
if mtu <= IKCP_OVERHEAD {
return -1
}
+8 -4
View File
@@ -1112,10 +1112,14 @@ func TestSetMTU(t *testing.T) {
}
defer cli.Close()
ok := cli.SetMtu(49)
if ok {
t.Fatal("can not set mtu small than 50")
return
if cli.SetMtu(IKCP_OVERHEAD) {
t.Fatal("should not allow MTU equal to IKCP_OVERHEAD")
}
if cli.SetMtu(IKCP_OVERHEAD - 1) {
t.Fatal("should not allow MTU below IKCP_OVERHEAD")
}
if !cli.SetMtu(IKCP_OVERHEAD + cryptHeaderSize + fecHeaderSizePlus2 + 1) {
t.Fatal("should allow MTU just above total overhead")
}
cli.SetMtu(1500)