|
|
@ -55,6 +55,10 @@ type Server struct { |
|
|
|
// Zero defaults to preset values.
|
|
|
|
// Zero defaults to preset values.
|
|
|
|
MaxPendingPeers int |
|
|
|
MaxPendingPeers int |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Discovery specifies whether the peer discovery mechanism should be started
|
|
|
|
|
|
|
|
// or not. Disabling is usually useful for protocol debugging (manual topology).
|
|
|
|
|
|
|
|
Discovery bool |
|
|
|
|
|
|
|
|
|
|
|
// Name sets the node name of this server.
|
|
|
|
// Name sets the node name of this server.
|
|
|
|
// Use common.MakeName to create a name that follows existing conventions.
|
|
|
|
// Use common.MakeName to create a name that follows existing conventions.
|
|
|
|
Name string |
|
|
|
Name string |
|
|
@ -240,6 +244,14 @@ func (srv *Server) Self() *discover.Node { |
|
|
|
if !srv.running { |
|
|
|
if !srv.running { |
|
|
|
return &discover.Node{IP: net.ParseIP("0.0.0.0")} |
|
|
|
return &discover.Node{IP: net.ParseIP("0.0.0.0")} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if srv.ntab == nil { |
|
|
|
|
|
|
|
addr := srv.listener.Addr().(*net.TCPAddr) |
|
|
|
|
|
|
|
return &discover.Node{ |
|
|
|
|
|
|
|
ID: discover.PubkeyID(&srv.PrivateKey.PublicKey), |
|
|
|
|
|
|
|
IP: addr.IP, |
|
|
|
|
|
|
|
TCP: uint16(addr.Port), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
return srv.ntab.Self() |
|
|
|
return srv.ntab.Self() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -290,15 +302,22 @@ func (srv *Server) Start() (err error) { |
|
|
|
srv.peerOpDone = make(chan struct{}) |
|
|
|
srv.peerOpDone = make(chan struct{}) |
|
|
|
|
|
|
|
|
|
|
|
// node table
|
|
|
|
// node table
|
|
|
|
ntab, err := discover.ListenUDP(srv.PrivateKey, srv.ListenAddr, srv.NAT, srv.NodeDatabase) |
|
|
|
if srv.Discovery { |
|
|
|
if err != nil { |
|
|
|
ntab, err := discover.ListenUDP(srv.PrivateKey, srv.ListenAddr, srv.NAT, srv.NodeDatabase) |
|
|
|
return err |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
srv.ntab = ntab |
|
|
|
} |
|
|
|
} |
|
|
|
srv.ntab = ntab |
|
|
|
|
|
|
|
dialer := newDialState(srv.StaticNodes, srv.ntab, srv.MaxPeers/2) |
|
|
|
dynPeers := srv.MaxPeers / 2 |
|
|
|
|
|
|
|
if !srv.Discovery { |
|
|
|
|
|
|
|
dynPeers = 0 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
dialer := newDialState(srv.StaticNodes, srv.ntab, dynPeers) |
|
|
|
|
|
|
|
|
|
|
|
// handshake
|
|
|
|
// handshake
|
|
|
|
srv.ourHandshake = &protoHandshake{Version: baseProtocolVersion, Name: srv.Name, ID: ntab.Self().ID} |
|
|
|
srv.ourHandshake = &protoHandshake{Version: baseProtocolVersion, Name: srv.Name, ID: discover.PubkeyID(&srv.PrivateKey.PublicKey)} |
|
|
|
for _, p := range srv.Protocols { |
|
|
|
for _, p := range srv.Protocols { |
|
|
|
srv.ourHandshake.Caps = append(srv.ourHandshake.Caps, p.cap()) |
|
|
|
srv.ourHandshake.Caps = append(srv.ourHandshake.Caps, p.cap()) |
|
|
|
} |
|
|
|
} |
|
|
@ -454,7 +473,9 @@ running: |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Terminate discovery. If there is a running lookup it will terminate soon.
|
|
|
|
// Terminate discovery. If there is a running lookup it will terminate soon.
|
|
|
|
srv.ntab.Close() |
|
|
|
if srv.ntab != nil { |
|
|
|
|
|
|
|
srv.ntab.Close() |
|
|
|
|
|
|
|
} |
|
|
|
// Disconnect all peers.
|
|
|
|
// Disconnect all peers.
|
|
|
|
for _, p := range peers { |
|
|
|
for _, p := range peers { |
|
|
|
p.Disconnect(DiscQuitting) |
|
|
|
p.Disconnect(DiscQuitting) |
|
|
@ -486,7 +507,7 @@ func (srv *Server) encHandshakeChecks(peers map[discover.NodeID]*Peer, c *conn) |
|
|
|
return DiscTooManyPeers |
|
|
|
return DiscTooManyPeers |
|
|
|
case peers[c.id] != nil: |
|
|
|
case peers[c.id] != nil: |
|
|
|
return DiscAlreadyConnected |
|
|
|
return DiscAlreadyConnected |
|
|
|
case c.id == srv.ntab.Self().ID: |
|
|
|
case c.id == srv.Self().ID: |
|
|
|
return DiscSelf |
|
|
|
return DiscSelf |
|
|
|
default: |
|
|
|
default: |
|
|
|
return nil |
|
|
|
return nil |
|
|
|