@@ -6,59 +6,62 @@ import (
66 "encoding/hex"
77 "errors"
88 "fmt"
9- "io"
109 "math/big"
1110 "math/rand"
1211 "net"
1312 "net/url"
1413 "strconv"
1514 "strings"
1615
16+ "github.com/ethereum/go-ethereum/common"
1717 "github.com/ethereum/go-ethereum/crypto"
1818 "github.com/ethereum/go-ethereum/crypto/secp256k1"
19- "github.com/ethereum/go-ethereum/rlp"
2019)
2120
2221const nodeIDBits = 512
2322
2423// Node represents a host on the network.
2524type Node struct {
26- ID NodeID
27- IP net.IP
25+ IP net.IP // len 4 for IPv4 or 16 for IPv6
26+ UDP , TCP uint16 // port numbers
27+ ID NodeID // the node's public key
2828
29- DiscPort int // UDP listening port for discovery protocol
30- TCPPort int // TCP listening port for RLPx
29+ // This is a cached copy of sha3(ID) which is used for node
30+ // distance calculations. This is part of Node in order to make it
31+ // possible to write tests that need a node at a certain distance.
32+ // In those tests, the content of sha will not actually correspond
33+ // with ID.
34+ sha common.Hash
3135}
3236
33- func newNode (id NodeID , addr * net.UDPAddr ) * Node {
37+ func newNode (id NodeID , ip net.IP , udpPort , tcpPort uint16 ) * Node {
38+ if ipv4 := ip .To4 (); ipv4 != nil {
39+ ip = ipv4
40+ }
3441 return & Node {
35- ID : id ,
36- IP : addr .IP ,
37- DiscPort : addr .Port ,
38- TCPPort : addr .Port ,
42+ IP : ip ,
43+ UDP : udpPort ,
44+ TCP : tcpPort ,
45+ ID : id ,
46+ sha : crypto .Sha3Hash (id [:]),
3947 }
4048}
4149
42- func (n * Node ) isValid () bool {
43- // TODO: don't accept localhost, LAN addresses from internet hosts
44- return ! n .IP .IsMulticast () && ! n .IP .IsUnspecified () && n .TCPPort != 0 && n .DiscPort != 0
45- }
46-
4750func (n * Node ) addr () * net.UDPAddr {
48- return & net.UDPAddr {IP : n .IP , Port : n . DiscPort }
51+ return & net.UDPAddr {IP : n .IP , Port : int ( n . UDP ) }
4952}
5053
5154// The string representation of a Node is a URL.
5255// Please see ParseNode for a description of the format.
5356func (n * Node ) String () string {
54- addr := net.TCPAddr {IP : n .IP , Port : n . TCPPort }
57+ addr := net.TCPAddr {IP : n .IP , Port : int ( n . TCP ) }
5558 u := url.URL {
5659 Scheme : "enode" ,
5760 User : url .User (fmt .Sprintf ("%x" , n .ID [:])),
5861 Host : addr .String (),
5962 }
60- if n .DiscPort != n .TCPPort {
61- u .RawQuery = "discport=" + strconv .Itoa (n . DiscPort )
63+ if n .UDP != n .TCP {
64+ u .RawQuery = "discport=" + strconv .Itoa (int ( n . UDP ) )
6265 }
6366 return u .String ()
6467}
@@ -80,36 +83,47 @@ func (n *Node) String() string {
8083//
8184// enode://<hex node id>@10.3.58.6:30303?discport=30301
8285func ParseNode (rawurl string ) (* Node , error ) {
83- var n Node
86+ var (
87+ id NodeID
88+ ip net.IP
89+ tcpPort , udpPort uint64
90+ )
8491 u , err := url .Parse (rawurl )
8592 if u .Scheme != "enode" {
8693 return nil , errors .New ("invalid URL scheme, want \" enode\" " )
8794 }
95+ // Parse the Node ID from the user portion.
8896 if u .User == nil {
8997 return nil , errors .New ("does not contain node ID" )
9098 }
91- if n . ID , err = HexID (u .User .String ()); err != nil {
99+ if id , err = HexID (u .User .String ()); err != nil {
92100 return nil , fmt .Errorf ("invalid node ID (%v)" , err )
93101 }
94- ip , port , err := net .SplitHostPort (u .Host )
102+ // Parse the IP address.
103+ host , port , err := net .SplitHostPort (u .Host )
95104 if err != nil {
96105 return nil , fmt .Errorf ("invalid host: %v" , err )
97106 }
98- if n . IP = net .ParseIP (ip ); n . IP == nil {
107+ if ip = net .ParseIP (host ); ip == nil {
99108 return nil , errors .New ("invalid IP address" )
100109 }
101- if n .TCPPort , err = strconv .Atoi (port ); err != nil {
110+ // Ensure the IP is 4 bytes long for IPv4 addresses.
111+ if ipv4 := ip .To4 (); ipv4 != nil {
112+ ip = ipv4
113+ }
114+ // Parse the port numbers.
115+ if tcpPort , err = strconv .ParseUint (port , 10 , 16 ); err != nil {
102116 return nil , errors .New ("invalid port" )
103117 }
118+ udpPort = tcpPort
104119 qv := u .Query ()
105- if qv .Get ("discport" ) == "" {
106- n .DiscPort = n .TCPPort
107- } else {
108- if n .DiscPort , err = strconv .Atoi (qv .Get ("discport" )); err != nil {
120+ if qv .Get ("discport" ) != "" {
121+ udpPort , err = strconv .ParseUint (qv .Get ("discport" ), 10 , 16 )
122+ if err != nil {
109123 return nil , errors .New ("invalid discport in query" )
110124 }
111125 }
112- return & n , nil
126+ return newNode ( id , ip , uint16 ( udpPort ), uint16 ( tcpPort )) , nil
113127}
114128
115129// MustParseNode parses a node URL. It panics if the URL is not valid.
@@ -121,22 +135,6 @@ func MustParseNode(rawurl string) *Node {
121135 return n
122136}
123137
124- func (n Node ) EncodeRLP (w io.Writer ) error {
125- return rlp .Encode (w , rpcNode {IP : n .IP .String (), Port : uint16 (n .TCPPort ), ID : n .ID })
126- }
127- func (n * Node ) DecodeRLP (s * rlp.Stream ) (err error ) {
128- var ext rpcNode
129- if err = s .Decode (& ext ); err == nil {
130- n .TCPPort = int (ext .Port )
131- n .DiscPort = int (ext .Port )
132- n .ID = ext .ID
133- if n .IP = net .ParseIP (ext .IP ); n .IP == nil {
134- return errors .New ("invalid IP string" )
135- }
136- }
137- return err
138- }
139-
140138// NodeID is a unique identifier for each node.
141139// The node identifier is a marshaled elliptic curve public key.
142140type NodeID [nodeIDBits / 8 ]byte
@@ -221,7 +219,7 @@ func recoverNodeID(hash, sig []byte) (id NodeID, err error) {
221219// distcmp compares the distances a->target and b->target.
222220// Returns -1 if a is closer to target, 1 if b is closer to target
223221// and 0 if they are equal.
224- func distcmp (target , a , b NodeID ) int {
222+ func distcmp (target , a , b common. Hash ) int {
225223 for i := range target {
226224 da := a [i ] ^ target [i ]
227225 db := b [i ] ^ target [i ]
@@ -271,7 +269,7 @@ var lzcount = [256]int{
271269}
272270
273271// logdist returns the logarithmic distance between a and b, log2(a ^ b).
274- func logdist (a , b NodeID ) int {
272+ func logdist (a , b common. Hash ) int {
275273 lz := 0
276274 for i := range a {
277275 x := a [i ] ^ b [i ]
@@ -285,8 +283,8 @@ func logdist(a, b NodeID) int {
285283 return len (a )* 8 - lz
286284}
287285
288- // randomID returns a random NodeID such that logdist(a, b) == n
289- func randomID (a NodeID , n int ) (b NodeID ) {
286+ // hashAtDistance returns a random hash such that logdist(a, b) == n
287+ func hashAtDistance (a common. Hash , n int ) (b common. Hash ) {
290288 if n == 0 {
291289 return a
292290 }
0 commit comments