/* * PROJECT: Aura Operating System Development * CONTENT: Ethernet frame * PROGRAMMERS: Valentin Charbonnier * Port of Cosmos Code. */ 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 instance of the class. /// protected EthernetPacket() { } /// /// Create new instance of the class, with specified raw data. /// /// Raw data. public 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 instance 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 instance 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. /// public 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; } } }