-
Notifications
You must be signed in to change notification settings - Fork 2
/
ext-com-common.go
165 lines (139 loc) · 3.13 KB
/
ext-com-common.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
package attrs
import (
"bytes"
"net/netip"
"strconv"
"github.com/bgpfix/bgpfix/caps"
"github.com/bgpfix/bgpfix/json"
)
// The basic (raw) Extended Community value
type ExtcomRaw struct {
uint64
}
func NewExtcomRaw(et ExtcomType) ExtcomValue {
return &ExtcomRaw{}
}
func (e *ExtcomRaw) Unmarshal(src uint64) error {
e.uint64 = src
return nil
}
func (e *ExtcomRaw) Marshal(cps caps.Caps) uint64 {
return uint64(e.uint64 & 0x0000ffffffffffff)
}
func (e *ExtcomRaw) ToJSON(dst []byte) []byte {
dst = append(dst, `"0x`...)
dst = strconv.AppendUint(dst, uint64(e.uint64&0x0000ffffffffffff), 16)
return append(dst, '"')
}
func (e *ExtcomRaw) FromJSON(src []byte) error {
v, err := strconv.ParseUint(json.SQ(src), 0, 48)
if err == nil {
e.uint64 = v
}
return err
}
// 2- or 4-byte ASN-specific value
type ExtcomASN struct {
et ExtcomType
ASN uint32
Value uint32
}
func NewExtcomASN(et ExtcomType) ExtcomValue {
return &ExtcomASN{et: et}
}
func (e *ExtcomASN) Unmarshal(raw uint64) error {
if e.et.Type()&EXTCOM_AS4 != 0 {
e.ASN = uint32(raw >> 16)
e.Value = uint32(raw & 0xffff)
} else {
e.ASN = uint32(raw >> 32)
e.Value = uint32(raw)
}
return nil
}
func (e *ExtcomASN) Marshal(cps caps.Caps) uint64 {
var raw uint64
if e.et.Type()&EXTCOM_AS4 != 0 {
raw |= uint64(e.ASN) << 16
raw |= uint64(e.Value & 0xffff)
} else {
raw |= uint64(e.ASN) << 32
raw |= uint64(e.Value)
}
return raw
}
func (e *ExtcomASN) ToJSON(dst []byte) []byte {
dst = append(dst, '"')
dst = strconv.AppendUint(dst, uint64(e.ASN), 10)
dst = append(dst, ':')
dst = strconv.AppendUint(dst, uint64(e.Value), 10)
dst = append(dst, '"')
return dst
}
func (e *ExtcomASN) FromJSON(src []byte) error {
d := bytes.Split(json.Q(src), []byte(":"))
if len(d) != 2 {
return ErrValue
}
v, err := strconv.ParseUint(json.S(d[0]), 10, 32)
if err != nil {
return err
}
e.ASN = uint32(v)
v, err = strconv.ParseUint(json.S(d[1]), 10, 32)
if err != nil {
return err
}
e.Value = uint32(v)
return nil
}
type ExtcomAddr struct {
Addr netip.Addr
Value uint16
}
func NewExtcomAddr(et ExtcomType) ExtcomValue {
return &ExtcomAddr{}
}
func (e *ExtcomAddr) Unmarshal(raw uint64) error {
e.Addr = netip.AddrFrom4([4]byte{
byte(raw >> 40), byte(raw >> 32),
byte(raw >> 24), byte(raw >> 16)})
e.Value = uint16(raw)
return nil
}
func (e *ExtcomAddr) Marshal(cps caps.Caps) uint64 {
var raw uint64
if addr := e.Addr.AsSlice(); len(addr) == 4 {
raw |= uint64(addr[0]) << 40
raw |= uint64(addr[1]) << 32
raw |= uint64(addr[2]) << 24
raw |= uint64(addr[3]) << 16
}
raw |= uint64(e.Value)
return raw
}
func (e *ExtcomAddr) ToJSON(dst []byte) []byte {
dst = append(dst, '"')
dst = e.Addr.AppendTo(dst)
dst = append(dst, ':')
dst = strconv.AppendUint(dst, uint64(e.Value), 10)
dst = append(dst, '"')
return dst
}
func (e *ExtcomAddr) FromJSON(src []byte) error {
d := bytes.Split(json.Q(src), []byte(":"))
if len(d) != 2 {
return ErrValue
}
a, err := netip.ParseAddr(json.S(d[0]))
if err != nil {
return err
}
e.Addr = a
v, err := strconv.ParseUint(json.S(d[1]), 10, 16)
if err != nil {
return err
}
e.Value = uint16(v)
return nil
}