Commit c35ed863 authored by Brickner_cp's avatar Brickner_cp

IpV4

parent 9b41304d
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Packets.Test
{
/// <summary>
/// Summary description for EthernetDatagramTests
/// </summary>
[TestClass]
public class EthernetDatagramTests
{
public EthernetDatagramTests()
{
//
// TODO: Add constructor logic here
//
}
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
[TestMethod]
public void EthernetDatagramTest()
{
MacAddress ethernetSource = new MacAddress("1:2:3:4:5:6");
MacAddress ethernetDestination = new MacAddress("7:8:9:10:11:12");
const EthernetType ethernetType = EthernetType.IpV4;
Datagram ethernetPayload = new Datagram(new byte[] {1, 2, 3, 4, 5});
Packet goodPacket = PacketBuilder.Ethernet(DateTime.Now,
ethernetSource, ethernetDestination,
ethernetType,
ethernetPayload);
Packet badPacket = new Packet(new byte[]{1,2,3,4,5}, DateTime.Now, new DataLink(DataLinkKind.Ethernet));
Assert.IsTrue(goodPacket.Ethernet.IsValid);
Assert.AreEqual(ethernetSource, goodPacket.Ethernet.Source);
Assert.AreEqual(ethernetDestination, goodPacket.Ethernet.Destination);
Assert.AreEqual(ethernetType, goodPacket.Ethernet.EtherType);
Assert.IsFalse(badPacket.Ethernet.IsValid);
}
}
}
\ No newline at end of file
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Packets.Test
{
/// <summary>
/// Summary description for IpV4DatagramTests
/// </summary>
[TestClass]
public class IpV4DatagramTests
{
public IpV4DatagramTests()
{
//
// TODO: Add constructor logic here
//
}
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
[TestMethod]
public void IpV4DatagramTest()
{
// PacketBuilder.IpV4(DateT)
}
}
}
\ No newline at end of file
......@@ -42,6 +42,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="EthernetDatagramTests.cs" />
<Compile Include="IpV4DatagramTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="IpV4AddressTests.cs" />
</ItemGroup>
......
using System;
namespace Packets
{
public class Datagram
public class Datagram : IEquatable<Datagram>
{
public Datagram(byte[] buffer)
: this(buffer, 0, buffer.Length)
......@@ -24,6 +26,45 @@ namespace Packets
get { return _length; }
}
public byte this[int offset]
{
get { return _buffer[StartOffset + offset]; }
}
public bool IsValid
{
get
{
if (_isValid == null)
_isValid = CalculateIsValid();
return _isValid.Value;
}
}
public virtual bool CalculateIsValid()
{
return true;
}
public bool Equals(Datagram other)
{
if (Length != other.Length)
return false;
for (int i = 0; i != Length; ++i)
{
if (this[i] != other[i])
return false;
}
return true;
}
public override bool Equals(object obj)
{
return Equals(obj as Datagram);
}
internal void Write(byte[] buffer, int offset)
{
System.Buffer.BlockCopy(_buffer, StartOffset, buffer, offset, Length);
......@@ -46,9 +87,10 @@ namespace Packets
return _buffer.ReadUShort(StartOffset + offset, endianity);
}
private static Datagram _empty = new Datagram(new byte[0], 0, 0);
private byte[] _buffer;
private int _startOffset;
private int _length;
private static readonly Datagram _empty = new Datagram(new byte[0], 0, 0);
private readonly byte[] _buffer;
private readonly int _startOffset;
private readonly int _length;
private bool? _isValid;
}
}
\ No newline at end of file
......@@ -20,7 +20,7 @@ namespace Packets
public const int HeaderLength = 14;
public EthernetDatagram(byte[] buffer, int offset, int length)
internal EthernetDatagram(byte[] buffer, int offset, int length)
: base(buffer, offset, length)
{
}
......@@ -49,6 +49,24 @@ namespace Packets
}
}
public override bool CalculateIsValid()
{
if (Length < HeaderLength)
return false;
switch (EtherType)
{
case EthernetType.Arp:
case EthernetType.Ieee8021Q:
case EthernetType.IpV4:
case EthernetType.IpV6:
return true;
default:
return false;
}
}
internal static void WriteHeader(byte[] buffer, int offset, MacAddress ethernetSource, MacAddress ethernetDestination, EthernetType ethernetType)
{
ethernetSource.Write(buffer, offset + Offset.Source);
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Packets
{
/// <summary>
/// +-----+---------+-----+-----------------+-------+-------+---------+
/// | Bit | 0-3 | 4-7 | 8-15 | 16-18 | 19-23 | 24-31 |
/// +-----+---------+-----+-----------------+-------+-------+---------+
/// | 0 | Version | IHL | Type of Service | Total Length |
/// +-----+---------+-----+-----------------+-------+-----------------+
/// | 32 | Identification | Flags | Fragment Offset |
/// +-----+---------------+-----------------+-------+-----------------+
/// | 64 | Time to Live | Protocol | Header Checksum |
/// +-----+---------------+-----------------+-------------------------+
/// | 96 | Source Address |
/// +-----+-----------------------------------------------------------+
/// | 128 | Destination Address |
/// +-----+-------------------------------------------------+---------+
/// | 160 | Options | Padding |
/// +-----+-------------------------------------------------+---------+
/// </summary>
public class IpV4Datagram : Datagram
{
public const int HeaderMinimumLength = 20;
internal IpV4Datagram(byte[] buffer, int offset, int length)
: base(buffer, offset, length)
{
}
internal static void WriteHeader(byte[] buffer, int offset, IpV4TypeOfService typeOfService, ushort identification, IpV4Fragmentation fragmentation, byte ttl, IpV4Protocol protocol, IpV4Address source, IpV4Address destinationAddress, IpV4Options options)
{
throw new NotImplementedException();
}
}
public struct IpV4TypeOfService
{
private byte _value;
}
public struct IpV4Fragmentation
{
private ushort _value;
}
public enum IpV4Protocol : byte
{
}
public class IpV4Options
{
public int Length
{
get { throw new NotImplementedException(); }
}
}
}
......@@ -70,8 +70,42 @@ namespace Packets
return hashCode;
}
public bool IsValid
{
get
{
if (_isValid == null)
_isValid = CalculateIsValid();
return _isValid.Value;
}
}
public EthernetDatagram Ethernet
{
get
{
if (_ethernet == null)
_ethernet = new EthernetDatagram(Buffer, 0, Length);
return _ethernet;
}
}
private bool CalculateIsValid()
{
switch (DataLink.Kind)
{
case DataLinkKind.Ethernet:
return Ethernet.IsValid;
default:
return false;
}
}
private readonly byte[] _data;
private readonly DateTime _timestamp;
private readonly IDataLink _dataLink;
private bool? _isValid;
private EthernetDatagram _ethernet;
}
}
......@@ -4,12 +4,34 @@ namespace Packets
{
public static class PacketBuilder
{
public static Packet Ethernet(DateTime timestamp, MacAddress ethernetSource, MacAddress ethernetDestination, EthernetType ethernetType, Datagram ethernetPayload)
public static Packet Ethernet(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination, EthernetType ethernetType,
Datagram ethernetPayload)
{
byte[] buffer = new byte[EthernetDatagram.HeaderLength + ethernetPayload.Length];
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, ethernetType);
ethernetPayload.Write(buffer, EthernetDatagram.HeaderLength);
return new Packet(buffer, timestamp, new DataLink(DataLinkKind.Ethernet));
}
public static Packet IpV4(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination, EthernetType ethernetType,
IpV4TypeOfService ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl, IpV4Protocol ipV4Protocol,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
Datagram ipV4Payload)
{
int ipHeaderLength = IpV4Datagram.HeaderMinimumLength + ipV4Options.Length;
byte[] buffer = new byte[EthernetDatagram.HeaderLength + ipHeaderLength + ipV4Payload.Length];
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, ethernetType);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, ipV4Protocol,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options);
ipV4Payload.Write(buffer, EthernetDatagram.HeaderLength + ipHeaderLength);
return new Packet(buffer, timestamp, new DataLink(DataLinkKind.Ethernet));
}
}
}
\ No newline at end of file
......@@ -66,6 +66,7 @@
<Compile Include="Ethernet\MacAddress.cs" />
<Compile Include="IDataLink.cs" />
<Compile Include="IpV4Address.cs" />
<Compile Include="IpV4\IpV4Datagram.cs" />
<Compile Include="MoreByteArray.cs" />
<Compile Include="Packet.cs" />
<Compile Include="PacketBuilder.cs" />
......
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