Fixing wrong range validations
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user