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

This commit is contained in:
Tal Aloni 2017-01-03 13:58:00 +02:00
parent 983e9e7dca
commit f3f11ba20a
8 changed files with 32 additions and 33 deletions

View file

@ -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 * 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, * the GNU Lesser Public License as published by the Free Software Foundation,
@ -23,9 +23,9 @@ namespace SMBLibrary.NetBios
this.Type = SessionPacketTypeName.NegativeSessionResponse; 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() public override byte[] GetBytes()

View file

@ -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 * 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, * the GNU Lesser Public License as published by the Free Software Foundation,
@ -21,7 +21,7 @@ namespace SMBLibrary.NetBios
this.Type = SessionPacketTypeName.PositiveSessionResponse; this.Type = SessionPacketTypeName.PositiveSessionResponse;
} }
public PositiveSessionResponsePacket(byte[] buffer) : base(buffer) public PositiveSessionResponsePacket(byte[] buffer, int offset) : base(buffer, offset)
{ {
} }

View file

@ -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 * 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, * the GNU Lesser Public License as published by the Free Software Foundation,
@ -21,7 +21,7 @@ namespace SMBLibrary.NetBios
this.Type = SessionPacketTypeName.SessionKeepAlive; this.Type = SessionPacketTypeName.SessionKeepAlive;
} }
public SessionKeepAlivePacket(byte[] buffer) : base(buffer) public SessionKeepAlivePacket(byte[] buffer, int offset) : base(buffer, offset)
{ {
} }

View file

@ -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 * 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, * the GNU Lesser Public License as published by the Free Software Foundation,
@ -21,7 +21,7 @@ namespace SMBLibrary.NetBios
this.Type = SessionPacketTypeName.SessionMessage; this.Type = SessionPacketTypeName.SessionMessage;
} }
public SessionMessagePacket(byte[] buffer) : base(buffer) public SessionMessagePacket(byte[] buffer, int offset) : base(buffer, offset)
{ {
} }
} }

View file

@ -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 * 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, * 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); Type = (SessionPacketTypeName)ByteReader.ReadByte(buffer, offset + 0);
Flags = ByteReader.ReadByte(buffer, 1); Flags = ByteReader.ReadByte(buffer, offset + 1);
Length = (Flags & 0x01) << 16 | BigEndianConverter.ToUInt16(buffer, 2); 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() public virtual byte[] GetBytes()
@ -53,23 +53,23 @@ namespace SMBLibrary.NetBios
return buffer; 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) switch (type)
{ {
case SessionPacketTypeName.SessionMessage: case SessionPacketTypeName.SessionMessage:
return new SessionMessagePacket(buffer); return new SessionMessagePacket(buffer, offset);
case SessionPacketTypeName.SessionRequest: case SessionPacketTypeName.SessionRequest:
return new SessionRequestPacket(buffer); return new SessionRequestPacket(buffer, offset);
case SessionPacketTypeName.PositiveSessionResponse: case SessionPacketTypeName.PositiveSessionResponse:
return new PositiveSessionResponsePacket(buffer); return new PositiveSessionResponsePacket(buffer, offset);
case SessionPacketTypeName.NegativeSessionResponse: case SessionPacketTypeName.NegativeSessionResponse:
return new NegativeSessionResponsePacket(buffer); return new NegativeSessionResponsePacket(buffer, offset);
case SessionPacketTypeName.RetargetSessionResponse: case SessionPacketTypeName.RetargetSessionResponse:
return new SessionRetargetResponsePacket(buffer); return new SessionRetargetResponsePacket(buffer, offset);
case SessionPacketTypeName.SessionKeepAlive: case SessionPacketTypeName.SessionKeepAlive:
return new SessionKeepAlivePacket(buffer); return new SessionKeepAlivePacket(buffer, offset);
default: default:
throw new InvalidRequestException("Invalid NetBIOS Session Packet"); throw new InvalidRequestException("Invalid NetBIOS Session Packet");
} }

View file

@ -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 * 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, * the GNU Lesser Public License as published by the Free Software Foundation,
@ -24,9 +24,8 @@ namespace SMBLibrary.NetBios
this.Type = SessionPacketTypeName.SessionRequest; 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); CalledName = NetBiosUtils.DecodeName(this.Trailer, ref offset);
CallingName = NetBiosUtils.DecodeName(this.Trailer, ref offset); CallingName = NetBiosUtils.DecodeName(this.Trailer, ref offset);
} }

View file

@ -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 * 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, * the GNU Lesser Public License as published by the Free Software Foundation,
@ -24,10 +24,10 @@ namespace SMBLibrary.NetBios
this.Type = SessionPacketTypeName.RetargetSessionResponse; 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); IPAddress = BigEndianConverter.ToUInt32(this.Trailer, offset + 0);
Port = BigEndianConverter.ToUInt16(this.Trailer, 4); Port = BigEndianConverter.ToUInt16(this.Trailer, offset + 4);
} }
public override byte[] GetBytes() public override byte[] GetBytes()

View file

@ -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 * 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, * the GNU Lesser Public License as published by the Free Software Foundation,
@ -238,11 +238,11 @@ namespace SMBLibrary.Server
{ {
SessionPacket packet = null; SessionPacket packet = null;
#if DEBUG #if DEBUG
packet = SessionPacket.GetSessionPacket(packetBytes); packet = SessionPacket.GetSessionPacket(packetBytes, 0);
#else #else
try try
{ {
packet = SessionPacket.GetSessionPacket(packetBytes); packet = SessionPacket.GetSessionPacket(packetBytes, 0);
} }
catch (Exception) catch (Exception)
{ {