Commit 9cd5f91f authored by Brickner_cp's avatar Brickner_cp

TCP

parent f9bfb007
......@@ -59,6 +59,9 @@ namespace PcapDotNet.Core.Test
case TcpOptionType.AlternateChecksumData:
return "Unknown (0x0f) (" + option.Length + " bytes)";
case TcpOptionType.Md5Signature:
return "TCP MD5 signature";
case (TcpOptionType)20:
return "SCPS capabilities" + (option.Length >= 4
? string.Empty
......@@ -105,6 +108,7 @@ namespace PcapDotNet.Core.Test
case TcpOptionType.ConnectionCountEcho:
case TcpOptionType.AlternateChecksumRequest:
case TcpOptionType.AlternateChecksumData:
case TcpOptionType.Md5Signature:
break;
case TcpOptionType.SelectiveAcknowledgment:
......
......@@ -303,8 +303,9 @@ namespace PcapDotNet.Packets.TestUtils
impossibleOptionTypes.Add(TcpOptionType.AlternateChecksumRequest);
if (maximumOptionLength < TcpOptionAlternateChecksumData.OptionMinimumLength)
impossibleOptionTypes.Add(TcpOptionType.AlternateChecksumData);
if (maximumOptionLength < TcpOptionMd5Signature.OptionLength)
impossibleOptionTypes.Add(TcpOptionType.Md5Signature);
impossibleOptionTypes.Add(TcpOptionType.Md5Signature);
impossibleOptionTypes.Add(TcpOptionType.QuickStartResponse);
impossibleOptionTypes.Add(TcpOptionType.UserTimeout);
......@@ -363,6 +364,9 @@ namespace PcapDotNet.Packets.TestUtils
case TcpOptionType.AlternateChecksumData:
return new TcpOptionAlternateChecksumData(random.NextBytes(random.Next(maximumOptionLength - TcpOptionAlternateChecksumData.OptionMinimumLength + 1)));
case TcpOptionType.Md5Signature:
return new TcpOptionMd5Signature(random.NextBytes(TcpOptionMd5Signature.OptionValueLength));
default:
throw new InvalidOperationException("optionType = " + optionType);
}
......
......@@ -122,6 +122,7 @@
<Compile Include="Transport\TcpOptionEcho.cs" />
<Compile Include="Transport\TcpOptionEchoReply.cs" />
<Compile Include="Transport\TcpOptionMaximumSegmentSize.cs" />
<Compile Include="Transport\TcpOptionMd5Signature.cs" />
<Compile Include="Transport\TcpOptionPartialOrderConnectionPermitted.cs" />
<Compile Include="Transport\TcpOptionPartialOrderServiceProfile.cs" />
<Compile Include="Transport\TcpOptions.cs" />
......
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace PcapDotNet.Packets.Transport
{
/// <summary>
/// +---------+---------+-------------------+
/// | Kind=19 |Length=18| MD5 digest... |
/// +---------+---------+-------------------+
/// | |
/// +---------------------------------------+
/// | |
/// +---------------------------------------+
/// | |
/// +-------------------+-------------------+
/// | |
/// +-------------------+
///
/// The MD5 digest is always 16 bytes in length, and the option would appear in every segment of a connection.
/// </summary>
[OptionTypeRegistration(typeof(TcpOptionType), TcpOptionType.Md5Signature)]
public class TcpOptionMd5Signature: TcpOptionComplex, IOptionComplexFactory, IEquatable<TcpOptionMd5Signature>
{
/// <summary>
/// The number of bytes this option take.
/// </summary>
public const int OptionLength = OptionHeaderLength + 16;
public const int OptionValueLength = OptionLength - OptionHeaderLength;
public TcpOptionMd5Signature(IList<byte> data)
: base(TcpOptionType.Md5Signature)
{
if (data.Count != OptionValueLength)
throw new ArgumentException("data must be " + OptionValueLength + " bytes and not " + data.Count + " bytes", "data");
Data = new ReadOnlyCollection<byte>(data);
}
public TcpOptionMd5Signature()
: this(new byte[OptionValueLength])
{
}
public ReadOnlyCollection<byte> Data { get; private set; }
/// <summary>
/// The number of bytes this option will take.
/// </summary>
public override int Length
{
get { return OptionLength; }
}
/// <summary>
/// True iff this option may appear at most once in a datagram.
/// </summary>
public override bool IsAppearsAtMostOnce
{
get { return true; }
}
public bool Equals(TcpOptionMd5Signature other)
{
if (other == null)
return false;
return Data.SequenceEqual(other.Data);
}
public override bool Equals(TcpOption other)
{
return Equals(other as TcpOptionMd5Signature);
}
/// <summary>
/// Tries to read the option from a buffer starting from the option value (after the type and length).
/// </summary>
/// <param name="buffer">The buffer to read the option from.</param>
/// <param name="offset">The offset to the first byte to read the buffer. Will be incremented by the number of bytes read.</param>
/// <param name="valueLength">The number of bytes the option value should take according to the length field that was already read.</param>
/// <returns>On success - the complex option read. On failure - null.</returns>
public Option CreateInstance(byte[] buffer, ref int offset, byte valueLength)
{
if (valueLength != OptionValueLength)
return null;
byte[] data = buffer.ReadBytes(ref offset, OptionValueLength);
return new TcpOptionMd5Signature(data);
}
internal override void Write(byte[] buffer, ref int offset)
{
base.Write(buffer, ref offset);
buffer.Write(ref offset, Data);
}
}
}
\ No newline at end of file
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