From 712d1506dbf22a7510b0a422fb6c9c14fca12b6e Mon Sep 17 00:00:00 2001 From: maikebing Date: Fri, 13 Nov 2026 03:44:37 +0800 Subject: [PATCH 1/4] fix(blockwise): release transfer state across terminal lifecycles --- CoAP.NET/Stack/BlockwiseLayer.cs | 183 ++++++++++++--- CoAP.Test/BlockwiseLifecycleTest.cs | 343 ++++++++++++++++++++++++++++ 2 files changed, 492 insertions(+), 34 deletions(-) create mode 100644 CoAP.Test/BlockwiseLifecycleTest.cs diff --git a/CoAP.NET/Stack/BlockwiseLayer.cs b/CoAP.NET/Stack/BlockwiseLayer.cs index c7f7983..38e5d3a 100644 --- a/CoAP.NET/Stack/BlockwiseLayer.cs +++ b/CoAP.NET/Stack/BlockwiseLayer.cs @@ -10,25 +10,30 @@ */ using System; +using System.Collections.Concurrent; using System.Timers; using Microsoft.Extensions.Logging; using CoAP.Net; namespace CoAP.Stack { - public class BlockwiseLayer : AbstractLayer + public class BlockwiseLayer : AbstractLayer, IDisposable { static readonly ILogger Log = CoapLogging.CreateLogger(typeof(BlockwiseLayer)); private Int32 _maxMessageSize; private Int32 _defaultBlockSize; private Int32 _blockTimeout; + private readonly ICoapConfig _config; + private readonly ConcurrentDictionary _activeExchanges = new ConcurrentDictionary(); + private Int32 _disposed; /// /// Constructs a new blockwise layer. /// public BlockwiseLayer(ICoapConfig config) { + _config = config; _maxMessageSize = config.MaxMessageSize; _defaultBlockSize = config.DefaultBlockSize; _blockTimeout = config.BlockwiseStatusLifetime; @@ -189,8 +194,7 @@ public override void ReceiveRequest(INextLayer nextLayer, Exchange exchange, Req // clean up blockwise status if (Log.IsEnabled(LogLevel.Debug)) Log.LogDebug("Ongoing is complete " + status); - exchange.ResponseBlockStatus = null; - ClearBlockCleanup(exchange); + ReleaseBlockwiseState(exchange, response.HasOption(OptionType.Observe) && Code.IsSuccess(response.Code)); } else { @@ -237,8 +241,7 @@ public override void SendResponse(INextLayer nextLayer, Exchange exchange, Respo // clean up blockwise status if (Log.IsEnabled(LogLevel.Debug)) Log.LogDebug("Ongoing finished on first block " + status); - exchange.ResponseBlockStatus = null; - ClearBlockCleanup(exchange); + ReleaseBlockwiseState(exchange, response.HasOption(OptionType.Observe) && Code.IsSuccess(response.Code)); } else { @@ -255,7 +258,7 @@ public override void SendResponse(INextLayer nextLayer, Exchange exchange, Respo response.SetOption(block1); exchange.CurrentResponse = response; // Block1 transfer completed - ClearBlockCleanup(exchange); + ReleaseBlockwiseState(exchange, response.HasOption(OptionType.Observe) && Code.IsSuccess(response.Code)); base.SendResponse(nextLayer, exchange, response); } } @@ -283,9 +286,13 @@ public override void ReceiveResponse(INextLayer nextLayer, Exchange exchange, Re return; } + if (exchange.Complete || exchange.Request.IsTimedOut || exchange.Request.IsRejected) + return; + if (!response.HasOption(OptionType.Block1) && !response.HasOption(OptionType.Block2)) { // There is no block1 or block2 option, therefore it is a normal response + ReleaseBlockwiseState(exchange, response.HasOption(OptionType.Observe) && Code.IsSuccess(response.Code)); exchange.Response = response; base.ReceiveResponse(nextLayer, exchange, response); return; @@ -357,6 +364,7 @@ public override void ReceiveResponse(INextLayer nextLayer, Exchange exchange, Re ClearBlockCleanup(exchange); if (!response.HasOption(OptionType.Block2)) { + ReleaseBlockwiseState(exchange, response.HasOption(OptionType.Observe) && Code.IsSuccess(response.Code)); exchange.Response = response; base.ReceiveResponse(nextLayer, exchange, response); return; @@ -406,6 +414,7 @@ public override void ReceiveResponse(INextLayer nextLayer, Exchange exchange, Re if (status.IsRandomAccess) { // The client has requested this specifc block and we deliver it + ReleaseBlockwiseState(exchange, response.HasOption(OptionType.Observe) && Code.IsSuccess(response.Code)); exchange.Response = response; base.ReceiveResponse(nextLayer, exchange, response); } @@ -456,6 +465,8 @@ public override void ReceiveResponse(INextLayer nextLayer, Exchange exchange, Re exchange.ResponseBlockStatus = null; } + ReleaseBlockwiseState(exchange, observe != BlockwiseStatus.NoObserve && Code.IsSuccess(response.Code)); + if (Log.IsEnabled(LogLevel.Debug)) Log.LogDebug("Assembled response: " + assembled); exchange.Response = assembled; @@ -475,18 +486,18 @@ public override void ReceiveResponse(INextLayer nextLayer, Exchange exchange, Re EmptyMessage rst = EmptyMessage.NewRST(response); base.SendEmptyMessage(nextLayer, exchange, rst); } - exchange.Request.IsCancelled = true; + CancelBlockwiseUpload(exchange); } } } private void CancelBlockwiseUpload(Exchange exchange) { - exchange.RequestBlockStatus = null; - exchange.ResponseBlockStatus = null; - ClearBlockCleanup(exchange); - exchange.Complete = true; - exchange.Request.Cancel(); + ReleaseBlockwiseState(exchange, false); + if (!exchange.Complete) + exchange.Complete = true; + if (!exchange.Request.IsCancelled && !exchange.Request.IsTimedOut && !exchange.Request.IsRejected) + exchange.Request.Cancel(); } private void EarlyBlock2Negotiation(Exchange exchange, Request request) @@ -674,26 +685,37 @@ private Boolean RequiresBlockwise(Exchange exchange, Response response) /// protected void PrepareBlockCleanup(Exchange exchange) { - System.Timers.Timer timer = new System.Timers.Timer(); - timer.AutoReset = false; - timer.Interval = _blockTimeout; - timer.Elapsed += (o, e) => BlockwiseTimeout(exchange); - - System.Timers.Timer old = exchange.Set("BlockCleanupTimer", timer) as System.Timers.Timer; - if (old != null) + lock (exchange) { - try + ObjectDisposedException.ThrowIf(System.Threading.Volatile.Read(ref _disposed) != 0, this); + if (exchange.Complete) + return; + CleanupRegistration registration; + if (!_activeExchanges.TryGetValue(exchange, out registration)) { - old.Stop(); - old.Dispose(); + Request original = exchange.Request ?? exchange.CurrentRequest; + registration = new CleanupRegistration(original); + registration.Terminated = (sender, e) => OnRequestTerminated(exchange, original); + registration.Completed = (sender, e) => OnExchangeCompleted(exchange, original); + _activeExchanges[exchange] = registration; + original.Cancelled += registration.Terminated; + original.TimedOut += registration.Terminated; + original.Rejected += registration.Terminated; + exchange.Completed += registration.Completed; } - catch (ObjectDisposedException) + + if (System.Threading.Volatile.Read(ref _disposed) != 0) { - // ignore + CancelBlockwiseUpload(exchange); + throw new ObjectDisposedException(nameof(BlockwiseLayer)); } - } - timer.Start(); + ClearBlockCleanup(exchange); + System.Timers.Timer timer = new System.Timers.Timer(_blockTimeout) { AutoReset = false }; + timer.Elapsed += (sender, e) => BlockwiseTimeout(exchange, timer); + exchange.Set("BlockCleanupTimer", timer); + timer.Start(); + } } /// @@ -716,19 +738,112 @@ protected void ClearBlockCleanup(Exchange exchange) } } - private void BlockwiseTimeout(Exchange exchange) + private void BlockwiseTimeout(Exchange exchange, System.Timers.Timer timer) { - if (exchange.Request == null) + Request original; + lock (exchange) { - if (Log.IsEnabled(LogLevel.Information)) - Log.LogInformation("Block1 transfer timed out: " + exchange.CurrentRequest); + CleanupRegistration registration; + if (!_activeExchanges.TryGetValue(exchange, out registration) + || !Object.ReferenceEquals(exchange.Get("BlockCleanupTimer"), timer)) + return; + original = registration.Request; + ReleaseBlockwiseState(exchange, false); + if (exchange.Complete || original.IsCancelled || original.IsRejected || original.IsTimedOut) + return; + exchange.TimedOut = true; } - else + + if (exchange.Origin == Origin.Local && !original.IsTimedOut && !original.IsCancelled && !original.IsRejected) + original.IsTimedOut = true; + if (exchange.CurrentRequest != null && !Object.ReferenceEquals(exchange.CurrentRequest, original)) + exchange.CurrentRequest.Cancel(); + } + + private void OnRequestTerminated(Exchange exchange, Request original) + { + ReleaseBlockwiseState(exchange, false); + if (!exchange.Complete) + { + if (original.IsTimedOut) + exchange.TimedOut = true; + else + exchange.Complete = true; + } + Request current = exchange.CurrentRequest; + if (current != null && !Object.ReferenceEquals(current, original) && !current.IsCancelled) + current.Cancel(); + } + + private void OnExchangeCompleted(Exchange exchange, Request original) + { + ReleaseBlockwiseState(exchange, false); + if (exchange.TimedOut && exchange.Origin == Origin.Local + && !original.IsTimedOut && !original.IsCancelled && !original.IsRejected) + original.IsTimedOut = true; + } + + private void ReleaseBlockwiseState(Exchange exchange, Boolean keepObservation) + { + lock (exchange) + { + exchange.RequestBlockStatus = null; + exchange.ResponseBlockStatus = null; + exchange.Block1ToAck = null; + ClearBlockCleanup(exchange); + if (keepObservation) + return; + CleanupRegistration registration; + if (_activeExchanges.TryRemove(exchange, out registration)) + { + registration.Request.Cancelled -= registration.Terminated; + registration.Request.TimedOut -= registration.Terminated; + registration.Request.Rejected -= registration.Terminated; + exchange.Completed -= registration.Completed; + } + } + } + + /// 取消当前分块交换并释放其状态和计时器,允许随后重新开始交换。 + public void Clear() + { + foreach (var entry in _activeExchanges.ToArray()) { - if (Log.IsEnabled(LogLevel.Information)) - Log.LogInformation("Block2 transfer timed out: " + exchange.Request); + Exchange exchange = entry.Key; + Request original = entry.Value.Request; + lock (exchange) + { + CleanupRegistration currentRegistration; + if (!_activeExchanges.TryGetValue(exchange, out currentRegistration) + || !Object.ReferenceEquals(currentRegistration, entry.Value)) + continue; + ReleaseBlockwiseState(exchange, false); + if (!exchange.Complete) + exchange.Complete = true; + if (exchange.Origin == Origin.Local && !original.IsCancelled && !original.IsTimedOut && !original.IsRejected) + original.Cancel(); + Request current = exchange.CurrentRequest; + if (current != null && !Object.ReferenceEquals(current, original) && !current.IsCancelled) + current.Cancel(); + } } - exchange.Complete = true; + } + + /// 取消所有分块交换并解除配置订阅。 + public void Dispose() + { + if (System.Threading.Interlocked.Exchange(ref _disposed, 1) != 0) + return; + Clear(); + _config.PropertyChanged -= ConfigChanged; + } + + private sealed class CleanupRegistration + { + public CleanupRegistration(Request request) { Request = request; } + public Request Request { get; } + public EventHandler Terminated { get; set; } + public EventHandler Completed { get; set; } } } } diff --git a/CoAP.Test/BlockwiseLifecycleTest.cs b/CoAP.Test/BlockwiseLifecycleTest.cs new file mode 100644 index 0000000..ecd207f --- /dev/null +++ b/CoAP.Test/BlockwiseLifecycleTest.cs @@ -0,0 +1,343 @@ +using System; +using System.Runtime.CompilerServices; +using Stopwatch = System.Diagnostics.Stopwatch; +using System.Net; +using CoAP.Channel; +using CoAP.Threading; +using System.Threading; +using System.Threading.Tasks; +using CoAP.Net; +using CoAP.Stack; +using NUnit.Framework; + +namespace CoAP +{ + [TestFixture] + public sealed class BlockwiseLifecycleTest + { + [Test] + public void ErrorWithoutBlockOptions_ReleasesUploadState() + { + using (Transfer transfer = new Transfer()) + { + transfer.StartUpload(); + Response error = new Response(StatusCode.BadRequest); + transfer.Layer.ReceiveResponse(transfer.Next, transfer.Exchange, error); + Assert.That(transfer.Exchange.RequestBlockStatus, Is.Null); + Assert.That(transfer.Exchange.ResponseBlockStatus, Is.Null); + Assert.That(transfer.Next.Response, Is.SameAs(error)); + } + } + + [Test] + public void FinalBlock2_ReleasesAssemblerAndDoesNotRetainPreviousPayload() + { + using (Transfer transfer = new Transfer()) + { + Response first = new Response(StatusCode.Content); + first.SetPayload("0123456789abcdef", MediaType.TextPlain); + first.SetBlock2(0, true, 0); + transfer.Layer.ReceiveResponse(transfer.Next, transfer.Exchange, first); + Response last = new Response(StatusCode.Content); + last.SetPayload("done", MediaType.TextPlain); + last.SetBlock2(0, false, 1); + transfer.Layer.ReceiveResponse(transfer.Next, transfer.Exchange, last); + Assert.That(transfer.Next.Response.PayloadString, Is.EqualTo("0123456789abcdefdone")); + Assert.That(transfer.Exchange.ResponseBlockStatus, Is.Null); + } + } + + [TestCase(false)] + [TestCase(true)] + public void CancelOrExchangeCompletion_ReleasesUploadState(bool complete) + { + using (Transfer transfer = new Transfer()) + { + transfer.StartUpload(); + if (complete) + transfer.Exchange.Complete = true; + else + transfer.Request.Cancel(); + Assert.That(transfer.Exchange.RequestBlockStatus, Is.Null); + Assert.That(transfer.Exchange.ResponseBlockStatus, Is.Null); + Assert.That(transfer.Exchange.Complete, Is.True); + } + } + + [Test] + public void BlockwiseDeadline_NotifiesOriginalCallerExactlyOnce() + { + using (Transfer transfer = new Transfer(40)) + using (ManualResetEventSlim timedOut = new ManualResetEventSlim()) + { + int failures = 0; + transfer.Request.TimedOut += (sender, e) => + { + Interlocked.Increment(ref failures); + timedOut.Set(); + }; + transfer.StartUpload(); + + Assert.That(timedOut.Wait(TimeSpan.FromSeconds(2)), Is.True); + Assert.That(transfer.Request.IsTimedOut, Is.True); + Assert.That(transfer.Exchange.TimedOut, Is.True); + Assert.That(transfer.Exchange.RequestBlockStatus, Is.Null); + Assert.That(transfer.Exchange.ResponseBlockStatus, Is.Null); + Assert.That(failures, Is.EqualTo(1)); + } + } + + [Test] + public void LayerDispose_CancelsPendingOriginalRequestAndReleasesState() + { + using (Transfer transfer = new Transfer()) + { + transfer.StartUpload(); + int cancellations = 0; + transfer.Request.Cancelled += (sender, e) => cancellations++; + IDisposable disposable = transfer.Layer as IDisposable; + Assert.That(disposable, Is.Not.Null); + disposable.Dispose(); + disposable.Dispose(); + Assert.That(transfer.Request.IsCancelled, Is.True); + Assert.That(transfer.Exchange.Complete, Is.True); + Assert.That(transfer.Exchange.RequestBlockStatus, Is.Null); + Assert.That(cancellations, Is.EqualTo(1)); + } + } + + [Test] + public void ExchangeTimeout_NotifiesOriginalCallerAndReleasesState() + { + using (Transfer transfer = new Transfer()) + { + transfer.StartUpload(); + int failures = 0; + transfer.Request.TimedOut += (sender, e) => failures++; + transfer.Exchange.TimedOut = true; + Assert.That(transfer.Request.IsTimedOut, Is.True); + Assert.That(failures, Is.EqualTo(1)); + Assert.That(transfer.Exchange.RequestBlockStatus, Is.Null); + } + } + + [Test] + public void FinalResponse_RemovesDeadlineAndDoesNotTimeoutLater() + { + using (Transfer transfer = new Transfer(40)) + using (ManualResetEventSlim timedOut = new ManualResetEventSlim()) + { + transfer.Request.TimedOut += (sender, e) => timedOut.Set(); + transfer.StartUpload(); + transfer.Layer.ReceiveResponse(transfer.Next, transfer.Exchange, new Response(StatusCode.BadRequest)); + Assert.That(timedOut.Wait(TimeSpan.FromMilliseconds(200)), Is.False); + Assert.That(transfer.Request.IsTimedOut, Is.False); + Assert.That(transfer.Exchange.RequestBlockStatus, Is.Null); + } + } + + [Test] + public void Clear_IsIdempotentAndAllowsAnotherTransfer() + { + using (Transfer transfer = new Transfer()) + { + transfer.StartUpload(); + int cancellations = 0; + transfer.Request.Cancelled += (sender, e) => cancellations++; + transfer.Layer.Clear(); + transfer.Layer.Clear(); + Assert.That(cancellations, Is.EqualTo(1)); + Request second = Request.NewPost(); + second.SetPayload(new byte[48], MediaType.ApplicationOctetStream); + Exchange next = new Exchange(second, Origin.Local) { Request = second }; + transfer.Layer.SendRequest(transfer.Next, next, second); + Assert.That(next.RequestBlockStatus, Is.Not.Null); + transfer.Layer.Clear(); + Assert.That(second.IsCancelled, Is.True); + Assert.That(next.RequestBlockStatus, Is.Null); + } + } + + [Test] + public void ConcurrentClearAndDispose_CancelEachOwnedExchangeOnce() + { + using (Transfer transfer = new Transfer()) + { + transfer.StartUpload(); + int cancellations = 0; + transfer.Request.Cancelled += (sender, e) => Interlocked.Increment(ref cancellations); + Task[] cleanup = + { + Task.Run(() => transfer.Layer.Clear()), + Task.Run(() => transfer.Layer.Dispose()), + Task.Run(() => transfer.Layer.Clear()) + }; + Assert.That(Task.WaitAll(cleanup, TimeSpan.FromSeconds(2)), Is.True); + Assert.That(cancellations, Is.EqualTo(1)); + Assert.That(transfer.Exchange.RequestBlockStatus, Is.Null); + Assert.That(transfer.Exchange.Complete, Is.True); + } + } + + [Test] + public void Cancellation_DetachesExchangeFromALivingLayer() + { + using (ExposedLayer layer = new ExposedLayer(new CoapConfig + { + MaxMessageSize = 16, + DefaultBlockSize = 16, + BlockwiseStatusLifetime = 60000 + })) + { + WeakReference exchange = CreateCancelledExchange(layer); + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + Assert.That(exchange.IsAlive, Is.False, "A cancelled exchange must not stay rooted by the layer or its timer."); + GC.KeepAlive(layer); + } + } + + [Test] + public void Cancellation_AllowsOriginalRequestToBeCollectedWhileEndpointRemainsAlive() + { + CoapConfig config = new CoapConfig + { + MaxMessageSize = 16, + DefaultBlockSize = 16, + AckTimeout = 5, + AckRandomFactor = 1, + MaxRetransmit = -1 + }; + using (CoAPEndPoint endpoint = new CoAPEndPoint(new DiscardingChannel(), config) + { + Executor = Executors.NoThreading + }) + { + endpoint.Start(); + WeakReference request = CreateCancelledEndpointRequest(endpoint); + Stopwatch budget = Stopwatch.StartNew(); + for (int attempt = 0; attempt < 6 && request.IsAlive && budget.Elapsed < TimeSpan.FromSeconds(1); attempt++) + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + if (request.IsAlive) + Thread.Sleep(20); + } + Assert.That(request.IsAlive, Is.False, "Endpoint indices and blockwise timers must not keep a cancelled request alive."); + GC.KeepAlive(endpoint); + } + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static WeakReference CreateCancelledEndpointRequest(CoAPEndPoint endpoint) + { + Request request = Request.NewPost(); + request.EndPoint = endpoint; + request.Destination = new IPEndPoint(IPAddress.Loopback, 5683); + request.SetPayload(new byte[48], MediaType.ApplicationOctetStream); + endpoint.SendRequest(request); + request.Cancel(); + return new WeakReference(request); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static WeakReference CreateCancelledExchange(ExposedLayer layer) + { + Request request = Request.NewPost(); + request.SetPayload(new byte[48], MediaType.ApplicationOctetStream); + Exchange exchange = new Exchange(request, Origin.Local) { Request = request }; + layer.SendRequest(new RecordingLayer(), exchange, request); + request.Cancel(); + return new WeakReference(exchange); + } + + [Test] + public void CompletedObserveNotification_ReleasesBodyButAllowsTheNextNotification() + { + using (Transfer transfer = new Transfer()) + { + transfer.Request.MarkObserve(); + for (int notification = 0; notification < 2; notification++) + { + Response first = new Response(StatusCode.Content); + first.SetPayload("0123456789abcdef", MediaType.TextPlain); + first.SetBlock2(0, true, 0); + first.Observe = notification; + transfer.Layer.ReceiveResponse(transfer.Next, transfer.Exchange, first); + Response last = new Response(StatusCode.Content); + last.SetPayload(notification.ToString(), MediaType.TextPlain); + last.SetBlock2(0, false, 1); + transfer.Layer.ReceiveResponse(transfer.Next, transfer.Exchange, last); + Assert.That(transfer.Next.Response.PayloadString, Is.EqualTo("0123456789abcdef" + notification)); + Assert.That(transfer.Next.Response.Observe, Is.EqualTo(notification)); + Assert.That(transfer.Exchange.ResponseBlockStatus, Is.Null); + Assert.That(transfer.Exchange.Complete, Is.False); + } + transfer.Request.Cancel(); + Assert.That(transfer.Exchange.Complete, Is.True); + } + } + + private sealed class Transfer : IDisposable + { + public Transfer(int lifetime = 60000) + { + Request = Request.NewPost(); + Exchange = new Exchange(Request, Origin.Local) { Request = Request }; + Layer = new ExposedLayer(new CoapConfig + { + MaxMessageSize = 16, + DefaultBlockSize = 16, + BlockwiseStatusLifetime = lifetime + }); + } + + public Request Request { get; } + public Exchange Exchange { get; } + public ExposedLayer Layer { get; } + public RecordingLayer Next { get; } = new RecordingLayer(); + + public void StartUpload() + { + Request.SetPayload(new byte[48], MediaType.ApplicationOctetStream); + Layer.SendRequest(Next, Exchange, Request); + Assert.That(Exchange.RequestBlockStatus, Is.Not.Null); + } + + public void Dispose() + { + Layer.StopTimer(Exchange); + (Layer as IDisposable)?.Dispose(); + } + } + + private class ExposedLayer : BlockwiseLayer + { + public ExposedLayer(ICoapConfig config) : base(config) { } + public void StopTimer(Exchange exchange) => ClearBlockCleanup(exchange); + } + + private sealed class RecordingLayer : INextLayer + { + public Response Response { get; private set; } + public void SendRequest(Exchange exchange, Request request) { } + public void SendResponse(Exchange exchange, Response response) { } + public void SendEmptyMessage(Exchange exchange, EmptyMessage message) { } + public void ReceiveRequest(Exchange exchange, Request request) { } + public void ReceiveResponse(Exchange exchange, Response response) => Response = response; + public void ReceiveEmptyMessage(Exchange exchange, EmptyMessage message) { } + } + + private sealed class DiscardingChannel : IChannel + { + public event EventHandler DataReceived { add { } remove { } } + public System.Net.EndPoint LocalEndPoint => new IPEndPoint(IPAddress.Loopback, 0); + public void Start() { } + public void Stop() { } + public void Dispose() { } + public void Send(byte[] data, System.Net.EndPoint destination) { } + } + } +} From 75af5de6fa0bcbed147548affd2ef53870dc1946 Mon Sep 17 00:00:00 2001 From: maikebing Date: Fri, 13 Nov 2026 03:45:36 +0800 Subject: [PATCH 2/4] feat(protocol): validate endpoint options with explicit registry --- CoAP.NET/Net/CoAPEndPoint.cs | 48 +++ CoAP.NET/Net/CoapOptionDefinition.cs | 102 ++++++ CoAP.NET/Net/CoapOptionRegistry.cs | 118 +++++++ CoAP.Test/OptionPolicyTest.cs | 483 +++++++++++++++++++++++++++ 4 files changed, 751 insertions(+) create mode 100644 CoAP.NET/Net/CoapOptionDefinition.cs create mode 100644 CoAP.NET/Net/CoapOptionRegistry.cs create mode 100644 CoAP.Test/OptionPolicyTest.cs diff --git a/CoAP.NET/Net/CoAPEndPoint.cs b/CoAP.NET/Net/CoAPEndPoint.cs index 4ada59d..b1e01c8 100644 --- a/CoAP.NET/Net/CoAPEndPoint.cs +++ b/CoAP.NET/Net/CoAPEndPoint.cs @@ -112,6 +112,12 @@ public ICoapConfig Config get { return _config; } } + /// 获取此端点实际支持的选项定义及自定义选项注册入口。 + public CoapOptionRegistry OptionRegistry { get; } = new CoapOptionRegistry(); + + /// 是否允许代理请求。仅在宿主提供实际代理消息分发器时启用;默认关闭。 + public Boolean SupportsProxying { get; set; } + public IExecutor Executor { get { return _executor; } @@ -270,6 +276,19 @@ private void ReceiveData(DataReceivedEventArgs e) request.Source = e.EndPoint; + if (!OptionRegistry.ValidateAndFilter(request, out String diagnostic)) + { + if (request.Type == MessageType.CON) + SendOptionError(request, StatusCode.BadOption, diagnostic); + return; + } + if (!SupportsProxying && (request.HasOption(OptionType.ProxyUri) || + request.HasOption(OptionType.ProxyScheme))) + { + SendOptionError(request, StatusCode.ProxyingNotSupported, null); + return; + } + Fire(ReceivingRequest, request); if (!request.IsCancelled) @@ -298,6 +317,13 @@ private void ReceiveData(DataReceivedEventArgs e) } response.Source = e.EndPoint; + if (!OptionRegistry.ValidateAndFilter(response, out _)) + { + if (response.Type == MessageType.CON) + Reject(response); + return; + } + Fire(ReceivingResponse, response); if (!response.IsCancelled) @@ -361,6 +387,28 @@ private void ReceiveData(DataReceivedEventArgs e) } } + private void SendOptionError(Request request, StatusCode status, String diagnostic) + { + Response response = Response.CreateResponse(request, status); + response.Type = request.Type == MessageType.CON ? MessageType.ACK : MessageType.NON; + response.ID = request.Type == MessageType.CON ? request.ID : Message.None; + if (diagnostic != null) + response.Payload = System.Text.Encoding.UTF8.GetBytes(diagnostic); + if (request.Type == MessageType.NON) + { + // A NON reply uses the local MID namespace and the normal + // exchange cleanup instead of echoing the request's MID. + Exchange exchange = _matcher.ReceiveRequest(request); + exchange.EndPoint = this; + exchange.Request = request; + _coapStack.SendResponse(exchange, response); + return; + } + Fire(SendingResponse, response); + if (!response.IsCancelled) + _channel.Send(Serialize(response), response.Destination); + } + private void HandleMalformedDatagram(IMessageDecoder decoder, DataReceivedEventArgs e) { if (Log.IsEnabled(LogLevel.Warning)) diff --git a/CoAP.NET/Net/CoapOptionDefinition.cs b/CoAP.NET/Net/CoapOptionDefinition.cs new file mode 100644 index 0000000..333a793 --- /dev/null +++ b/CoAP.NET/Net/CoapOptionDefinition.cs @@ -0,0 +1,102 @@ +using System; +using System.Text; + +namespace CoAP.Net +{ + /// 描述当前端点实际理解的选项及其入站校验规则。 + public sealed class CoapOptionDefinition + { + private static readonly UTF8Encoding StrictUtf8 = new UTF8Encoding(false, true); + private readonly Func _validator; + + /// 创建选项定义;注册意味着宿主实际理解该选项,而不只是知道其编号。 + /// 选项编号。 + /// 诊断名称。 + /// 值的格式。 + /// 最小字节长度。 + /// 最大字节长度。 + /// 是否用于请求。 + /// 是否用于响应。 + /// 请求中是否可重复。 + /// 响应中是否可重复。 + /// 可选的值和消息上下文校验器;仅在基本校验通过后调用。 + public CoapOptionDefinition(Int32 number, String name, OptionFormat format, + Int32 minimumLength, Int32 maximumLength, Boolean requestOption, Boolean responseOption, + Boolean repeatableInRequest = false, Boolean repeatableInResponse = false, + Func validator = null) + { + ArgumentOutOfRangeException.ThrowIfNegative(number); + ArgumentOutOfRangeException.ThrowIfGreaterThan(number, UInt16.MaxValue); + ArgumentException.ThrowIfNullOrEmpty(name); + ArgumentOutOfRangeException.ThrowIfNegative(minimumLength); + ArgumentOutOfRangeException.ThrowIfLessThan(maximumLength, minimumLength); + ArgumentOutOfRangeException.ThrowIfGreaterThan(maximumLength, 65804); + if (format == OptionFormat.Integer && maximumLength > sizeof(UInt64)) + throw new ArgumentOutOfRangeException(nameof(maximumLength)); + if (!requestOption && !responseOption) + throw new ArgumentException("The option must apply to requests or responses."); + + Number = number; + Name = name; + Format = format; + MinimumLength = minimumLength; + MaximumLength = maximumLength; + RequestOption = requestOption; + ResponseOption = responseOption; + RepeatableInRequest = repeatableInRequest; + RepeatableInResponse = repeatableInResponse; + _validator = validator; + } + + /// 选项编号。 + public Int32 Number { get; } + /// 诊断名称。 + public String Name { get; } + /// 值的格式。 + public OptionFormat Format { get; } + /// 最小字节长度。 + public Int32 MinimumLength { get; } + /// 最大字节长度。 + public Int32 MaximumLength { get; } + /// 是否用于请求。 + public Boolean RequestOption { get; } + /// 是否用于响应。 + public Boolean ResponseOption { get; } + /// 请求中是否可重复。 + public Boolean RepeatableInRequest { get; } + /// 响应中是否可重复。 + public Boolean RepeatableInResponse { get; } + + internal Boolean IsValid(Message message, Option option, Int32 occurrence) + { + if (message.IsRequest ? !RequestOption : !ResponseOption) + return false; + if (occurrence > 1 && !(message.IsRequest ? RepeatableInRequest : RepeatableInResponse)) + return false; + + Byte[] value = option.RawValue ?? Array.Empty(); + if (value.Length < MinimumLength || value.Length > MaximumLength) + return false; + if (Format == OptionFormat.String) + { + try + { + StrictUtf8.GetCharCount(value); + } + catch (DecoderFallbackException) + { + return false; + } + } + return _validator == null || _validator(message, option); + } + + internal static UInt64 ReadUnsigned(Option option) + { + UInt64 result = 0; + foreach (Byte value in option.RawValue ?? Array.Empty()) + result = (result << 8) | value; + return result; + } + } +} diff --git a/CoAP.NET/Net/CoapOptionRegistry.cs b/CoAP.NET/Net/CoapOptionRegistry.cs new file mode 100644 index 0000000..ee6d515 --- /dev/null +++ b/CoAP.NET/Net/CoapOptionRegistry.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace CoAP.Net +{ + /// 为单个端点注册实际支持的选项;原始解码器不使用此语义注册表。 + public sealed class CoapOptionRegistry + { + private readonly Object _sync = new Object(); + private Dictionary _definitions = new Dictionary(); + + /// 创建含核心、Observe 和 UDP Blockwise 选项定义的注册表。 + public CoapOptionRegistry() + { + Add(new CoapOptionDefinition(1, "If-Match", OptionFormat.Opaque, 0, 8, true, false, true)); + Add(new CoapOptionDefinition(3, "Uri-Host", OptionFormat.String, 1, 255, true, false)); + Add(new CoapOptionDefinition(4, "ETag", OptionFormat.Opaque, 1, 8, true, true, true, + validator: (message, option) => !message.IsRequest || ((Request)message).Method == Method.GET)); + Add(new CoapOptionDefinition(5, "If-None-Match", OptionFormat.Opaque, 0, 0, true, false, + validator: (message, option) => ((Request)message).Method == Method.PUT)); + Add(new CoapOptionDefinition(6, "Observe", OptionFormat.Integer, 0, 3, true, true, + validator: (message, option) => message.IsRequest + ? ((Request)message).Method == Method.GET && CoapOptionDefinition.ReadUnsigned(option) <= 1 + : Code.IsSuccess(message.Code))); + Add(new CoapOptionDefinition(7, "Uri-Port", OptionFormat.Integer, 0, 2, true, false)); + Add(new CoapOptionDefinition(8, "Location-Path", OptionFormat.String, 0, 255, false, true, false, true)); + Add(new CoapOptionDefinition(11, "Uri-Path", OptionFormat.String, 0, 255, true, false, true, + validator: (message, option) => option.StringValue != "." && option.StringValue != "..")); + Add(new CoapOptionDefinition(12, "Content-Format", OptionFormat.Integer, 0, 2, true, true)); + Add(new CoapOptionDefinition(14, "Max-Age", OptionFormat.Integer, 0, 4, false, true)); + Add(new CoapOptionDefinition(15, "Uri-Query", OptionFormat.String, 0, 255, true, false, true)); + Add(new CoapOptionDefinition(17, "Accept", OptionFormat.Integer, 0, 2, true, false)); + Add(new CoapOptionDefinition(20, "Location-Query", OptionFormat.String, 0, 255, false, true, false, true)); + Add(new CoapOptionDefinition(23, "Block2", OptionFormat.Integer, 0, 3, true, true, + validator: (message, option) => (CoapOptionDefinition.ReadUnsigned(option) & 7) != 7)); + Add(new CoapOptionDefinition(27, "Block1", OptionFormat.Integer, 0, 3, true, true, + validator: (message, option) => (CoapOptionDefinition.ReadUnsigned(option) & 7) != 7)); + Add(new CoapOptionDefinition(28, "Size2", OptionFormat.Integer, 0, 4, true, true, + validator: (message, option) => !message.IsRequest || CoapOptionDefinition.ReadUnsigned(option) == 0)); + Add(new CoapOptionDefinition(35, "Proxy-Uri", OptionFormat.String, 1, 1034, true, false, + validator: (message, option) => Uri.TryCreate(option.StringValue, UriKind.Absolute, out Uri uri) + && uri.Fragment.Length == 0)); + Add(new CoapOptionDefinition(39, "Proxy-Scheme", OptionFormat.String, 1, 255, true, false, + validator: (message, option) => Uri.CheckSchemeName(option.StringValue))); + Add(new CoapOptionDefinition(60, "Size1", OptionFormat.Integer, 0, 4, true, true, + validator: (message, option) => message.IsRequest || message.Code == Code.RequestEntityTooLarge)); + } + + /// 注册宿主实际处理的自定义选项;不能覆盖已有协议定义。 + /// 不可变的选项定义。 + public void Register(CoapOptionDefinition definition) + { + ArgumentNullException.ThrowIfNull(definition); + lock (_sync) + { + Dictionary next = new Dictionary(_definitions); + next.Add(definition.Number, definition); + Volatile.Write(ref _definitions, next); + } + } + + private void Add(CoapOptionDefinition definition) + { + _definitions.Add(definition.Number, definition); + } + + internal Boolean ValidateAndFilter(Message message, out String diagnostic) + { + Dictionary definitions = Volatile.Read(ref _definitions); + Dictionary occurrences = new Dictionary(); + Dictionary> retained = new Dictionary>(); + HashSet changed = new HashSet(); + foreach (Option option in message.GetOptions()) + { + occurrences.TryGetValue(option.Type, out Int32 count); + occurrences[option.Type] = ++count; + Boolean known = definitions.TryGetValue((Int32)option.Type, out CoapOptionDefinition definition); + Boolean valid = known && definition.IsValid(message, option, count); + if (!valid && Option.IsCritical(option.Type)) + { + diagnostic = "Unrecognized or invalid option " + (Int32)option.Type; + return false; + } + if (known && !valid) + { + changed.Add(option.Type); + continue; + } + if (!retained.TryGetValue(option.Type, out List Accept = 17, /// - /// C, Sequence of Bytes, 1-2 B, -. NOTE: this option has been replaced with since draft 13. - /// draft-ietf-core-coap-03, draft-ietf-core-coap-12 + /// RFC 9177 的 Q-Block1 option number;仅定义 wire registry,不表示已实现 Q-Block 传输。 /// + QBlock1 = 19, + /// + /// 旧草案 Token option 的兼容名称;现行数字 19 表示 Q-Block1,报文头 Token 使用 。 + /// + [System.Obsolete("旧草案 Token option 已弃用。请使用 Message.Token 设置报文头 Token;现行 option 19 为 Q-Block1。")] Token = 19, /// /// E, String, 1-270 B, - @@ -139,6 +143,10 @@ public enum OptionType /// Block1 = 27, /// + /// RFC 9177 的 Q-Block2 option number;仅定义 wire registry,不表示已实现 Q-Block 传输。 + /// + QBlock2 = 31, + /// /// draft-ietf-core-block /// Size2 = 28, diff --git a/CoAP.NET/Spec.cs b/CoAP.NET/Spec.cs index 1ce498e..fb26284 100644 --- a/CoAP.NET/Spec.cs +++ b/CoAP.NET/Spec.cs @@ -22,6 +22,13 @@ namespace CoAP { +#if DRAFT + internal static class LegacyOptionNumbers + { + internal const OptionType Token = (OptionType)19; + } +#endif + #region CoAP 03 #if COAPALL || COAP03 static partial class Spec @@ -123,7 +130,7 @@ private static Int32 GetOptionNumber(OptionType optionType) return 8; case OptionType.UriPath: return 9; - case OptionType.Token: + case LegacyOptionNumbers.Token: return 11; case OptionType.UriQuery: return 15; @@ -165,7 +172,7 @@ private static OptionType GetOptionType(Int32 optionNumber) case 9: return OptionType.UriPath; case 11: - return OptionType.Token; + return LegacyOptionNumbers.Token; case 15: return OptionType.UriQuery; case 10: @@ -254,8 +261,10 @@ protected override void Serialize(DatagramWriter writer, Message msg, Int32 code IEnumerable