ip.go 2.66 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package manet

import (
Steven Allen's avatar
Steven Allen committed
4
	"net"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5

6
	ma "github.com/multiformats/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 8 9 10 11 12 13 14 15 16
)

// Loopback Addresses
var (
	// IP4Loopback is the ip4 loopback multiaddr
	IP4Loopback = ma.StringCast("/ip4/127.0.0.1")

	// IP6Loopback is the ip6 loopback multiaddr
	IP6Loopback = ma.StringCast("/ip6/::1")

Steven Allen's avatar
Steven Allen committed
17 18
	// IP4MappedIP6Loopback is the IPv4 Mapped IPv6 loopback address.
	IP4MappedIP6Loopback = ma.StringCast("/ip6/::ffff:127.0.0.1")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19 20 21 22 23 24 25 26 27 28 29
)

// Unspecified Addresses (used for )
var (
	IP4Unspecified = ma.StringCast("/ip4/0.0.0.0")
	IP6Unspecified = ma.StringCast("/ip6/::")
)

// IsThinWaist returns whether a Multiaddr starts with "Thin Waist" Protocols.
// This means: /{IP4, IP6}[/{TCP, UDP}]
func IsThinWaist(m ma.Multiaddr) bool {
mwnx's avatar
mwnx committed
30 31 32 33
	m = zoneless(m)
	if m == nil {
		return false
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	p := m.Protocols()

	// nothing? not even a waist.
	if len(p) == 0 {
		return false
	}

	if p[0].Code != ma.P_IP4 && p[0].Code != ma.P_IP6 {
		return false
	}

	// only IP? still counts.
	if len(p) == 1 {
		return true
	}

	switch p[1].Code {
	case ma.P_TCP, ma.P_UDP, ma.P_IP4, ma.P_IP6:
		return true
	default:
		return false
	}
}

58 59 60
// IsIPLoopback returns whether a Multiaddr starts with a "Loopback" IP address
// This means either /ip4/127.*.*.*/*, /ip6/::1/*, or /ip6/::ffff:127.*.*.*.*/*,
// or /ip6zone/<any value>/ip6/<one of the preceding ip6 values>/*
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61
func IsIPLoopback(m ma.Multiaddr) bool {
mwnx's avatar
mwnx committed
62
	m = zoneless(m)
63
	c, _ := ma.SplitFirst(m)
mwnx's avatar
mwnx committed
64 65 66
	if c == nil {
		return false
	}
Steven Allen's avatar
Steven Allen committed
67 68 69
	switch c.Protocol().Code {
	case ma.P_IP4, ma.P_IP6:
		return net.IP(c.RawValue()).IsLoopback()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70 71 72 73
	}
	return false
}

mwnx's avatar
mwnx committed
74 75 76
// IsIP6LinkLocal returns whether a Multiaddr starts with an IPv6 link-local
// multiaddress (with zero or one leading zone). These addresses are non
// routable.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77
func IsIP6LinkLocal(m ma.Multiaddr) bool {
mwnx's avatar
mwnx committed
78 79 80 81 82 83 84
	m = zoneless(m)
	c, _ := ma.SplitFirst(m)
	if c == nil || c.Protocol().Code != ma.P_IP6 {
		return false
	}
	ip := net.IP(c.RawValue())
	return ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast()
85 86
}

87 88
// IsIPUnspecified returns whether a Multiaddr starts with an Unspecified IP address
// This means either /ip4/0.0.0.0/* or /ip6/::/*
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
89
func IsIPUnspecified(m ma.Multiaddr) bool {
mwnx's avatar
mwnx committed
90 91 92 93
	m = zoneless(m)
	if m == nil {
		return false
	}
94 95
	c, _ := ma.SplitFirst(m)
	return net.IP(c.RawValue()).IsUnspecified()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96
}
mwnx's avatar
mwnx committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

// If m matches [zone,ip6,...], return [ip6,...]
// else if m matches [], [zone], or [zone,...], return nil
// else return m
func zoneless(m ma.Multiaddr) ma.Multiaddr {
	head, tail := ma.SplitFirst(m)
	if head == nil {
		return nil
	}
	if head.Protocol().Code == ma.P_IP6ZONE {
		if tail == nil {
			return nil
		}
		tailhead, _ := ma.SplitFirst(tail)
		if tailhead.Protocol().Code != ma.P_IP6 {
			return nil
		}
		return tail
	} else {
		return m
	}
}