mirror of
https://github.com/waku-org/go-multiaddr.git
synced 2025-02-23 11:38:20 +00:00
Merge pull request #49 from multiformats/feat/support-dns-in-DialArgs
Feat: support hostnames in DialArgs() function
This commit is contained in:
commit
ef212b5194
20
convert.go
20
convert.go
@ -5,6 +5,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
|
|
||||||
ma "github.com/multiformats/go-multiaddr"
|
ma "github.com/multiformats/go-multiaddr"
|
||||||
|
madns "github.com/multiformats/go-multiaddr-dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
var errIncorrectNetAddr = fmt.Errorf("incorrect network addr conversion")
|
var errIncorrectNetAddr = fmt.Errorf("incorrect network addr conversion")
|
||||||
@ -93,11 +94,14 @@ func FromIP(ip net.IP) (ma.Multiaddr, error) {
|
|||||||
return FromIPAndZone(ip, "")
|
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) {
|
func DialArgs(m ma.Multiaddr) (string, string, error) {
|
||||||
var (
|
var (
|
||||||
zone, network, ip, port string
|
zone, network, ip, port string
|
||||||
err error
|
err error
|
||||||
|
hostname bool
|
||||||
)
|
)
|
||||||
|
|
||||||
ma.ForEach(m, func(c ma.Component) bool {
|
ma.ForEach(m, func(c ma.Component) bool {
|
||||||
@ -123,6 +127,16 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
|
|||||||
network = "ip4"
|
network = "ip4"
|
||||||
ip = c.Value()
|
ip = c.Value()
|
||||||
return true
|
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":
|
case "ip4":
|
||||||
switch c.Protocol().Code {
|
switch c.Protocol().Code {
|
||||||
@ -151,6 +165,7 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch network {
|
switch network {
|
||||||
case "ip6":
|
case "ip6":
|
||||||
if zone != "" {
|
if zone != "" {
|
||||||
@ -165,6 +180,9 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
|
|||||||
if zone != "" {
|
if zone != "" {
|
||||||
ip += "%" + zone
|
ip += "%" + zone
|
||||||
}
|
}
|
||||||
|
if hostname {
|
||||||
|
return network, ip + ":" + port, nil
|
||||||
|
}
|
||||||
return network, "[" + ip + "]" + ":" + port, nil
|
return network, "[" + ip + "]" + ":" + port, nil
|
||||||
default:
|
default:
|
||||||
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
|
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_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("/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_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",
|
"hash": "QmRKLtwMw131aK7ugC3G7ybpumMz78YrJe5dzneyindvG1",
|
||||||
"name": "go-multiaddr",
|
"name": "go-multiaddr",
|
||||||
"version": "1.3.6"
|
"version": "1.3.6"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"author": "lgierth",
|
||||||
|
"hash": "QmT4zgnKCyZBpRyxzsvZqUjzUkMWLJ2pZCw7uk6M6Kto5m",
|
||||||
|
"name": "go-multiaddr-dns",
|
||||||
|
"version": "0.2.6"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"gxVersion": "0.6.0",
|
"gxVersion": "0.6.0",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user