/// <summary> /// generates new pow2request object /// resets state when necessary /// </summary> public Pow2RequestState GenerateOrGetExistingPow2(IPEndPoint requesterEndpoint) { var timeNowRel = TimeNowRel; if (timeNowRel > _nextPeriodSwitchTimeRel || _currentPeriodStates.Count > _config.Pow2RequestStatesTableMaxSize) { // switch tables _previousPeriodStates = _currentPeriodStates; _currentPeriodStates = new Dictionary <IPEndPoint, Pow2RequestState>(); _nextPeriodSwitchTimeRel = timeNowRel + _config.Pow2RequestStatesTablePeriod; } var existingPow2RequestState = TryGetPow2RequestState(requesterEndpoint); if (existingPow2RequestState != null) { return(existingPow2RequestState); } var r = new Pow2RequestState { ProofOfWork2Request = new byte[16] }; _rnd.NextBytes(r.ProofOfWork2Request); _currentPeriodStates.Add(requesterEndpoint, r); return(r); }
/// <summary> /// is executed by receiver thread /// </summary> void ProcessRegisterReqAtoEpPacket(IPEndPoint requesterEndpoint, byte[] udpData, Stopwatch reqReceivedAtSW) { Pow2RequestState pow2RequestState = null; if (!Configuration.SandboxModeOnly_DisablePoW) { pow2RequestState = _pow2RequestsTable.TryGetPow2RequestState(requesterEndpoint); if (pow2RequestState == null) { OnReceivedRegisterReqAtoEpPacketFromUnknownSource(requesterEndpoint); return; } } var req = RegisterRequestPacket.Decode_OptionallyVerifyNeighborHMAC(udpData, null, Configuration.SandboxModeOnly_NumberOfDimensions); if (!Configuration.SandboxModeOnly_DisablePoW) { if (!Pow2IsOK(req, pow2RequestState.ProofOfWork2Request)) { OnReceivedRegisterReqAtoEpPacketWithBadPow2(requesterEndpoint); // intentionally we dont respond to requester, in case if it is attack return; } } EngineThreadQueue.Enqueue(() => { _ = ProcessRegisterReqAtoEpPacket2Async(requesterEndpoint, req, reqReceivedAtSW); }, "ProcessRegisterReqAtoEpPacket2Async436"); }