diff --git a/aurp/aurp.go b/aurp/aurp.go index 7f10801..bc46784 100644 --- a/aurp/aurp.go +++ b/aurp/aurp.go @@ -101,6 +101,14 @@ type Packet interface { io.WriterTo } +// Inc increments a uint16. It avoids 0 (65535 + 1 = 1). +func Inc(p *uint16) { + *p++ + if *p == 0 { + *p++ + } +} + // ParsePacket parses the body of a UDP packet for a domain header, and then // based on the packet type, an AURP-Tr header, an AURP routing header, and // then a particular packet type. diff --git a/aurp/transport.go b/aurp/transport.go index e9bf0b3..63f7716 100644 --- a/aurp/transport.go +++ b/aurp/transport.go @@ -69,20 +69,6 @@ type Transport struct { LocalSeq, RemoteSeq uint16 } -func (tr *Transport) IncLocalSeq() { - tr.LocalSeq++ - if tr.LocalSeq == 0 { - tr.LocalSeq = 1 - } -} - -func (tr *Transport) IncRemoteSeq() { - tr.RemoteSeq++ - if tr.RemoteSeq == 0 { - tr.RemoteSeq = 1 - } -} - // domainHeader returns a new domain header suitable for sending a packet. func (tr *Transport) domainHeader(pt PacketType) DomainHeader { return DomainHeader{ diff --git a/main.go b/main.go index d0555be..192adcc 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ import ( "errors" "flag" "log" + "math/rand/v2" "net" "os" "os/signal" @@ -74,7 +75,10 @@ func main() { log.Printf("EtherTalk configuration: %+v", cfg.EtherTalk) peers := make(map[udpAddr]*peer) - nextConnID := uint16(1) + var nextConnID uint16 + for nextConnID == 0 { + nextConnID = uint16(rand.IntN(0x10000)) + } ln, err := net.ListenUDP("udp4", &net.UDPAddr{Port: int(cfg.ListenPort)}) if err != nil { @@ -124,9 +128,9 @@ func main() { raddr: raddr, recv: make(chan aurp.Packet, 1024), } - nextConnID++ - goHandler(peer) + aurp.Inc(&nextConnID) peers[udpAddrFromNet(raddr)] = peer + goHandler(peer) } // Incoming packet loop @@ -176,7 +180,6 @@ func main() { pr := peers[ra] if pr == nil { // New peer! - nextConnID++ pr = &peer{ cfg: cfg, tr: &aurp.Transport{ @@ -188,6 +191,7 @@ func main() { raddr: raddr, recv: make(chan aurp.Packet, 1024), } + aurp.Inc(&nextConnID) peers[ra] = pr goHandler(pr) }