Unverified Commit ef212b51 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #49 from multiformats/feat/support-dns-in-DialArgs

Feat: support hostnames in DialArgs() function
parents f0af4033 3504e962
......@@ -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 (we do not support the unix* ones yet).
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)
......
......@@ -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
}
......@@ -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",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment