/// <summary> /// Initializes a new instance of the Nic class using the specified /// MAC address. /// </summary> /// <param name="address">A MAC-48 address to assign to the NIC. If this /// is null, the NIC is assigned a random MAC address.</param> protected Nic(MacAddress address = null) { if (address != null) MacAddress = address; else MacAddress = new MacAddress(); Connector = new Connector(); }
/// <summary> /// Initializes a new instance of the ArpEntry class using the specified /// values. /// </summary> /// <param name="ipAddress">The IPv4 address to initialize this entry /// with.</param> /// <param name="macAddress">The MAC-48 address to initialize this entry /// with.</param> /// <exception cref="ArgumentNullException">Thrown if either argument is /// null.</exception> public ArpEntry(IpAddress ipAddress, MacAddress macAddress) { ipAddress.ThrowIfNull("ipAddress"); macAddress.ThrowIfNull("macAddress"); IpAddress = ipAddress; MacAddress = macAddress; ExpiryTime = Simulation.Time + maxAge; }
/// <summary> /// Initializes a new instance of the ArpPacket class creating an ARP /// request message. /// </summary> /// <param name="macAddressSender">The MAC-48 address of the sender.</param> /// <param name="ipAddressSender">The IPv4 address of the sender.</param> /// <param name="ipAddressTarget">The IPv4 address of the target.</param> /// <exception cref="ArgumentNullException">Thrown if any of the arguments /// is null.</exception> public ArpPacket(MacAddress macAddressSender, IpAddress ipAddressSender, IpAddress ipAddressTarget) { macAddressSender.ThrowIfNull("macAddressSender"); ipAddressSender.ThrowIfNull("ipAddressSender"); ipAddressTarget.ThrowIfNull("ipAddressTarget"); IsRequest = true; MacAddressSender = macAddressSender; IpAddressSender = ipAddressSender; IpAddressTarget = ipAddressTarget; // Broadcast to all stations. MacAddressTarget = broadcastAddress; }
/// <summary> /// Initializes a new EthernetFrame instance using the specified values. /// </summary> /// <param name="destination">The physical address of the destination /// host.</param> /// <param name="source">The physical address of the source host.</param> /// <param name="payload">The data transmitted in the frame's payload /// field.</param> /// <param name="type">The type of the encapsulated protocol.</param> /// <exception cref="ArgumentNullException">Thrown if any of the /// arguments is null.</exception> /// <exception cref="ArgumentException">Thrown if the payload parameter /// does not satisfy the Ethernet 802.3 length requirements.</exception> public Frame(MacAddress destination, MacAddress source, byte[] payload, EtherType type) { destination.ThrowIfNull("destination"); source.ThrowIfNull("source"); payload.ThrowIfNull("payload"); if (payload.Length > MaximumPayloadSize) throw new ArgumentException("The frame payload data must " + "not exceed 1500 bytes of data.", nameof(payload)); Destination = destination; Source = source; Payload = payload; Type = type; CheckSequence = Crc32.Compute( new ByteBuilder() .Append(Destination.Bytes) .Append(Source.Bytes) .Append((short)Type) .Append(Payload) .ToArray() ); }
/// <summary> /// Hands down the specified data to the data-link layer implementation of the NIC. /// </summary> /// <param name="destination">The phyical destination address.</param> /// <param name="data">The data to send as the frame's payload.</param> /// <param name="type">The type of the data.</param> public void Output(MacAddress destination, byte[] data, EtherType type = EtherType.IPv4) { Nic.Output(destination, data, type); }
/// <summary> /// Deserializes an ArpPacket instance from the specified sequence of /// bytes. /// </summary> /// <param name="data">The sequence of bytes to deserialize an ArpPacket /// object from.</param> /// <returns>A deserialized ArpPacket object.</returns> public static ArpPacket Deserialize(byte[] data) { using (MemoryStream ms = new MemoryStream(data)) { using (BinaryReader reader = new BinaryReader(ms)) { bool isRequest = reader.ReadBoolean(); MacAddress macSender = new MacAddress(reader.ReadBytes(6)); IpAddress ipSender = new IpAddress(reader.ReadBytes(4)); MacAddress macTarget = new MacAddress(reader.ReadBytes(6)); IpAddress ipTarget = new IpAddress(reader.ReadBytes(4)); if (isRequest) return new ArpPacket(macSender, ipSender, ipTarget); else return new ArpPacket(macSender, ipSender, macTarget, ipTarget); } } }
/// <summary> /// Initializes a new instance of the ArpPacket class creating an ARP /// response message. /// </summary> /// <param name="macAddressSender">The MAC-48 address of the sender.</param> /// <param name="ipAddressSender">The IPv4 address of the sender.</param> /// <param name="macAddressTarget">The MAC-48 address of the target.</param> /// <param name="ipAddressTarget">The IPv4 address of the target.</param> /// <exception cref="ArgumentNullException">Thrown if any of the arguments /// is null.</exception> public ArpPacket(MacAddress macAddressSender, IpAddress ipAddressSender, MacAddress macAddressTarget, IpAddress ipAddressTarget) : this(macAddressSender, ipAddressSender, ipAddressTarget) { macAddressTarget.ThrowIfNull("macAddressTarget"); MacAddressTarget = macAddressTarget; IsRequest = false; }
/// <summary> /// Deserializes an EthernetFrame instance from the specified sequence of /// bytes. /// </summary> /// <param name="data">The sequence of bytes to deserialize an EthernetFrame /// object from.</param> /// <returns>A deserialized EthernetFrame object.</returns> /// <exception cref="ArgumentNullException">Thrown if the data argument is /// null.</exception> public static Frame Deserialize(byte[] data) { data.ThrowIfNull("data"); using (var ms = new MemoryStream(data)) { using (var reader = new BinaryReader(ms)) { MacAddress dest = new MacAddress(reader.ReadBytes(6)), source = new MacAddress(reader.ReadBytes(6)); var type = (EtherType) reader.ReadInt16(); var payloadLen = reader.ReadInt32(); var payload = reader.ReadBytes(payloadLen); // Skip the padding bytes, if any. if (payloadLen < minimumPayloadSize) reader.ReadBytes(minimumPayloadSize - payloadLen); var fcs = reader.ReadUInt32(); return new Frame(dest, source, payload, fcs, type); } } }
/// <summary> /// Initializes a new instance of the EthernetFrame class using the /// specified values. /// </summary> /// <param name="destination">The physical address of the destination /// host.</param> /// <param name="source">The physical address of the source host.</param> /// <param name="payload">The data transmitted in the frame's payload /// field.</param> /// <param name="checkSequence">The value to set as the frame's /// check sequence.</param> /// <param name="type">The type of the encapsulated protocol.</param> /// <exception cref="ArgumentNullException">Thrown if any of the /// arguments is null.</exception> /// <remarks>This is a private constructor solely used for /// deserialization.</remarks> private Frame(MacAddress destination, MacAddress source, byte[] payload, uint checkSequence, EtherType type) { destination.ThrowIfNull("destination"); source.ThrowIfNull("source"); payload.ThrowIfNull("payload"); Destination = destination; Source = source; Payload = payload; Type = type; CheckSequence = checkSequence; }
/// <summary> /// Wraps the specified data into an Ethernet frame and queues it /// for transmission. /// </summary> /// <param name="destination">The MAC address of the destination /// host.</param> /// <param name="data">The data to transmit as part of the frame's /// payload.</param> /// <param name="type">The type of the data.</param> public override void Output(MacAddress destination, byte[] data, EtherType type = EtherType.IPv4) { destination.ThrowIfNull("destination"); data.ThrowIfNull("data"); // Start emptying the FIFO, if we're not already doing it. if (emptyingFifo == false) Simulation.Callback(0, EmptySendFifo); // Enqueue the data. sendFifo.Enqueue(new Frame(destination, MacAddress, data, type)); }
/// <summary> /// Initializes a new instance of the Nic class using the specified /// MAC address. /// </summary> /// <param name="address">A MAC-48 address to assign to the NIC. If this /// is null, the NIC is assigned a random MAC address.</param> public Nic(MacAddress address = null) : base(address) { Connector.SignalSense += OnSignalSense; Connector.SignalCease += OnSignalCease; }
/// <summary> /// Constructs a new data-link frame for the specified destination, /// containing the specified data as payload. /// </summary> /// <param name="destination">The phyical destination address.</param> /// <param name="data">The data to send as the frame's payload.</param> /// <param name="type">The type of the data.</param> /// <exception cref="ArgumentNullException">Thrown if the destination /// or the data parameter is null.</exception> public abstract void Output(MacAddress destination, byte[] data, EtherType type);