mirror of
https://github.com/xtaci/kcp-go.git
synced 2026-10-19 16:14:38 +00:00
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:
@@ -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
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user