mirror of
https://github.com/logos-messaging/go-multiaddr.git
synced 2026-05-04 00:23:09 +00:00
Refactor ToIP on DialArgs (#69)
Refactor ToIP to use logic from DialArgs address #67 fix #68
This commit is contained in:
parent
fc1542af81
commit
e68f4d6331
109
convert.go
109
convert.go
@ -100,20 +100,19 @@ func FromIP(ip net.IP) (ma.Multiaddr, error) {
|
|||||||
|
|
||||||
// ToIP converts a Multiaddr to a net.IP when possible
|
// ToIP converts a Multiaddr to a net.IP when possible
|
||||||
func ToIP(addr ma.Multiaddr) (net.IP, error) {
|
func ToIP(addr ma.Multiaddr) (net.IP, error) {
|
||||||
n, err := ToNetAddr(addr)
|
_, network, ip, _, hostname, err := dialArgComponents(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch netAddr := n.(type) {
|
if hostname {
|
||||||
case *net.UDPAddr:
|
return nil, fmt.Errorf("non IP Multiaddr: %s %s", network, ip)
|
||||||
return netAddr.IP, nil
|
}
|
||||||
case *net.TCPAddr:
|
switch network {
|
||||||
return netAddr.IP, nil
|
case "ip", "ip4", "ip6", "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
|
||||||
case *net.IPAddr:
|
return net.ParseIP(ip), nil
|
||||||
return netAddr.IP, nil
|
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("non IP Multiaddr: %T", netAddr)
|
return nil, fmt.Errorf("non IP Multiaddr: %s %s", network, ip)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,12 +121,52 @@ func ToIP(addr ma.Multiaddr) (net.IP, error) {
|
|||||||
// possible return values (we do not support the unixpacket ones yet). Unix
|
// possible return values (we do not support the unixpacket ones yet). Unix
|
||||||
// addresses do not, at present, compose.
|
// addresses do not, at present, compose.
|
||||||
func DialArgs(m ma.Multiaddr) (string, string, error) {
|
func DialArgs(m ma.Multiaddr) (string, string, error) {
|
||||||
var (
|
zone, network, ip, port, hostname, err := dialArgComponents(m)
|
||||||
zone, network, ip, port string
|
if err != nil {
|
||||||
err error
|
return "", "", err
|
||||||
hostname bool
|
}
|
||||||
)
|
|
||||||
|
|
||||||
|
// If we have a hostname (dns*), we don't want any fancy ipv6 formatting
|
||||||
|
// logic (zone, brackets, etc.).
|
||||||
|
if hostname {
|
||||||
|
switch network {
|
||||||
|
case "ip", "ip4", "ip6":
|
||||||
|
return network, ip, nil
|
||||||
|
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
|
||||||
|
return network, ip + ":" + port, nil
|
||||||
|
}
|
||||||
|
// Hostname is only true when network is one of the above.
|
||||||
|
panic("unreachable")
|
||||||
|
}
|
||||||
|
|
||||||
|
switch network {
|
||||||
|
case "ip6":
|
||||||
|
if zone != "" {
|
||||||
|
ip += "%" + zone
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case "ip4":
|
||||||
|
return network, ip, nil
|
||||||
|
case "tcp4", "udp4":
|
||||||
|
return network, ip + ":" + port, nil
|
||||||
|
case "tcp6", "udp6":
|
||||||
|
if zone != "" {
|
||||||
|
ip += "%" + zone
|
||||||
|
}
|
||||||
|
return network, "[" + ip + "]" + ":" + port, nil
|
||||||
|
case "unix":
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
// convert /c:/... to c:\...
|
||||||
|
ip = filepath.FromSlash(strings.TrimLeft(ip, "/"))
|
||||||
|
}
|
||||||
|
return network, ip, nil
|
||||||
|
default:
|
||||||
|
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// dialArgComponents extracts the raw pieces used in dialing a Multiaddr
|
||||||
|
func dialArgComponents(m ma.Multiaddr) (zone, network, ip, port string, hostname bool, err error) {
|
||||||
ma.ForEach(m, func(c ma.Component) bool {
|
ma.ForEach(m, func(c ma.Component) bool {
|
||||||
switch network {
|
switch network {
|
||||||
case "":
|
case "":
|
||||||
@ -205,47 +244,7 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
|
|||||||
// Done.
|
// Done.
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
if err != nil {
|
return
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we have a hostname (dns*), we don't want any fancy ipv6 formatting
|
|
||||||
// logic (zone, brackets, etc.).
|
|
||||||
if hostname {
|
|
||||||
switch network {
|
|
||||||
case "ip", "ip4", "ip6":
|
|
||||||
return network, ip, nil
|
|
||||||
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
|
|
||||||
return network, ip + ":" + port, nil
|
|
||||||
}
|
|
||||||
// Hostname is only true when network is one of the above.
|
|
||||||
panic("unreachable")
|
|
||||||
}
|
|
||||||
|
|
||||||
switch network {
|
|
||||||
case "ip6":
|
|
||||||
if zone != "" {
|
|
||||||
ip += "%" + zone
|
|
||||||
}
|
|
||||||
fallthrough
|
|
||||||
case "ip4":
|
|
||||||
return network, ip, nil
|
|
||||||
case "tcp4", "udp4":
|
|
||||||
return network, ip + ":" + port, nil
|
|
||||||
case "tcp6", "udp6":
|
|
||||||
if zone != "" {
|
|
||||||
ip += "%" + zone
|
|
||||||
}
|
|
||||||
return network, "[" + ip + "]" + ":" + port, nil
|
|
||||||
case "unix":
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
// convert /c:/... to c:\...
|
|
||||||
ip = filepath.FromSlash(strings.TrimLeft(ip, "/"))
|
|
||||||
}
|
|
||||||
return network, ip, nil
|
|
||||||
default:
|
|
||||||
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTCPNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
func parseTCPNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user