]> Sergey Matveev's repositories - btrtrc.git/commitdiff
dht: Expose SecureNodeId and NodeIdSecure
authorMatt Joiner <anacrolix@gmail.com>
Tue, 4 Aug 2015 16:37:43 +0000 (02:37 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 4 Aug 2015 16:37:43 +0000 (02:37 +1000)
dht/dht.go
dht/dht_test.go

index 17ead0b4505a70004f305ff57837fc19f58eebde..6585a6ce24d0c6b794dbb94a410a8a508355616b 100644 (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)
index a797a60610edde5b503f7df57e90a7601dd9f465..e158346024c48c676a94983e1823cfd2b173df8c 100644 (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")
        }
 }