client.go | 2 +- global.go | 17 ++++------------- handshake.go | 7 +++++++ handshake_test.go | 2 +- diff --git a/client.go b/client.go index bab9e5601cabb925b42983552421a15d29f0a368..fbdef7e3c8ecc4326e8de1a5ac43b5fe8e7029cd 100644 --- a/client.go +++ b/client.go @@ -278,7 +278,7 @@ cl.downloadLimit = rate.NewLimiter(rate.Inf, 0) } else { cl.downloadLimit = cfg.DownloadRateLimiter } - missinggo.CopyExact(&cl.extensionBytes, defaultExtensionBytes) + cl.extensionBytes = defaultPeerExtensionBytes() cl.event.L = &cl.mu storageImpl := cfg.DefaultStorage if storageImpl == nil { diff --git a/global.go b/global.go index 4b27f2e50b7a2e277e57a2a28157ca12c7c7332d..8012fe6155d1361e1b3fd975232896796ebe4f57 100644 --- a/global.go +++ b/global.go @@ -10,24 +10,15 @@ pieceHash = crypto.SHA1 maxRequests = 250 // Maximum pending requests we allow peers to send us. defaultChunkSize = 0x4000 // 16KiB - // Justification for set bits follows. - // - // Extension protocol ([5]|=0x10): - // http://www.bittorrent.org/beps/bep_0010.html - // - // Fast Extension ([7]|=0x04): - // http://bittorrent.org/beps/bep_0006.html. - // Disabled until AllowedFast is implemented. TODO - // - // DHT ([7]|=1): - // http://www.bittorrent.org/beps/bep_0005.html - defaultExtensionBytes = "\x00\x00\x00\x00\x00\x10\x00\x01" - // These are our extended message IDs. Peers will use these values to // select which extension a message is intended for. metadataExtendedId = iota + 1 // 0 is reserved for deleting keys pexExtendedId ) + +func defaultPeerExtensionBytes() peerExtensionBytes { + return newPeerExtensionBytes(ExtensionBitDHT, ExtensionBitExtended) +} // I could move a lot of these counters to their own file, but I suspect they // may be attached to a Client someday. diff --git a/handshake.go b/handshake.go index 0262e81c67f033e29169678de8c54dab4c94a2ec..f15f5bbf6377221fd95f22a3b9e08ef1d9027d80 100644 --- a/handshake.go +++ b/handshake.go @@ -38,6 +38,13 @@ type ( peerExtensionBytes [8]byte ) +func newPeerExtensionBytes(bits ...ExtensionBit) (ret peerExtensionBytes) { + for _, b := range bits { + ret.SetBit(b) + } + return +} + func (pex peerExtensionBytes) SupportsExtended() bool { return pex.GetBit(ExtensionBitExtended) } diff --git a/handshake_test.go b/handshake_test.go index b477a2dfda062e53f92ae29d5dcf88cf8a30a702..cc761399bd819fd2f87e5314f3b72ddd507f5f84 100644 --- a/handshake_test.go +++ b/handshake_test.go @@ -9,7 +9,7 @@ ) func TestDefaultExtensionBytes(t *testing.T) { var pex peerExtensionBytes - missinggo.CopyExact(&pex, defaultExtensionBytes) + missinggo.CopyExact(&pex, defaultPeerExtensionBytes()) assert.True(t, pex.SupportsDHT()) assert.True(t, pex.SupportsExtended()) assert.False(t, pex.SupportsFast())