Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## [Unreleased]

### Token、unsigned option 与 Observe deadline — 2026-11-13

- #33 的自动请求 Token 改为平台 CSPRNG 生成八字节,保留显式 Token、重传、Blockwise 与 Observe 身份。当前 channel/exchange 没有真实 DTLS session/traffic-key/re-key identity,RFC 9175 secure-context non-reuse 仍 UNKNOWN,issue 保持开放。
- #34 新增 `Option.UIntValue`、`Message.Size1Unsigned` / `Size2Unsigned`,覆盖完整 UInt32 范围;旧 signed API 和签名保留,typed raw getter 对过长值抛 `OverflowException`。Resource/MVC 反射与生成绑定支持 `uint` / `uint?`;组装保留只在首块出现的 Size 提示,不以提示预分配或替代 M 位。
- #35 的 Observe 重注册采用 monotonic deadline、单一 `ITimer` 和最长 `Int32.MaxValue` 毫秒分片,支持 `UInt32.MaxValue` Max-Age。通知更新 deadline,取消、终态、Clear、Stop、Dispose 释放 timer。
- [本批审计](docs/audits/coap-20261113/next-batch-validation.json)记录原始 FAIL 与本地回归;真实 peer、NativeAOT 实物、公开包与现场验收保持独立,#26 双实现矩阵为 NOT_RUN。

### Modern option semantics and lifecycle — 2026-11-13

- 按用户批准的兼容选择,默认 AddOption/codec 不再把 wire number 19 转成消息头 Token;旧 Token 名称保留并弃用,迁移到 Message.Token。QBlock1/2 仅登记现行名称/格式,默认未启用其处理器。
Expand Down
52 changes: 51 additions & 1 deletion CoAP.NET/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ public Int64 MaxAge
get
{
Option opt = GetFirstOption(OptionType.MaxAge);
return (null == opt) ? CoapConstants.DefaultMaxAge : opt.LongValue;
return (null == opt) ? CoapConstants.DefaultMaxAge : (Int64)opt.UIntValue;
}
set
{
Expand Down Expand Up @@ -1006,6 +1006,11 @@ public Int32? Observe
/// <summary>
/// Gets or sets the Size1 option. Be <code>null</code> if not set.
/// </summary>
/// <remarks>
/// Retains the legacy signed four-byte mapping, so 0x80000000..0xffffffff
/// read as negative values. Use <see cref="Size1Unsigned"/> for the full range.
/// </remarks>
/// <exception cref="OverflowException">The raw value is longer than four bytes.</exception>
public Int32? Size1
{
get
Expand All @@ -1025,6 +1030,11 @@ public Int32? Size1
/// <summary>
/// Gets or sets the Size2 option. Be <code>null</code> if not set.
/// </summary>
/// <remarks>
/// Retains the legacy signed four-byte mapping, so 0x80000000..0xffffffff
/// read as negative values. Use <see cref="Size2Unsigned"/> for the full range.
/// </remarks>
/// <exception cref="OverflowException">The raw value is longer than four bytes.</exception>
public Int32? Size2
{
get
Expand All @@ -1041,6 +1051,46 @@ public Int32? Size2
}
}

/// <summary>
/// Gets or sets Size1 in the full RFC 7959 unsigned range. Null means absent.
/// </summary>
/// <exception cref="OverflowException">The raw value is longer than four bytes.</exception>
public UInt32? Size1Unsigned
{
get
{
Option opt = GetFirstOption(OptionType.Size1);
return opt == null ? default(UInt32?) : opt.UIntValue;
}
set
{
if (value.HasValue)
SetOption(Option.Create(OptionType.Size1, value.Value));
else
RemoveOptions(OptionType.Size1);
}
}

/// <summary>
/// Gets or sets Size2 in the full RFC 7959 unsigned range. Null means absent.
/// </summary>
/// <exception cref="OverflowException">The raw value is longer than four bytes.</exception>
public UInt32? Size2Unsigned
{
get
{
Option opt = GetFirstOption(OptionType.Size2);
return opt == null ? default(UInt32?) : opt.UIntValue;
}
set
{
if (value.HasValue)
SetOption(Option.Create(OptionType.Size2, value.Value));
else
RemoveOptions(OptionType.Size2);
}
}

public BlockOption Block1
{
get { return GetFirstOption(OptionType.Block1) as BlockOption; }
Expand Down
78 changes: 76 additions & 2 deletions CoAP.NET/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,16 @@ public String StringValue
/// <summary>
/// Gets or sets int value of the option.
/// </summary>
/// <remarks>
/// Four-byte values retain the legacy signed bit pattern. Use
/// <see cref="UIntValue"/> for unsigned CoAP integers up to UInt32.MaxValue.
/// </remarks>
/// <exception cref="OverflowException">The raw value exceeds the integer or option width.</exception>
public Int32 IntValue
{
get
{
ValidateIntegerLength(sizeof(Int32), Length);
return (Int32)int32Convertor.Decode(this._valueBytes);
}
set
Expand All @@ -108,10 +114,16 @@ public Int32 IntValue
/// <summary>
/// Gets or sets long value of the option.
/// </summary>
/// <remarks>
/// Known integer options enforce their wire length. Other options retain
/// the legacy signed eight-byte bit pattern.
/// </remarks>
/// <exception cref="OverflowException">The raw value exceeds the integer or option width.</exception>
public Int64 LongValue
{
get
{
ValidateIntegerLength(sizeof(Int64), Length);
return (Int64)int64Convertor.Decode(_valueBytes);
}
set
Expand All @@ -120,6 +132,53 @@ public Int64 LongValue
}
}

/// <summary>
/// Gets or sets an unsigned integer in the range 0..UInt32.MaxValue.
/// Zero is encoded as an empty value; legal leading zero bytes are accepted.
/// </summary>
/// <exception cref="OverflowException">The value exceeds the integer or option width.</exception>
public UInt32 UIntValue
{
get
{
ValidateIntegerLength(sizeof(UInt32), Length);
return unchecked((UInt32)int32Convertor.Decode(_valueBytes));
}
set
{
Byte[] bytes = int64Convertor.Encode(value);
ValidateIntegerLength(sizeof(UInt32), bytes.Length);
_valueBytes = bytes;
}
}

private void ValidateIntegerLength(Int32 width, Int32 length)
{
switch (_type)
{
case OptionType.ContentFormat:
case OptionType.Accept:
case OptionType.UriPort:
width = Math.Min(width, 2);
break;
case OptionType.Observe:
case OptionType.Block1:
case OptionType.Block2:
case OptionType.QBlock1:
case OptionType.QBlock2:
width = Math.Min(width, 3);
break;
case OptionType.MaxAge:
case OptionType.Size1:
case OptionType.Size2:
width = Math.Min(width, 4);
break;
}

if (length > width)
throw new OverflowException(Name + " integer value exceeds " + width + " bytes.");
}

/// <summary>
/// Gets the value of the option according to its type.
/// </summary>
Expand All @@ -128,6 +187,8 @@ public Object Value
get
{
IConvertor convertor = GetConvertor(this._type);
if (GetFormatByType(_type) == OptionFormat.Integer)
ValidateIntegerLength(sizeof(Int32), Length);
return null == convertor ? null : convertor.Decode(this._valueBytes);
}
}
Expand All @@ -143,7 +204,7 @@ public Boolean IsDefault
switch (this._type)
{
case OptionType.MaxAge:
return IntValue == CoapConstants.DefaultMaxAge;
return UIntValue == CoapConstants.DefaultMaxAge;
default:
return false;
}
Expand All @@ -157,7 +218,7 @@ public String ToValueString()
case OptionFormat.Integer:
return (_type == OptionType.Accept || _type == OptionType.ContentFormat) ?
("\"" + MediaType.ToString(IntValue) + "\"") :
IntValue.ToString();
UIntValue.ToString();
case OptionFormat.String:
return "\"" + StringValue + "\"";
default:
Expand Down Expand Up @@ -274,6 +335,19 @@ public static Option Create(OptionType type, Int64 val)
return opt;
}

