mirror of
https://github.com/waku-org/go-multiaddr.git
synced 2025-02-23 19:48:18 +00:00
Merge pull request #47 from multiformats/feat/use-mulitaddr-parsers
use multiaddr parsers
This commit is contained in:
commit
2503d99944
90
convert.go
90
convert.go
@ -3,7 +3,6 @@ package manet
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
|
||||||
|
|
||||||
ma "github.com/multiformats/go-multiaddr"
|
ma "github.com/multiformats/go-multiaddr"
|
||||||
)
|
)
|
||||||
@ -68,43 +67,82 @@ func parseBasicNetMaddr(maddr ma.Multiaddr) (net.Addr, error) {
|
|||||||
|
|
||||||
// FromIP converts a net.IP type to a Multiaddr.
|
// FromIP converts a net.IP type to a Multiaddr.
|
||||||
func FromIP(ip net.IP) (ma.Multiaddr, error) {
|
func FromIP(ip net.IP) (ma.Multiaddr, error) {
|
||||||
|
var proto string
|
||||||
switch {
|
switch {
|
||||||
case ip.To4() != nil:
|
case ip.To4() != nil:
|
||||||
return ma.NewMultiaddr("/ip4/" + ip.String())
|
proto = "ip4"
|
||||||
case ip.To16() != nil:
|
case ip.To16() != nil:
|
||||||
return ma.NewMultiaddr("/ip6/" + ip.String())
|
proto = "ip6"
|
||||||
default:
|
default:
|
||||||
return nil, errIncorrectNetAddr
|
return nil, errIncorrectNetAddr
|
||||||
}
|
}
|
||||||
|
return ma.NewComponent(proto, ip.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialArgs is a convenience function returning arguments for use in net.Dial
|
// DialArgs is a convenience function returning arguments for use in net.Dial
|
||||||
func DialArgs(m ma.Multiaddr) (string, string, error) {
|
func DialArgs(m ma.Multiaddr) (string, string, error) {
|
||||||
// TODO: find a 'good' way to eliminate the function.
|
var (
|
||||||
// My preference is with a multiaddr.Format(...) function
|
zone, network, ip, port string
|
||||||
if !IsThinWaist(m) {
|
)
|
||||||
|
|
||||||
|
ma.ForEach(m, func(c ma.Component) bool {
|
||||||
|
switch network {
|
||||||
|
case "":
|
||||||
|
switch c.Protocol().Code {
|
||||||
|
case ma.P_IP6ZONE:
|
||||||
|
zone = c.Value()
|
||||||
|
return true
|
||||||
|
case ma.P_IP6:
|
||||||
|
network = "ip6"
|
||||||
|
ip = c.Value()
|
||||||
|
return true
|
||||||
|
case ma.P_IP4:
|
||||||
|
network = "ip4"
|
||||||
|
ip = c.Value()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case "ip4":
|
||||||
|
switch c.Protocol().Code {
|
||||||
|
case ma.P_UDP:
|
||||||
|
network = "udp4"
|
||||||
|
case ma.P_TCP:
|
||||||
|
network = "tcp4"
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
port = c.Value()
|
||||||
|
case "ip6":
|
||||||
|
switch c.Protocol().Code {
|
||||||
|
case ma.P_UDP:
|
||||||
|
network = "udp6"
|
||||||
|
case ma.P_TCP:
|
||||||
|
network = "tcp6"
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
port = c.Value()
|
||||||
|
}
|
||||||
|
// Done.
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
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
|
||||||
|
default:
|
||||||
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
|
return "", "", fmt.Errorf("%s is not a 'thin waist' address", m)
|
||||||
}
|
}
|
||||||
|
|
||||||
str := m.String()
|
|
||||||
parts := strings.Split(str, "/")[1:]
|
|
||||||
|
|
||||||
if len(parts) == 2 { // only IP
|
|
||||||
return parts[0], parts[1], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
network := parts[2]
|
|
||||||
|
|
||||||
var host string
|
|
||||||
switch parts[0] {
|
|
||||||
case "ip4":
|
|
||||||
network = network + "4"
|
|
||||||
host = strings.Join([]string{parts[1], parts[3]}, ":")
|
|
||||||
case "ip6":
|
|
||||||
network = network + "6"
|
|
||||||
host = fmt.Sprintf("[%s]:%s", parts[1], parts[3])
|
|
||||||
}
|
|
||||||
return network, host, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTCPNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
func parseTCPNetAddr(a net.Addr) (ma.Multiaddr, error) {
|
||||||
|
@ -141,4 +141,10 @@ func TestDialArgs(t *testing.T) {
|
|||||||
test("/ip4/127.0.0.1/tcp/4321", "tcp4", "127.0.0.1:4321")
|
test("/ip4/127.0.0.1/tcp/4321", "tcp4", "127.0.0.1:4321")
|
||||||
test("/ip6/::1/udp/1234", "udp6", "[::1]:1234")
|
test("/ip6/::1/udp/1234", "udp6", "[::1]:1234")
|
||||||
test("/ip6/::1/tcp/4321", "tcp6", "[::1]:4321")
|
test("/ip6/::1/tcp/4321", "tcp6", "[::1]:4321")
|
||||||
|
test("/ip6/::1", "ip6", "::1") // Just an IP
|
||||||
|
test("/ip4/1.2.3.4", "ip4", "1.2.3.4") // Just an IP
|
||||||
|
test("/ip6zone/foo/ip6/::1/tcp/4321", "tcp6", "[::1%foo]:4321") // zone
|
||||||
|
test("/ip6zone/foo/ip6/::1", "ip6", "::1%foo") // no TCP
|
||||||
|
test("/ip6zone/foo/ip6/::1/ip6zone/bar", "ip6", "::1%foo") // IP over IP
|
||||||
|
test("/ip6zone/foo/ip4/127.0.0.1/ip6zone/bar", "ip4", "127.0.0.1") // Skip zones in IP
|
||||||
}
|
}
|
||||||
|
51
ip.go
51
ip.go
@ -1,7 +1,7 @@
|
|||||||
package manet
|
package manet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"net"
|
||||||
|
|
||||||
ma "github.com/multiformats/go-multiaddr"
|
ma "github.com/multiformats/go-multiaddr"
|
||||||
)
|
)
|
||||||
@ -24,14 +24,6 @@ var (
|
|||||||
IP6Unspecified = ma.StringCast("/ip6/::")
|
IP6Unspecified = ma.StringCast("/ip6/::")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Loopback multiaddr prefixes. Any multiaddr beginning with one of the
|
|
||||||
// following byte sequences is considered a loopback multiaddr.
|
|
||||||
var loopbackPrefixes = [][]byte{
|
|
||||||
{ma.P_IP4, 127}, // 127.*
|
|
||||||
{ma.P_IP6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 127}, // ::ffff:127.*
|
|
||||||
IP6Loopback.Bytes(), // ::1
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsThinWaist returns whether a Multiaddr starts with "Thin Waist" Protocols.
|
// IsThinWaist returns whether a Multiaddr starts with "Thin Waist" Protocols.
|
||||||
// This means: /{IP4, IP6}[/{TCP, UDP}]
|
// This means: /{IP4, IP6}[/{TCP, UDP}]
|
||||||
func IsThinWaist(m ma.Multiaddr) bool {
|
func IsThinWaist(m ma.Multiaddr) bool {
|
||||||
@ -62,22 +54,41 @@ func IsThinWaist(m ma.Multiaddr) bool {
|
|||||||
// IsIPLoopback returns whether a Multiaddr is a "Loopback" IP address
|
// IsIPLoopback returns whether a Multiaddr is a "Loopback" IP address
|
||||||
// This means either /ip4/127.*.*.*, /ip6/::1, or /ip6/::ffff:127.*.*.*.*
|
// This means either /ip4/127.*.*.*, /ip6/::1, or /ip6/::ffff:127.*.*.*.*
|
||||||
func IsIPLoopback(m ma.Multiaddr) bool {
|
func IsIPLoopback(m ma.Multiaddr) bool {
|
||||||
b := m.Bytes()
|
c, rest := ma.SplitFirst(m)
|
||||||
for _, prefix := range loopbackPrefixes {
|
if rest != nil {
|
||||||
if bytes.HasPrefix(b, prefix) {
|
// Not *just* an IPv4 addr
|
||||||
return true
|
return false
|
||||||
}
|
}
|
||||||
|
switch c.Protocol().Code {
|
||||||
|
case ma.P_IP4, ma.P_IP6:
|
||||||
|
return net.IP(c.RawValue()).IsLoopback()
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsIP6LinkLocal returns if a multiaddress is an IPv6 local link. These
|
// IsIP6LinkLocal returns if a an IPv6 link-local multiaddress (with zero or
|
||||||
// addresses are non routable. The prefix is technically
|
// more leading zones). These addresses are non routable.
|
||||||
// fe80::/10, but we test fe80::/16 for simplicity (no need to mask).
|
|
||||||
// So far, no hardware interfaces exist long enough to use those 2 bits.
|
|
||||||
// Send a PR if there is.
|
|
||||||
func IsIP6LinkLocal(m ma.Multiaddr) bool {
|
func IsIP6LinkLocal(m ma.Multiaddr) bool {
|
||||||
return bytes.HasPrefix(m.Bytes(), []byte{ma.P_IP6, 0xfe, 0x80})
|
matched := false
|
||||||
|
ma.ForEach(m, func(c ma.Component) bool {
|
||||||
|
// Too much.
|
||||||
|
if matched {
|
||||||
|
matched = false
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch c.Protocol().Code {
|
||||||
|
case ma.P_IP6ZONE:
|
||||||
|
return true
|
||||||
|
case ma.P_IP6:
|
||||||
|
ip := net.IP(c.RawValue())
|
||||||
|
matched = ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast()
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return matched
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsIPUnspecified returns whether a Multiaddr is am Unspecified IP address
|
// IsIPUnspecified returns whether a Multiaddr is am Unspecified IP address
|
||||||
|
@ -306,7 +306,7 @@ func TestIPLoopback(t *testing.T) {
|
|||||||
t.Error("IP6Loopback incorrect:", IP6Loopback)
|
t.Error("IP6Loopback incorrect:", IP6Loopback)
|
||||||
}
|
}
|
||||||
|
|
||||||
if IP4MappedIP6Loopback.String() != "/ip6/127.0.0.1" {
|
if IP4MappedIP6Loopback.String() != "/ip6/::ffff:127.0.0.1" {
|
||||||
t.Error("IP4MappedIP6Loopback incorrect:", IP4MappedIP6Loopback)
|
t.Error("IP4MappedIP6Loopback incorrect:", IP4MappedIP6Loopback)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -322,6 +322,10 @@ func TestIPLoopback(t *testing.T) {
|
|||||||
t.Error("IsIPLoopback false positive (/ip4/112.123.11.1)")
|
t.Error("IsIPLoopback false positive (/ip4/112.123.11.1)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if IsIPLoopback(newMultiaddr(t, "/ip4/127.0.0.1/ip4/127.0.0.1")) {
|
||||||
|
t.Error("IsIPLoopback false positive (/ip4/127.0.0.1/127.0.0.1)")
|
||||||
|
}
|
||||||
|
|
||||||
if IsIPLoopback(newMultiaddr(t, "/ip4/192.168.0.1/ip6/::1")) {
|
if IsIPLoopback(newMultiaddr(t, "/ip4/192.168.0.1/ip6/::1")) {
|
||||||
t.Error("IsIPLoopback false positive (/ip4/192.168.0.1/ip6/::1)")
|
t.Error("IsIPLoopback false positive (/ip4/192.168.0.1/ip6/::1)")
|
||||||
}
|
}
|
||||||
@ -363,7 +367,7 @@ func TestIPUnspecified(t *testing.T) {
|
|||||||
|
|
||||||
func TestIP6LinkLocal(t *testing.T) {
|
func TestIP6LinkLocal(t *testing.T) {
|
||||||
for a := 0; a < 65536; a++ {
|
for a := 0; a < 65536; a++ {
|
||||||
isLinkLocal := (a == 0xfe80)
|
isLinkLocal := (a&0xffc0 == 0xfe80 || a&0xff0f == 0xff02)
|
||||||
m := newMultiaddr(t, fmt.Sprintf("/ip6/%x::1", a))
|
m := newMultiaddr(t, fmt.Sprintf("/ip6/%x::1", a))
|
||||||
if IsIP6LinkLocal(m) != isLinkLocal {
|
if IsIP6LinkLocal(m) != isLinkLocal {
|
||||||
t.Errorf("IsIP6LinkLocal failed (%s != %v)", m, isLinkLocal)
|
t.Errorf("IsIP6LinkLocal failed (%s != %v)", m, isLinkLocal)
|
||||||
|
@ -9,9 +9,9 @@
|
|||||||
"gxDependencies": [
|
"gxDependencies": [
|
||||||
{
|
{
|
||||||
"author": "multiformats",
|
"author": "multiformats",
|
||||||
"hash": "QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7",
|
"hash": "QmT4U94DnD8FRfqr21obWY32HLM5VExccPKMjQHofeYqr9",
|
||||||
"name": "go-multiaddr",
|
"name": "go-multiaddr",
|
||||||
"version": "1.3.0"
|
"version": "1.3.5"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"gxVersion": "0.6.0",
|
"gxVersion": "0.6.0",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user