forked from mdlayher/ndp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addr.go
110 lines (93 loc) · 2.66 KB
/
addr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package ndp
import (
"fmt"
"net"
)
// An Addr is an IPv6 unicast address.
type Addr string
// Possible Addr types for an IPv6 unicast address.
const (
Unspecified Addr = "unspecified"
LinkLocal Addr = "linklocal"
UniqueLocal Addr = "uniquelocal"
Global Addr = "global"
)
// chooseAddr selects an Addr from the interface based on the specified Addr type.
func chooseAddr(addrs []net.Addr, zone string, addr Addr) (*net.IPAddr, error) {
// Does the caller want an unspecified address?
if addr == Unspecified {
return &net.IPAddr{
IP: net.IPv6unspecified,
Zone: zone,
}, nil
}
// Select an IPv6 address from the interface's addresses.
var match func(ip net.IP) bool
switch addr {
case LinkLocal:
match = linkLocalUnicastAddr
case UniqueLocal:
match = uniqueLocalUnicastAddr
case Global:
match = globalUnicastAddr
default:
// Special case: try to match Addr as a literal IPv6 address.
ip := net.ParseIP(string(addr))
if ip == nil {
return nil, fmt.Errorf("ndp: invalid IPv6 address: %q", addr)
}
if err := checkIPv6(ip); err != nil {
return nil, err
}
match = func(check net.IP) bool {
return ip.Equal(check)
}
}
return findAddr(addrs, addr, zone, match)
}
// findAddr searches for a valid IPv6 address in the slice of net.Addr that
// matches the input function. If none is found, the IPv6 unspecified address
// "::" is returned.
func findAddr(addrs []net.Addr, addr Addr, zone string, match func(ip net.IP) bool) (*net.IPAddr, error) {
for _, a := range addrs {
ipn, ok := a.(*net.IPNet)
if !ok {
continue
}
if err := checkIPv6(ipn.IP); err != nil {
continue
}
// From here on, we can assume that only IPv6 addresses are
// being checked.
if match(ipn.IP) {
return &net.IPAddr{
IP: ipn.IP,
Zone: zone,
}, nil
}
}
// No matching address on this interface.
return nil, fmt.Errorf("ndp: address %q not found on interface %q", addr, zone)
}
// linkLocalUnicastAddr matches link-local unicast addresses.
func linkLocalUnicastAddr(ip net.IP) bool {
return ip.IsLinkLocalUnicast()
}
// ula is the prefix for unique local unicast addresses.
var ula = func() *net.IPNet {
_, p, err := net.ParseCIDR("fc00::/7")
if err != nil {
panic(fmt.Sprintf("failed to parse unique local address prefix: %v", err))
}
return p
}()
// uniqueLocalUnicastAddr matches unique local unicast addresses.
func uniqueLocalUnicastAddr(ip net.IP) bool {
return ula.Contains(ip)
}
// globalUnicastAddr matches global unicast addresses.
func globalUnicastAddr(ip net.IP) bool {
// Note that IsGlobalUnicast also matches ULAs, so we must exclude
// them specifically.
return !ula.Contains(ip) && ip.IsGlobalUnicast()
}