From 1573ded639d86ad4e646cefc9cc7489daf09ffb4 Mon Sep 17 00:00:00 2001 From: Kleissner Date: Thu, 4 Nov 2021 22:35:57 +0100 Subject: [PATCH] UDT: Fix invalid initial packet sequence number when random number was a signed negative int --- udt/packet/pktseq.go | 7 +++++++ udt/udtsocket.go | 3 +-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/udt/packet/pktseq.go b/udt/packet/pktseq.go index 03a1b9f..130a634 100644 --- a/udt/packet/pktseq.go +++ b/udt/packet/pktseq.go @@ -1,5 +1,7 @@ package packet +import "math/rand" + // PacketID represents a UDT packet ID sequence type PacketID struct { Seq uint32 @@ -28,3 +30,8 @@ func (p PacketID) BlindDiff(rhs PacketID) int32 { } return int32(result) } + +// RandomPacketSequence returns a random packet sequence +func RandomPacketSequence() PacketID { + return PacketID{rand.Uint32() & 0x7FFFFFFF} +} diff --git a/udt/udtsocket.go b/udt/udtsocket.go index fb7c77a..aeaefbc 100644 --- a/udt/udtsocket.go +++ b/udt/udtsocket.go @@ -2,7 +2,6 @@ package udt import ( "errors" - "math/rand" "net" "sync" "syscall" @@ -420,7 +419,7 @@ func newSocket(m *multiplexer, config *Config, sockID uint32, isServer bool, isD maxFlowWinSize: maxFlowWinSize, isDatagram: isDatagram, sockID: sockID, - initPktSeq: packet.PacketID{Seq: rand.Uint32()}, + initPktSeq: packet.RandomPacketSequence(), messageIn: make(chan []byte, 256), messageOut: make(chan sendMessage, 256), recvEvent: make(chan recvPktEvent, 256),