From 900949c6484d7c168b5f904210c8bf5a607befc8 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 21 Sep 2020 17:26:19 +0300 Subject: [PATCH] [#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 --- pkg/network/address.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/network/address.go b/pkg/network/address.go index e581578ce..d1a430e1e 100644 --- a/pkg/network/address.go +++ b/pkg/network/address.go @@ -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() +}