forked from bronze1man/radius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avp.go
308 lines (263 loc) · 7.25 KB
/
avp.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
package radius
import (
"bytes"
"crypto"
"encoding/binary"
"errors"
"fmt"
"net"
"reflect"
"strconv"
)
type AVP struct {
Type AttributeType
Value []byte
}
func (a AVP) Copy() AVP {
value := make([]byte, len(a.Value))
copy(value, a.Value)
return AVP{
Type: a.Type,
Value: a.Value,
}
}
func (a AVP) Encode(b []byte) (n int, err error) {
fullLen := len(a.Value) + 2 //type and length
if fullLen > 255 || fullLen < 2 {
return 0, errors.New("value too big for attribute")
}
b[0] = uint8(a.Type)
b[1] = uint8(fullLen)
copy(b[2:], a.Value)
return fullLen, err
}
func (a AVP) Decode(p *Packet) interface{} {
return getAttributeTypeDesc(a.Type).dataType.Value(p, a)
}
func (a AVP) String() string {
return "AVP type: " + a.Type.String() + " " + getAttributeTypeDesc(a.Type).dataType.String(nil, a)
}
func (a AVP) StringWithPacket(p *Packet) string {
return "AVP type: " + a.Type.String() + " " + getAttributeTypeDesc(a.Type).dataType.String(p, a)
}
type avpDataType interface {
Value(p *Packet, a AVP) interface{}
String(p *Packet, a AVP) string
}
var avpString avpStringt
type avpStringt struct{}
func (s avpStringt) Value(p *Packet, a AVP) interface{} {
return string(a.Value)
}
func (s avpStringt) String(p *Packet, a AVP) string {
return string(a.Value)
}
var avpIP avpIPt
type avpIPt struct{}
func (s avpIPt) Value(p *Packet, a AVP) interface{} {
return net.IP(a.Value)
}
func (s avpIPt) String(p *Packet, a AVP) string {
return net.IP(a.Value).String()
}
var avpUint32 avpUint32t
type avpUint32t struct{}
func (s avpUint32t) Value(p *Packet, a AVP) interface{} {
return uint32(binary.BigEndian.Uint32(a.Value))
}
func (s avpUint32t) String(p *Packet, a AVP) string {
return strconv.Itoa(int(binary.BigEndian.Uint32(a.Value)))
}
var avpBinary avpBinaryt
type avpBinaryt struct{}
func (s avpBinaryt) Value(p *Packet, a AVP) interface{} {
return a.Value
}
func (s avpBinaryt) String(p *Packet, a AVP) string {
return fmt.Sprintf("%#v", a.Value)
}
var avpPassword avpPasswordt
type avpPasswordt struct{}
func (s avpPasswordt) Value(p *Packet, a AVP) interface{} {
if p == nil {
return ""
}
buff := a.Value
pass := make([]byte, 0)
last := make([]byte, 16)
copy(last, p.Authenticator[:])
for len(buff) > 0 {
m := crypto.Hash(crypto.MD5).New()
m.Write(append([]byte(p.Secret), last...))
h := m.Sum(nil)
for i := 0; i < 16; i++ {
pass = append(pass, buff[i]^h[i])
}
last = buff[:16]
buff = buff[16:]
}
pass = bytes.TrimRight(pass, string([]rune{0}))
return string(pass)
}
func (s avpPasswordt) String(p *Packet, a AVP) string {
return s.Value(p, a).(string)
}
type avpUint32EnumList []string
func (s avpUint32EnumList) Value(p *Packet, a AVP) interface{} {
return uint32(binary.BigEndian.Uint32(a.Value))
}
func (s avpUint32EnumList) String(p *Packet, a AVP) string {
number := int(binary.BigEndian.Uint32(a.Value))
if number > len(s) {
return "unknow " + strconv.Itoa(number)
}
out := s[number]
if out == "" {
return "unknow " + strconv.Itoa(number)
}
return out
}
type avpUint32Enum struct {
t interface{} // t should from a uint32 type like AcctStatusTypeEnum
}
func (s avpUint32Enum) Value(p *Packet, a AVP) interface{} {
value := reflect.New(reflect.TypeOf(s.t)).Elem()
value.SetUint(uint64(binary.BigEndian.Uint32(a.Value)))
return value.Interface()
}
func (s avpUint32Enum) String(p *Packet, a AVP) string {
number := binary.BigEndian.Uint32(a.Value)
value := reflect.New(reflect.TypeOf(s.t)).Elem()
value.SetUint(uint64(number))
method := value.MethodByName("String")
if !method.IsValid() {
return strconv.Itoa(int(number))
}
out := method.Call(nil)
return out[0].Interface().(string)
}
var avpEapMessage avpEapMessaget
type avpEapMessaget struct{}
func (s avpEapMessaget) Value(p *Packet, a AVP) interface{} {
eap, err := EapDecode(a.Value)
if err != nil {
//TODO error handle
fmt.Println("EapDecode fail ", err)
return nil
}
return eap
}
func (s avpEapMessaget) String(p *Packet, a AVP) string {
eap := s.Value(p, a)
if eap == nil {
return "nil"
}
return eap.(*EapPacket).String()
}
type AcctStatusTypeEnum uint32
const (
AcctStatusTypeEnumStart AcctStatusTypeEnum = 1
AcctStatusTypeEnumStop AcctStatusTypeEnum = 2
AcctStatusTypeEnumInterimUpdate AcctStatusTypeEnum = 3
AcctStatusTypeEnumAccountingOn AcctStatusTypeEnum = 7
AcctStatusTypeEnumAccountingOff AcctStatusTypeEnum = 8
)
func (e AcctStatusTypeEnum) String() string {
switch e {
case AcctStatusTypeEnumStart:
return "Start"
case AcctStatusTypeEnumStop:
return "Stop"
case AcctStatusTypeEnumInterimUpdate:
return "InterimUpdate"
case AcctStatusTypeEnumAccountingOn:
return "AccountingOn"
case AcctStatusTypeEnumAccountingOff:
return "AccountingOff"
}
return "unknow code " + strconv.Itoa(int(e))
}
type NASPortTypeEnum uint32
// TODO finish it
const (
NASPortTypeEnumAsync NASPortTypeEnum = 0
NASPortTypeEnumSync NASPortTypeEnum = 1
NASPortTypeEnumISDNSync NASPortTypeEnum = 2
NASPortTypeEnumISDNSyncV120 NASPortTypeEnum = 3
NASPortTypeEnumISDNSyncV110 NASPortTypeEnum = 4
NASPortTypeEnumVirtual NASPortTypeEnum = 5
NASPortTypeEnumPIAFS NASPortTypeEnum = 6
NASPortTypeEnumHDLCClearChannel NASPortTypeEnum = 7
NASPortTypeEnumEthernet NASPortTypeEnum = 15
NASPortTypeEnumCable NASPortTypeEnum = 17
)
func (e NASPortTypeEnum) String() string {
switch e {
case NASPortTypeEnumAsync:
return "Async"
case NASPortTypeEnumSync:
return "Sync"
case NASPortTypeEnumISDNSync:
return "ISDNSync"
case NASPortTypeEnumISDNSyncV120:
return "ISDNSyncV120"
case NASPortTypeEnumISDNSyncV110:
return "ISDNSyncV110"
case NASPortTypeEnumVirtual:
return "Virtual"
case NASPortTypeEnumPIAFS:
return "PIAFS"
case NASPortTypeEnumHDLCClearChannel:
return "HDLCClearChannel"
case NASPortTypeEnumEthernet:
return "Ethernet"
case NASPortTypeEnumCable:
return "Cable"
}
return "unknow code " + strconv.Itoa(int(e))
}
type ServiceTypeEnum uint32
// TODO finish it
const (
ServiceTypeEnumLogin ServiceTypeEnum = 1
ServiceTypeEnumFramed ServiceTypeEnum = 2
ServiceTypeEnumCallbackLogin ServiceTypeEnum = 3
ServiceTypeEnumCallbackFramed ServiceTypeEnum = 4
ServiceTypeEnumOutbound ServiceTypeEnum = 5
)
func (e ServiceTypeEnum) String() string {
switch e {
case ServiceTypeEnumLogin:
return "Login"
case ServiceTypeEnumFramed:
return "Framed"
case ServiceTypeEnumCallbackLogin:
return "CallbackLogin"
case ServiceTypeEnumCallbackFramed:
return "CallbackFramed"
case ServiceTypeEnumOutbound:
return "Outbound"
}
return "unknow code " + strconv.Itoa(int(e))
}
type AcctTerminateCauseEnum uint32
// TODO finish it
const (
AcctTerminateCauseEnumUserRequest AcctTerminateCauseEnum = 1
AcctTerminateCauseEnumLostCarrier AcctTerminateCauseEnum = 2
AcctTerminateCauseEnumLostService AcctTerminateCauseEnum = 3
AcctTerminateCauseEnumIdleTimeout AcctTerminateCauseEnum = 4
)
func (e AcctTerminateCauseEnum) String() string {
switch e {
case AcctTerminateCauseEnumUserRequest:
return "UserRequest"
case AcctTerminateCauseEnumLostCarrier:
return "LostCarrier"
case AcctTerminateCauseEnumLostService:
return "LostService"
case AcctTerminateCauseEnumIdleTimeout:
return "IdleTimeout"
}
return "unknow code " + strconv.Itoa(int(e))
}