Skip to content

Commit 2f4bc0d

Browse files
committed
Add ping support
1 parent 1f8d697 commit 2f4bc0d

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

olproxy/Program.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,49 @@ void ProcessLocalPacket(IPEndPoint endPoint, byte[] packet)
286286
bcast.SendMulti(msgStr, remoteSocket.Client, remotePeers.Keys, pktPid, isNew);
287287
}
288288

289+
private void ProcessPingPacket(byte[] packetData, IPEndPoint senderEndPoint)
290+
{
291+
byte[] outBuf = new byte[packetData.Length];
292+
Array.Copy(packetData, outBuf, packetData.Length);
293+
294+
// calculate incoming hash
295+
Array.Copy(BitConverter.GetBytes(0), 0, outBuf, 8, 4);
296+
uint srcHash = xxHash.CalculateHash(outBuf);
297+
298+
if (srcHash != BitConverter.ToUInt32(packetData, 8)) // ignore packet with invalid hash
299+
return;
300+
301+
Array.Copy(BitConverter.GetBytes((int)-2), 0, outBuf, 0, 4);
302+
if (outBuf.Length >= 19 + 4)
303+
Array.Copy(BitConverter.GetBytes(0), 0, outBuf, 19, 4); // version
304+
if (outBuf.Length >= 19 + 4 + 4)
305+
Array.Copy(BitConverter.GetBytes(255), 0, outBuf, 19 + 4, 4); // status, 255 = unknown
306+
307+
// calculate outgoing hash
308+
Array.Copy(BitConverter.GetBytes(0), 0, outBuf, 8, 4);
309+
uint hash = xxHash.CalculateHash(outBuf);
310+
Array.Copy(BitConverter.GetBytes(hash), 0, outBuf, 8, 4);
311+
312+
remoteSocket.Send(outBuf, outBuf.Length, senderEndPoint);
313+
}
314+
315+
public static bool IsPingPacket(byte[] packetData)
316+
{
317+
return BitConverter.ToInt32(packetData, 0) == -1;
318+
}
319+
320+
289321
void ProcessRemotePacket(IPEndPoint endPoint, byte[] packet)
290322
{
291323
if (LocalIPSet.Contains(endPoint.Address))
292324
return;
293325

326+
if (IsPingPacket(packet))
327+
{
328+
ProcessPingPacket(packet, endPoint);
329+
return;
330+
}
331+
294332
// store peer last seen
295333
remotePeers[endPoint] = DateTime.UtcNow;
296334

0 commit comments

Comments
 (0)