Commit a399a230 authored by Brickner_cp's avatar Brickner_cp

IPv6

parent 4d099490
......@@ -43,16 +43,16 @@ DataLinkKind PcapDataLink::Kind::get()
{
switch (Value)
{
case 1:
case DLT_EN10MB:
return DataLinkKind::Ethernet;
case 12:
case DLT_RAW:
return DataLinkKind::IpV4;
case 143:
case DLT_DOCSIS:
return DataLinkKind::Docsis;
case 204:
case DLT_PPP_WITH_DIR:
return DataLinkKind::PppWithDirection;
default:
......@@ -74,7 +74,7 @@ String^ PcapDataLink::Name::get()
switch (Value)
{
case 204:
case DLT_PPP_WITH_DIR:
return "PPP_WITH_DIR";
default:
......@@ -90,7 +90,7 @@ String^ PcapDataLink::Description::get()
switch (Value)
{
case 204:
case DLT_PPP_WITH_DIR:
return "PPP with Directional Info";
default:
......@@ -141,16 +141,16 @@ int PcapDataLink::KindToValue(DataLinkKind kind)
switch (kind)
{
case DataLinkKind::Ethernet:
return 1;
return DLT_EN10MB;
case DataLinkKind::IpV4:
return 12;
return DLT_RAW;
case DataLinkKind::Docsis:
return 143;
return DLT_DOCSIS;
case DataLinkKind::PppWithDirection:
return 204;
return DLT_PPP_WITH_DIR;
default:
throw gcnew NotSupportedException(PcapDataLink::typeid->Name + " kind " + kind.ToString() + " is unsupported");
......
......@@ -232,6 +232,17 @@ namespace PcapDotNet.Packets
return Buffer.ReadUShort(StartOffset + offset, endianity);
}
/// <summary>
/// Reads 3 bytes from a specific offset in the segment as a UInt24 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 UInt24 ReadUInt24(int offset, Endianity endianity)
{
return Buffer.ReadUInt24(StartOffset + offset, endianity);
}
/// <summary>
/// Reads 4 bytes from a specific offset in the segment as an int with a given endianity.
/// </summary>
......
......@@ -66,7 +66,7 @@ namespace PcapDotNet.Packets.IpV4
public const int DefaultVersion = 0x4;
/// <summary>
/// The header length in bytes.
/// Indicates the format of the internet header.
/// </summary>
public int Version
{
......
using System;
using System.Collections.ObjectModel;
using System.Linq;
using PcapDotNet.Base;
using PcapDotNet.Packets.IpV4;
namespace PcapDotNet.Packets.IpV6
{
/// <summary>
/// Represents an IPv6 datagram.
/// <pre>
/// +-----+---------+---------------+-------+-------------+-----------+
/// | Bit | 0-3 | 4-11 | 12-15 | 16-23 | 24-31 |
/// +-----+---------+---------------+-------+-------------+-----------+
/// | 0 | Version | Traffic Class | Flow Label |
/// +-----+---------+---------------+-------+-------------+-----------+
/// | 32 | Payload Length | Next Header | Hop Limit |
/// +-----+---------------------------------+-------------+-----------+
/// | 64 | Source Address |
/// | | |
/// | | |
/// | | |
/// +-----+-----------------------------------------------------------+
/// | 192 | Destination Address |
/// | | |
/// | | |
/// | | |
/// +-----+-----------------------------------------------------------+
/// | 320 | Extension Headers (optional) |
/// | | ... |
/// +-----+-----------------------------------------------------------+
/// </pre>
/// </summary>
public sealed class IpV6Datagram : Datagram
{
/// <summary>
/// The number of bytes the header takes in bytes (not including extension headers).
/// </summary>
public const int HeaderLength = 40;
private static class Offset
{
public const int Version = 0;
public const int TrafficClass = 0;
public const int FlowLabel = 1;
public const int PayloadLength = 4;
public const int NextHeader = 6;
public const int HopLimit = 7;
public const int SourceAddress = 8;
public const int DestinationAddress = 24;
}
private static class Mask
{
public const byte Version = 0xF0;
public const ushort TrafficClass = 0x0FF0;
public static readonly UInt24 FlowLabel = (UInt24)0x0FFFFF;
}
private static class Shift
{
public const int Version = 4;
public const int TrafficClass = 4;
}
/// <summary>
/// The version (6).
/// </summary>
public const int DefaultVersion = 0x6;
/// <summary>
/// Internet Protocol version number.
/// </summary>
public byte Version
{
get { return (byte)((this[Offset.Version] & Mask.Version) >> Shift.Version); }
}
/// <summary>
/// Available for use by originating nodes and/or forwarding routers to identify and distinguish between different classes or priorities of
/// IPv6 packets.
/// </summary>
public byte TrafficClass
{
get { return (byte)((ReadUShort(Offset.TrafficClass, Endianity.Big) & Mask.TrafficClass) >> Shift.TrafficClass); }
}
/// <summary>
/// May be used by a source to label sequences of packets for which it requests special handling by the IPv6 routers,
/// such as non-default quality of service or "real-time" service.
/// Hosts or routers that do not support the functions of the Flow Label field are required to set the field to zero when originating a packet,
/// pass the field on unchanged when forwarding a packet, and ignore the field when receiving a packet.
/// </summary>
public int FlowLabel
{
get { return ReadUInt24(Offset.FlowLabel, Endianity.Big) & Mask.FlowLabel; }
}
/// <summary>
/// Length of the IPv6 payload, i.e., the rest of the packet following this IPv6 header, in octets.
/// Note that any extension headers present are considered part of the payload, i.e., included in the length count.
/// </summary>
public ushort PayloadLength
{
get { return ReadUShort(Offset.PayloadLength, Endianity.Big); }
}
/// <summary>
/// Identifies the type of header immediately following the IPv6 header.
/// Uses the same values as the IPv4 Protocol field.
/// </summary>
public IpV4Protocol NextHeader
{
get { return (IpV4Protocol)this[Offset.NextHeader]; }
}
/// <summary>
/// Decremented by 1 by each node that forwards the packet.
/// The packet is discarded if Hop Limit is decremented to zero.
/// </summary>
public byte HopLimit
{
get { return this[Offset.HopLimit]; }
}
/// <summary>
/// Address of the originator of the packet.
/// </summary>
public IpV6Address Source
{
get { return ReadIpV6Address(Offset.SourceAddress, Endianity.Big); }
}
/// <summary>
/// Address of the intended recipient of the packet (possibly not the ultimate recipient, if a Routing header is present).
/// </summary>
public IpV6Address CurrentDestination
{
get { return ReadIpV6Address(Offset.DestinationAddress, Endianity.Big); }
}
/// <summary>
/// Creates a Layer that represents the datagram to be used with PacketBuilder.
/// </summary>
public override ILayer ExtractLayer()
{
return null;
// TODO: Implement.
// return new IpV6Layer
// {
// Version = Version,
// TrafficClass = TrafficClass,
// FlowLabel = FlowLabel,
// PayloadLength = PayloadLength,
// NextHeader = NextHeader,
// HopLimit = HopLimit,
// Source = Source,
// CurrentDestination = CurrentDestination,
// ExtensionHeaders = ExtensionHeaders,
// };
}
internal IpV6Datagram(byte[] buffer, int offset, int length)
: base(buffer, offset, length)
{
}
internal static void WriteHeader(byte[] buffer, int offset,
byte version, byte trafficClass, int flowLabel, ushort payloadLength, IpV4Protocol nextHeader, byte hopLimit,
IpV6Address source, IpV6Address currentDestination)
{
buffer.Write(offset + Offset.Version, (uint)(((((version << Shift.Version) << 8) | trafficClass) << 16) | flowLabel), Endianity.Big);
buffer.Write(offset + Offset.PayloadLength, payloadLength, Endianity.Big);
buffer.Write(offset + Offset.NextHeader, (byte)nextHeader);
buffer.Write(offset + Offset.HopLimit, hopLimit);
buffer.Write(offset + Offset.SourceAddress, source, Endianity.Big);
buffer.Write(offset + Offset.DestinationAddress, currentDestination, Endianity.Big);
}
protected override bool CalculateIsValid()
{
// TODO: Implement.
return true;
}
}
}
......@@ -323,6 +323,7 @@
<Compile Include="IpV4\IpV4Layer.cs" />
<Compile Include="IpV4\IpV4OptionUnknown.cs" />
<Compile Include="IpV6\IpV6Address.cs" />
<Compile Include="IpV6\IpV6Datagram.cs" />
<Compile Include="Layer.cs" />
<Compile Include="Option.cs" />
<Compile Include="IOptionComplexFactory.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