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
166 lines (138 loc) · 4.25 KB
/
Copy pathEncryptionManager.cs
File metadata and controls
166 lines (138 loc) · 4.25 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
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 EndPoint Address;
public byte[] SendKey;
public byte[] ReceiveKey;
public void Reset()
{
ExpireTime = -1.0;
LastAccessTime = -1000.0;
Address = null;
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)
{
for (int i = 0; i < numEncryptionMappings; i++)
{
if ( MiscUtils.AddressEqual( encryptionMappings[i].Address, address )
&& encryptionMappings[i].LastAccessTime + Defines.NETCODE_TIMEOUT_SECONDS >= time)
{
encryptionMappings[i].ExpireTime = expireTime;
encryptionMappings[i].LastAccessTime = time;
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].LastAccessTime + Defines.NETCODE_TIMEOUT_SECONDS < time ||
(encryptionMappings[i].ExpireTime >= 0.0 && encryptionMappings[i].ExpireTime < time))
{
encryptionMappings[i].Address = address;
encryptionMappings[i].ExpireTime = expireTime;
encryptionMappings[i].LastAccessTime = time;
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[i].LastAccessTime + Defines.NETCODE_TIMEOUT_SECONDS >= time &&
(encryptionMappings[i].ExpireTime < 0 || encryptionMappings[i].ExpireTime > time))
break;
index--;
}
numEncryptionMappings = index + 1;
}
return true;
}
}
return false;
}
public byte[] GetSendKey(int idx)
{
return encryptionMappings[idx].SendKey;
}
public byte[] GetReceiveKey(int idx)
{
return encryptionMappings[idx].ReceiveKey;
}
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 + Defines.NETCODE_TIMEOUT_SECONDS >= time &&
(encryptionMappings[i].ExpireTime < 0.0 || encryptionMappings[i].ExpireTime >= time))
{
encryptionMappings[i].LastAccessTime = time;
return i;
}
}
return -1;
}
}
}