-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcapsule.go
268 lines (238 loc) · 6.93 KB
/
capsule.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package connectip
import (
"encoding/binary"
"errors"
"fmt"
"io"
"net/netip"
"github.com/quic-go/quic-go/http3"
"github.com/quic-go/quic-go/quicvarint"
)
const (
capsuleTypeAddressAssign http3.CapsuleType = 1
capsuleTypeAddressRequest http3.CapsuleType = 2
capsuleTypeRouteAdvertisement http3.CapsuleType = 3
)
// addressAssignCapsule represents an ADDRESS_ASSIGN capsule
type addressAssignCapsule struct {
AssignedAddresses []AssignedAddress
}
// AssignedAddress represents an Assigned Address within an ADDRESS_ASSIGN capsule
type AssignedAddress struct {
RequestID uint64
IPPrefix netip.Prefix
}
func (a AssignedAddress) len() int {
return quicvarint.Len(a.RequestID) + 1 + a.IPPrefix.Addr().BitLen()/8 + 1
}
// addressRequestCapsule represents an ADDRESS_REQUEST capsule
type addressRequestCapsule struct {
RequestedAddresses []RequestedAddress
}
// RequestedAddress represents an Requested Address within an ADDRESS_REQUEST capsule
type RequestedAddress struct {
RequestID uint64
IPPrefix netip.Prefix
}
func (r RequestedAddress) len() int {
return quicvarint.Len(r.RequestID) + 1 + r.IPPrefix.Addr().BitLen()/8 + 1
}
func parseAddressAssignCapsule(r io.Reader) (*addressAssignCapsule, error) {
var assignedAddresses []AssignedAddress
for {
requestID, prefix, err := parseAddress(r)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
assignedAddresses = append(assignedAddresses, AssignedAddress{RequestID: requestID, IPPrefix: prefix})
}
return &addressAssignCapsule{AssignedAddresses: assignedAddresses}, nil
}
func (c *addressAssignCapsule) append(b []byte) []byte {
totalLen := 0
for _, addr := range c.AssignedAddresses {
totalLen += addr.len()
}
b = quicvarint.Append(b, uint64(capsuleTypeAddressAssign))
b = quicvarint.Append(b, uint64(totalLen))
for _, addr := range c.AssignedAddresses {
b = quicvarint.Append(b, addr.RequestID)
if addr.IPPrefix.Addr().Is4() {
b = append(b, 4)
} else {
b = append(b, 6)
}
b = append(b, addr.IPPrefix.Addr().AsSlice()...)
b = append(b, byte(addr.IPPrefix.Bits()))
}
return b
}
func parseAddressRequestCapsule(r io.Reader) (*addressRequestCapsule, error) {
var requestedAddresses []RequestedAddress
for {
requestID, prefix, err := parseAddress(r)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
requestedAddresses = append(requestedAddresses, RequestedAddress{RequestID: requestID, IPPrefix: prefix})
}
return &addressRequestCapsule{RequestedAddresses: requestedAddresses}, nil
}
func (c *addressRequestCapsule) append(b []byte) []byte {
var totalLen int
for _, addr := range c.RequestedAddresses {
totalLen += addr.len()
}
b = quicvarint.Append(b, uint64(capsuleTypeAddressRequest))
b = quicvarint.Append(b, uint64(totalLen))
for _, addr := range c.RequestedAddresses {
b = quicvarint.Append(b, addr.RequestID)
if addr.IPPrefix.Addr().Is4() {
b = append(b, 4)
} else {
b = append(b, 6)
}
b = append(b, addr.IPPrefix.Addr().AsSlice()...)
b = append(b, byte(addr.IPPrefix.Bits()))
}
return b
}
func parseAddress(r io.Reader) (requestID uint64, prefix netip.Prefix, _ error) {
vr := quicvarint.NewReader(r)
requestID, err := quicvarint.Read(vr)
if err != nil {
return 0, netip.Prefix{}, err
}
ipVersion, err := vr.ReadByte()
if err != nil {
return 0, netip.Prefix{}, err
}
var ip netip.Addr
switch ipVersion {
case 4:
var ipv4 [4]byte
if _, err := io.ReadFull(r, ipv4[:]); err != nil {
return 0, netip.Prefix{}, err
}
ip = netip.AddrFrom4(ipv4)
case 6:
var ipv6 [16]byte
if _, err := io.ReadFull(r, ipv6[:]); err != nil {
return 0, netip.Prefix{}, err
}
ip = netip.AddrFrom16(ipv6)
default:
return 0, netip.Prefix{}, fmt.Errorf("invalid IP version: %d", ipVersion)
}
prefixLen, err := vr.ReadByte()
if err != nil {
return 0, netip.Prefix{}, err
}
if int(prefixLen) > ip.BitLen() {
return 0, netip.Prefix{}, fmt.Errorf("prefix length %d exceeds IP address length (%d)", prefixLen, ip.BitLen())
}
prefix = netip.PrefixFrom(ip, int(prefixLen))
if prefix != prefix.Masked() {
return 0, netip.Prefix{}, errors.New("lower bits not covered by prefix length are not all zero")
}
return requestID, prefix, nil
}
// routeAdvertisementCapsule represents a ROUTE_ADVERTISEMENT capsule
type routeAdvertisementCapsule struct {
IPAddressRanges []IPRoute
}
// IPRoute represents an IP Address Range
type IPRoute struct {
StartIP netip.Addr
EndIP netip.Addr
// IPProtocol is the Internet Protocol Number for traffic that can be sent to this range.
// If the value is 0, all protocols are allowed.
IPProtocol uint8
}
func (r IPRoute) len() int { return 1 + r.StartIP.BitLen()/8 + r.EndIP.BitLen()/8 + 1 }
// Prefixes returns the prefixes that this IP address range covers.
// Note that depending on the start and end addresses,
// this conversion can result in a large number of prefixes.
func (r IPRoute) Prefixes() []netip.Prefix { return rangeToPrefixes(r.StartIP, r.EndIP) }
func parseRouteAdvertisementCapsule(r io.Reader) (*routeAdvertisementCapsule, error) {
var ranges []IPRoute
for {
ipRange, err := parseIPAddressRange(r)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
ranges = append(ranges, ipRange)
}
return &routeAdvertisementCapsule{IPAddressRanges: ranges}, nil
}
func (c *routeAdvertisementCapsule) append(b []byte) []byte {
var totalLen int
for _, ipRange := range c.IPAddressRanges {
totalLen += ipRange.len()
}
b = quicvarint.Append(b, uint64(capsuleTypeRouteAdvertisement))
b = quicvarint.Append(b, uint64(totalLen))
for _, ipRange := range c.IPAddressRanges {
if ipRange.StartIP.Is4() {
b = append(b, 4)
} else {
b = append(b, 6)
}
b = append(b, ipRange.StartIP.AsSlice()...)
b = append(b, ipRange.EndIP.AsSlice()...)
b = append(b, ipRange.IPProtocol)
}
return b
}
func parseIPAddressRange(r io.Reader) (IPRoute, error) {
var ipVersion uint8
if err := binary.Read(r, binary.LittleEndian, &ipVersion); err != nil {
return IPRoute{}, err
}
var startIP, endIP netip.Addr
switch ipVersion {
case 4:
var start, end [4]byte
if _, err := io.ReadFull(r, start[:]); err != nil {
return IPRoute{}, err
}
if _, err := io.ReadFull(r, end[:]); err != nil {
return IPRoute{}, err
}
startIP = netip.AddrFrom4(start)
endIP = netip.AddrFrom4(end)
case 6:
var start, end [16]byte
if _, err := io.ReadFull(r, start[:]); err != nil {
return IPRoute{}, err
}
if _, err := io.ReadFull(r, end[:]); err != nil {
return IPRoute{}, err
}
startIP = netip.AddrFrom16(start)
endIP = netip.AddrFrom16(end)
default:
return IPRoute{}, fmt.Errorf("invalid IP version: %d", ipVersion)
}
if startIP.Compare(endIP) > 0 {
return IPRoute{}, errors.New("start IP is greater than end IP")
}
var ipProtocol uint8
if err := binary.Read(r, binary.LittleEndian, &ipProtocol); err != nil {
return IPRoute{}, err
}
return IPRoute{
StartIP: startIP,
EndIP: endIP,
IPProtocol: ipProtocol,
}, nil
}