convert.go 6.92 KB
Newer Older
1
package manet
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
2 3 4 5

import (
	"fmt"
	"net"
Cole Brown's avatar
Cole Brown committed
6
	"path/filepath"
Steven Allen's avatar
Steven Allen committed
7 8
	"runtime"
	"strings"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9

10
	ma "github.com/multiformats/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12 13 14
)

var errIncorrectNetAddr = fmt.Errorf("incorrect network addr conversion")

Jeromy's avatar
Jeromy committed
15 16
// FromNetAddr converts a net.Addr type to a Multiaddr.
func FromNetAddr(a net.Addr) (ma.Multiaddr, error) {
Jeromy's avatar
Jeromy committed
17 18 19
	return defaultCodecs.FromNetAddr(a)
}

20
// FromNetAddr converts a net.Addr to Multiaddress.
Jeromy's avatar
Jeromy committed
21
func (cm *CodecMap) FromNetAddr(a net.Addr) (ma.Multiaddr, error) {
Jeromy's avatar
Jeromy committed
22 23 24
	if a == nil {
		return nil, fmt.Errorf("nil multiaddr")
	}
Jeromy's avatar
Jeromy committed
25
	p, err := cm.getAddrParser(a.Network())
Jeromy's avatar
Jeromy committed
26 27 28
	if err != nil {
		return nil, err
	}
Jeromy's avatar
Jeromy committed
29

Jeromy's avatar
Jeromy committed
30 31
	return p(a)
}
Jeromy's avatar
Jeromy committed
32

Jeromy's avatar
Jeromy committed
33 34 35 36
// 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) {
Jeromy's avatar
Jeromy committed
37 38 39
	return defaultCodecs.ToNetAddr(maddr)
}

40
// ToNetAddr converts a Multiaddress to a standard net.Addr.
Jeromy's avatar
Jeromy committed
41
func (cm *CodecMap) ToNetAddr(maddr ma.Multiaddr) (net.Addr, error) {
Jeromy's avatar
Jeromy committed
42 43
	protos := maddr.Protocols()
	final := protos[len(protos)-1]
Jeromy's avatar
Jeromy committed
44

Jeromy's avatar
Jeromy committed
45
	p, err := cm.getMaddrParser(final.Name)
Jeromy's avatar
Jeromy committed
46 47 48 49 50
	if err != nil {
		return nil, err
	}

	return p(maddr)
Jeromy's avatar
Jeromy committed
51 52
}

Jeromy's avatar
Jeromy committed
53 54 55 56
func parseBasicNetMaddr(maddr ma.Multiaddr) (net.Addr, error) {
	network, host, err := DialArgs(maddr)
	if err != nil {
		return nil, err
Jeromy's avatar
Jeromy committed
57 58
	}

Jeromy's avatar
Jeromy committed
59 60 61 62 63 64 65
	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)
Cole Brown's avatar
Cole Brown committed
66 67
	case "unix":
		return net.ResolveUnixAddr(network, host)
Jeromy's avatar
Jeromy committed
68 69
	}

Jeromy's avatar
Jeromy committed
70
	return nil, fmt.Errorf("network not supported: %s", network)
Jeromy's avatar
Jeromy committed
71 72
}

mwnx's avatar
mwnx committed
73
func FromIPAndZone(ip net.IP, zone string) (ma.Multiaddr, error) {
Jeromy's avatar
Jeromy committed
74 75
	switch {
	case ip.To4() != nil:
mwnx's avatar
mwnx committed
76
		return ma.NewComponent("ip4", ip.String())
Jeromy's avatar
Jeromy committed
77
	case ip.To16() != nil:
mwnx's avatar
mwnx committed
78 79 80 81 82 83 84 85 86 87 88 89 90
		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
		}
Jeromy's avatar
Jeromy committed
91 92 93
	default:
		return nil, errIncorrectNetAddr
	}
mwnx's avatar
mwnx committed
94 95 96 97 98
}

// FromIP converts a net.IP type to a Multiaddr.
func FromIP(ip net.IP) (ma.Multiaddr, error) {
	return FromIPAndZone(ip, "")
Jeromy's avatar
Jeromy committed
99 100
}

Will Scott's avatar
Will Scott committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
// ToIP converts a Multiaddr to a net.IP when possible
func ToIP(addr ma.Multiaddr) (net.IP, error) {
	n, err := ToNetAddr(addr)
	if err != nil {
		return nil, err
	}

	switch netAddr := n.(type) {
	case *net.UDPAddr:
		return netAddr.IP, nil
	case *net.TCPAddr:
		return netAddr.IP, nil
	case *net.IPAddr:
		return netAddr.IP, nil
	default:
		return nil, fmt.Errorf("non IP Multiaddr: %T", netAddr)
	}
}

