Skip to content

Commit 0bd5f16

Browse files
committed
Feat: support hostnames in DialArgs() function
License: MIT Signed-off-by: Hector Sanjuan <[email protected]>
1 parent f0af403 commit 0bd5f16

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

convert.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net"
66

77
ma "github.com/multiformats/go-multiaddr"
8+
madns "github.com/multiformats/go-multiaddr-dns"
89
)
910

1011
var errIncorrectNetAddr = fmt.Errorf("incorrect network addr conversion")
@@ -93,11 +94,14 @@ func FromIP(ip net.IP) (ma.Multiaddr, error) {
9394
return FromIPAndZone(ip, "")
9495
}
9596

96-
// DialArgs is a convenience function returning arguments for use in net.Dial
97+
// DialArgs is a convenience function that returns network and address as
98+
// expected by net.Dial. See https://godoc.org/net#Dial for an overview of
99+
// possible return values.
97100
func DialArgs(m ma.Multiaddr) (string, string, error) {
98101
var (
99102
zone, network, ip, port string
100103
err error
104+
hostname bool
101105
)
102106

103107
ma.ForEach(m, func(c ma.Component) bool {
@@ -123,6 +127,16 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
123127
network = "ip4"
124128
ip = c.Value()
125129
return true
130+
case madns.Dns4Protocol.Code:
131+
network = "ip4"
132+
hostname = true
133+
ip = c.Value()
134+
return true
135+
case madns.Dns6Protocol.Code:
136+
network = "ip6"
137+
hostname = true
138+
ip = c.Value()
139+
return true
126140
}
127141
case "ip4":
128142
switch c.Protocol().Code {
@@ -151,6 +165,7 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
151165
if err != nil {
152166
return "", "", err
153167
}
168+
154169
switch network {
155170
case "ip6":
156171
if zone != "" {
@@ -165,6 +180,9 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
165180
if zone != "" {
166181
ip += "%" + zone
167182
}
183+
if hostname {
184+
return network, ip + ":" + port, nil
185+
}
168186
return network, "[" + ip + "]" + ":" + port, nil
169187
default:
170188
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)

convert_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,8 @@ func TestDialArgs(t *testing.T) {
168168
test_error("/ip6zone/foo/ip4/127.0.0.1") // IP4 doesn't take zone
169169
test("/ip6zone/foo/ip6/::1/ip6zone/bar", "ip6", "::1%foo") // IP over IP
170170
test_error("/ip6zone/foo/ip6zone/bar/ip6/::1") // Only one zone per IP6
171+
test("/dns4/abc.com/tcp/1234", "tcp4", "abc.com:1234") // DNS4:port
172+
test("/dns4/abc.com", "ip4", "abc.com") // Just DNS4
173+
test("/dns6/abc.com/udp/1234", "udp6", "abc.com:1234") // DNS6:port
174+
test("/dns6/abc.com", "ip6", "abc.com") // Just DNS6
171175
}

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
"hash": "QmRKLtwMw131aK7ugC3G7ybpumMz78YrJe5dzneyindvG1",
1313
"name": "go-multiaddr",
1414
"version": "1.3.6"
15+
},
16+
{
17+
"author": "lgierth",
18+
"hash": "QmT4zgnKCyZBpRyxzsvZqUjzUkMWLJ2pZCw7uk6M6Kto5m",
19+
"name": "go-multiaddr-dns",
20+
"version": "0.2.6"
1521
}
1622
],
1723
"gxVersion": "0.6.0",

0 commit comments

Comments
 (0)