From f3f11ba20a5aab49b66416251769403bd34a00cc Mon Sep 17 00:00:00 2001 From: Tal Aloni Date: Tue, 3 Jan 2017 13:58:00 +0200 Subject: [PATCH] NetBIOS packets can now be read starting from a specified buffer offset to avoid unnecessary memory copy operation --- .../NegativeSessionResponsePacket.cs | 6 ++-- .../PositiveSessionResponsePacket.cs | 4 +-- .../SessionPackets/SessionKeepAlivePacket.cs | 4 +-- .../SessionPackets/SessionMessagePacket.cs | 4 +-- .../NetBios/SessionPackets/SessionPacket.cs | 28 +++++++++---------- .../SessionPackets/SessionRequestPacket.cs | 5 ++-- .../SessionRetargetResponsePacket.cs | 8 +++--- SMBLibrary/Server/SMBServer.cs | 6 ++-- 8 files changed, 32 insertions(+), 33 deletions(-) diff --git a/SMBLibrary/NetBios/SessionPackets/NegativeSessionResponsePacket.cs b/SMBLibrary/NetBios/SessionPackets/NegativeSessionResponsePacket.cs index f4ce628..f53b054 100644 --- a/SMBLibrary/NetBios/SessionPackets/NegativeSessionResponsePacket.cs +++ b/SMBLibrary/NetBios/SessionPackets/NegativeSessionResponsePacket.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2017 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, @@ -23,9 +23,9 @@ namespace SMBLibrary.NetBios this.Type = SessionPacketTypeName.NegativeSessionResponse; } - public NegativeSessionResponsePacket(byte[] buffer) : base(buffer) + public NegativeSessionResponsePacket(byte[] buffer, int offset) : base(buffer, offset) { - ErrorCode = ByteReader.ReadByte(this.Trailer, 0); + ErrorCode = ByteReader.ReadByte(this.Trailer, offset + 0); } public override byte[] GetBytes() diff --git a/SMBLibrary/NetBios/SessionPackets/PositiveSessionResponsePacket.cs b/SMBLibrary/NetBios/SessionPackets/PositiveSessionResponsePacket.cs index dad5aec..91805fb 100644 --- a/SMBLibrary/NetBios/SessionPackets/PositiveSessionResponsePacket.cs +++ b/SMBLibrary/NetBios/SessionPackets/PositiveSessionResponsePacket.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2017 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, @@ -21,7 +21,7 @@ namespace SMBLibrary.NetBios this.Type = SessionPacketTypeName.PositiveSessionResponse; } - public PositiveSessionResponsePacket(byte[] buffer) : base(buffer) + public PositiveSessionResponsePacket(byte[] buffer, int offset) : base(buffer, offset) { } diff --git a/SMBLibrary/NetBios/SessionPackets/SessionKeepAlivePacket.cs b/SMBLibrary/NetBios/SessionPackets/SessionKeepAlivePacket.cs index d6cdb66..a5f434e 100644 --- a/SMBLibrary/NetBios/SessionPackets/SessionKeepAlivePacket.cs +++ b/SMBLibrary/NetBios/SessionPackets/SessionKeepAlivePacket.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2017 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, @@ -21,7 +21,7 @@ namespace SMBLibrary.NetBios this.Type = SessionPacketTypeName.SessionKeepAlive; } - public SessionKeepAlivePacket(byte[] buffer) : base(buffer) + public SessionKeepAlivePacket(byte[] buffer, int offset) : base(buffer, offset) { } diff --git a/SMBLibrary/NetBios/SessionPackets/SessionMessagePacket.cs b/SMBLibrary/NetBios/SessionPackets/SessionMessagePacket.cs index e0b0c28..1cb5b4e 100644 --- a/SMBLibrary/NetBios/SessionPackets/SessionMessagePacket.cs +++ b/SMBLibrary/NetBios/SessionPackets/SessionMessagePacket.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2017 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, @@ -21,7 +21,7 @@ namespace SMBLibrary.NetBios this.Type = SessionPacketTypeName.SessionMessage; } - public SessionMessagePacket(byte[] buffer) : base(buffer) + public SessionMessagePacket(byte[] buffer, int offset) : base(buffer, offset) { } } diff --git a/SMBLibrary/NetBios/SessionPackets/SessionPacket.cs b/SMBLibrary/NetBios/SessionPackets/SessionPacket.cs index 2058c48..a0ec47b 100644 --- a/SMBLibrary/NetBios/SessionPackets/SessionPacket.cs +++ b/SMBLibrary/NetBios/SessionPackets/SessionPacket.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2017 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, @@ -25,13 +25,13 @@ namespace SMBLibrary.NetBios { } - public SessionPacket(byte[] buffer) + public SessionPacket(byte[] buffer, int offset) { - Type = (SessionPacketTypeName)ByteReader.ReadByte(buffer, 0); - Flags = ByteReader.ReadByte(buffer, 1); - Length = (Flags & 0x01) << 16 | BigEndianConverter.ToUInt16(buffer, 2); + Type = (SessionPacketTypeName)ByteReader.ReadByte(buffer, offset + 0); + Flags = ByteReader.ReadByte(buffer, offset + 1); + Length = (Flags & 0x01) << 16 | BigEndianConverter.ToUInt16(buffer, offset + 2); - this.Trailer = ByteReader.ReadBytes(buffer, 4, Length); + this.Trailer = ByteReader.ReadBytes(buffer, offset + 4, Length); } public virtual byte[] GetBytes() @@ -53,23 +53,23 @@ namespace SMBLibrary.NetBios return buffer; } - public static SessionPacket GetSessionPacket(byte[] buffer) + public static SessionPacket GetSessionPacket(byte[] buffer, int offset) { - SessionPacketTypeName type = (SessionPacketTypeName)ByteReader.ReadByte(buffer, 0); + SessionPacketTypeName type = (SessionPacketTypeName)ByteReader.ReadByte(buffer, offset); switch (type) { case SessionPacketTypeName.SessionMessage: - return new SessionMessagePacket(buffer); + return new SessionMessagePacket(buffer, offset); case SessionPacketTypeName.SessionRequest: - return new SessionRequestPacket(buffer); + return new SessionRequestPacket(buffer, offset); case SessionPacketTypeName.PositiveSessionResponse: - return new PositiveSessionResponsePacket(buffer); + return new PositiveSessionResponsePacket(buffer, offset); case SessionPacketTypeName.NegativeSessionResponse: - return new NegativeSessionResponsePacket(buffer); + return new NegativeSessionResponsePacket(buffer, offset); case SessionPacketTypeName.RetargetSessionResponse: - return new SessionRetargetResponsePacket(buffer); + return new SessionRetargetResponsePacket(buffer, offset); case SessionPacketTypeName.SessionKeepAlive: - return new SessionKeepAlivePacket(buffer); + return new SessionKeepAlivePacket(buffer, offset); default: throw new InvalidRequestException("Invalid NetBIOS Session Packet"); } diff --git a/SMBLibrary/NetBios/SessionPackets/SessionRequestPacket.cs b/SMBLibrary/NetBios/SessionPackets/SessionRequestPacket.cs index 1782124..b59a4ab 100644 --- a/SMBLibrary/NetBios/SessionPackets/SessionRequestPacket.cs +++ b/SMBLibrary/NetBios/SessionPackets/SessionRequestPacket.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2017 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, @@ -24,9 +24,8 @@ namespace SMBLibrary.NetBios this.Type = SessionPacketTypeName.SessionRequest; } - public SessionRequestPacket(byte[] buffer) : base(buffer) + public SessionRequestPacket(byte[] buffer, int offset) : base(buffer, offset) { - int offset = 0; CalledName = NetBiosUtils.DecodeName(this.Trailer, ref offset); CallingName = NetBiosUtils.DecodeName(this.Trailer, ref offset); } diff --git a/SMBLibrary/NetBios/SessionPackets/SessionRetargetResponsePacket.cs b/SMBLibrary/NetBios/SessionPackets/SessionRetargetResponsePacket.cs index 82889e5..52548db 100644 --- a/SMBLibrary/NetBios/SessionPackets/SessionRetargetResponsePacket.cs +++ b/SMBLibrary/NetBios/SessionPackets/SessionRetargetResponsePacket.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2017 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, @@ -24,10 +24,10 @@ namespace SMBLibrary.NetBios this.Type = SessionPacketTypeName.RetargetSessionResponse; } - public SessionRetargetResponsePacket(byte[] buffer) : base(buffer) + public SessionRetargetResponsePacket(byte[] buffer, int offset) : base(buffer, offset) { - IPAddress = BigEndianConverter.ToUInt32(this.Trailer, 0); - Port = BigEndianConverter.ToUInt16(this.Trailer, 4); + IPAddress = BigEndianConverter.ToUInt32(this.Trailer, offset + 0); + Port = BigEndianConverter.ToUInt16(this.Trailer, offset + 4); } public override byte[] GetBytes() diff --git a/SMBLibrary/Server/SMBServer.cs b/SMBLibrary/Server/SMBServer.cs index 4483331..3e51b10 100644 --- a/SMBLibrary/Server/SMBServer.cs +++ b/SMBLibrary/Server/SMBServer.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2014-2016 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2017 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, @@ -238,11 +238,11 @@ namespace SMBLibrary.Server { SessionPacket packet = null; #if DEBUG - packet = SessionPacket.GetSessionPacket(packetBytes); + packet = SessionPacket.GetSessionPacket(packetBytes, 0); #else try { - packet = SessionPacket.GetSessionPacket(packetBytes); + packet = SessionPacket.GetSessionPacket(packetBytes, 0); } catch (Exception) {