dht: Expose SecureNodeId and NodeIdSecure

This commit is contained in:
Matt Joiner 2015-08-05 02:37:43 +10:00
parent 92e1e505b4
commit a0cdda16a4
2 changed files with 12 additions and 10 deletions

View File

@ -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)

View File

@ -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")
}
}