Przeglądaj źródła

NetBios: ResourceRecord: Improved implementation

Tal Aloni 5 lat temu
rodzic
commit
f48763f049

+ 8 - 0
SMBLibrary/NetBios/NameServicePackets/Enums/ResourceRecordClass.cs

@@ -0,0 +1,8 @@
+
+namespace SMBLibrary.NetBios
+{
+    public enum ResourceRecordClass : ushort
+    {
+        In = 0x0001,
+    }
+}

+ 2 - 2
SMBLibrary/NetBios/NameServicePackets/NameRegistrationRequest.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2020 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,
@@ -34,7 +34,7 @@ namespace SMBLibrary.NetBios
             Header.ARCount = 1;
             Header.Flags = OperationFlags.Broadcast | OperationFlags.RecursionDesired;
             Question = new QuestionSection();
-            Resource = new ResourceRecord();
+            Resource = new ResourceRecord(NameRecordType.NB);
             Address = new byte[4];
         }
 

+ 2 - 3
SMBLibrary/NetBios/NameServicePackets/NodeStatusResponse.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2020 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,
@@ -29,8 +29,7 @@ namespace SMBLibrary.NetBios
             Header.OpCode = NameServiceOperation.QueryResponse;
             Header.Flags = OperationFlags.AuthoritativeAnswer | OperationFlags.RecursionAvailable;
             Header.ANCount = 1;
-            Resource = new ResourceRecord();
-            Resource.Type = NameRecordType.NBStat;
+            Resource = new ResourceRecord(NameRecordType.NBStat);
             Statistics = new NodeStatistics();
         }
 

+ 2 - 2
SMBLibrary/NetBios/NameServicePackets/PositiveNameQueryResponse.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2020 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,
@@ -29,7 +29,7 @@ namespace SMBLibrary.NetBios
             Header.Flags = OperationFlags.AuthoritativeAnswer | OperationFlags.RecursionDesired;
             Header.OpCode = NameServiceOperation.QueryResponse;
             Header.ANCount = 1;
-            Resource = new ResourceRecord();
+            Resource = new ResourceRecord(NameRecordType.NB);
         }
 
         public byte[] GetBytes()

+ 17 - 5
SMBLibrary/NetBios/NameServicePackets/Structures/ResourceRecord.cs

@@ -1,4 +1,4 @@
-/* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
+/* Copyright (C) 2014-2020 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,
@@ -18,19 +18,31 @@ namespace SMBLibrary.NetBios
     public class ResourceRecord
     {
         public string Name;
-        public NameRecordType Type = NameRecordType.NB; // NB
-        public ushort Class = 0x0001; // IN
+        public NameRecordType Type;
+        public ResourceRecordClass Class;
         public uint TTL;
         // ushort DataLength
         public byte[] Data;
 
-        public ResourceRecord()
+        public ResourceRecord(NameRecordType type)
         {
             Name = String.Empty;
+            Type = type;
+            Class = ResourceRecordClass.In;
             TTL = (uint)new TimeSpan(7, 0, 0, 0).TotalSeconds;
             Data = new byte[0];
         }
 
+        public ResourceRecord(byte[] buffer, ref int offset)
+        {
+            Name = NetBiosUtils.DecodeName(buffer, ref offset);
+            Type = (NameRecordType)BigEndianReader.ReadUInt16(buffer, ref offset);
+            Class = (ResourceRecordClass)BigEndianReader.ReadUInt16(buffer, ref offset);
+            TTL = BigEndianReader.ReadUInt32(buffer, ref offset);
+            ushort dataLength = BigEndianReader.ReadUInt16(buffer, ref offset);
+            Data = ByteReader.ReadBytes(buffer, ref offset, dataLength);
+        }
+
         public void WriteBytes(Stream stream)
         {
             WriteBytes(stream, null);
@@ -48,7 +60,7 @@ namespace SMBLibrary.NetBios
                 ByteWriter.WriteBytes(stream, encodedName);
             }
             BigEndianWriter.WriteUInt16(stream, (ushort)Type);
-            BigEndianWriter.WriteUInt16(stream, Class);
+            BigEndianWriter.WriteUInt16(stream, (ushort)Class);
             BigEndianWriter.WriteUInt32(stream, TTL);
             BigEndianWriter.WriteUInt16(stream, (ushort)Data.Length);
             ByteWriter.WriteBytes(stream, Data);

+ 1 - 0
SMBLibrary/SMBLibrary.csproj

@@ -73,6 +73,7 @@
     <Compile Include="Enums\Win32Error.cs" />
     <Compile Include="Exceptions\UnsupportedInformationLevelException.cs" />
     <Compile Include="Helpers\FileTimeHelper.cs" />
+    <Compile Include="NetBios\NameServicePackets\Enums\ResourceRecordClass.cs" />
     <Compile Include="NetBios\NameServicePackets\Enums\NameRecordType.cs" />
     <Compile Include="NetBios\NameServicePackets\Enums\NameServiceOperation.cs" />
     <Compile Include="NetBios\NameServicePackets\Enums\NetBiosSuffix.cs" />