2
0
mirror of synced 2025-02-23 14:18:13 +00:00

Clarify maximum value of "metadata_size" (#609)

This commit is contained in:
YenForYang 2021-09-14 07:36:19 -05:00 committed by GitHub
parent 57b4e78b0f
commit e80b989f8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 5 deletions

View File

@ -10,6 +10,10 @@ import (
const (
pieceHash = crypto.SHA1
defaultChunkSize = 0x4000 // 16KiB
// Arbitrary maximum of "metadata_size" (see https://www.bittorrent.org/beps/bep_0009.html)
// This value is 2x what libtorrent-rasterbar uses, which should be plenty
maxMetadataSize uint32 = 8*1024*1024
)
// These are our extended message IDs. Peers will use these values to

View File

@ -481,19 +481,19 @@ func (t *Torrent) haveAllMetadataPieces() bool {
}
// TODO: Propagate errors to disconnect peer.
func (t *Torrent) setMetadataSize(bytes int) (err error) {
func (t *Torrent) setMetadataSize(size int) (err error) {
if t.haveInfo() {
// We already know the correct metadata size.
return
}
if bytes <= 0 || bytes > 10000000 { // 10MB, pulled from my ass.
if uint32(size) > maxMetadataSize {
return errors.New("bad size")
}
if t.metadataBytes != nil && len(t.metadataBytes) == int(bytes) {
if len(t.metadataBytes) == size {
return
}
t.metadataBytes = make([]byte, bytes)
t.metadataCompletedChunks = make([]bool, (bytes+(1<<14)-1)/(1<<14))
t.metadataBytes = make([]byte, size)
t.metadataCompletedChunks = make([]bool, (size+(1<<14)-1)/(1<<14))
t.metadataChanged.Broadcast()
for c := range t.conns {
c.requestPendingMetadata()