Commit 1f35830f authored by Brickner_cp's avatar Brickner_cp

Added Datagram.ToMemoryStream() method.

Added default constructor for TcpLayer.
parent b76f996a
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.TestUtils;
using PcapDotNet.Packets.Transport;
namespace PcapDotNet.Packets.Test
{
......@@ -101,5 +104,29 @@ namespace PcapDotNet.Packets.Test
Datagram data = new Datagram(new byte[]{1,2,3});
Assert.IsTrue(data.IsValid);
}
[TestMethod]
public void DatagramToMemoryStreamTest()
{
Datagram tcpPayload = new Datagram(new byte[] {1, 2, 3});
Packet packet = PacketBuilder.Build(DateTime.Now,
new EthernetLayer(),
new IpV4Layer(),
new TcpLayer(),
new PayloadLayer {Data = tcpPayload});
using (MemoryStream stream = packet.Ethernet.IpV4.Tcp.Payload.ToMemoryStream())
{
Assert.IsTrue(stream.CanRead, "CanRead");
Assert.IsTrue(stream.CanSeek, "CanSeek");
Assert.IsFalse(stream.CanTimeout, "CanTimeout");
Assert.IsFalse(stream.CanWrite, "CanWrite");
Assert.AreEqual(tcpPayload.Length, stream.Length);
for (int i = 0; i != tcpPayload.Length; ++i)
{
Assert.AreEqual(i, stream.Position);
Assert.AreEqual(i + 1, stream.ReadByte());
}
}
}
}
}
\ No newline at end of file
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using PcapDotNet.Base;
using PcapDotNet.Packets.Ethernet;
......@@ -63,6 +64,11 @@ namespace PcapDotNet.Packets
get { return _buffer[StartOffset + offset]; }
}
public MemoryStream ToMemoryStream()
{
return new MemoryStream(Buffer, StartOffset, Length, false, false);
}
/// <summary>
/// A datagram is checked for validity according to the protocol.
/// </summary>
......
......@@ -9,6 +9,11 @@ namespace PcapDotNet.Packets.Transport
/// </summary>
public class TcpLayer : TransportLayer
{
public TcpLayer()
{
Options = TcpOptions.None;
}
/// <summary>
/// The sequence number of the first data octet in this segment (except when SYN is present).
/// If SYN is present the sequence number is the initial sequence number (ISN) and the first data octet is ISN+1.
......
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