From c9507786f2b38a74b3732d9b45f2fa6661f2b2bc Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Fri, 2 Feb 2018 16:06:24 +1100
Subject: [PATCH] Count requested chunk lengths

Have seen some very ridiculous lengths causing data read errors
---
 connection.go             | 1 +
 global.go                 | 1 +
 peer_protocol/protocol.go | 4 ++++
 3 files changed, 6 insertions(+)

diff --git a/connection.go b/connection.go
index 46083d56..fbf86e56 100644
--- a/connection.go
+++ b/connection.go
@@ -884,6 +884,7 @@ func (c *connection) mainReadLoop() error {
 		case pp.Have:
 			err = c.peerSentHave(int(msg.Index))
 		case pp.Request:
+			requestedChunkLengths.Add(strconv.FormatUint(msg.Length.Uint64(), 10), 1)
 			if c.Choked {
 				break
 			}
diff --git a/global.go b/global.go
index ffc30f49..4b27f2e5 100644
--- a/global.go
+++ b/global.go
@@ -62,6 +62,7 @@ var (
 	postedKeepalives           = expvar.NewInt("postedKeepalives")
 	// Requests received for pieces we don't have.
 	requestsReceivedForMissingPieces = expvar.NewInt("requestsReceivedForMissingPieces")
+	requestedChunkLengths            = expvar.NewMap("requestedChunkLengths")
 
 	messageTypesReceived = expvar.NewMap("messageTypesReceived")
 	messageTypesSent     = expvar.NewMap("messageTypesSent")
diff --git a/peer_protocol/protocol.go b/peer_protocol/protocol.go
index bc4d6ee6..27827eed 100644
--- a/peer_protocol/protocol.go
+++ b/peer_protocol/protocol.go
@@ -25,6 +25,10 @@ func (i Integer) Int() int {
 	return int(i)
 }
 
+func (i Integer) Uint64() uint64 {
+	return uint64(i)
+}
+
 const (
 	Protocol = "\x13BitTorrent protocol"
 )
-- 
2.50.0