Esempio n. 1
0
 public static void LogUnhandledRead(this IPeripheral peripheral, long offset)
 {
     peripheral.Log(LogLevel.Warning, "Unhandled read from offset 0x{0:X}.", offset);
 }
Esempio n. 2
0
 public static void LogUnhandledWrite(this IPeripheral peripheral, long offset, long value)
 {
     peripheral.Log(LogLevel.Warning, "Unhandled write to offset 0x{0:X}, value 0x{1:X}.", offset, value);
 }
Esempio n. 3
0
        //TODO: Support for ipv6
        public static void FillPacketWithChecksums(IPeripheral source, byte[] packet, params TransportLayerProtocol[] interpretedProtocols)
        {
            if (packet.Length < MACLength)
            {
                source.Log(LogLevel.Error, String.Format("Expected packet of at least {0} bytes, got {1}.", MACLength, packet.Length));
                return;
            }
            var packet_type = (PacketType)((packet[12] << 8) | packet[13]);

            if (packet_type == PacketType.ARP)
            {
                // ARP
                return;
            }
            else if (packet_type != PacketType.IP)
            {
                source.Log(LogLevel.Error, String.Format("Unknown packet type: 0x{0:X}. Supported are: 0x800 (IP) and 0x806 (ARP).", (ushort)packet_type));
                return;
            }
            if (packet.Length < (MACLength + 12))
            {
                source.Log(LogLevel.Error, "IP Packet is too short!");
                return;
            }
            // IPvX
            if ((packet[MACLength] >> 4) != 0x04)
            {
                source.Log(LogLevel.Error, String.Format("Only IPv4 packets are supported. Got IPv{0}", (packet[MACLength] >> 4)));
                return;
            }
            // IPv4
            var ipLength = (packet[MACLength] & 0x0F) * 4;

            if (ipLength != 0)
            {
                var ipChecksum = ComputeHeaderIpChecksum(packet, MACLength, ipLength);
                packet[MACLength + 10] = (byte)(ipChecksum >> 8);
                packet[MACLength + 11] = (byte)(ipChecksum & 0xFF);
            }
            else
            {
                source.Log(LogLevel.Error, "Something is wrong - IP packet of len 0");
            }
            if (interpretedProtocols != null && interpretedProtocols.Contains((TransportLayerProtocol)packet[MACLength + 9]))
            {
                var payloadStart = MACLength + ipLength;
                var protocol     = (TransportLayerProtocol)packet[MACLength + 9];
                var checksum     = GetPacketChecksum(packet, MACLength, payloadStart, protocol != TransportLayerProtocol.ICMP);
                switch (protocol)
                {
                case TransportLayerProtocol.ICMP:
                    packet[payloadStart + 2] = (byte)((checksum >> 8) & 0xFF);
                    packet[payloadStart + 3] = (byte)((checksum) & 0xFF);
                    break;

                case TransportLayerProtocol.TCP:
                    packet[payloadStart + 16] = (byte)((checksum >> 8) & 0xFF);
                    packet[payloadStart + 17] = (byte)((checksum) & 0xFF);
                    break;

                case TransportLayerProtocol.UDP:
                    packet[payloadStart + 6] = (byte)((checksum >> 8) & 0xFF);
                    packet[payloadStart + 7] = (byte)((checksum) & 0xFF);
                    break;

                default:
                    throw new NotImplementedException();
                }
            }
        }