From 8ebe57a602e19dc4f70193455630e7d221e794a5 Mon Sep 17 00:00:00 2001 From: Leo Balduf Date: Wed, 17 Aug 2016 19:04:26 -0400 Subject: [PATCH 1/2] udp: fix response encoding --- frontend/udp/writer.go | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/frontend/udp/writer.go b/frontend/udp/writer.go index 6055d66..e3a495f 100644 --- a/frontend/udp/writer.go +++ b/frontend/udp/writer.go @@ -26,32 +26,44 @@ func WriteError(w io.Writer, txID []byte, err error) { // WriteAnnounce encodes an announce response according to BEP 15. func WriteAnnounce(w io.Writer, txID []byte, resp *bittorrent.AnnounceResponse) { - writeHeader(w, txID, announceActionID) - binary.Write(w, binary.BigEndian, uint32(resp.Interval/time.Second)) - binary.Write(w, binary.BigEndian, uint32(resp.Incomplete)) - binary.Write(w, binary.BigEndian, uint32(resp.Complete)) + var buf bytes.Buffer + + writeHeader(&buf, txID, announceActionID) + binary.Write(&buf, binary.BigEndian, uint32(resp.Interval/time.Second)) + binary.Write(&buf, binary.BigEndian, uint32(resp.Incomplete)) + binary.Write(&buf, binary.BigEndian, uint32(resp.Complete)) for _, peer := range resp.IPv4Peers { - w.Write(peer.IP) - binary.Write(w, binary.BigEndian, peer.Port) + buf.Write(peer.IP) + binary.Write(&buf, binary.BigEndian, peer.Port) } + + w.Write(buf.Bytes()) } // WriteScrape encodes a scrape response according to BEP 15. func WriteScrape(w io.Writer, txID []byte, resp *bittorrent.ScrapeResponse) { - writeHeader(w, txID, scrapeActionID) + var buf bytes.Buffer + + writeHeader(&buf, txID, scrapeActionID) for _, scrape := range resp.Files { - binary.Write(w, binary.BigEndian, scrape.Complete) - binary.Write(w, binary.BigEndian, scrape.Snatches) - binary.Write(w, binary.BigEndian, scrape.Incomplete) + binary.Write(&buf, binary.BigEndian, scrape.Complete) + binary.Write(&buf, binary.BigEndian, scrape.Snatches) + binary.Write(&buf, binary.BigEndian, scrape.Incomplete) } + + w.Write(buf.Bytes()) } // WriteConnectionID encodes a new connection response according to BEP 15. func WriteConnectionID(w io.Writer, txID, connID []byte) { - writeHeader(w, txID, connectActionID) - w.Write(connID) + var buf bytes.Buffer + + writeHeader(&buf, txID, connectActionID) + buf.Write(connID) + + w.Write(buf.Bytes()) } // writeHeader writes the action and transaction ID to the provided response From a4639a1aacdc3d7d5b78783a0d61fd842dba307b Mon Sep 17 00:00:00 2001 From: Leo Balduf Date: Wed, 17 Aug 2016 19:04:53 -0400 Subject: [PATCH 2/2] udp: clean up --- frontend/udp/frontend.go | 10 +++------- frontend/udp/parser.go | 1 - 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/frontend/udp/frontend.go b/frontend/udp/frontend.go index fc65c72..1cf3d03 100644 --- a/frontend/udp/frontend.go +++ b/frontend/udp/frontend.go @@ -6,7 +6,6 @@ import ( "bytes" "context" "encoding/binary" - "log" "net" "sync" "time" @@ -94,13 +93,12 @@ func (t *Frontend) ListenAndServe() error { } defer t.socket.Close() - pool := bytepool.New(256, 2048) + pool := bytepool.New(2048, 2048) for { // Check to see if we need to shutdown. select { case <-t.closing: - t.wg.Wait() return nil default: } @@ -124,7 +122,6 @@ func (t *Frontend) ListenAndServe() error { continue } - log.Println("Got UDP Request") t.wg.Add(1) go func() { defer t.wg.Done() @@ -132,11 +129,10 @@ func (t *Frontend) ListenAndServe() error { // Handle the request. start := time.Now() - response, action, err := t.handleRequest( + action, err := t.handleRequest( Request{buffer[:n], addr.IP}, ResponseWriter{t.socket, addr}, ) - log.Printf("Handled UDP Request: %s, %s, %s\n", response, action, err) recordResponseDuration(action, err, time.Since(start)) }() } @@ -162,7 +158,7 @@ func (w ResponseWriter) Write(b []byte) (int, error) { } // handleRequest parses and responds to a UDP Request. -func (t *Frontend) handleRequest(r Request, w ResponseWriter) (response []byte, actionName string, err error) { +func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string, err error) { if len(r.Packet) < 16 { // Malformed, no client packets are less than 16 bytes. // We explicitly return nothing in case this is a DoS attempt. diff --git a/frontend/udp/parser.go b/frontend/udp/parser.go index a73fe33..43dc329 100644 --- a/frontend/udp/parser.go +++ b/frontend/udp/parser.go @@ -12,7 +12,6 @@ const ( announceActionID scrapeActionID errorActionID - announceDualStackActionID ) // Option-Types as described in BEP 41 and BEP 45.