Commit d859fb0d authored by Can ZHANG's avatar Can ZHANG
Browse files

Track more info for observed addresses

parent 2787133b
...@@ -393,7 +393,8 @@ func (ids *IDService) consumeObservedAddress(observed []byte, c inet.Conn) { ...@@ -393,7 +393,8 @@ func (ids *IDService) consumeObservedAddress(observed []byte, c inet.Conn) {
// ok! we have the observed version of one of our ListenAddresses! // ok! we have the observed version of one of our ListenAddresses!
log.Debugf("added own observed listen addr: %s --> %s", c.LocalMultiaddr(), maddr) log.Debugf("added own observed listen addr: %s --> %s", c.LocalMultiaddr(), maddr)
ids.observedAddrs.Add(maddr, c.RemoteMultiaddr()) ids.observedAddrs.Add(maddr, c.LocalMultiaddr(), c.RemoteMultiaddr(),
c.Stat().Direction)
} }
func addrInAddrs(a ma.Multiaddr, as []ma.Multiaddr) bool { func addrInAddrs(a ma.Multiaddr, as []ma.Multiaddr) bool {
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"sync" "sync"
"time" "time"
net "github.com/libp2p/go-libp2p-net"
pstore "github.com/libp2p/go-libp2p-peerstore" pstore "github.com/libp2p/go-libp2p-peerstore"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
) )
...@@ -16,10 +17,12 @@ const ActivationThresh = 4 ...@@ -16,10 +17,12 @@ const ActivationThresh = 4
// - have been observed at least once recently (1h), because our position in the // - have been observed at least once recently (1h), because our position in the
// network, or network port mapppings, may have changed. // network, or network port mapppings, may have changed.
type ObservedAddr struct { type ObservedAddr struct {
Addr ma.Multiaddr Addr ma.Multiaddr // observed address by peers
SeenBy map[string]time.Time InternalAddr ma.Multiaddr // corresponding internal address
LastSeen time.Time SeenBy map[string]time.Time
Activated bool LastSeen time.Time
ConnDirection net.Direction
Activated bool
} }
func (oa *ObservedAddr) TryActivate(ttl time.Duration) bool { func (oa *ObservedAddr) TryActivate(ttl time.Duration) bool {
...@@ -70,7 +73,9 @@ func (oas *ObservedAddrSet) Addrs() []ma.Multiaddr { ...@@ -70,7 +73,9 @@ func (oas *ObservedAddrSet) Addrs() []ma.Multiaddr {
return addrs return addrs
} }
func (oas *ObservedAddrSet) Add(addr ma.Multiaddr, observer ma.Multiaddr) { func (oas *ObservedAddrSet) Add(observed, local, observer ma.Multiaddr,
direction net.Direction) {
oas.Lock() oas.Lock()
defer oas.Unlock() defer oas.Unlock()
...@@ -80,14 +85,16 @@ func (oas *ObservedAddrSet) Add(addr ma.Multiaddr, observer ma.Multiaddr) { ...@@ -80,14 +85,16 @@ func (oas *ObservedAddrSet) Add(addr ma.Multiaddr, observer ma.Multiaddr) {
oas.ttl = pstore.OwnObservedAddrTTL oas.ttl = pstore.OwnObservedAddrTTL
} }
s := addr.String() s := observed.String()
oa, found := oas.addrs[s] oa, found := oas.addrs[s]
// first time seeing address. // first time seeing address.
if !found { if !found {
oa = &ObservedAddr{ oa = &ObservedAddr{
Addr: addr, Addr: observed,
SeenBy: make(map[string]time.Time), InternalAddr: local,
SeenBy: make(map[string]time.Time),
ConnDirection: direction,
} }
oas.addrs[s] = oa oas.addrs[s] = oa
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment