[#33] network: Implement tool to work with local address

Implement read-only local network address source. Implement function for
check if network address is local.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
support/v0.27
Leonard Lyubich 2020-09-21 17:26:19 +03:00 committed by Alex Vanin
parent d3349a293a
commit 900949c648
1 changed files with 12 additions and 0 deletions

View File

@ -12,6 +12,12 @@ type Address struct {
ma multiaddr.Multiaddr
}
// LocalAddressSource is an interface of local
// network address container with read access.
type LocalAddressSource interface {
LocalAddress() *Address
}
func (a Address) String() string {
return a.ma.String()
}
@ -37,3 +43,9 @@ func AddressFromString(s string) (*Address, error) {
ma: ma,
}, nil
}
// IsLocalAddress returns true if network endpoint from local address
// source is equal to network endpoint of passed address.
func IsLocalAddress(src LocalAddressSource, addr *Address) bool {
return src.LocalAddress().NetAddr() == addr.NetAddr()
}