mirror of
https://github.com/danbulant/Cosmos
synced 2026-06-12 03:01:32 +00:00
Done IPPacket api docs
This commit is contained in:
parent
4a4a50cb30
commit
215e393c8e
1 changed files with 113 additions and 1 deletions
|
|
@ -1,14 +1,26 @@
|
|||
using Cosmos.HAL.Network;
|
||||
using sys = System;
|
||||
using Cosmos.HAL.Network;
|
||||
using Cosmos.System.Network.ARP;
|
||||
|
||||
namespace Cosmos.System.Network.IPv4
|
||||
{
|
||||
/// <summary>
|
||||
/// IPPacket class. See also: <seealso cref="EthernetPacket"/>.
|
||||
/// </summary>
|
||||
public class IPPacket : EthernetPacket
|
||||
{
|
||||
protected byte ipHeaderLength;
|
||||
|
||||
private static ushort sNextFragmentID;
|
||||
|
||||
/// <summary>
|
||||
/// IPv4 handler.
|
||||
/// </summary>
|
||||
/// <param name="packetData">Packet data.</param>
|
||||
/// <exception cref="sys.ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception>
|
||||
/// <exception cref="sys.IO.IOException">Thrown on IO error.</exception>
|
||||
/// <exception cref="sys.ArgumentException">Thrown on fatal error (contact support).</exception>
|
||||
/// <exception cref="sys.OverflowException">Thrown if packetData array length is greater than Int32.MaxValue.</exception>
|
||||
internal static void IPv4Handler(byte[] packetData)
|
||||
{
|
||||
IPPacket ip_packet = new IPPacket(packetData);
|
||||
|
|
@ -38,17 +50,31 @@ namespace Cosmos.System.Network.IPv4
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get next IP fragment ID.
|
||||
/// </summary>
|
||||
public static ushort NextIPFragmentID => sNextFragmentID++;
|
||||
|
||||
/// <summary>
|
||||
/// Create new inctanse of the <see cref="IPPacket"/> class.
|
||||
/// </summary>
|
||||
internal IPPacket()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create new inctanse of the <see cref="IPPacket"/> class.
|
||||
/// </summary>
|
||||
/// <param name="rawData">Raw data.</param>
|
||||
internal IPPacket(byte[] rawData)
|
||||
: base(rawData)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Init IPPacket fields.
|
||||
/// </summary>
|
||||
/// <exception cref="sys.ArgumentException">Thrown if RawData is invalid or null.</exception>
|
||||
protected override void initFields()
|
||||
{
|
||||
base.initFields();
|
||||
|
|
@ -67,10 +93,29 @@ namespace Cosmos.System.Network.IPv4
|
|||
DataOffset = (ushort)(14 + HeaderLength);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create new inctanse of the <see cref="IPPacket"/> class.
|
||||
/// </summary>
|
||||
/// <param name="dataLength">Data length.</param>
|
||||
/// <param name="protocol">Protocol.</param>
|
||||
/// <param name="source">Source address.</param>
|
||||
/// <param name="dest">Destionation address.</param>
|
||||
/// <param name="Flags">Flags.</param>
|
||||
protected IPPacket(ushort dataLength, byte protocol, Address source, Address dest, byte Flags)
|
||||
: this(MACAddress.None, MACAddress.None, dataLength, protocol, source, dest, Flags)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Create new inctanse of the <see cref="IPPacket"/> class.
|
||||
/// </summary>
|
||||
/// <param name="srcMAC">Source MAC address.</param>
|
||||
/// <param name="destMAC">Destination MAC address.</param>
|
||||
/// <param name="dataLength">Data length.</param>
|
||||
/// <param name="protocol">Protocol.</param>
|
||||
/// <param name="source">Source address.</param>
|
||||
/// <param name="dest">Destionation address.</param>
|
||||
/// <param name="Flags">Flags.</param>
|
||||
/// <exception cref="sys.ArgumentException">Thrown if RawData is invalid or null.</exception>
|
||||
public IPPacket(MACAddress srcMAC, MACAddress destMAC, ushort dataLength, byte protocol,
|
||||
Address source, Address dest, byte Flags)
|
||||
: base(destMAC, srcMAC, 0x0800, dataLength + 14 + 20)
|
||||
|
|
@ -103,8 +148,21 @@ namespace Cosmos.System.Network.IPv4
|
|||
initFields();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calcutale CRC.
|
||||
/// </summary>
|
||||
/// <param name="offset">Offset.</param>
|
||||
/// <param name="length">Length.</param>
|
||||
/// <returns></returns>
|
||||
protected ushort CalcOcCRC(ushort offset, ushort length) => CalcOcCRC(RawData, offset, length);
|
||||
|
||||
/// <summary>
|
||||
/// Calcutale CRC.
|
||||
/// </summary>
|
||||
/// <param name="buffer">Buffer.</param>
|
||||
/// <param name="offset">Offset.</param>
|
||||
/// <param name="length">Length.</param>
|
||||
/// <returns>ushort value.</returns>
|
||||
protected static ushort CalcOcCRC(byte[] buffer, ushort offset, int length)
|
||||
{
|
||||
uint crc = 0;
|
||||
|
|
@ -119,29 +177,83 @@ namespace Cosmos.System.Network.IPv4
|
|||
return (ushort)crc;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calcutale CRC.
|
||||
/// </summary>
|
||||
/// <param name="headerLength">Header length.</param>
|
||||
/// <returns>ushort value.</returns>
|
||||
protected ushort CalcIPCRC(ushort headerLength)
|
||||
{
|
||||
return CalcOcCRC(14, headerLength);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get IP version.
|
||||
/// </summary>
|
||||
internal byte IPVersion { get; private set; }
|
||||
/// <summary>
|
||||
/// Get header length.
|
||||
/// </summary>
|
||||
internal ushort HeaderLength => (ushort)(ipHeaderLength * 4);
|
||||
|
||||
/// <summary>
|
||||
/// Get type of service.
|
||||
/// </summary>
|
||||
internal byte TypeOfService { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Get IP length.
|
||||
/// </summary>
|
||||
internal ushort IPLength { get; private set; }
|
||||
/// <summary>
|
||||
/// Get fragment ID.
|
||||
/// </summary>
|
||||
internal ushort FragmentID { get; private set; }
|
||||
/// <summary>
|
||||
/// Get flags.
|
||||
/// </summary>
|
||||
internal byte Flags { get; private set; }
|
||||
/// <summary>
|
||||
/// Get fragment offset.
|
||||
/// </summary>
|
||||
internal ushort FragmentOffset { get; private set; }
|
||||
/// <summary>
|
||||
/// Get TTL.
|
||||
/// </summary>
|
||||
internal byte TTL { get; private set; }
|
||||
/// <summary>
|
||||
/// Get protocol.
|
||||
/// </summary>
|
||||
internal byte Protocol { get; private set; }
|
||||
/// <summary>
|
||||
/// Get IPCRC.
|
||||
/// </summary>
|
||||
internal ushort IPCRC { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Get source IP.
|
||||
/// </summary>
|
||||
internal Address SourceIP { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Get destination IP.
|
||||
/// </summary>
|
||||
internal Address DestinationIP { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Get data offset.
|
||||
/// </summary>
|
||||
internal ushort DataOffset { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Get data length.
|
||||
/// </summary>
|
||||
internal ushort DataLength => (ushort)(IPLength - HeaderLength);
|
||||
|
||||
/// <summary>
|
||||
/// To string.
|
||||
/// </summary>
|
||||
/// <returns>string value.</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return "IP Packet Src=" + SourceIP + ", Dest=" + DestinationIP + ", Protocol=" + Protocol + ", TTL=" + TTL + ", DataLen=" + DataLength;
|
||||
|
|
|
|||
Loading…
Reference in a new issue