5
5
"net"
6
6
7
7
ma "github.com/multiformats/go-multiaddr"
8
+ madns "github.com/multiformats/go-multiaddr-dns"
8
9
)
9
10
10
11
var errIncorrectNetAddr = fmt .Errorf ("incorrect network addr conversion" )
@@ -93,11 +94,14 @@ func FromIP(ip net.IP) (ma.Multiaddr, error) {
93
94
return FromIPAndZone (ip , "" )
94
95
}
95
96
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.
97
100
func DialArgs (m ma.Multiaddr ) (string , string , error ) {
98
101
var (
99
102
zone , network , ip , port string
100
103
err error
104
+ hostname bool
101
105
)
102
106
103
107
ma .ForEach (m , func (c ma.Component ) bool {
@@ -123,6 +127,16 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
123
127
network = "ip4"
124
128
ip = c .Value ()
125
129
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
126
140
}
127
141
case "ip4" :
128
142
switch c .Protocol ().Code {
@@ -151,6 +165,7 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
151
165
if err != nil {
152
166
return "" , "" , err
153
167
}
168
+
154
169
switch network {
155
170
case "ip6" :
156
171
if zone != "" {
@@ -165,6 +180,9 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
165
180
if zone != "" {
166
181
ip += "%" + zone
167
182
}
183
+ if hostname {
184
+ return network , ip + ":" + port , nil
185
+ }
168
186
return network , "[" + ip + "]" + ":" + port , nil
169
187
default :
170
188
return "" , "" , fmt .Errorf ("%s is not a 'thin waist' address" , m )
0 commit comments