-
Notifications
You must be signed in to change notification settings - Fork 28
/
errors.go
134 lines (122 loc) · 3.17 KB
/
errors.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package mmdbwriter
import (
"bytes"
"errors"
"fmt"
"net"
"net/netip"
)
// AliasedNetworkError is returned when inserting a aliased network into
// a Tree where DisableIPv4Aliasing in Options is false.
type AliasedNetworkError struct {
// AliasedNetwork is the aliased network being inserted into.
AliasedNetwork netip.Prefix
// InsertedNetwork is the network being inserted into the Tree.
InsertedNetwork netip.Prefix
}
func newAliasedNetworkError(netIP net.IP, curPrefixLen, recPrefixLen int) error {
anErr := &AliasedNetworkError{}
ip, ok := netip.AddrFromSlice(netIP)
if !ok {
return errors.Join(
fmt.Errorf("creating netip.Addr from %s", netIP),
anErr,
)
}
var err error
// We are using netip here despite using net.IP/net.IPNet internally as
// it seems quite likely that we will switch to netip throughout.
anErr.InsertedNetwork, err = ip.Prefix(recPrefixLen)
if err != nil {
return errors.Join(
fmt.Errorf(
"creating prefix from addr %s and prefix length %d: %w",
ip,
recPrefixLen,
err,
),
anErr,
)
}
anErr.AliasedNetwork, err = ip.Prefix(curPrefixLen)
if err != nil {
return errors.Join(
fmt.Errorf(
"creating prefix from addr %s and prefix length %d: %w",
ip,
curPrefixLen,
err,
),
anErr,
)
}
return anErr
}
func (r *AliasedNetworkError) Error() string {
return fmt.Sprintf(
"attempt to insert %s into %s, which is an aliased network",
r.InsertedNetwork,
r.AliasedNetwork,
)
}
// ReservedNetworkError is returned when inserting a reserved network into
// a Tree where IncludeReservedNetworks in Options is false.
type ReservedNetworkError struct {
// InsertedNetwork is the network being inserted into the Tree.
InsertedNetwork netip.Prefix
// ReservedNetwork is the reserved network being inserted into.
ReservedNetwork netip.Prefix
}
var _ error = &ReservedNetworkError{}
var ipv4Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
func newReservedNetworkError(netIP net.IP, curPrefixLen, recPrefixLen int) error {
// Check if we are in the IPv4 subtree. If so, convert everything to IPv4.
if bytes.HasPrefix(netIP, ipv4Prefix) && curPrefixLen > 96 && recPrefixLen > 96 {
netIP = netIP[12:]
curPrefixLen -= 96
recPrefixLen -= 96
}
rnErr := &ReservedNetworkError{}
ip, ok := netip.AddrFromSlice(netIP)
if !ok {
return errors.Join(
fmt.Errorf("creating netip.Addr from %s", netIP),
rnErr,
)
}
var err error
// We are using netip here despite using net.IP/net.IPNet internally as
// it seems quite likely that we will switch to netip throughout.
rnErr.InsertedNetwork, err = ip.Prefix(recPrefixLen)
if err != nil {
return errors.Join(
fmt.Errorf(
"creating prefix from addr %s and prefix length %d: %w",
ip,
recPrefixLen,
err,
),
rnErr,
)
}
rnErr.ReservedNetwork, err = ip.Prefix(curPrefixLen)
if err != nil {
return errors.Join(
fmt.Errorf(
"creating prefix from addr %s and prefix length %d: %w",
ip,
curPrefixLen,
err,
),
rnErr,
)
}
return rnErr
}
func (r *ReservedNetworkError) Error() string {
return fmt.Sprintf(
"attempt to insert %s into %s, which is a reserved network",
r.InsertedNetwork,
r.ReservedNetwork,
)
}