mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
Improve MTU settings and documentation. #41
This commit is contained in:
@@ -75,7 +75,9 @@ func DecodeTransfer(msg *MessageRaw) (result *MessageTransfer, err error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// TransferMaxEmbedSize is the maximum size of embedded data inside the Transfer message.
|
||||
// TransferMaxEmbedSize is a recommended default upper size of embedded data inside the Transfer message, to be used as MaxPacketSize limit in the embedded protocol.
|
||||
// This value is chosen as the lowest denominator of different environments (IPv4, IPv6, Ethernet, Internet) for safe transfer, not for highest performance.
|
||||
// The caller may send bigger payloads but may risk that data packets are simply dropped and never arrive. A MTU negotiation or detection could pimp that.
|
||||
const TransferMaxEmbedSize = internetSafeMTU - PacketLengthMin - transferPayloadHeaderSize
|
||||
|
||||
// EncodeTransfer encodes a transfer message. The embedded packet size must be smaller than TransferMaxEmbedSize.
|
||||
|
||||
@@ -22,12 +22,17 @@ type MessageRaw struct {
|
||||
SequenceInfo *SequenceExpiry // Sequence
|
||||
}
|
||||
|
||||
// The maximum packet size is 65507 bytes = 65535 - 8 UDP byte header - 20 byte IP header.
|
||||
// The maximum packet size is = 65535 - 8 UDP byte header - 40 byte IPv6 header (IPv4 header is only 20 bytes).
|
||||
// However, due to the MTU soft limit and fragmentation, packets should be as small as possible.
|
||||
const udpMaxPacketSize = 65507
|
||||
const udpMaxPacketSize = 65535 - 8 - 40
|
||||
|
||||
// internetSafeMTU is a value relatively safe to use for transmitting over the internet
|
||||
const internetSafeMTU = 1500 - 20 - 8
|
||||
// Theory: The value is different for IPv4 (min 576 bytes, Ethernet 1500 bytes) and IPv6 (min 1280 bytes). 8 byte UDP header must be subtracted, as well as the IP header (20 bytes for IPv4, 40 for IPv6).
|
||||
// One simple test during development showed that 1500 - 8 - 40 - 8 worked for file transfer over IPv6 in Prague.
|
||||
// For IPv6 the internet recommends the minimal possible value: 1280 bytes.
|
||||
// This will be good enough for now. MTU negotiation that deviates from this value can be implemented separately (for example as part of file transfer).
|
||||
// Since packets may be sent at anytime via IPv4/IPv6 connections (even concurrently on multiple), there is a single MTU value here.
|
||||
const internetSafeMTU = 1280 - 8 - 40
|
||||
|
||||
// isPacketSizeExceed checks if the max packet size would be exceeded with the payload
|
||||
func isPacketSizeExceed(currentSize int, testSize int) bool {
|
||||
|
||||
@@ -120,15 +120,15 @@ func PacketEncrypt(senderPrivateKey *btcec.PrivateKey, receiverPublicKey *btcec.
|
||||
}
|
||||
|
||||
func packetGarbage(packetLength int) (random []byte) {
|
||||
// Align maximum length at 508 bytes (UDP minimum no fragmentation) and 1472 bytes (MTU).
|
||||
// Align maximum length at 508 bytes (UDP minimum no fragmentation) and at a relatively safe MTU.
|
||||
maxLength := maxRandomGarbage
|
||||
switch {
|
||||
case packetLength == 508, packetLength == 1472:
|
||||
case packetLength == 508, packetLength == internetSafeMTU:
|
||||
return nil
|
||||
case packetLength < 508 && (508-packetLength) < maxRandomGarbage:
|
||||
maxLength = packetLength - 508
|
||||
case packetLength < 1472 && (1472-packetLength) < maxRandomGarbage:
|
||||
maxLength = packetLength - 1472
|
||||
case packetLength < internetSafeMTU && (internetSafeMTU-packetLength) < maxRandomGarbage:
|
||||
maxLength = packetLength - internetSafeMTU
|
||||
}
|
||||
|
||||
b := make([]byte, rand.Intn(maxLength))
|
||||
|
||||
Reference in New Issue
Block a user