-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpacket.go
92 lines (74 loc) · 1.57 KB
/
packet.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
package radius
import (
"math/rand"
"net"
"time"
"encoding/binary"
)
// incrementing packet identification
var ident uint8
type Packet struct {
buff []byte
read int
addr *net.UDPAddr
err error
}
func NewPacket(code, ident uint8, auth []byte, ats ...Attribute) Packet {
buff := make([]byte, 6)
buff[0] = byte(code)
buff[1] = byte(ident)
buff[3] = auth[0]
buff[4] = auth[1]
}
func AuthPacket(ats ...Attribute) Packet {
ident += 1
c := Access_Request
i := ident
authenticator := DefaultAuthenticator()
return NewPacket(c, i, authenticator)
}
const (
Invalid_Type uint8 = 0
Access_Request uint8 = 1
Access_Accept uint8 = 2
Access_Reject uint8 = 3
Access_Challange uint8 = 11
)
const (
code int = 0
identifier = 1
len_start = 2
len_end = 3
authenticator_start = 4
authenticator_end = 20
)
func (p *Packet) Code() uint8 {
pt := packetType(p.buff[0])
return pt
}
func (p *Packet) Identifier() uint8 {
return uint8(p.buff[1])
}
func (p *Packet) Len() uint8 {
return uint8(p.buff[2])
}
func packetType(code byte) uint8 {
switch uint8(code) {
case Access_Request:
return Access_Request
case Access_Accept:
return Access_Accept
case Access_Reject:
return Access_Reject
default:
return Invalid_Type
}
}
// Utility function to generate a pseudo random 2 byte auth
// Uses time.Now().Unix() as a Source
func DefaultAuthenticator() []byte {
r := rand.New(rand.NewSource(time.Now().Unix()))
res := make([]byte, 2)
binary.LittleEndian.PutUint32(res, r.Uint32())
return res
}