diff --git a/NET Core/LibUA/LibUA.csproj b/NET Core/LibUA/LibUA.csproj index bf876f1..603e641 100644 --- a/NET Core/LibUA/LibUA.csproj +++ b/NET Core/LibUA/LibUA.csproj @@ -10,7 +10,7 @@ LibUA Core git https://github.com/nauful/libua - 1.0.30 + 1.0.31 README.md LICENSE diff --git a/NET Core/LibUA/NetDispatcher.cs b/NET Core/LibUA/NetDispatcher.cs index aacaa21..c8be8c8 100644 --- a/NET Core/LibUA/NetDispatcher.cs +++ b/NET Core/LibUA/NetDispatcher.cs @@ -564,13 +564,22 @@ protected override int Consume(MemoryBuffer recvBuf) } else if (messageType == (uint)MessageType.Open) { + uint messageSize = + (uint)recvBuf.Buffer[4] | (uint)(recvBuf.Buffer[5] << 8) | + (uint)(recvBuf.Buffer[6] << 16) | (uint)(recvBuf.Buffer[7] << 24); + + if (messageSize > recvBuf.Capacity) + { + return 0; + } + if (config.TL == null) { logger?.Log(LogLevel.Error, "{LoggerID}: Message type 0x{messageType:X} is not supported before Hello", LoggerID(), messageType); return ErrorInternal; } - return DispatchOpen(config, recvBuf); + return DispatchOpen(config, recvBuf.Duplicate((int)messageSize)); } else if (messageType == (uint)MessageType.Message || messageType == (uint)MessageType.Close) diff --git a/NET/LibUA/Client.cs b/NET/LibUA/Client.cs index 7dab743..1efad42 100644 --- a/NET/LibUA/Client.cs +++ b/NET/LibUA/Client.cs @@ -1397,15 +1397,23 @@ private int Consume(SLChannel config, MemoryBuffer recvBuf) } else if (messageType == (uint)MessageType.Open) { - ManualResetEvent ev = null; + messageSize = + (uint)recvBuf.Buffer[4] | (uint)(recvBuf.Buffer[5] << 8) | + (uint)(recvBuf.Buffer[6] << 16) | (uint)(recvBuf.Buffer[7] << 24); + if (messageSize > recvBuf.Capacity) + { + return 0; + } + + ManualResetEvent ev = null; lock (recvQueue) { var key = new Tuple(messageType, 0); recvQueue[key] = new RecvHandler() { Header = null, - RecvBuf = recvBuf.Duplicate(), + RecvBuf = recvBuf.Duplicate((int)messageSize), Type = NodeId.Zero }; diff --git a/NET/LibUA/MemoryBuffer.cs b/NET/LibUA/MemoryBuffer.cs index 9b68b9d..0da92b9 100644 --- a/NET/LibUA/MemoryBuffer.cs +++ b/NET/LibUA/MemoryBuffer.cs @@ -766,6 +766,16 @@ public MemoryBuffer Duplicate() return mb; } + + public MemoryBuffer Duplicate(int TargetCapacity) + { + var mb = new MemoryBuffer(TargetCapacity); + + mb.Append(Buffer, TargetCapacity); + mb.Position = Position; + + return mb; + } } } } diff --git a/NET/LibUA/NetDispatcher.cs b/NET/LibUA/NetDispatcher.cs index a58e63a..19923b5 100644 --- a/NET/LibUA/NetDispatcher.cs +++ b/NET/LibUA/NetDispatcher.cs @@ -565,13 +565,22 @@ protected override int Consume(MemoryBuffer recvBuf) } else if (messageType == (uint)MessageType.Open) { + uint messageSize = + (uint)recvBuf.Buffer[4] | (uint)(recvBuf.Buffer[5] << 8) | + (uint)(recvBuf.Buffer[6] << 16) | (uint)(recvBuf.Buffer[7] << 24); + + if (messageSize > recvBuf.Capacity) + { + return 0; + } + if (config.TL == null) { logger?.Log(LogLevel.Error, "{LoggerID}: Message type 0x{messageType:X} is not supported before Hello", LoggerID(), messageType); return ErrorInternal; } - return DispatchOpen(config, recvBuf); + return DispatchOpen(config, recvBuf.Duplicate((int)messageSize)); } else if (messageType == (uint)MessageType.Message || messageType == (uint)MessageType.Close) diff --git a/README.md b/README.md index 4ac3c94..de3fba4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # LibUA Open-source OPC UA client and server library for .NET Framework and .NET Core based on IEC 62541. Available a library, a demo client and a demo server. Tested and commercially used in industrial applications with commercial vendors' UA servers and clients. -Available as a nuget package for .NET Core (1.0.30): +Available as a nuget package for .NET Core (1.0.31): https://www.nuget.org/packages/nauful-LibUA-core ### Features