2020-07-24 13:54:03 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
2020-09-21 09:32:19 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr-net"
|
|
|
|
"github.com/pkg/errors"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Address represents the NeoFS node
|
|
|
|
// network address.
|
2020-09-17 09:09:49 +00:00
|
|
|
type Address struct {
|
|
|
|
ma multiaddr.Multiaddr
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:26:19 +00:00
|
|
|
// LocalAddressSource is an interface of local
|
|
|
|
// network address container with read access.
|
|
|
|
type LocalAddressSource interface {
|
|
|
|
LocalAddress() *Address
|
|
|
|
}
|
|
|
|
|
2020-09-17 09:09:49 +00:00
|
|
|
func (a Address) String() string {
|
|
|
|
return a.ma.String()
|
|
|
|
}
|
|
|
|
|
2020-09-21 09:32:19 +00:00
|
|
|
// NetAddr returns network endpoint address in string format.
|
|
|
|
func (a Address) NetAddr() string {
|
|
|
|
ip, err := manet.ToNetAddr(a.ma)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "could not get net addr"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip.String()
|
|
|
|
}
|
|
|
|
|
2020-09-17 09:09:49 +00:00
|
|
|
// AddressFromString restores address from a string representation.
|
|
|
|
func AddressFromString(s string) (*Address, error) {
|
|
|
|
ma, err := multiaddr.NewMultiaddr(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Address{
|
|
|
|
ma: ma,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-09-21 14:26:19 +00:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|