From 085e676ef079b78e07ba7f2ce711388be121fd82 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sat, 3 Feb 2018 13:36:17 +1100 Subject: [PATCH] Generate default peerExtensionBytes using helpers --- client.go | 2 +- global.go | 17 ++++------------- handshake.go | 7 +++++++ handshake_test.go | 2 +- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/client.go b/client.go index bab9e560..fbdef7e3 100644 --- a/client.go +++ b/client.go @@ -278,7 +278,7 @@ func NewClient(cfg *Config) (cl *Client, err error) { } 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 4b27f2e5..8012fe61 100644 --- a/global.go +++ b/global.go @@ -10,25 +10,16 @@ const ( 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. var ( diff --git a/handshake.go b/handshake.go index 0262e81c..f15f5bbf 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 b477a2df..cc761399 100644 --- a/handshake_test.go +++ b/handshake_test.go @@ -9,7 +9,7 @@ import ( 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()) -- 2.44.0