Skip to content
This repository was archived by the owner on Oct 5, 2021. It is now read-only.

Commit e68f4d6

Browse files
authored
Refactor ToIP on DialArgs (#69)
Refactor ToIP to use logic from DialArgs address #67 fix #68
1 parent fc1542a commit e68f4d6

File tree

1 file changed

+54
-55
lines changed

1 file changed

+54
-55
lines changed

convert.go

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,19 @@ func FromIP(ip net.IP) (ma.Multiaddr, error) {
100100

101101
// ToIP converts a Multiaddr to a net.IP when possible
102102
func ToIP(addr ma.Multiaddr) (net.IP, error) {
103-
n, err := ToNetAddr(addr)
103+
_, network, ip, _, hostname, err := dialArgComponents(addr)
104104
if err != nil {
105105
return nil, err
106106
}
107107

108-
switch netAddr := n.(type) {
109-
case *net.UDPAddr:
110-
return netAddr.IP, nil
111-
case *net.TCPAddr:
112-
return netAddr.IP, nil
113-
case *net.IPAddr:
114-
return netAddr.IP, nil
108+
if hostname {
109+
return nil, fmt.Errorf("non IP Multiaddr: %s %s", network, ip)
110+
}
111+
switch network {
112+
case "ip", "ip4", "ip6", "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
113+
return net.ParseIP(ip), nil
115114
default:
116-
return nil, fmt.Errorf("non IP Multiaddr: %T", netAddr)
115+
return nil, fmt.Errorf("non IP Multiaddr: %s %s", network, ip)
117116
}
118117
}
119118

@@ -122,12 +121,52 @@ func ToIP(addr ma.Multiaddr) (net.IP, error) {
122121
// possible return values (we do not support the unixpacket ones yet). Unix
123122
// addresses do not, at present, compose.
124123
func DialArgs(m ma.Multiaddr) (string, string, error) {
125-
var (
126-
zone, network, ip, port string
127-
err error
128-
hostname bool
129-
)
124+
zone, network, ip, port, hostname, err := dialArgComponents(m)
125+
if err != nil {
126+
return "", "", err
127+
}
128+
129+
// If we have a hostname (dns*), we don't want any fancy ipv6 formatting
130+
// logic (zone, brackets, etc.).
131+
if hostname {
132+
switch network {
133+
case "ip", "ip4", "ip6":
134+
return network, ip, nil
135+
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
136+
return network, ip + ":" + port, nil
137+
}
138+
// Hostname is only true when network is one of the above.
139+
panic("unreachable")
140+
}
141+
142+
switch network {
143+
case "ip6":
144+
if zone != "" {
145+
ip += "%" + zone
146+
}
147+
fallthrough
148+
case "ip4":
149+
return network, ip, nil
150+
case "tcp4", "udp4":
151+
return network, ip + ":" + port, nil
152+
case "tcp6", "udp6":
153+
if zone != "" {
154+
ip += "%" + zone
155+
}
156+
return network, "[" + ip + "]" + ":" + port, nil
157+
case "unix":
158+
if runtime.GOOS == "windows" {
159+
// convert /c:/... to c:\...
160+
ip = filepath.FromSlash(strings.TrimLeft(ip, "/"))
161+
}
162+
return network, ip, nil
163+
default:
164+
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
165+
}
166+
}
130167

168+
// dialArgComponents extracts the raw pieces used in dialing a Multiaddr
169+
func dialArgComponents(m ma.Multiaddr) (zone, network, ip, port string, hostname bool, err error) {
131170
ma.ForEach(m, func(c ma.Component) bool {
132171
switch network {
133172
case "":
@@ -205,47 +244,7 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
205244
// Done.
206245
return false
207246
})
208-
if err != nil {
209-
return "", "", err
210-
}
211-
212-
// If we have a hostname (dns*), we don't want any fancy ipv6 formatting
213-
// logic (zone, brackets, etc.).
214-
if hostname {
215-
switch network {
216-
case "ip", "ip4", "ip6":
217-
return network, ip, nil
218-
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
219-
return network, ip + ":" + port, nil
220-
}
221-
// Hostname is only true when network is one of the above.
222-
panic("unreachable")
223-
}
224-
225-
switch network {
226-
case "ip6":
227-
if zone != "" {
228-
ip += "%" + zone
229-
}
230-
fallthrough
231-
case "ip4":
232-
return network, ip, nil
233-
case "tcp4", "udp4":
234-
return network, ip + ":" + port, nil
235-
case "tcp6", "udp6":
236-
if zone != "" {
237-
ip += "%" + zone
238-
}
239-
return network, "[" + ip + "]" + ":" + port, nil
240-
case "unix":
241-
if runtime.GOOS == "windows" {
242-
// convert /c:/... to c:\...
243-
ip = filepath.FromSlash(strings.TrimLeft(ip, "/"))
244-
}
245-
return network, ip, nil
246-
default:
247-
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
248-
}
247+
return
249248
}
250249

251250
func parseTCPNetAddr(a net.Addr) (ma.Multiaddr, error) {

0 commit comments

Comments
 (0)