Skip to content

Commit

Permalink
p2p/proto/identify: use observed listen addrs
Browse files Browse the repository at this point in the history
This commit finally makes use of the sent observed addrs.
If the connection's local address is from one of our
listen addrs, then the remote's observed addr is its
natted mapping, which is useful to us. For now, we add
it directly to our address book. (a future commit should
make addressbook addresses expire)
  • Loading branch information
jbenet committed Jan 19, 2015
1 parent 083db2b commit e379943
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c inet.Conn) {
p := c.RemotePeer()

// mes.Protocols

// mes.ObservedAddr
ids.consumeObservedAddress(mes.GetObservedAddr(), c)

// mes.ListenAddrs
laddrs := mes.GetListenAddrs()
Expand Down Expand Up @@ -208,3 +210,44 @@ func (ids *IDService) IdentifyWait(c inet.Conn) <-chan struct{} {
close(ch)
return ch
}

func (ids *IDService) consumeObservedAddress(observed []byte, c inet.Conn) {
if observed == nil {
return
}

maddr, err := ma.NewMultiaddrBytes(observed)
if err != nil {
log.Debugf("error parsing received observed addr for %s: %s", c, err)
return
}

// we should only use ObservedAddr when our connection's LocalAddr is one
// of our ListenAddrs. If we Dial out using an ephemeral addr, knowing that
// address's external mapping is not very useful because the port will not be
// the same as the listen addr.
ifaceaddrs, err := ids.Host.Network().InterfaceListenAddresses()
if err != nil {
log.Infof("failed to get interface listen addrs", err)
return
}

log.Debugf("identify identifying observed multiaddr: %s %s", c.LocalMultiaddr(), ifaceaddrs)
if !addrInAddrs(c.LocalMultiaddr(), ifaceaddrs) {
// not in our list
return
}

// ok! we have the observed version of one of our ListenAddresses!
log.Debugf("added own observed listen addr: %s --> %s", c.LocalMultiaddr(), maddr)
ids.Host.Peerstore().AddAddress(ids.Host.ID(), maddr)
}

func addrInAddrs(a ma.Multiaddr, as []ma.Multiaddr) bool {
for _, b := range as {
if a.Equal(b) {
return true
}
}
return false
}

0 comments on commit e379943

Please sign in to comment.