udp: Add comments to pass lint

This commit is contained in:
Justin Li 2015-02-21 00:21:14 -05:00
parent 4c3cd6cc0a
commit f98c675bc7
3 changed files with 8 additions and 3 deletions

View file

@ -11,11 +11,14 @@ import (
"crypto/rand" "crypto/rand"
) )
// ConnectionIDGenerator represents the logic to generate 64-bit UDP
// connection IDs from peer IP addresses.
type ConnectionIDGenerator struct { type ConnectionIDGenerator struct {
iv, iv2 []byte iv, iv2 []byte
block cipher.Block block cipher.Block
} }
// Init generates the AES key and sets up the first initialization vector.
func (g *ConnectionIDGenerator) Init() error { func (g *ConnectionIDGenerator) Init() error {
key := make([]byte, 16) key := make([]byte, 16)
_, err := rand.Read(key) _, err := rand.Read(key)
@ -31,6 +34,7 @@ func (g *ConnectionIDGenerator) Init() error {
return g.NewIV() return g.NewIV()
} }
// Generate returns the 64-bit connection ID for an IP
func (g *ConnectionIDGenerator) Generate(ip []byte) []byte { func (g *ConnectionIDGenerator) Generate(ip []byte) []byte {
return g.generate(ip, g.iv) return g.generate(ip, g.iv)
} }
@ -55,6 +59,8 @@ func (g *ConnectionIDGenerator) generate(ip []byte, iv []byte) []byte {
return ct[:8] 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 { func (g *ConnectionIDGenerator) Matches(id []byte, ip []byte) bool {
if expected := g.generate(ip, g.iv); bytes.Equal(id, expected) { if expected := g.generate(ip, g.iv); bytes.Equal(id, expected) {
return true return true
@ -69,6 +75,7 @@ func (g *ConnectionIDGenerator) Matches(id []byte, ip []byte) bool {
return false return false
} }
// NewIV generates a new initialization vector and rotates the current one.
func (g *ConnectionIDGenerator) NewIV() error { func (g *ConnectionIDGenerator) NewIV() error {
newiv := make([]byte, 16) newiv := make([]byte, 16)
if _, err := rand.Read(newiv); err != nil { if _, err := rand.Read(newiv); err != nil {

View file

@ -129,8 +129,6 @@ func (s *Server) newAnnounce(packet []byte, ip net.IP) (*models.Announce, error)
ip = ipv4 ip = ipv4
} }
// TODO(pushrax): what exactly is the key "key" used for?
numWant := binary.BigEndian.Uint32(packet[92:96]) numWant := binary.BigEndian.Uint32(packet[92:96])
port := binary.BigEndian.Uint16(packet[96:98]) port := binary.BigEndian.Uint16(packet[96:98])

View file

@ -43,7 +43,7 @@ func (w *Writer) WriteAnnounce(res *models.AnnounceResponse) error {
return nil 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 { func (w *Writer) WriteScrape(res *models.ScrapeResponse) error {
w.writeHeader(2) w.writeHeader(2)