/// <summary>
/// Creates an option from an unsigned integer without narrowing its value.
/// </summary>
/// <param name="type">The type of the option</param>
/// <param name="val">The unsigned value of the option</param>
/// <returns>The new option</returns>
public static Option Create(OptionType type, UInt32 val)
{
Option opt = Create(type);
opt.UIntValue = val;
return opt;
}

/// <summary>
/// Splits a string into a set of options, e.g. a uri path.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions CoAP.NET/Server/Routing/CoapActionParameterBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ private static object ConvertOptionValue(Option option, Type targetType, string
return option.LongValue;
}

if (targetType == typeof(uint))
{
return option.UIntValue;
}

if (targetType == typeof(bool))
{
return true;
Expand Down
5 changes: 5 additions & 0 deletions CoAP.NET/Server/Routing/CoapGeneratedEndpointHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,11 @@ private static T ConvertOptionValue<T>(Option option, string parameterName)
return (T)(object)option.LongValue;
}

if (targetType == typeof(uint))
{
return (T)(object)option.UIntValue;
}

if (targetType == typeof(bool))
{
return (T)(object)true;
Expand Down
12 changes: 12 additions & 0 deletions CoAP.NET/Stack/BlockwiseLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ public override void ReceiveRequest(INextLayer nextLayer, Exchange exchange, Req
if (IsTerminated(exchange) || !Object.ReferenceEquals(status, exchange.RequestBlockStatus))
return;
status.AddBlock(request.Payload);
if (request.HasOption(OptionType.Size1))
status.Size1 = request.Size1Unsigned;
}
}
else
Expand Down Expand Up @@ -466,6 +468,8 @@ public override void ReceiveResponse(INextLayer nextLayer, Exchange exchange, Re
if (IsTerminated(exchange) || !Object.ReferenceEquals(status, exchange.ResponseBlockStatus))
return;
status.AddBlock(response.Payload);
if (response.HasOption(OptionType.Size2))
status.Size2 = response.Size2Unsigned;
Int32? obs = response.Observe;
if (obs.HasValue)
status.Observe = obs.Value;
Expand Down Expand Up @@ -845,6 +849,14 @@ private void AssembleMessage(BlockwiseStatus status, Message message, Message la
message.Type = last.Type;
message.SetOptions(last.GetOptions());

// An unchanged size indication may be omitted after the first block
// (RFC 7959 section 4). It remains metadata, not an allocation limit
// or an alternative to the Block option's M bit.
if (message is Request && !message.HasOption(OptionType.Size1))
message.Size1Unsigned = status.Size1;
if (message is Response && !message.HasOption(OptionType.Size2))
message.Size2Unsigned = status.Size2;

Int32 length = 0;
foreach (Byte[] block in status.Blocks)
length += block.Length;
Expand Down
5 changes: 5 additions & 0 deletions CoAP.NET/Stack/BlockwiseStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class BlockwiseStatus
private Int32 _observe = NoObserve;
private List<Byte[]> _blocks = new List<Byte[]>();

// RFC 7959 section 4 permits a size estimate on the first block only.
// Keep just the latest scalar indication, never allocate from it.
internal UInt32? Size1 { get; set; }
internal UInt32? Size2 { get; set; }

/// <summary>
/// Instantiates a new blockwise status.
/// </summary>
Expand Down
Loading
Loading