forked from layeh/radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codecs.go
317 lines (279 loc) · 7.94 KB
/
codecs.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package radius
import (
"encoding/binary"
"errors"
"net"
"reflect"
"strconv"
"time"
"unicode/utf8"
)
type AttributeType int
const (
AttributeString AttributeType = iota + 1
AttributeOctets
AttributeIPAddr
AttributeDate
AttributeInteger
AttributeSigned
AttributeShort
AttributeIPv6Addr
AttributeIPv6Prefix
AttributeIFID
AttributeInteger64
AttributeVSA
AttributeUnknown = -1
// TODO: non-standard types?
)
func (t AttributeType) String() string {
switch t {
case AttributeString:
return "string"
case AttributeOctets:
return "octets"
case AttributeIPAddr:
return "ipaddr"
case AttributeDate:
return "date"
case AttributeInteger:
return "integer"
case AttributeSigned:
return "signed"
case AttributeShort:
return "short"
case AttributeIPv6Addr:
return "ipv6addr"
case AttributeIPv6Prefix:
return "ipv6prefix"
case AttributeIFID:
return "ifid"
case AttributeInteger64:
return "integer64"
case AttributeVSA:
return "vsa"
case AttributeUnknown:
return "unknown"
}
return "AttributeType(" + strconv.Itoa(int(t)) + ")"
}
var Codecs map[AttributeType]AttributeCodec
func init() {
Codecs = make(map[AttributeType]AttributeCodec)
Codecs[AttributeString] = attributeString{}
Codecs[AttributeOctets] = attributeOctets{}
Codecs[AttributeIPAddr] = attributeAddress{}
Codecs[AttributeDate] = attributeDate{}
Codecs[AttributeInteger] = attributeInteger{}
Codecs[AttributeSigned] = attributeSigned{}
Codecs[AttributeShort] = attributeShort{}
Codecs[AttributeIPv6Addr] = attributeAddress6{}
// TODO: ipv6 prefix
Codecs[AttributeIPv6Prefix] = attributeOctets{}
Codecs[AttributeIFID] = attributeOctets{}
Codecs[AttributeInteger64] = attributeInteger64{}
Codecs[AttributeVSA] = attributeOctets{}
Codecs[AttributeUnknown] = attributeOctets{}
}
type attributeString struct{}
func (attributeString) Decode(value Attribute) (interface{}, error) {
if !utf8.Valid(value) {
return nil, errors.New("text attribute is not valid UTF-8")
}
return string(value), nil
}
func (attributeString) Encode(value interface{}) (Attribute, error) {
str, ok := value.(string)
if ok {
return Attribute(str), nil
}
raw, ok := value.([]byte)
if ok {
return raw, nil
}
return nil, errors.New("text attribute must be string or []byte")
}
type attributeOctets struct{}
func (attributeOctets) Decode(value Attribute) (interface{}, error) {
v := make([]byte, len(value))
copy(v, value)
return v, nil
}
func (attributeOctets) Encode(value interface{}) (Attribute, error) {
raw, ok := value.(Attribute)
if ok {
return raw, nil
}
str, ok := value.(string)
if ok {
return Attribute(str), nil
}
data, ok := value.([]byte)
if ok {
return Attribute(data), nil
}
return nil, errors.New("string attribute must be Attribute or string")
}
type attributeAddress struct{}
func (attributeAddress) Decode(value Attribute) (interface{}, error) {
if len(value) != net.IPv4len {
return nil, errors.New("address attribute has invalid size")
}
v := make(Attribute, len(value))
copy(v, value)
return net.IP(v), nil
}
func (attributeAddress) Encode(value interface{}) (Attribute, error) {
ip, ok := value.(net.IP)
if !ok {
return nil, errors.New("address attribute must be net.IP")
}
ip = ip.To4()
if ip == nil {
return nil, errors.New("address attribute must be an IPv4 net.IP")
}
return Attribute(ip), nil
}
type attributeAddress6 struct{}
func (attributeAddress6) Decode(value Attribute) (interface{}, error) {
if len(value) != net.IPv6len {
return nil, errors.New("address attribute has invalid size")
}
v := make(Attribute, len(value))
copy(v, value)
return net.IP(v), nil
}
func (attributeAddress6) Encode(value interface{}) (Attribute, error) {
ip, ok := value.(net.IP)
if !ok {
return nil, errors.New("address attribute must be net.IP")
}
ip = ip.To16()
if ip == nil {
return nil, errors.New("address attribute must be an IPv6 net.IP")
}
return Attribute(ip), nil
}
type attributeSigned struct{}
func (attributeSigned) Decode(value Attribute) (interface{}, error) {
if len(value) != 4 {
return nil, errors.New("signed attribute has invalid size")
}
return int32(binary.BigEndian.Uint32(value)), nil
}
func (attributeSigned) Encode(value interface{}) (Attribute, error) {
integer, ok := value.(int32)
if !ok {
return nil, errors.New("signed attribute must be int32")
}
raw := make(Attribute, 4)
binary.BigEndian.PutUint32(raw, (uint32)(integer))
return raw, nil
}
func (attributeSigned) Transform(value interface{}) (interface{}, error) {
switch t := value.(type) {
case int, int8, int16, int32, int64:
a := reflect.ValueOf(t).Int() // a has type int64
return int32(a), nil
case uint, uint8, uint16, uint32, uint64:
a := reflect.ValueOf(t).Uint() // a has type uint64
return int32(a), nil
}
return nil, errors.New("invalid signed attribute")
}
type attributeInteger struct{}
func (attributeInteger) Decode(value Attribute) (interface{}, error) {
if len(value) != 4 {
return nil, errors.New("integer attribute has invalid size")
}
return binary.BigEndian.Uint32(value), nil
}
func (attributeInteger) Encode(value interface{}) (Attribute, error) {
integer, ok := value.(uint32)
if !ok {
return nil, errors.New("integer attribute must be uint32")
}
raw := make(Attribute, 4)
binary.BigEndian.PutUint32(raw, integer)
return raw, nil
}
func (attributeInteger) Transform(value interface{}) (interface{}, error) {
switch t := value.(type) {
case int, int8, int16, int32, int64:
a := reflect.ValueOf(t).Int() // a has type int64
return uint32(a), nil
case uint, uint8, uint16, uint32, uint64:
a := reflect.ValueOf(t).Uint() // a has type uint64
return uint32(a), nil
}
return nil, errors.New("invalid integer attribute")
}
type attributeInteger64 struct{}
func (attributeInteger64) Decode(value Attribute) (interface{}, error) {
if len(value) != 8 {
return nil, errors.New("integer64 attribute has invalid size")
}
return binary.BigEndian.Uint64(value), nil
}
func (attributeInteger64) Encode(value interface{}) (Attribute, error) {
integer, ok := value.(uint64)
if !ok {
return nil, errors.New("integer64 attribute must be uint64")
}
raw := make(Attribute, 8)
binary.BigEndian.PutUint64(raw, integer)
return raw, nil
}
func (attributeInteger64) Transform(value interface{}) (interface{}, error) {
switch t := value.(type) {
case int, int8, int16, int32, int64:
a := reflect.ValueOf(t).Int() // a has type int64
return uint64(a), nil
case uint, uint8, uint16, uint32, uint64:
a := reflect.ValueOf(t).Uint() // a has type uint64
return uint64(a), nil
}
return nil, errors.New("invalid integer64 attribute")
}
type attributeDate struct{}
func (attributeDate) Decode(value Attribute) (interface{}, error) {
if len(value) != 4 {
return nil, errors.New("time attribute has invalid size")
}
return time.Unix(int64(binary.BigEndian.Uint32(value)), 0), nil
}
func (attributeDate) Encode(value interface{}) (Attribute, error) {
timestamp, ok := value.(time.Time)
if !ok {
return nil, errors.New("time attribute must be time.Time")
}
raw := make(Attribute, 4)
binary.BigEndian.PutUint32(raw, uint32(timestamp.Unix()))
return raw, nil
}
type attributeShort struct{}
func (attributeShort) Decode(value Attribute) (interface{}, error) {
if len(value) != 2 {
return nil, errors.New("signed attribute has invalid size")
}
return uint16(binary.BigEndian.Uint16(value)), nil
}
func (attributeShort) Encode(value interface{}) (Attribute, error) {
integer, ok := value.(uint16)
if !ok {
return nil, errors.New("short attribute must be uint16")
}
raw := make(Attribute, 2)
binary.BigEndian.PutUint16(raw, (uint16)(integer))
return raw, nil
}
func (attributeShort) Transform(value interface{}) (interface{}, error) {
switch t := value.(type) {
case int, int8, int16, int32, int64:
a := reflect.ValueOf(t).Int() // a has type int64
return uint16(a), nil
case uint, uint8, uint16, uint32, uint64:
a := reflect.ValueOf(t).Uint() // a has type uint64
return uint16(a), nil
}
return nil, errors.New("invalid short attribute")
}