120 121
// 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
Cole Brown's avatar
Cole Brown committed
122 123
// possible return values (we do not support the unixpacket ones yet). Unix
// addresses do not, at present, compose.
Jeromy's avatar
Jeromy committed
124
func DialArgs(m ma.Multiaddr) (string, string, error) {
Steven Allen's avatar
Steven Allen committed
125 126
	var (
		zone, network, ip, port string
mwnx's avatar
mwnx committed
127
		err                     error
128
		hostname                bool
Steven Allen's avatar
Steven Allen committed
129 130 131 132 133 134 135
	)

	ma.ForEach(m, func(c ma.Component) bool {
		switch network {
		case "":
			switch c.Protocol().Code {
			case ma.P_IP6ZONE:
mwnx's avatar
mwnx committed
136 137 138 139
				if zone != "" {
					err = fmt.Errorf("%s has multiple zones", m)
					return false
				}
Steven Allen's avatar
Steven Allen committed
140 141 142 143 144 145 146
				zone = c.Value()
				return true
			case ma.P_IP6:
				network = "ip6"
				ip = c.Value()
				return true
			case ma.P_IP4:
mwnx's avatar
mwnx committed
147 148 149 150
				if zone != "" {
					err = fmt.Errorf("%s has ip4 with zone", m)
					return false
				}
Steven Allen's avatar
Steven Allen committed
151 152 153
				network = "ip4"
				ip = c.Value()
				return true
154 155 156 157 158
			case ma.P_DNS:
				network = "ip"
				hostname = true
				ip = c.Value()
				return true
159
			case ma.P_DNS4:
160 161 162 163
				network = "ip4"
				hostname = true
				ip = c.Value()
				return true
164
			case ma.P_DNS6:
165 166 167 168
				network = "ip6"
				hostname = true
				ip = c.Value()
				return true
Cole Brown's avatar
Cole Brown committed
169 170 171 172
			case ma.P_UNIX:
				network = "unix"
				ip = c.Value()
				return false
Steven Allen's avatar
Steven Allen committed
173
			}
174 175 176 177 178 179 180 181 182 183
		case "ip":
			switch c.Protocol().Code {
			case ma.P_UDP:
				network = "udp"
			case ma.P_TCP:
				network = "tcp"
			default:
				return false
			}
			port = c.Value()
Steven Allen's avatar
Steven Allen committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
		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
	})
mwnx's avatar
mwnx committed
208 209 210
	if err != nil {
		return "", "", err
	}
211

212 213 214 215 216 217 218 219 220 221 222 223 224
	// If we have a hostname (dns*), we don't want any fancy ipv6 formatting
	// logic (zone, brackets, etc.).
	if hostname {
		switch network {
		case "ip", "ip4", "ip6":
			return network, ip, nil
		case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
			return network, ip + ":" + port, nil
		}
		// Hostname is only true when network is one of the above.
		panic("unreachable")
	}

Steven Allen's avatar
Steven Allen committed
225
	switch network {
Jeromy's avatar
Jeromy committed
226
	case "ip6":
Steven Allen's avatar
Steven Allen committed
227 228 229 230 231 232 233 234 235 236 237 238 239
		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
		}
		return network, "[" + ip + "]" + ":" + port, nil
Cole Brown's avatar
Cole Brown committed
240
	case "unix":
Steven Allen's avatar
Steven Allen committed
241 242 243 244 245
		if runtime.GOOS == "windows" {
			// convert /c:/... to c:\...
			ip = filepath.FromSlash(strings.TrimLeft(ip, "/"))
		}
		return network, ip, nil
Steven Allen's avatar
Steven Allen committed
246 247
	default:
		return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
Jeromy's avatar
Jeromy committed
248
	}
Jeromy's avatar
Jeromy committed
249 250
}

251
func parseTCPNetAddr(a net.Addr) (ma.Multiaddr, error) {
Jeromy's avatar
Jeromy committed
252 253 254 255 256 257
	ac, ok := a.(*net.TCPAddr)
	if !ok {
		return nil, errIncorrectNetAddr
	}

	// Get IP Addr
mwnx's avatar
mwnx committed
258
	ipm, err := FromIPAndZone(ac.IP, ac.Zone)
Jeromy's avatar
Jeromy committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272
	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
}

273
func parseUDPNetAddr(a net.Addr) (ma.Multiaddr, error) {
Jeromy's avatar
Jeromy committed
274 275 276 277 278 279
	ac, ok := a.(*net.UDPAddr)
	if !ok {
		return nil, errIncorrectNetAddr
	}

	// Get IP Addr
mwnx's avatar
mwnx committed
280
	ipm, err := FromIPAndZone(ac.IP, ac.Zone)
Jeromy's avatar
Jeromy committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294
	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
}

295
func parseIPNetAddr(a net.Addr) (ma.Multiaddr, error) {
Jeromy's avatar
Jeromy committed
296 297 298 299
	ac, ok := a.(*net.IPAddr)
	if !ok {
		return nil, errIncorrectNetAddr
	}
mwnx's avatar
mwnx committed
300
	return FromIPAndZone(ac.IP, ac.Zone)
Jeromy's avatar
Jeromy committed
301 302
}

303
func parseIPPlusNetAddr(a net.Addr) (ma.Multiaddr, error) {
Jeromy's avatar
Jeromy committed
304 305 306 307 308 309
	ac, ok := a.(*net.IPNet)
	if !ok {
		return nil, errIncorrectNetAddr
	}
	return FromIP(ac.IP)
}
Cole Brown's avatar
Cole Brown committed
310 311 312 313 314 315

func parseUnixNetAddr(a net.Addr) (ma.Multiaddr, error) {
	ac, ok := a.(*net.UnixAddr)
	if !ok {
		return nil, errIncorrectNetAddr
	}
316

Steven Allen's avatar
Steven Allen committed
317 318 319 320 321 322 323 324 325 326 327
	path := ac.Name
	if runtime.GOOS == "windows" {
		// Convert c:\foobar\... to c:/foobar/...
		path = filepath.ToSlash(path)
	}
	if len(path) == 0 || path[0] != '/' {
		// convert "" and "c:/..." to "/..."
		path = "/" + path
	}

	return ma.NewComponent("unix", path)
Cole Brown's avatar
Cole Brown committed
328
}