forked from PatrickCronin/routesum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routesum.go
144 lines (119 loc) · 3.26 KB
/
routesum.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
135
136
137
138
139
140
141
142
143
144
// Package routesum summarizes a list of IPs and networks to its shortest form.
package routesum
import (
"fmt"
"net/netip"
"strings"
"github.com/PatrickCronin/routesum/pkg/routesum/bitslice"
"github.com/PatrickCronin/routesum/pkg/routesum/rstrie"
"github.com/pkg/errors"
"inet.af/netaddr"
)
// RouteSum has methods supporting route summarization of networks and hosts
type RouteSum struct {
ipv4, ipv6 *rstrie.RSTrie
}
// NewRouteSum returns an initialized RouteSum object
func NewRouteSum() *RouteSum {
rs := new(RouteSum)
rs.ipv4 = rstrie.NewRSTrie()
rs.ipv6 = rstrie.NewRSTrie()
return rs
}
// InsertFromString adds either a string-formatted network or IP to the summary
func (rs *RouteSum) InsertFromString(s string) error {
var ip netip.Addr
var ipBits bitslice.BitSlice
var err error
if strings.Contains(s, "/") {
ipPrefix, err := netip.ParsePrefix(s)
if err != nil {
return fmt.Errorf("parse network: %w", err)
}
if !ipPrefix.IsValid() {
return errors.Errorf("%s is not valid CIDR", s)
}
ip = ipPrefix.Addr()
ipBits, err = ipBitsForIPPrefix(ipPrefix)
if err != nil {
return err
}
} else {
ip, err = netip.ParseAddr(s)
if err != nil {
return fmt.Errorf("parse IP: %w", err)
}
if !ip.IsValid() {
return errors.Errorf("%s is not a valid IP", s)
}
ipBits, err = ipBitsForIP(ip)
if err != nil {
return err
}
}
if ip.Is4() {
rs.ipv4.InsertRoute(ipBits)
} else {
rs.ipv6.InsertRoute(ipBits)
}
return nil
}
func ipBitsForIPPrefix(ipPrefix netip.Prefix) (bitslice.BitSlice, error) {
ipBytes, err := ipPrefix.Addr().MarshalBinary()
if err != nil {
return nil, errors.Wrapf(err, "express %s as bytes", ipPrefix.Addr().String())
}
ipBits, err := bitslice.NewFromBytes(ipBytes)
if err != nil {
return nil, fmt.Errorf("express %s as bits: %w", ipPrefix.Addr().String(), err)
}
return ipBits[:ipPrefix.Bits()], nil
}
func ipBitsForIP(ip netip.Addr) (bitslice.BitSlice, error) {
ipBytes, err := ip.MarshalBinary()
if err != nil {
return nil, errors.Wrapf(err, "express %s as bytes", ip.String())
}
ipBits, err := bitslice.NewFromBytes(ipBytes)
if err != nil {
return nil, fmt.Errorf("express %s as bits: %w", ip.String(), err)
}
return ipBits, nil
}
// SummaryStrings returns a summary of all received routes as a string slice.
func (rs *RouteSum) SummaryStrings() []string {
strs := []string{}
ipv4BitSlices := rs.ipv4.Contents()
for _, bits := range ipv4BitSlices {
ip := ipv4FromBits(bits)
if len(bits) == 8*4 {
strs = append(strs, ip.String())
} else {
ipPrefix := netaddr.IPPrefixFrom(ip, uint8(len(bits)))
strs = append(strs, ipPrefix.String())
}
}
ipv6BitSlices := rs.ipv6.Contents()
for _, bits := range ipv6BitSlices {
ip := ipv6FromBits(bits)
if len(bits) == 8*16 {
strs = append(strs, ip.String())
} else {
ipPrefix := netaddr.IPPrefixFrom(ip, uint8(len(bits)))
strs = append(strs, ipPrefix.String())
}
}
return strs
}
func ipv4FromBits(bits bitslice.BitSlice) netaddr.IP {
bytes := bits.ToBytes(4)
byteArray := [4]byte{}
copy(byteArray[:], bytes[0:4])
return netaddr.IPFrom4(byteArray)
}
func ipv6FromBits(bits bitslice.BitSlice) netaddr.IP {
bytes := bits.ToBytes(16)
byteArray := [16]byte{}
copy(byteArray[:], bytes[0:16])
return netaddr.IPv6Raw(byteArray)
}