dht/dht.go | 14 ++++++++------ dht/dht_test.go | 8 ++++---- diff --git a/dht/dht.go b/dht/dht.go index 17ead0b4505a70004f305ff57837fc19f58eebde..6585a6ce24d0c6b794dbb94a410a8a508355616b 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 @@ ip[0] |= r << 5 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 @@ } else { 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 a797a60610edde5b503f7df57e90a7601dd9f465..e158346024c48c676a94983e1823cfd2b173df8c 100644 --- a/dht/dht_test.go +++ b/dht/dht_test.go @@ -188,13 +188,13 @@ id, err := hex.DecodeString(case_.nodeIDHex) 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 @@ if err != nil { 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") } }