diff --git a/udp/connection.go b/udp/connection.go index ce4d063..49b2c1a 100644 --- a/udp/connection.go +++ b/udp/connection.go @@ -11,11 +11,14 @@ import ( "crypto/rand" ) +// ConnectionIDGenerator represents the logic to generate 64-bit UDP +// connection IDs from peer IP addresses. type ConnectionIDGenerator struct { iv, iv2 []byte block cipher.Block } +// Init generates the AES key and sets up the first initialization vector. func (g *ConnectionIDGenerator) Init() error { key := make([]byte, 16) _, err := rand.Read(key) @@ -31,6 +34,7 @@ func (g *ConnectionIDGenerator) Init() error { return g.NewIV() } +// Generate returns the 64-bit connection ID for an IP func (g *ConnectionIDGenerator) Generate(ip []byte) []byte { return g.generate(ip, g.iv) } @@ -55,6 +59,8 @@ func (g *ConnectionIDGenerator) generate(ip []byte, iv []byte) []byte { return ct[:8] } +// Matches checks if the given connection ID matches an IP with the current or +// previous initialization vectors. func (g *ConnectionIDGenerator) Matches(id []byte, ip []byte) bool { if expected := g.generate(ip, g.iv); bytes.Equal(id, expected) { return true @@ -69,6 +75,7 @@ func (g *ConnectionIDGenerator) Matches(id []byte, ip []byte) bool { return false } +// NewIV generates a new initialization vector and rotates the current one. func (g *ConnectionIDGenerator) NewIV() error { newiv := make([]byte, 16) if _, err := rand.Read(newiv); err != nil { diff --git a/udp/protocol.go b/udp/protocol.go index 6948ad7..15c0ef8 100644 --- a/udp/protocol.go +++ b/udp/protocol.go @@ -129,8 +129,6 @@ func (s *Server) newAnnounce(packet []byte, ip net.IP) (*models.Announce, error) ip = ipv4 } - // TODO(pushrax): what exactly is the key "key" used for? - numWant := binary.BigEndian.Uint32(packet[92:96]) port := binary.BigEndian.Uint16(packet[96:98]) diff --git a/udp/writer.go b/udp/writer.go index a2b503f..2d26f9f 100644 --- a/udp/writer.go +++ b/udp/writer.go @@ -43,7 +43,7 @@ func (w *Writer) WriteAnnounce(res *models.AnnounceResponse) error { return nil } -// WriteAnnounce encodes a scrape response according to the UDP spec. +// WriteScrape encodes a scrape response according to the UDP spec. func (w *Writer) WriteScrape(res *models.ScrapeResponse) error { w.writeHeader(2)