2 Commits

Author SHA1 Message Date
6a63dbb739 Bump to v0.3.1 2024-06-28 21:37:26 +02:00
1536c60336 Fixing wrong range validations 2024-06-28 21:37:00 +02:00
7 changed files with 42 additions and 22 deletions

View File

@@ -46,7 +46,7 @@ namespace AMWD.Protocols.Modbus.Proxy
ListenAddress = listenAddress ?? IPAddress.Loopback; ListenAddress = listenAddress ?? IPAddress.Loopback;
if (ushort.MinValue < listenPort || listenPort < ushort.MaxValue) if (listenPort < ushort.MinValue || ushort.MaxValue < listenPort)
throw new ArgumentOutOfRangeException(nameof(listenPort)); throw new ArgumentOutOfRangeException(nameof(listenPort));
try try

View File

@@ -2,7 +2,7 @@
With this package the server and client implementations will be combined as proxy. With this package the server and client implementations will be combined as proxy.
You can use any `ModbusBasClient` implementation as target client and plug it into the implemented `ModbusTcpProxy` or `ModbusRtuProxy`, which implement the server side. You can use any `ModbusBaseClient` implementation as target client and plug it into the implemented `ModbusTcpProxy` or `ModbusRtuProxy`, which implement the server side.
--- ---

View File

