Skip to content

Commit

Permalink
multi: ensure addresses are no longer assumed to be TCP addresses only
Browse files Browse the repository at this point in the history
In this commit, we go through the codebase looking for TCP address
assumptions and modifying them to include the recently introduced onion
addresses. This enables us to fully support onion addresses within the
daemon.
  • Loading branch information
wpaulino committed Jun 5, 2018
1 parent 08f80b6 commit 2e0484b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
4 changes: 1 addition & 3 deletions channeldb/nodes.go
Expand Up @@ -54,8 +54,6 @@ type LinkNode struct {
// Addresses is a list of IP address in which either we were able to
// reach the node over in the past, OR we received an incoming
// authenticated connection for the stored identity public key.
//
// TODO(roasbeef): also need to support hidden service addrs
Addresses []net.Addr

db *DB
Expand Down Expand Up @@ -85,7 +83,7 @@ func (l *LinkNode) UpdateLastSeen(lastSeen time.Time) error {

// AddAddress appends the specified TCP address to the list of known addresses
// this node is/was known to be reachable at.
func (l *LinkNode) AddAddress(addr *net.TCPAddr) error {
func (l *LinkNode) AddAddress(addr net.Addr) error {
for _, a := range l.Addresses {
if a.String() == addr.String() {
return nil
Expand Down
14 changes: 7 additions & 7 deletions discovery/bootstrapper.go
Expand Up @@ -165,12 +165,12 @@ func (c *ChannelGraphBootstrapper) SampleNodeAddrs(numAddrs uint32,
// If we haven't yet reached our limit, then
// we'll copy over the details of this node
// into the set of addresses to be returned.
tcpAddr, ok := nodeAddr.(*net.TCPAddr)
if !ok {
// If this isn't a valid TCP address,
// then we'll ignore it as currently
// we'll only attempt to connect out to
// TCP peers.
switch nodeAddr.(type) {
case *net.TCPAddr, *tor.OnionAddr:
default:
// If this isn't a valid address
// supported by the protocol, then we'll
// skip this node.
return nil
}

Expand All @@ -179,7 +179,7 @@ func (c *ChannelGraphBootstrapper) SampleNodeAddrs(numAddrs uint32,
// error.
a = append(a, &lnwire.NetAddress{
IdentityKey: node.PubKey(),
Address: tcpAddr,
Address: nodeAddr,
})
}

Expand Down
17 changes: 6 additions & 11 deletions pilot.go
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/autopilot"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/tor"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/wire"
"github.com/roasbeef/btcutil"
Expand Down Expand Up @@ -49,18 +50,12 @@ func (c *chanController) OpenChannel(target *btcec.PublicKey,
// advertised IP addresses, or have made a connection.
var connected bool
for _, addr := range addrs {
// If the address doesn't already have a port, then
// we'll assume the current default port.
tcpAddr, ok := addr.(*net.TCPAddr)
if !ok {
return fmt.Errorf("TCP address required instead "+
"have %T", addr)
switch addr.(type) {
case *net.TCPAddr, *tor.OnionAddr:
lnAddr.Address = addr
default:
return fmt.Errorf("unknown address type %T", addr)
}
if tcpAddr.Port == 0 {
tcpAddr.Port = defaultPeerPort
}

lnAddr.Address = tcpAddr

// TODO(roasbeef): make perm connection in server after
// chan open?
Expand Down
40 changes: 21 additions & 19 deletions server.go
Expand Up @@ -1016,17 +1016,7 @@ func (s *server) establishPersistentConnections() error {
return err
}
for _, node := range linkNodes {
for _, address := range node.Addresses {
switch addr := address.(type) {
case *net.TCPAddr:
if addr.Port == 0 {
addr.Port = defaultPeerPort
}
}

}
pubStr := string(node.IdentityPub.SerializeCompressed())

nodeAddrs := &nodeAddresses{
pubKey: node.IdentityPub,
addresses: node.Addresses,
Expand Down Expand Up @@ -1059,17 +1049,29 @@ func (s *server) establishPersistentConnections() error {
linkNodeAddrs, ok := nodeAddrsMap[pubStr]
if ok {
for _, lnAddress := range linkNodeAddrs.addresses {
lnAddrTCP, ok := lnAddress.(*net.TCPAddr)
if !ok {
var addrHost string
switch addr := lnAddress.(type) {
case *net.TCPAddr:
addrHost = addr.IP.String()
case *tor.OnionAddr:
addrHost = addr.OnionService
default:
continue
}

var addrMatched bool
for _, polAddress := range policy.Node.Addresses {
polTCPAddr, ok := polAddress.(*net.TCPAddr)
if ok && polTCPAddr.IP.Equal(lnAddrTCP.IP) {
addrMatched = true
addrs = append(addrs, polTCPAddr)
switch addr := polAddress.(type) {
case *net.TCPAddr:
if addr.IP.String() == addrHost {
addrMatched = true
addrs = append(addrs, addr)
}
case *tor.OnionAddr:
if addr.OnionService == addrHost {
addrMatched = true
addrs = append(addrs, addr)
}
}
}
if !addrMatched {
Expand All @@ -1078,9 +1080,9 @@ func (s *server) establishPersistentConnections() error {
}
} else {
for _, addr := range policy.Node.Addresses {
polTCPAddr, ok := addr.(*net.TCPAddr)
if ok {
addrs = append(addrs, polTCPAddr)
switch addr.(type) {
case *net.TCPAddr, *tor.OnionAddr:
addrs = append(addrs, addr)
}
}
}
Expand Down

0 comments on commit 2e0484b

Please sign in to comment.