Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
ef51fdbe
Commit
ef51fdbe
authored
Sep 07, 2018
by
Can ZHANG
Browse files
Deduplicate addresses of host output
parent
521cd9ea
Changes
1
Hide whitespace changes
Inline
Side-by-side
p2p/host/basic/basic_host.go
View file @
ef51fdbe
...
...
@@ -469,26 +469,41 @@ func (h *BasicHost) Addrs() []ma.Multiaddr {
return
h
.
addrs
(
h
.
AllAddrs
())
}
// mergeAddrs merges input address lists, leave only unique addresses
func
mergeAddrs
(
addrLists
...
[]
ma
.
Multiaddr
)
(
uniqueAddrs
[]
ma
.
Multiaddr
)
{
exists
:=
make
(
map
[
string
]
bool
)
for
_
,
addrList
:=
range
addrLists
{
for
_
,
addr
:=
range
addrList
{
if
exists
[
addr
.
String
()]
{
continue
}
exists
[
addr
.
String
()]
=
true
uniqueAddrs
=
append
(
uniqueAddrs
,
addr
)
}
}
return
uniqueAddrs
}
// AllAddrs returns all the addresses of BasicHost at this moment in time.
// It's ok to not include addresses if they're not available to be used now.
func
(
h
*
BasicHost
)
AllAddrs
()
[]
ma
.
Multiaddr
{
a
ddrs
,
err
:=
h
.
Network
()
.
InterfaceListenAddresses
()
listenA
ddrs
,
err
:=
h
.
Network
()
.
InterfaceListenAddresses
()
if
err
!=
nil
{
log
.
Debug
(
"error retrieving network interface addrs"
)
}
if
h
.
ids
!=
nil
{
// add external observed addresses
addrs
=
append
(
addrs
,
h
.
ids
.
OwnObservedAddrs
()
...
)
var
observedAddrs
[]
ma
.
Multiaddr
if
h
.
ids
!=
nil
{
// peer observed addresses
observedAddrs
=
h
.
ids
.
OwnObservedAddrs
()
}
if
h
.
natmgr
!=
nil
{
// natmgr is nil if we do not use nat option.
nat
:=
h
.
natmgr
.
NAT
()
if
nat
!=
nil
{
// nat is nil if not ready, or no nat is available.
addrs
=
append
(
addrs
,
nat
.
ExternalAddrs
()
...
)
}
var
natAddrs
[]
ma
.
Multiaddr
// natmgr is nil if we do not use nat option;
// h.natmgr.NAT() is nil if not ready, or no nat is available.
if
h
.
natmgr
!=
nil
&&
h
.
natmgr
.
NAT
()
!=
nil
{
natAddrs
=
h
.
natmgr
.
NAT
()
.
ExternalAddrs
()
}
return
a
ddrs
return
mergeAddrs
(
listenAddrs
,
observedAddrs
,
natA
ddrs
)
}
// Close shuts down the Host's services (network, etc).
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment