Initial working Kademlia network - peer list exchange is working!

Using the announcement and response messages to exchange peer lists. Fixes in encoding.
Add arbitrary Info structure to DHT Lite/Node.
This commit is contained in:
Kleissner
2021-03-15 22:25:05 +01:00
parent 323f379ee7
commit 3080119fc1
13 changed files with 210 additions and 33 deletions

View File

@@ -40,6 +40,11 @@ func (c *Connection) Equal(other *Connection) bool {
return c.Address.IP.Equal(other.Address.IP) && c.Network.address.IP.Equal(other.Network.address.IP)
}
// IsLocal checks if the connection is a local network one (LAN)
func (c *Connection) IsLocal() bool {
return IsIPLocal(c.Address.IP)
}
// GetConnections returns the list of connections
func (peer *PeerInfo) GetConnections(active bool) (connections []*Connection) {
peer.RLock()
@@ -51,6 +56,25 @@ func (peer *PeerInfo) GetConnections(active bool) (connections []*Connection) {
return peer.connectionInactive
}
// GetConnection2Share returns a connection to share. Nil if none.
// allowLocal specifies whether it is OK to return local IPs.
func (peer *PeerInfo) GetConnection2Share(allowLocal bool) (connections *Connection) {
peer.RLock()
defer peer.RUnlock()
if peer.connectionLatest != nil && !(!allowLocal && peer.connectionLatest.IsLocal()) {
return peer.connectionLatest
}
for _, connection := range peer.connectionActive {
if !(!allowLocal && connection.IsLocal()) {
return connection
}
}
return nil
}
// registerConnection registers an incoming connection for an existing peer. If new, it will add to the list. If previously inactive, it will elevate.
func (peer *PeerInfo) registerConnection(incoming *Connection) (result *Connection) {
peer.Lock()