forked from GlaireDaggers/Netcode.IO.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptionManager.cs
More file actions
196 lines (164 loc) · 5.4 KB
/
Copy pathEncryptionManager.cs
File metadata and controls
196 lines (164 loc) · 5.4 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
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
using System;
using System.Net;
using NetcodeIO.NET.Utils;
namespace NetcodeIO.NET
{
internal class EncryptionManager
{
internal struct encryptionMapEntry
{
public double ExpireTime;
public double LastAccessTime;
public int TimeoutSeconds;
public uint ClientID;
public EndPoint Address;
public byte[] SendKey;
public byte[] ReceiveKey;
public void Reset()
{
ExpireTime = -1.0;
LastAccessTime = -1000.0;
Address = null;
TimeoutSeconds = 0;
ClientID = 0;
Array.Clear(SendKey, 0, SendKey.Length);
Array.Clear(ReceiveKey, 0, ReceiveKey.Length);
}
}
internal int numEncryptionMappings;
internal encryptionMapEntry[] encryptionMappings;
public EncryptionManager(int maxClients)
{
encryptionMappings = new encryptionMapEntry[maxClients * 4];
for (int i = 0; i < encryptionMappings.Length; i++)
{
encryptionMappings[i].SendKey = new byte[32];
encryptionMappings[i].ReceiveKey = new byte[32];
}
Reset();
}
public void Reset()
{
numEncryptionMappings = 0;
for (int i = 0; i < encryptionMappings.Length; i++)
{
encryptionMappings[i].Reset();
}
}
public bool AddEncryptionMapping(EndPoint address, byte[] sendKey, byte[] receiveKey, double time, double expireTime, int timeoutSeconds, uint clientID)
{
for (int i = 0; i < numEncryptionMappings; i++)
{
if (MiscUtils.AddressEqual(encryptionMappings[i].Address, address)
&& ( timeoutSeconds >= 0 && encryptionMappings[i].LastAccessTime + timeoutSeconds >= time ))
{
encryptionMappings[i].ExpireTime = expireTime;
encryptionMappings[i].LastAccessTime = time;
encryptionMappings[i].TimeoutSeconds = timeoutSeconds;
encryptionMappings[i].ClientID = clientID;
Buffer.BlockCopy(sendKey, 0, encryptionMappings[i].SendKey, 0, 32);
Buffer.BlockCopy(receiveKey, 0, encryptionMappings[i].ReceiveKey, 0, 32);
return true;
}
}
for (int i = 0; i < encryptionMappings.Length; i++)
{
if ((encryptionMappings[i].TimeoutSeconds >= 0 && encryptionMappings[i].LastAccessTime + encryptionMappings[i].TimeoutSeconds < time) ||
(encryptionMappings[i].ExpireTime >= 0.0 && encryptionMappings[i].ExpireTime < time))
{
encryptionMappings[i].Address = address;
encryptionMappings[i].ExpireTime = expireTime;
encryptionMappings[i].LastAccessTime = time;
encryptionMappings[i].TimeoutSeconds = timeoutSeconds;
encryptionMappings[i].ClientID = clientID;
Buffer.BlockCopy(sendKey, 0, encryptionMappings[i].SendKey, 0, 32);
Buffer.BlockCopy(receiveKey, 0, encryptionMappings[i].ReceiveKey, 0, 32);
if (i + 1 > numEncryptionMappings)
numEncryptionMappings = i + 1;
return true;
}
}
return false;
}
public bool RemoveEncryptionMapping(EndPoint address, double time)
{
for (int i = 0; i < numEncryptionMappings; i++)
{
if (MiscUtils.AddressEqual(encryptionMappings[i].Address, address))
{
encryptionMappings[i].Reset();
if (i + 1 == numEncryptionMappings)
{
int index = i - 1;
while (index >= 0)
{
if ((encryptionMappings[index].TimeoutSeconds < 0 || encryptionMappings[index].LastAccessTime + encryptionMappings[index].TimeoutSeconds >= time ) &&
(encryptionMappings[index].ExpireTime < 0 || encryptionMappings[index].ExpireTime > time))
break;
index--;
}
numEncryptionMappings = index + 1;
}
return true;
}
}
return false;
}
public byte[] GetSendKey(int idx)
{
if (idx == -1 || idx >= encryptionMappings.Length) return null;
return encryptionMappings[idx].SendKey;
}
public byte[] GetReceiveKey(int idx)
{
if (idx == -1 || idx >= encryptionMappings.Length) return null;
return encryptionMappings[idx].ReceiveKey;
}
public int GetTimeoutSeconds(int idx)
{
if (idx == -1 || idx >= encryptionMappings.Length) return -1;
return encryptionMappings[idx].TimeoutSeconds;
}
public uint GetClientID(int idx)
{
if (idx == -1 || idx >= encryptionMappings.Length) return 0;
return encryptionMappings[idx].ClientID;
}
public void SetClientID(int idx, uint clientID)
{
if (idx < 0 || idx >= numEncryptionMappings)
throw new IndexOutOfRangeException();
encryptionMappings[idx].ClientID = clientID;
}
public bool Touch(int index, EndPoint address, double time)
{
if (index < 0 || index >= numEncryptionMappings)
throw new IndexOutOfRangeException();
if (!MiscUtils.AddressEqual(encryptionMappings[index].Address, address))
return false;
encryptionMappings[index].LastAccessTime = time;
encryptionMappings[index].ExpireTime = time + Defines.NETCODE_TIMEOUT_SECONDS;
return true;
}
public void SetExpireTime(int index, double expireTime)
{
if (index < 0 || index >= numEncryptionMappings)
throw new IndexOutOfRangeException();
encryptionMappings[index].ExpireTime = expireTime;
}
public int FindEncryptionMapping(EndPoint address, double time)
{
for (int i = 0; i < numEncryptionMappings; i++)
{
if (MiscUtils.AddressEqual(encryptionMappings[i].Address, address) &&
(encryptionMappings[i].LastAccessTime + encryptionMappings[i].TimeoutSeconds >= time || encryptionMappings[i].TimeoutSeconds < 0) &&
(encryptionMappings[i].ExpireTime < 0.0 || encryptionMappings[i].ExpireTime >= time))
{
encryptionMappings[i].LastAccessTime = time;
return i;
}
}
return -1;
}
}
}