From: Matt Joiner Date: Tue, 4 Aug 2015 16:37:43 +0000 (+1000) Subject: dht: Expose SecureNodeId and NodeIdSecure X-Git-Tag: v1.0.0~1081 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=a0cdda16a4017cae4b267bd1367461319cd6674d;p=btrtrc.git dht: Expose SecureNodeId and NodeIdSecure --- diff --git a/dht/dht.go b/dht/dht.go index 17ead0b4..6585a6ce 100644 --- a/dht/dht.go +++ b/dht/dht.go @@ -221,7 +221,7 @@ func (n *node) IsSecure() bool { if n.id.IsUnset() { return false } - return nodeIdSecure(n.id.ByteString(), n.addr.IP()) + return NodeIdSecure(n.id.ByteString(), n.addr.IP()) } func (n *node) idString() string { @@ -521,16 +521,18 @@ func crcIP(ip net.IP, rand uint8) uint32 { return crc32.Checksum(ip[:len(mask)], crc32.MakeTable(crc32.Castagnoli)) } -// Makes a node ID valid, in-place. -func secureNodeId(id []byte, ip net.IP) { +// Makes a node ID secure, in-place. The ID is 20 raw bytes. +// http://www.libtorrent.org/dht_sec.html +func SecureNodeId(id []byte, ip net.IP) { crc := crcIP(ip, id[19]) id[0] = byte(crc >> 24 & 0xff) id[1] = byte(crc >> 16 & 0xff) id[2] = byte(crc>>8&0xf8) | id[2]&7 } -// http://www.libtorrent.org/dht_sec.html -func nodeIdSecure(id string, ip net.IP) bool { +// Returns whether the node ID is considered secure. The id is the 20 raw +// bytes. http://www.libtorrent.org/dht_sec.html +func NodeIdSecure(id string, ip net.IP) bool { if len(id) != 20 { panic(fmt.Sprintf("%q", id)) } @@ -573,7 +575,7 @@ func (s *Server) setDefaults() (err error) { return missinggo.AddrIP(s.socket.LocalAddr()) } }() - secureNodeId(id[:], publicIP) + SecureNodeId(id[:], publicIP) s.id = string(id[:]) } s.nodes = make(map[string]*node, maxNodes) diff --git a/dht/dht_test.go b/dht/dht_test.go index a797a606..e1583460 100644 --- a/dht/dht_test.go +++ b/dht/dht_test.go @@ -188,13 +188,13 @@ func TestDHTSec(t *testing.T) { if err != nil { t.Fatal(err) } - secure := nodeIdSecure(string(id), ip) + secure := NodeIdSecure(string(id), ip) if secure != case_.valid { t.Fatalf("case failed: %v", case_) } if !secure { - secureNodeId(id, ip) - if !nodeIdSecure(string(id), ip) { + SecureNodeId(id, ip) + if !NodeIdSecure(string(id), ip) { t.Fatal("failed to secure node id") } } @@ -207,7 +207,7 @@ func TestServerDefaultNodeIdSecure(t *testing.T) { t.Fatal(err) } defer s.Close() - if !nodeIdSecure(s.ID(), missinggo.AddrIP(s.Addr())) { + if !NodeIdSecure(s.ID(), missinggo.AddrIP(s.Addr())) { t.Fatal("not secure") } }