using System;
using Cosmos.HAL.Network;
namespace Cosmos.System.Network
{
// for more info, http://standards.ieee.org/about/get/802/802.3.html
///
/// EthernetPacket class.
///
public class EthernetPacket
{
///
/// Source MAC address.
///
protected MACAddress srcMAC;
///
/// Destination MAC address.
///
protected MACAddress destMAC;
///
/// Create new inctanse of the class.
///
protected EthernetPacket()
{
}
///
/// Create new inctanse of the class, with specified raw data.
///
/// Raw data.
protected EthernetPacket(byte[] rawData)
{
RawData = rawData;
initFields();
}
///
/// Init EthernetPacket fields.
///
protected virtual void initFields()
{
destMAC = new MACAddress(RawData, 0);
srcMAC = new MACAddress(RawData, 6);
EthernetType = (ushort)((RawData[12] << 8) | RawData[13]);
}
///
/// Create new inctanse of the class, with specified type and size.
///
/// Type.
/// Size.
protected EthernetPacket(ushort type, int packet_size)
: this(MACAddress.None, MACAddress.None, type, packet_size)
{
}
///
/// Create new inctanse of the class, with specified dsetination, source, type and size.
///
/// Destination.
/// Source.
/// Type.
/// Size.
protected EthernetPacket(MACAddress dest, MACAddress src, ushort type, int packet_size)
{
RawData = new byte[packet_size];
for (int i = 0; i < 6; i++)
{
RawData[i] = dest.bytes[i];
RawData[6 + i] = src.bytes[i];
}
RawData[12] = (byte)(type >> 8);
RawData[13] = (byte)(type >> 0);
initFields();
}
///
/// Get raw data byte array.
///
internal byte[] RawData { get; }
///
/// Get and set source MAC address.
///
internal MACAddress SourceMAC
{
get => srcMAC;
set
{
for (int i = 0; i < 6; i++)
{
RawData[6 + i] = value.bytes[i];
}
initFields();
}
}
///
/// Get and set destination MAC address.
///
internal MACAddress DestinationMAC
{
get => destMAC;
set
{
for (int i = 0; i < 6; i++)
{
RawData[i] = value.bytes[i];
}
initFields();
}
}
///
/// Get packet type.
///
internal ushort EthernetType { get; private set; }
///
/// Prepare packet for sending.
/// Not implemented.
///
public virtual void PrepareForSending()
{
}
///
/// To string.
///
/// string value.
public override string ToString()
{
return "Ethernet Packet : Src=" + srcMAC + ", Dest=" + destMAC + ", Type=" + EthernetType;
}
}
}