Commit 13e7b291 authored by Brickner_cp's avatar Brickner_cp

IpV4

parent 990042d6
......@@ -43,7 +43,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="EthernetDatagramTests.cs" />
<Compile Include="IpV4DatagramTests.cs" />
<Compile Include="IpV4Tests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="IpV4AddressTests.cs" />
</ItemGroup>
......
......@@ -81,6 +81,7 @@ namespace Packets
}
}
internal EthernetDatagram(byte[] buffer, int offset, int length)
: base(buffer, offset, length)
{
......
......@@ -18,6 +18,11 @@ namespace Packets
(byte.Parse(values[3])));
}
public static IpV4Address Zero
{
get { return _zero; }
}
public uint ToValue()
{
return _value;
......@@ -74,5 +79,6 @@ namespace Packets
}
private readonly uint _value;
private static readonly IpV4Address _zero = new IpV4Address(0);
}
}
\ No newline at end of file
This diff is collapsed.
using System;
namespace Packets
{
public struct IpV4Fragmentation : IEquatable<IpV4Fragmentation>
{
public static IpV4Fragmentation None
{
get { return _none; }
}
public IpV4Fragmentation(IpV4FragmentationFlags flags, ushort offset)
: this((ushort)((ushort)flags | (offset / 8)))
{
}
public IpV4FragmentationFlags Flags
{
get { return (IpV4FragmentationFlags)(_value & 0xE000); }
}
public ushort Offset
{
get { return (ushort)((_value & 0x1FFF) * 8); }
}
public bool Equals(IpV4Fragmentation other)
{
return _value == other._value;
}
public override bool Equals(object obj)
{
return (obj is IpV4Fragmentation &&
Equals((IpV4Fragmentation)obj));
}
internal IpV4Fragmentation(ushort value)
{
_value = value;
}
internal void Write(byte[] buffer, int offset)
{
buffer.Write(offset, _value, Endianity.Big);
}
private static readonly IpV4Fragmentation _none = new IpV4Fragmentation(IpV4FragmentationFlags.None, 0);
private readonly ushort _value;
}
}
\ No newline at end of file
using System;
namespace Packets
{
[Flags]
public enum IpV4FragmentationFlags : ushort
{
None = 0x0 << 13,
Reserved = 0x4 << 13,
DontFragment = 0x2 << 13,
MoreFragments = 0x1 << 13
}
}
\ No newline at end of file
namespace Packets
{
public enum IpV4Protocol : byte
{
Tcp = 0x06
}
}
\ No newline at end of file
......@@ -39,6 +39,13 @@ namespace Packets
return (uint)ReadInt(buffer, offset, endianity);
}
public static uint ReadUInt(this byte[] buffer, ref int offset, Endianity endianity)
{
uint result = ReadUInt(buffer, offset, endianity);
offset += 4;
return result;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "int")]
public static int ReadInt(this byte[] buffer, int offset, Endianity endianity)
{
......@@ -60,6 +67,18 @@ namespace Packets
Write(buffer, offset, (short)value, endianity);
}
public static void Write(this byte[] buffer, int offset, int value, Endianity endianity)
{
if (IsWrongEndianity(endianity))
value = IPAddress.HostToNetworkOrder(value);
Write(buffer, offset, value);
}
public static void Write(this byte[] buffer, int offset, uint value, Endianity endianity)
{
Write(buffer, offset, (int)value, endianity);
}
private static bool IsWrongEndianity(Endianity endianity)
{
return (BitConverter.IsLittleEndian == (endianity == Endianity.Big));
......@@ -97,5 +116,16 @@ namespace Packets
}
}
}
private static void Write(byte[] buffer, int offset, int value)
{
unsafe
{
fixed (byte* ptr = &buffer[offset])
{
*((int*)ptr) = value;
}
}
}
}
}
\ No newline at end of file
......@@ -67,6 +67,9 @@
<Compile Include="IDataLink.cs" />
<Compile Include="IpV4\IpV4Address.cs" />
<Compile Include="IpV4\IpV4Datagram.cs" />
<Compile Include="IpV4\IpV4Fragmentation.cs" />
<Compile Include="IpV4\IpV4FragmentationFlags.cs" />
<Compile Include="IpV4\IpV4Protocol.cs" />
<Compile Include="MoreByteArray.cs" />
<Compile Include="Packet.cs" />
<Compile Include="PacketBuilder.cs" />
......
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Packets;
namespace PcapDotNet.Core.Test
{
/// <summary>
/// Summary description for DumpPacketsTests
/// </summary>
[TestClass]
public class DumpPacketsTests
{
public DumpPacketsTests()
{
//
// 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 DumpIpV4PacketTest()
{
MacAddress ethernetSource = new MacAddress("00:01:02:03:04:05");
MacAddress ethernetDestination = new MacAddress("A0:A1:A2:A3:A4:A5");
const EthernetType ethernetType = EthernetType.IpV4;
Random random = new Random();
byte ipV4TypeOfService = (byte)random.Next(256);
ushort ipV4Identification = (ushort)random.Next(65536);
byte ipV4Ttl = (byte)random.Next(256);
IpV4FragmentationFlags ipV4FragmentationFlags = (IpV4FragmentationFlags)(random.Next(4) << 13);
ushort ipV4FragmentationOffset = (ushort)random.Next(65536);
IpV4Fragmentation ipV4Fragmentation = new IpV4Fragmentation(ipV4FragmentationFlags, ipV4FragmentationOffset);
const IpV4Protocol ipV4Protocol = IpV4Protocol.Tcp;
IpV4Address ipV4Source = new IpV4Address((uint)random.Next());
IpV4Address ipV4Destination = new IpV4Address((uint)random.Next());
IpV4Options ipV4Options = IpV4Options.None;
byte[] ipV4PayloadBuffer = new byte[random.Next(0, 50 * 1024)];
random.NextBytes(ipV4PayloadBuffer);
Datagram ipV4Payload = new Datagram(ipV4PayloadBuffer);
Packet packet = PacketBuilder.IpV4(DateTime.Now,
ethernetSource, ethernetDestination, ethernetType,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4Ttl, ipV4Protocol,
ipV4Source, ipV4Destination, ipV4Options,
ipV4Payload);
using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice())
{
// using (PacketDumpFile dumpFile = communicator.OpenDump(@"c:\wow.pcap"))
// {
// dumpFile.Dump(packet);
// }
}
}
}
}
\ No newline at end of file
......@@ -43,6 +43,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BerkeleyPacketFilterTests.cs" />
<Compile Include="DumpPacketsTests.cs" />
<Compile Include="MoreAssert.cs" />
<Compile Include="LivePacketDeviceTests.cs" />
<Compile Include="MoreRandom.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