Refactor increments, rando connID

This commit is contained in:
Josh Deprez 2024-04-05 10:43:29 +11:00
parent 4afe8f17bb
commit f7fa4fb688
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw
3 changed files with 16 additions and 18 deletions

View file

@ -101,6 +101,14 @@ type Packet interface {
io.WriterTo 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 // 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 // based on the packet type, an AURP-Tr header, an AURP routing header, and
// then a particular packet type. // then a particular packet type.

View file

@ -69,20 +69,6 @@ type Transport struct {
LocalSeq, RemoteSeq uint16 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. // domainHeader returns a new domain header suitable for sending a packet.
func (tr *Transport) domainHeader(pt PacketType) DomainHeader { func (tr *Transport) domainHeader(pt PacketType) DomainHeader {
return DomainHeader{ return DomainHeader{

12
main.go
View file

@ -21,6 +21,7 @@ import (
"errors" "errors"
"flag" "flag"
"log" "log"
"math/rand/v2"
"net" "net"
"os" "os"
"os/signal" "os/signal"
@ -74,7 +75,10 @@ func main() {
log.Printf("EtherTalk configuration: %+v", cfg.EtherTalk) log.Printf("EtherTalk configuration: %+v", cfg.EtherTalk)
peers := make(map[udpAddr]*peer) 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)}) ln, err := net.ListenUDP("udp4", &net.UDPAddr{Port: int(cfg.ListenPort)})
if err != nil { if err != nil {
@ -124,9 +128,9 @@ func main() {
raddr: raddr, raddr: raddr,
recv: make(chan aurp.Packet, 1024), recv: make(chan aurp.Packet, 1024),
} }
nextConnID++ aurp.Inc(&nextConnID)
goHandler(peer)
peers[udpAddrFromNet(raddr)] = peer peers[udpAddrFromNet(raddr)] = peer
goHandler(peer)
} }
// Incoming packet loop // Incoming packet loop
@ -176,7 +180,6 @@ func main() {
pr := peers[ra] pr := peers[ra]
if pr == nil { if pr == nil {
// New peer! // New peer!
nextConnID++
pr = &peer{ pr = &peer{
cfg: cfg, cfg: cfg,
tr: &aurp.Transport{ tr: &aurp.Transport{
@ -188,6 +191,7 @@ func main() {
raddr: raddr, raddr: raddr,
recv: make(chan aurp.Packet, 1024), recv: make(chan aurp.Packet, 1024),
} }
aurp.Inc(&nextConnID)
peers[ra] = pr peers[ra] = pr
goHandler(pr) goHandler(pr)
} }