Commit be380b07 authored by Brickner_cp's avatar Brickner_cp

DNS

parent 8e1d67cd
...@@ -150,6 +150,9 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -150,6 +150,9 @@ namespace PcapDotNet.Packets.TestUtils
(random.Next(-180, 180) * random.NextDouble()).ToString("0.##########"), (random.Next(-180, 180) * random.NextDouble()).ToString("0.##########"),
(random.Next(-500, 50000) * random.NextDouble()).ToString("0.##########")); (random.Next(-500, 50000) * random.NextDouble()).ToString("0.##########"));
case DnsType.Aaaa:
return new DnsResourceDataIpV6(random.NextIpV6Address());
default: default:
return new DnsResourceDataAnything(random.NextDataSegment(random.Next(100))); return new DnsResourceDataAnything(random.NextDataSegment(random.Next(100)));
} }
......
...@@ -7,6 +7,7 @@ using PcapDotNet.Base; ...@@ -7,6 +7,7 @@ using PcapDotNet.Base;
using PcapDotNet.Packets.Ethernet; using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.Http; using PcapDotNet.Packets.Http;
using PcapDotNet.Packets.IpV4; using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.IpV6;
namespace PcapDotNet.Packets namespace PcapDotNet.Packets
{ {
...@@ -316,6 +317,22 @@ namespace PcapDotNet.Packets ...@@ -316,6 +317,22 @@ namespace PcapDotNet.Packets
offset += UInt48.SizeOf; offset += UInt48.SizeOf;
return result; return result;
} }
/// <summary>
/// Reads 16 bytes from a specific offset as a UInt128 with a given endianity.
/// </summary>
/// <param name="buffer">The buffer to read the bytes from.</param>
/// <param name="offset">The offset in the buffer to start reading.</param>
/// <param name="endianity">The endianity to use to translate the bytes to the value.</param>
/// <returns>The value converted from the read bytes according to the endianity.</returns>
public static UInt128 ReadUInt128(this byte[] buffer, int offset, Endianity endianity)
{
UInt128 value = ReadUInt128(buffer, offset);
if (IsWrongEndianity(endianity))
value = HostToNetworkOrder(value);
return value;
}
/* /*
/// <summary> /// <summary>
/// Reads 8 bytes from a specific offset as an int with a given endianity. /// Reads 8 bytes from a specific offset as an int with a given endianity.
...@@ -418,6 +435,18 @@ namespace PcapDotNet.Packets ...@@ -418,6 +435,18 @@ namespace PcapDotNet.Packets
return new IpV4TimeOfDay(buffer.ReadUInt(ref offset, endianity)); return new IpV4TimeOfDay(buffer.ReadUInt(ref offset, endianity));
} }
/// <summary>
/// Reads 16 bytes from a specific offset as an IPv6 address with a given endianity.
/// </summary>
/// <param name="buffer">The buffer to read the bytes from.</param>
/// <param name="offset">The offset in the buffer to start reading.</param>
/// <param name="endianity">The endianity to use to translate the bytes to the value.</param>
/// <returns>The value converted from the read bytes according to the endianity.</returns>
public static IpV6Address ReadIpV6Address(this byte[] buffer, int offset, Endianity endianity)
{
return new IpV6Address(buffer.ReadUInt128(offset, endianity));
}
/// <summary> /// <summary>
/// Writes the given value to the buffer. /// Writes the given value to the buffer.
/// </summary> /// </summary>
...@@ -606,6 +635,20 @@ namespace PcapDotNet.Packets ...@@ -606,6 +635,20 @@ namespace PcapDotNet.Packets
offset += UInt48.SizeOf; offset += UInt48.SizeOf;
} }
/// <summary>
/// Writes the given value to the buffer using the given endianity.
/// </summary>
/// <param name="buffer">The buffer to write the value to.</param>
/// <param name="offset">The offset in the buffer to start writing.</param>
/// <param name="value">The value to write.</param>
/// <param name="endianity">The endianity to use when converting the value to bytes.</param>
public static void Write(this byte[] buffer, int offset, UInt128 value, Endianity endianity)
{
if (IsWrongEndianity(endianity))
value = HostToNetworkOrder(value);
Write(buffer, offset, value);
}
/// <summary> /// <summary>
/// Writes a string to a byte array in a specific offset using the given encoding. /// Writes a string to a byte array in a specific offset using the given encoding.
/// Increments the offset by the number of bytes written. /// Increments the offset by the number of bytes written.
...@@ -723,6 +766,18 @@ namespace PcapDotNet.Packets ...@@ -723,6 +766,18 @@ namespace PcapDotNet.Packets
buffer.Write(ref offset, value.MillisecondsSinceMidnightUniversalTime, endianity); buffer.Write(ref offset, value.MillisecondsSinceMidnightUniversalTime, endianity);
} }
/// <summary>
/// Writes the given value to the buffer using the given endianity.
/// </summary>
/// <param name="buffer">The buffer to write the value to.</param>
/// <param name="offset">The offset in the buffer to start writing.</param>
/// <param name="value">The value to write.</param>
/// <param name="endianity">The endianity to use when converting the value to bytes.</param>
public static void Write(this byte[] buffer, int offset, IpV6Address value, Endianity endianity)
{
buffer.Write(offset, value.ToValue(), endianity);
}
// public static void WriteCarriageReturnLinefeed(this byte[] buffer, int offset) // public static void WriteCarriageReturnLinefeed(this byte[] buffer, int offset)
// { // {
// buffer.Write(ref offset, AsciiBytes.CarriageReturn); // buffer.Write(ref offset, AsciiBytes.CarriageReturn);
...@@ -801,6 +856,25 @@ namespace PcapDotNet.Packets ...@@ -801,6 +856,25 @@ namespace PcapDotNet.Packets
return result; return result;
} }
private static UInt128 HostToNetworkOrder(UInt128 value)
{
UInt128 result;
unsafe
{
UInt128* resultPtr = &result;
byte* resultBytePtr = (byte*)resultPtr;
UInt128* valuePtr = &value;
byte* valueBytePtr = (byte*)valuePtr;
for (int i = 0; i != UInt128.SizeOf; ++i)
resultBytePtr[i] = valueBytePtr[UInt128.SizeOf - i - 1];
}
return result;
}
private static short ReadShort(byte[] buffer, int offset) private static short ReadShort(byte[] buffer, int offset)
{ {
unsafe unsafe
...@@ -845,16 +919,16 @@ namespace PcapDotNet.Packets ...@@ -845,16 +919,16 @@ namespace PcapDotNet.Packets
} }
} }
// private static long ReadLong(byte[] buffer, int offset) private static UInt128 ReadUInt128(byte[] buffer, int offset)
// { {
// unsafe unsafe
// { {
// fixed (byte* ptr = &buffer[offset]) fixed (byte* ptr = &buffer[offset])
// { {
// return *((long*)ptr); return *((UInt128*)ptr);
// } }
// } }
// } }
private static void Write(byte[] buffer, int offset, short value) private static void Write(byte[] buffer, int offset, short value)
{ {
...@@ -899,5 +973,16 @@ namespace PcapDotNet.Packets ...@@ -899,5 +973,16 @@ namespace PcapDotNet.Packets
} }
} }
} }
private static void Write(byte[] buffer, int offset, UInt128 value)
{
unsafe
{
fixed (byte* ptr = &buffer[offset])
{
*((UInt128*)ptr) = value;
}
}
}
} }
} }
\ No newline at end of file
...@@ -6,6 +6,7 @@ using System.Text; ...@@ -6,6 +6,7 @@ using System.Text;
using PcapDotNet.Base; using PcapDotNet.Base;
using PcapDotNet.Packets.Ethernet; using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4; using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.IpV6;
namespace PcapDotNet.Packets namespace PcapDotNet.Packets
{ {
...@@ -262,6 +263,17 @@ namespace PcapDotNet.Packets ...@@ -262,6 +263,17 @@ namespace PcapDotNet.Packets
return Buffer.ReadIpV4TimeOfDay(StartOffset + offset, endianity); return Buffer.ReadIpV4TimeOfDay(StartOffset + offset, endianity);
} }
/// <summary>
/// Reads 4 bytes from a specific offset in the segment as an IpV4Address with a given endianity.
/// </summary>
/// <param name="offset">The offset in the segment to start reading.</param>
/// <param name="endianity">The endianity to use to translate the bytes to the value.</param>
/// <returns>The value converted from the read bytes according to the endianity.</returns>
internal IpV6Address ReadIpV6Address(int offset, Endianity endianity)
{
return Buffer.ReadIpV6Address(StartOffset + offset, endianity);
}
/// <summary> /// <summary>
/// Converts the given 16 bits sum to a checksum. /// Converts the given 16 bits sum to a checksum.
/// Sums the two 16 bits in the 32 bits value and if the result is bigger than a 16 bits value repeat. /// Sums the two 16 bits in the 32 bits value and if the result is bigger than a 16 bits value repeat.
......
...@@ -6,6 +6,7 @@ using System.Reflection; ...@@ -6,6 +6,7 @@ using System.Reflection;
using System.Text; using System.Text;
using PcapDotNet.Base; using PcapDotNet.Base;
using PcapDotNet.Packets.IpV4; using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.IpV6;
namespace PcapDotNet.Packets.Dns namespace PcapDotNet.Packets.Dns
{ {
...@@ -2035,4 +2036,56 @@ namespace PcapDotNet.Packets.Dns ...@@ -2035,4 +2036,56 @@ namespace PcapDotNet.Packets.Dns
return new DnsResourceDataGeographicalPosition(longtitude, latitude, altitude); return new DnsResourceDataGeographicalPosition(longtitude, latitude, altitude);
} }
} }
/// <summary>
/// <pre>
/// +-----+-------+
/// | bit | 0-127 |
/// +-----+-------+
/// | 0 | IP |
/// +-----+-------+
/// </pre>
/// </summary>
[DnsTypeRegistration(Type = DnsType.Aaaa)]
public sealed class DnsResourceDataIpV6 : DnsResourceDataSimple, IEquatable<DnsResourceDataIpV6>
{
public DnsResourceDataIpV6()
: this(IpV6Address.Zero)
{
}
public DnsResourceDataIpV6(IpV6Address data)
{
Data = data;
}
public IpV6Address Data { get; private set; }
public bool Equals(DnsResourceDataIpV6 other)
{
return other != null && Data.Equals(other.Data);
}
public override bool Equals(DnsResourceData other)
{
return Equals(other as DnsResourceDataIpV6);
}
internal override int GetLength()
{
return IpV6Address.SizeOf;
}
internal override void WriteDataSimple(byte[] buffer, int offset)
{
buffer.Write(offset, Data, Endianity.Big);
}
internal override DnsResourceData CreateInstance(DataSegment data)
{
if (data.Length != IpV6Address.SizeOf)
return null;
return new DnsResourceDataIpV6(data.ReadIpV6Address(0, Endianity.Big));
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment