2018-07-04 10:51:47 +00:00
|
|
|
package manet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-06-09 07:24:20 +00:00
|
|
|
"path/filepath"
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2019-06-09 07:24:20 +00:00
|
|
|
madns "github.com/multiformats/go-multiaddr-dns"
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var errIncorrectNetAddr = fmt.Errorf("incorrect network addr conversion")
|
|
|
|
|
|
|
|
// FromNetAddr converts a net.Addr type to a Multiaddr.
|
|
|
|
func FromNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
|
|
|
return defaultCodecs.FromNetAddr(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromNetAddr converts a net.Addr to Multiaddress.
|
|
|
|
func (cm *CodecMap) FromNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
|
|
|
if a == nil {
|
|
|
|
return nil, fmt.Errorf("nil multiaddr")
|
|
|
|
}
|
|
|
|
p, err := cm.getAddrParser(a.Network())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToNetAddr converts a Multiaddr to a net.Addr
|
|
|
|
// Must be ThinWaist. acceptable protocol stacks are:
|
|
|
|
// /ip{4,6}/{tcp, udp}
|
|
|
|
func ToNetAddr(maddr ma.Multiaddr) (net.Addr, error) {
|
|
|
|
return defaultCodecs.ToNetAddr(maddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToNetAddr converts a Multiaddress to a standard net.Addr.
|
|
|
|
func (cm *CodecMap) ToNetAddr(maddr ma.Multiaddr) (net.Addr, error) {
|
|
|
|
protos := maddr.Protocols()
|
|
|
|
final := protos[len(protos)-1]
|
|
|
|
|
|
|
|
p, err := cm.getMaddrParser(final.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p(maddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseBasicNetMaddr(maddr ma.Multiaddr) (net.Addr, error) {
|
|
|
|
network, host, err := DialArgs(maddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch network {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
return net.ResolveTCPAddr(network, host)
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
return net.ResolveUDPAddr(network, host)
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
return net.ResolveIPAddr(network, host)
|
2019-06-09 07:24:20 +00:00
|
|
|
case "unix":
|
|
|
|
return net.ResolveUnixAddr(network, host)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("network not supported: %s", network)
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func FromIPAndZone(ip net.IP, zone string) (ma.Multiaddr, error) {
|
2018-07-04 10:51:47 +00:00
|
|
|
switch {
|
|
|
|
case ip.To4() != nil:
|
2019-06-09 07:24:20 +00:00
|
|
|
return ma.NewComponent("ip4", ip.String())
|
2018-07-04 10:51:47 +00:00
|
|
|
case ip.To16() != nil:
|
2019-06-09 07:24:20 +00:00
|
|
|
ip6, err := ma.NewComponent("ip6", ip.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if zone == "" {
|
|
|
|
return ip6, nil
|
|
|
|
} else {
|
|
|
|
zone, err := ma.NewComponent("ip6zone", zone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return zone.Encapsulate(ip6), nil
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
default:
|
|
|
|
return nil, errIncorrectNetAddr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
// FromIP converts a net.IP type to a Multiaddr.
|
|
|
|
func FromIP(ip net.IP) (ma.Multiaddr, error) {
|
|
|
|
return FromIPAndZone(ip, "")
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
// DialArgs is a convenience function that returns network and address as
|
|
|
|
// expected by net.Dial. See https://godoc.org/net#Dial for an overview of
|
|
|
|
// possible return values (we do not support the unixpacket ones yet). Unix
|
|
|
|
// addresses do not, at present, compose.
|
|
|
|
func DialArgs(m ma.Multiaddr) (string, string, error) {
|
|
|
|
var (
|
|
|
|
zone, network, ip, port string
|
|
|
|
err error
|
|
|
|
hostname bool
|
|
|
|
)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
ma.ForEach(m, func(c ma.Component) bool {
|
|
|
|
switch network {
|
|
|
|
case "":
|
|
|
|
switch c.Protocol().Code {
|
|
|
|
case ma.P_IP6ZONE:
|
|
|
|
if zone != "" {
|
|
|
|
err = fmt.Errorf("%s has multiple zones", m)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
zone = c.Value()
|
|
|
|
return true
|
|
|
|
case ma.P_IP6:
|
|
|
|
network = "ip6"
|
|
|
|
ip = c.Value()
|
|
|
|
return true
|
|
|
|
case ma.P_IP4:
|
|
|
|
if zone != "" {
|
|
|
|
err = fmt.Errorf("%s has ip4 with zone", m)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
network = "ip4"
|
|
|
|
ip = c.Value()
|
|
|
|
return true
|
|
|
|
case madns.Dns4Protocol.Code:
|
|
|
|
network = "ip4"
|
|
|
|
hostname = true
|
|
|
|
ip = c.Value()
|
|
|
|
return true
|
|
|
|
case madns.Dns6Protocol.Code:
|
|
|
|
network = "ip6"
|
|
|
|
hostname = true
|
|
|
|
ip = c.Value()
|
|
|
|
return true
|
|
|
|
case ma.P_UNIX:
|
|
|
|
network = "unix"
|
|
|
|
ip = c.Value()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case "ip4":
|
|
|
|
switch c.Protocol().Code {
|
|
|
|
case ma.P_UDP:
|
|
|
|
network = "udp4"
|
|
|
|
case ma.P_TCP:
|
|
|
|
network = "tcp4"
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
port = c.Value()
|
|
|
|
case "ip6":
|
|
|
|
switch c.Protocol().Code {
|
|
|
|
case ma.P_UDP:
|
|
|
|
network = "udp6"
|
|
|
|
case ma.P_TCP:
|
|
|
|
network = "tcp6"
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
port = c.Value()
|
|
|
|
}
|
|
|
|
// Done.
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
switch network {
|
2018-07-04 10:51:47 +00:00
|
|
|
case "ip6":
|
2019-06-09 07:24:20 +00:00
|
|
|
if zone != "" {
|
|
|
|
ip += "%" + zone
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case "ip4":
|
|
|
|
return network, ip, nil
|
|
|
|
case "tcp4", "udp4":
|
|
|
|
return network, ip + ":" + port, nil
|
|
|
|
case "tcp6", "udp6":
|
|
|
|
if zone != "" {
|
|
|
|
ip += "%" + zone
|
|
|
|
}
|
|
|
|
if hostname {
|
|
|
|
return network, ip + ":" + port, nil
|
|
|
|
}
|
|
|
|
return network, "[" + ip + "]" + ":" + port, nil
|
|
|
|
case "unix":
|
|
|
|
return network, ip, nil
|
|
|
|
default:
|
|
|
|
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTCPNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
|
|
|
ac, ok := a.(*net.TCPAddr)
|
|
|
|
if !ok {
|
|
|
|
return nil, errIncorrectNetAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get IP Addr
|
2019-06-09 07:24:20 +00:00
|
|
|
ipm, err := FromIPAndZone(ac.IP, ac.Zone)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errIncorrectNetAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get TCP Addr
|
|
|
|
tcpm, err := ma.NewMultiaddr(fmt.Sprintf("/tcp/%d", ac.Port))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errIncorrectNetAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encapsulate
|
|
|
|
return ipm.Encapsulate(tcpm), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseUDPNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
|
|
|
ac, ok := a.(*net.UDPAddr)
|
|
|
|
if !ok {
|
|
|
|
return nil, errIncorrectNetAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get IP Addr
|
2019-06-09 07:24:20 +00:00
|
|
|
ipm, err := FromIPAndZone(ac.IP, ac.Zone)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errIncorrectNetAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get UDP Addr
|
|
|
|
udpm, err := ma.NewMultiaddr(fmt.Sprintf("/udp/%d", ac.Port))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errIncorrectNetAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encapsulate
|
|
|
|
return ipm.Encapsulate(udpm), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseIPNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
|
|
|
ac, ok := a.(*net.IPAddr)
|
|
|
|
if !ok {
|
|
|
|
return nil, errIncorrectNetAddr
|
|
|
|
}
|
2019-06-09 07:24:20 +00:00
|
|
|
return FromIPAndZone(ac.IP, ac.Zone)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseIPPlusNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
|
|
|
ac, ok := a.(*net.IPNet)
|
|
|
|
if !ok {
|
|
|
|
return nil, errIncorrectNetAddr
|
|
|
|
}
|
|
|
|
return FromIP(ac.IP)
|
|
|
|
}
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
func parseUnixNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
|
|
|
ac, ok := a.(*net.UnixAddr)
|
|
|
|
if !ok {
|
|
|
|
return nil, errIncorrectNetAddr
|
|
|
|
}
|
|
|
|
cleaned := filepath.Clean(ac.Name)
|
|
|
|
return ma.NewComponent("unix", cleaned)
|
|
|
|
}
|