Browse Source

NetBIOS packets can now be read starting from a specified buffer offset to avoid unnecessary memory copy operation

Tal Aloni 8 years ago
parent
commit
f3f11ba20a

+ 3 - 3
SMBLibrary/NetBios/SessionPackets/NegativeSessionResponsePacket.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. 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()

+ 2 - 2
SMBLibrary/NetBios/SessionPackets/PositiveSessionResponsePacket.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. 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)
         {
         }
 

+ 2 - 2
SMBLibrary/NetBios/SessionPackets/SessionKeepAlivePacket.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. 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)
         {
         }
 

+ 2 - 2
SMBLibrary/NetBios/SessionPackets/SessionMessagePacket.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. 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)
         {
         }
     }

+ 14 - 14
SMBLibrary/NetBios/SessionPackets/SessionPacket.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. 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");
             }

+ 2 - 3
SMBLibrary/NetBios/SessionPackets/SessionRequestPacket.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. 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);
         }

+ 4 - 4
SMBLibrary/NetBios/SessionPackets/SessionRetargetResponsePacket.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. 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()

+ 3 - 3
SMBLibrary/Server/SMBServer.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. 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)
             {