From 0bd5f16380b19a655e45302cc141453d4752bf88 Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Thu, 22 Nov 2018 19:53:48 +0100 Subject: [PATCH] Feat: support hostnames in DialArgs() function License: MIT Signed-off-by: Hector Sanjuan --- convert.go | 20 +++++++++++++++++++- convert_test.go | 4 ++++ package.json | 6 ++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/convert.go b/convert.go index 06fb414..c8fbab1 100644 --- a/convert.go +++ b/convert.go @@ -5,6 +5,7 @@ import ( "net" ma "github.com/multiformats/go-multiaddr" + madns "github.com/multiformats/go-multiaddr-dns" ) var errIncorrectNetAddr = fmt.Errorf("incorrect network addr conversion") @@ -93,11 +94,14 @@ func FromIP(ip net.IP) (ma.Multiaddr, error) { return FromIPAndZone(ip, "") } -// DialArgs is a convenience function returning arguments for use in net.Dial +// 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. func DialArgs(m ma.Multiaddr) (string, string, error) { var ( zone, network, ip, port string err error + hostname bool ) ma.ForEach(m, func(c ma.Component) bool { @@ -123,6 +127,16 @@ func DialArgs(m ma.Multiaddr) (string, string, error) { 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 "ip4": switch c.Protocol().Code { @@ -151,6 +165,7 @@ func DialArgs(m ma.Multiaddr) (string, string, error) { if err != nil { return "", "", err } + switch network { case "ip6": if zone != "" { @@ -165,6 +180,9 @@ func DialArgs(m ma.Multiaddr) (string, string, error) { if zone != "" { ip += "%" + zone } + if hostname { + return network, ip + ":" + port, nil + } return network, "[" + ip + "]" + ":" + port, nil default: return "", "", fmt.Errorf("%s is not a 'thin waist' address", m) diff --git a/convert_test.go b/convert_test.go index 105f6b7..33250a0 100644 --- a/convert_test.go +++ b/convert_test.go @@ -168,4 +168,8 @@ func TestDialArgs(t *testing.T) { test_error("/ip6zone/foo/ip4/127.0.0.1") // IP4 doesn't take zone test("/ip6zone/foo/ip6/::1/ip6zone/bar", "ip6", "::1%foo") // IP over IP test_error("/ip6zone/foo/ip6zone/bar/ip6/::1") // Only one zone per IP6 + test("/dns4/abc.com/tcp/1234", "tcp4", "abc.com:1234") // DNS4:port + test("/dns4/abc.com", "ip4", "abc.com") // Just DNS4 + test("/dns6/abc.com/udp/1234", "udp6", "abc.com:1234") // DNS6:port + test("/dns6/abc.com", "ip6", "abc.com") // Just DNS6 } diff --git a/package.json b/package.json index 9808069..b1dd678 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,12 @@ "hash": "QmRKLtwMw131aK7ugC3G7ybpumMz78YrJe5dzneyindvG1", "name": "go-multiaddr", "version": "1.3.6" + }, + { + "author": "lgierth", + "hash": "QmT4zgnKCyZBpRyxzsvZqUjzUkMWLJ2pZCw7uk6M6Kto5m", + "name": "go-multiaddr-dns", + "version": "0.2.6" } ], "gxVersion": "0.6.0",