@@ -186,7 +186,7 @@ namespace AMWD.Protocols.Modbus.Serial
private Task StopAsyncInternal(CancellationToken cancellationToken) private Task StopAsyncInternal(CancellationToken cancellationToken)
{ {
_stopCts.Cancel(); _stopCts?.Cancel();
_serialPort.Close(); _serialPort.Close();
_serialPort.DataReceived -= OnDataReceived; _serialPort.DataReceived -= OnDataReceived;
@@ -328,7 +328,7 @@ namespace AMWD.Protocols.Modbus.Serial
ushort firstAddress = requestBytes.GetBigEndianUInt16(2); ushort firstAddress = requestBytes.GetBigEndianUInt16(2);
ushort count = requestBytes.GetBigEndianUInt16(4); ushort count = requestBytes.GetBigEndianUInt16(4);
if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_DISCRETE_READ_COUNT) if (count < RtuProtocol.MIN_READ_COUNT || RtuProtocol.MAX_DISCRETE_READ_COUNT < count)
{ {
responseBytes[1] |= 0x80; responseBytes[1] |= 0x80;
responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue);
@@ -385,7 +385,7 @@ namespace AMWD.Protocols.Modbus.Serial
ushort firstAddress = requestBytes.GetBigEndianUInt16(2); ushort firstAddress = requestBytes.GetBigEndianUInt16(2);
ushort count = requestBytes.GetBigEndianUInt16(4); ushort count = requestBytes.GetBigEndianUInt16(4);
if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_DISCRETE_READ_COUNT) if (count < RtuProtocol.MIN_READ_COUNT || RtuProtocol.MAX_DISCRETE_READ_COUNT < count)
{ {
responseBytes[1] |= 0x80; responseBytes[1] |= 0x80;
responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue);
@@ -442,7 +442,7 @@ namespace AMWD.Protocols.Modbus.Serial
ushort firstAddress = requestBytes.GetBigEndianUInt16(2); ushort firstAddress = requestBytes.GetBigEndianUInt16(2);
ushort count = requestBytes.GetBigEndianUInt16(4); ushort count = requestBytes.GetBigEndianUInt16(4);
if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_REGISTER_READ_COUNT) if (count < RtuProtocol.MIN_READ_COUNT || RtuProtocol.MAX_REGISTER_READ_COUNT < count)
{ {
responseBytes[1] |= 0x80; responseBytes[1] |= 0x80;
responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue);
@@ -496,7 +496,7 @@ namespace AMWD.Protocols.Modbus.Serial
ushort firstAddress = requestBytes.GetBigEndianUInt16(2); ushort firstAddress = requestBytes.GetBigEndianUInt16(2);
ushort count = requestBytes.GetBigEndianUInt16(4); ushort count = requestBytes.GetBigEndianUInt16(4);
if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_REGISTER_READ_COUNT) if (count < RtuProtocol.MIN_READ_COUNT || RtuProtocol.MAX_REGISTER_READ_COUNT < count)
{ {
responseBytes[1] |= 0x80; responseBytes[1] |= 0x80;
responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue);

View File

@@ -34,8 +34,8 @@ namespace AMWD.Protocols.Modbus.Tcp
private readonly Task _processingTask; private readonly Task _processingTask;
private readonly AsyncQueue<RequestQueueItem> _requestQueue = new(); private readonly AsyncQueue<RequestQueueItem> _requestQueue = new();
private TimeSpan _readTimeout = TimeSpan.FromMilliseconds(1); private TimeSpan _readTimeout = TimeSpan.FromSeconds(1);
private TimeSpan _writeTimeout = TimeSpan.FromMilliseconds(1); private TimeSpan _writeTimeout = TimeSpan.FromSeconds(1);
#endregion Fields #endregion Fields

View File

@@ -36,6 +36,8 @@ namespace AMWD.Protocols.Modbus.Tcp
private readonly ReaderWriterLockSlim _deviceListLock = new(); private readonly ReaderWriterLockSlim _deviceListLock = new();
private readonly Dictionary<byte, ModbusDevice> _devices = []; private readonly Dictionary<byte, ModbusDevice> _devices = [];
private TimeSpan _readWriteTimeout = TimeSpan.FromSeconds(1);
#endregion Fields #endregion Fields
#region Constructors #region Constructors
@@ -49,7 +51,7 @@ namespace AMWD.Protocols.Modbus.Tcp
{ {
ListenAddress = listenAddress ?? IPAddress.Loopback; ListenAddress = listenAddress ?? IPAddress.Loopback;
if (ushort.MinValue < listenPort || listenPort < ushort.MaxValue) if (listenPort < ushort.MinValue || ushort.MaxValue < listenPort)
throw new ArgumentOutOfRangeException(nameof(listenPort)); throw new ArgumentOutOfRangeException(nameof(listenPort));
try try
@@ -105,7 +107,17 @@ namespace AMWD.Protocols.Modbus.Tcp
/// <summary> /// <summary>
/// Gets or sets the read/write timeout. /// Gets or sets the read/write timeout.
/// </summary> /// </summary>
public TimeSpan ReadWriteTimeout { get; set; } public TimeSpan ReadWriteTimeout
{
get => _readWriteTimeout;
set
{
if (value < TimeSpan.Zero)
throw new ArgumentOutOfRangeException(nameof(value));
_readWriteTimeout = value;
}
}
#endregion Properties #endregion Properties
@@ -151,11 +163,11 @@ namespace AMWD.Protocols.Modbus.Tcp
private async Task StopAsyncInternal(CancellationToken cancellationToken = default) private async Task StopAsyncInternal(CancellationToken cancellationToken = default)
{ {
_stopCts.Cancel(); _stopCts?.Cancel();
_listener.Stop(); _listener?.Stop();
#if NET8_0_OR_GREATER #if NET8_0_OR_GREATER
_listener.Dispose(); _listener?.Dispose();
#endif #endif
try try
{ {
@@ -354,7 +366,7 @@ namespace AMWD.Protocols.Modbus.Tcp
ushort firstAddress = requestBytes.GetBigEndianUInt16(8); ushort firstAddress = requestBytes.GetBigEndianUInt16(8);
ushort count = requestBytes.GetBigEndianUInt16(10); ushort count = requestBytes.GetBigEndianUInt16(10);
if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_DISCRETE_READ_COUNT) if (count < TcpProtocol.MIN_READ_COUNT || TcpProtocol.MAX_DISCRETE_READ_COUNT < count)
{ {
responseBytes[7] |= 0x80; responseBytes[7] |= 0x80;
responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue);
@@ -406,7 +418,7 @@ namespace AMWD.Protocols.Modbus.Tcp
ushort firstAddress = requestBytes.GetBigEndianUInt16(8); ushort firstAddress = requestBytes.GetBigEndianUInt16(8);
ushort count = requestBytes.GetBigEndianUInt16(10); ushort count = requestBytes.GetBigEndianUInt16(10);
if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_DISCRETE_READ_COUNT) if (count < TcpProtocol.MIN_READ_COUNT || TcpProtocol.MAX_DISCRETE_READ_COUNT < count)
{ {
responseBytes[7] |= 0x80; responseBytes[7] |= 0x80;
responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue);
@@ -458,7 +470,7 @@ namespace AMWD.Protocols.Modbus.Tcp
ushort firstAddress = requestBytes.GetBigEndianUInt16(8); ushort firstAddress = requestBytes.GetBigEndianUInt16(8);
ushort count = requestBytes.GetBigEndianUInt16(10); ushort count = requestBytes.GetBigEndianUInt16(10);
if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_REGISTER_READ_COUNT) if (count < TcpProtocol.MIN_READ_COUNT || TcpProtocol.MAX_REGISTER_READ_COUNT < count)
{ {
responseBytes[7] |= 0x80; responseBytes[7] |= 0x80;
responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue);
@@ -507,7 +519,7 @@ namespace AMWD.Protocols.Modbus.Tcp
ushort firstAddress = requestBytes.GetBigEndianUInt16(8); ushort firstAddress = requestBytes.GetBigEndianUInt16(8);
ushort count = requestBytes.GetBigEndianUInt16(10); ushort count = requestBytes.GetBigEndianUInt16(10);
if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_REGISTER_READ_COUNT) if (count < TcpProtocol.MIN_READ_COUNT || TcpProtocol.MAX_REGISTER_READ_COUNT < count)
{ {
responseBytes[7] |= 0x80; responseBytes[7] |= 0x80;
responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue); responseBytes.Add((byte)ModbusErrorCode.IllegalDataValue);

View File

@@ -21,10 +21,10 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" /> <PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.2" /> <PackageReference Include="MSTest.TestAdapter" Version="3.4.3" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" /> <PackageReference Include="MSTest.TestFramework" Version="3.4.3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -10,6 +10,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
_no changes_ _no changes_
## [v0.3.1] (2024-06-28)
### Fixed
- Issues with range validation on several lines of code in server implementations.
## [v0.3.0] (2024-05-31) ## [v0.3.0] (2024-05-31)
### Added ### Added
@@ -38,6 +45,7 @@ So this tag is only here for documentation purposes of the NuGet Gallery.
[Unreleased]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.3.0...HEAD [Unreleased]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.3.1...HEAD
[v0.3.1]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.3.0...v0.3.1
[v0.3.0]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.2.0...v0.3.0 [v0.3.0]: https://github.com/AM-WD/AMWD.Protocols.Modbus/compare/v0.2.0...v0.3.0
[v0.2.0]: https://github.com/AM-WD/AMWD.Protocols.Modbus/tree/v0.2.0 [v0.2.0]: https://github.com/AM-WD/AMWD.Protocols.Modbus/tree/v0.2.0