-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.go
More file actions
142 lines (123 loc) · 3.85 KB
/
Copy pathencryption.go
File metadata and controls
142 lines (123 loc) · 3.85 KB
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
package netcode
const maxEncryptionMappings = MaxClients * 4
// encryptionManager maps packet source addresses to the send and receive keys
// from the private connect token, so the server can decrypt packets from
// clients that are mid-handshake or connected. Mappings expire after timeout
// seconds without being touched, or at an absolute expire time while the
// client has not yet established a connection.
type encryptionManager struct {
numEncryptionMappings int
timeout [maxEncryptionMappings]int32
expireTime [maxEncryptionMappings]float64
lastAccessTime [maxEncryptionMappings]float64
address [maxEncryptionMappings]Address
clientIndex [maxEncryptionMappings]int
sendKey [maxEncryptionMappings][KeyBytes]byte
receiveKey [maxEncryptionMappings][KeyBytes]byte
}
func (m *encryptionManager) reset() {
printf(LogLevelDebug, "reset encryption manager\n")
m.numEncryptionMappings = 0
for i := 0; i < maxEncryptionMappings; i++ {
m.clientIndex[i] = -1
m.expireTime[i] = -1.0
m.lastAccessTime[i] = -1000.0
m.address[i] = Address{}
m.timeout[i] = 0
m.sendKey[i] = [KeyBytes]byte{}
m.receiveKey[i] = [KeyBytes]byte{}
}
}
func (m *encryptionManager) entryExpired(index int, time float64) bool {
return (m.timeout[index] > 0 && m.lastAccessTime[index]+float64(m.timeout[index]) < time) ||
(m.expireTime[index] >= 0.0 && m.expireTime[index] < time)
}
func (m *encryptionManager) addEncryptionMapping(address *Address, sendKey, receiveKey []byte, time, expireTime float64, timeout int32) bool {
for i := 0; i < m.numEncryptionMappings; i++ {
if m.address[i].Equal(*address) && !m.entryExpired(i, time) {
m.timeout[i] = timeout
m.expireTime[i] = expireTime
m.lastAccessTime[i] = time
copy(m.sendKey[i][:], sendKey)
copy(m.receiveKey[i][:], receiveKey)
return true
}
}
for i := 0; i < maxEncryptionMappings; i++ {
if m.address[i].Type == AddressNone ||
(m.entryExpired(i, time) && m.clientIndex[i] == -1) {
m.timeout[i] = timeout
m.address[i] = *address
m.expireTime[i] = expireTime
m.lastAccessTime[i] = time
copy(m.sendKey[i][:], sendKey)
copy(m.receiveKey[i][:], receiveKey)
if i+1 > m.numEncryptionMappings {
m.numEncryptionMappings = i + 1
}
return true
}
}
return false
}
func (m *encryptionManager) removeEncryptionMapping(address *Address, time float64) bool {
for i := 0; i < m.numEncryptionMappings; i++ {
if m.address[i].Equal(*address) {
m.expireTime[i] = -1.0
m.lastAccessTime[i] = -1000.0
m.address[i] = Address{}
m.sendKey[i] = [KeyBytes]byte{}
m.receiveKey[i] = [KeyBytes]byte{}
if i+1 == m.numEncryptionMappings {
index := i - 1
for index >= 0 {
if !m.entryExpired(index, time) || m.clientIndex[index] != -1 {
break
}
m.address[index].Type = AddressNone
index--
}
m.numEncryptionMappings = index + 1
}
return true
}
}
return false
}
func (m *encryptionManager) findEncryptionMapping(address *Address, time float64) int {
for i := 0; i < m.numEncryptionMappings; i++ {
if m.address[i].Equal(*address) && !m.entryExpired(i, time) {
m.lastAccessTime[i] = time
return i
}
}
return -1
}
func (m *encryptionManager) touch(index int, address *Address, time float64) bool {
if !m.address[index].Equal(*address) {
return false
}
m.lastAccessTime[index] = time
return true
}
func (m *encryptionManager) setExpireTime(index int, expireTime float64) {
m.expireTime[index] = expireTime
}
func (m *encryptionManager) getSendKey(index int) []byte {
if index == -1 {
return nil
}
return m.sendKey[index][:]
}
func (m *encryptionManager) getReceiveKey(index int) []byte {
if index == -1 {
return nil
}
return m.receiveKey[index][:]
}
func (m *encryptionManager) getTimeout(index int) int32 {
if index == -1 {
return 0
}
return m.timeout[index]
}