public ArpGenericData(UInt64 destinationEthernetAddress, ArpOperation arpOperation, UInt64 targetPhysicalAddress, UInt32 targetProtocolAddress)
 {
     this.DestinationEthernetAddress = destinationEthernetAddress;
     this.ArpOperaton           = arpOperation;
     this.TargetPhysicalAddress = targetPhysicalAddress;
     this.TargetProtocolAddress = targetProtocolAddress;
 }
 public ArpGenericData(UInt64 destinationEthernetAddress, ArpOperation arpOperation, UInt64 targetPhysicalAddress, UInt32 targetProtocolAddress)
 {
     this.DestinationEthernetAddress = destinationEthernetAddress;
     this.ArpOperaton = arpOperation;
     this.TargetPhysicalAddress = targetPhysicalAddress;
     this.TargetProtocolAddress = targetProtocolAddress;
 }
        void SendArpGenericInBackground(UInt64 destinationEthernetAddress, ArpOperation arpOperation, UInt64 targetPhysicalAddress, UInt32 targetProtocolAddress)
        {
            ArpGenericData arpGenericData = new ArpGenericData(destinationEthernetAddress, arpOperation, targetPhysicalAddress, targetProtocolAddress);

            _sendArpGenericInBackgroundQueue.Enqueue(arpGenericData);
            _sendArpGenericInBackgroundEvent.Set();
        }
Exemple #4
0
#pragma warning restore 0169, 0649
#endif

        /// <summary>
        /// Create an ArpPacket from values
        /// </summary>
        /// <param name="operation">
        /// A <see cref="ArpOperation" />
        /// </param>
        /// <param name="targetHardwareAddress">
        /// A <see cref="PhysicalAddress" />
        /// </param>
        /// <param name="targetProtocolAddress">
        /// A <see cref="IPAddress" />
        /// </param>
        /// <param name="senderHardwareAddress">
        /// A <see cref="PhysicalAddress" />
        /// </param>
        /// <param name="senderProtocolAddress">
        /// A <see cref="IPAddress" />
        /// </param>
        public ArpPacket
        (
            ArpOperation operation,
            PhysicalAddress targetHardwareAddress,
            IPAddress targetProtocolAddress,
            PhysicalAddress senderHardwareAddress,
            IPAddress senderProtocolAddress)
        {
            Log.Debug("");

            // allocate memory for this packet
            var length      = ArpFields.HeaderLength;
            var headerBytes = new byte[length];

            Header = new ByteArraySegment(headerBytes, 0, length);

            Operation             = operation;
            TargetHardwareAddress = targetHardwareAddress;
            TargetProtocolAddress = targetProtocolAddress;
            SenderHardwareAddress = senderHardwareAddress;
            SenderProtocolAddress = senderProtocolAddress;

            // set some internal properties to fully define the packet
            HardwareAddressType   = LinkLayers.Ethernet;
            HardwareAddressLength = EthernetFields.MacAddressLength;

            ProtocolAddressType   = EthernetType.IPv4;
            ProtocolAddressLength = IPv4Fields.AddressLength;
        }
Exemple #5
0
        public ArpPacket GetArpPacketFromGUI(ArpOperation arpOperation, string RemoteMAC, string RemoteIP, string LocalMAC, string LocalIP)
        {
            ArpPacket arpPacket = new ArpPacket(
                arpOperation,
                PhysicalAddress.Parse(RemoteMAC),
                IPAddress.Parse(RemoteIP),
                PhysicalAddress.Parse(LocalMAC),
                IPAddress.Parse(LocalIP));

            return(arpPacket);
        }
        void SendArpGeneric(UInt64 destinationEthernetAddress, ArpOperation arpOperation, UInt64 targetPhysicalAddress, UInt32 targetProtocolAddress, Int64 timeoutInMachineTicks)
        {
            if (_isDisposed)
            {
                return;
            }

            lock (_arpFrameBufferLock)
            {
                UInt64 physicalAddress = _ethernetInterface.PhysicalAddressAsUInt64;

                // configure ARP packet
                /* Op: request (1) or reply (2) */
                _arpFrameBuffer[6] = (byte)(((UInt16)arpOperation >> 8) & 0xFF);
                _arpFrameBuffer[7] = (byte)((UInt16)arpOperation & 0xFF);
                /* Sender Harwdare Address */
                _arpFrameBuffer[8]  = (byte)((physicalAddress >> 40) & 0xFF);
                _arpFrameBuffer[9]  = (byte)((physicalAddress >> 32) & 0xFF);
                _arpFrameBuffer[10] = (byte)((physicalAddress >> 24) & 0xFF);
                _arpFrameBuffer[11] = (byte)((physicalAddress >> 16) & 0xFF);
                _arpFrameBuffer[12] = (byte)((physicalAddress >> 8) & 0xFF);
                _arpFrameBuffer[13] = (byte)(physicalAddress & 0xFF);
                /* Sender Protocol Address */
                _arpFrameBuffer[14] = (byte)((_ipv4ProtocolAddress >> 24) & 0xFF);
                _arpFrameBuffer[15] = (byte)((_ipv4ProtocolAddress >> 16) & 0xFF);
                _arpFrameBuffer[16] = (byte)((_ipv4ProtocolAddress >> 8) & 0xFF);
                _arpFrameBuffer[17] = (byte)(_ipv4ProtocolAddress & 0xFF);
                /* Target Harwdare Address (if known) */
                _arpFrameBuffer[18] = (byte)((targetPhysicalAddress >> 40) & 0xFF);
                _arpFrameBuffer[19] = (byte)((targetPhysicalAddress >> 32) & 0xFF);
                _arpFrameBuffer[20] = (byte)((targetPhysicalAddress >> 24) & 0xFF);
                _arpFrameBuffer[21] = (byte)((targetPhysicalAddress >> 16) & 0xFF);
                _arpFrameBuffer[22] = (byte)((targetPhysicalAddress >> 8) & 0xFF);
                _arpFrameBuffer[23] = (byte)(targetPhysicalAddress & 0xFF);
                /* Target Protocol Address */
                _arpFrameBuffer[24] = (byte)((targetProtocolAddress >> 24) & 0xFF);
                _arpFrameBuffer[25] = (byte)((targetProtocolAddress >> 16) & 0xFF);
                _arpFrameBuffer[26] = (byte)((targetProtocolAddress >> 8) & 0xFF);
                _arpFrameBuffer[27] = (byte)(targetProtocolAddress & 0xFF);

                _bufferArray[0] = _arpFrameBuffer;
                _ethernetInterface.Send(destinationEthernetAddress, DATA_TYPE_ARP /* dataType: ARP */, _ipv4ProtocolAddress, targetProtocolAddress, 1 /* one buffer in bufferArray */, _bufferArray, _indexArray, _countArray, timeoutInMachineTicks);
            }
        }
        void SendArpGeneric(UInt64 destinationEthernetAddress, ArpOperation arpOperation, UInt64 targetPhysicalAddress, UInt32 targetProtocolAddress, Int64 timeoutInMachineTicks)
        {
            if (_isDisposed) return;

            lock (_arpFrameBufferLock)
            {
                UInt64 physicalAddress = _ethernetInterface.PhysicalAddressAsUInt64;

                // configure ARP packet
                /* Op: request (1) or reply (2) */
                _arpFrameBuffer[6] = (byte)(((UInt16)arpOperation >> 8) & 0xFF);
                _arpFrameBuffer[7] = (byte)((UInt16)arpOperation & 0xFF);
                /* Sender Harwdare Address */
                _arpFrameBuffer[8] = (byte)((physicalAddress >> 40) & 0xFF);
                _arpFrameBuffer[9] = (byte)((physicalAddress >> 32) & 0xFF);
                _arpFrameBuffer[10] = (byte)((physicalAddress >> 24) & 0xFF);
                _arpFrameBuffer[11] = (byte)((physicalAddress >> 16) & 0xFF);
                _arpFrameBuffer[12] = (byte)((physicalAddress >> 8) & 0xFF);
                _arpFrameBuffer[13] = (byte)(physicalAddress & 0xFF);
                /* Sender Protocol Address */
                _arpFrameBuffer[14] = (byte)((_ipv4ProtocolAddress >> 24) & 0xFF);
                _arpFrameBuffer[15] = (byte)((_ipv4ProtocolAddress >> 16) & 0xFF);
                _arpFrameBuffer[16] = (byte)((_ipv4ProtocolAddress >> 8) & 0xFF);
                _arpFrameBuffer[17] = (byte)(_ipv4ProtocolAddress & 0xFF);
                /* Target Harwdare Address (if known) */
                _arpFrameBuffer[18] = (byte)((targetPhysicalAddress >> 40) & 0xFF);
                _arpFrameBuffer[19] = (byte)((targetPhysicalAddress >> 32) & 0xFF);
                _arpFrameBuffer[20] = (byte)((targetPhysicalAddress >> 24) & 0xFF);
                _arpFrameBuffer[21] = (byte)((targetPhysicalAddress >> 16) & 0xFF);
                _arpFrameBuffer[22] = (byte)((targetPhysicalAddress >> 8) & 0xFF);
                _arpFrameBuffer[23] = (byte)(targetPhysicalAddress & 0xFF);
                /* Target Protocol Address */
                _arpFrameBuffer[24] = (byte)((targetProtocolAddress >> 24) & 0xFF);
                _arpFrameBuffer[25] = (byte)((targetProtocolAddress >> 16) & 0xFF);
                _arpFrameBuffer[26] = (byte)((targetProtocolAddress >> 8) & 0xFF);
                _arpFrameBuffer[27] = (byte)(targetProtocolAddress & 0xFF);

                _bufferArray[0] = _arpFrameBuffer;
                _ethernetInterface.Send(destinationEthernetAddress, DATA_TYPE_ARP /* dataType: ARP */, _ipv4ProtocolAddress, targetProtocolAddress, 1 /* one buffer in bufferArray */, _bufferArray, _indexArray, _countArray, timeoutInMachineTicks);
            }
        }
 void SendArpGenericInBackground(UInt64 destinationEthernetAddress, ArpOperation arpOperation, UInt64 targetPhysicalAddress, UInt32 targetProtocolAddress)
 {
     ArpGenericData arpGenericData = new ArpGenericData(destinationEthernetAddress, arpOperation, targetPhysicalAddress, targetProtocolAddress);
     _sendArpGenericInBackgroundQueue.Enqueue(arpGenericData);
     _sendArpGenericInBackgroundEvent.Set();
 }
Exemple #9
0
 internal static void WriteHeader(byte[] buffer, int offset,
                                  ArpHardwareType hardwareType, EthernetType protocolType, ArpOperation operation,
                                  IList<byte> senderHardwareAddress, IList<byte> senderProtocolAddress,
                                  IList<byte> targetHardwareAddress, IList<byte> targetProtocolAddress)
 {
     buffer.Write(ref offset, (ushort)hardwareType, Endianity.Big);
     buffer.Write(ref offset, (ushort)protocolType, Endianity.Big);
     buffer.Write(ref offset, (byte)senderHardwareAddress.Count);
     buffer.Write(ref offset, (byte)senderProtocolAddress.Count);
     buffer.Write(ref offset, (ushort)operation, Endianity.Big);
     buffer.Write(ref offset, senderHardwareAddress);
     buffer.Write(ref offset, senderProtocolAddress);
     buffer.Write(ref offset, targetHardwareAddress);
     buffer.Write(ref offset, targetProtocolAddress);
 }
Exemple #10
0
 internal static void WriteHeader(byte[] buffer, int offset, ArpHardwareType hardwareType, EthernetType protocolType, ArpOperation operation, IList <byte> senderHardwareAddress, IList <byte> senderProtocolAddress, IList <byte> targetHardwareAddress, IList <byte> targetProtocolAddress)
 {
     ByteArrayExtensions.Write(buffer, ref offset, (ushort)hardwareType, Endianity.Big);
     ByteArrayExtensions.Write(buffer, ref offset, (ushort)protocolType, Endianity.Big);
     ByteArrayExtensions.Write(buffer, ref offset, (byte)senderHardwareAddress.Count);
     ByteArrayExtensions.Write(buffer, ref offset, (byte)senderProtocolAddress.Count);
     ByteArrayExtensions.Write(buffer, ref offset, (ushort)operation, Endianity.Big);
     ByteArrayExtensions.Write(buffer, ref offset, (IEnumerable <byte>)senderHardwareAddress);
     ByteArrayExtensions.Write(buffer, ref offset, (IEnumerable <byte>)senderProtocolAddress);
     ByteArrayExtensions.Write(buffer, ref offset, (IEnumerable <byte>)targetHardwareAddress);
     ByteArrayExtensions.Write(buffer, ref offset, (IEnumerable <byte>)targetProtocolAddress);
 }
Exemple #11
0
 internal static void WriteHeader(byte[] buffer, int offset,
                                  ArpHardwareType hardwareType, EthernetType protocolType, ArpOperation operation,
                                  IList <byte> senderHardwareAddress, IList <byte> senderProtocolAddress,
                                  IList <byte> targetHardwareAddress, IList <byte> targetProtocolAddress)
 {
     buffer.Write(ref offset, (ushort)hardwareType, Endianity.Big);
     buffer.Write(ref offset, (ushort)protocolType, Endianity.Big);
     buffer.Write(ref offset, (byte)senderHardwareAddress.Count);
     buffer.Write(ref offset, (byte)senderProtocolAddress.Count);
     buffer.Write(ref offset, (ushort)operation, Endianity.Big);
     buffer.Write(ref offset, senderHardwareAddress);
     buffer.Write(ref offset, senderProtocolAddress);
     buffer.Write(ref offset, targetHardwareAddress);
     buffer.Write(ref offset, targetProtocolAddress);
 }
        void _ethernetInterface_ARPFrameReceived(object sender, byte[] buffer, int index, int count)
        {
            // verify that our ARP frame is long enough
            if (count < ARP_FRAME_BUFFER_LENGTH)
            {
                return; // discard packet
            }
            /* parse our ARP frame */
            // first validate our frame's fixed header
            UInt16 hardwareType = (UInt16)((buffer[index] << 8) + buffer[index + 1]);

            if (hardwareType != HARDWARE_TYPE_ETHERNET)
            {
                return; // only Ethernet hardware is supported; discard frame
            }
            UInt16 protocolType = (UInt16)((buffer[index + 2] << 8) + buffer[index + 3]);

            if (protocolType != PROTOCOL_TYPE_IPV4)
            {
                return; // only IPv4 protocol is supported; discard frame
            }
            byte hardwareAddressSize = buffer[index + 4];

            if (hardwareAddressSize != HARDWARE_ADDRESS_SIZE)
            {
                return; // invalid hardware address size
            }
            byte protocolAddressSize = buffer[index + 5];

            if (protocolAddressSize != PROTOCOL_ADDRESS_SIZE)
            {
                return; // invalid protocol address size
            }
            ArpOperation operation = (ArpOperation)((buffer[index + 6] << 8) + buffer[index + 7]);
            // NOTE: we will validate the operation a bit later after we compare the incoming targetProtocolAddress.
            // retrieve the sender and target addresses
            UInt64 senderPhysicalAddress = (UInt64)(((UInt64)buffer[index + 8] << 40) + ((UInt64)buffer[index + 9] << 32) + ((UInt64)buffer[index + 10] << 24) + ((UInt64)buffer[index + 11] << 16) + ((UInt64)buffer[index + 12] << 8) + (UInt64)buffer[index + 13]);
            UInt32 senderProtocolAddress = (UInt32)(((UInt32)buffer[index + 14] << 24) + ((UInt32)buffer[index + 15] << 16) + ((UInt32)buffer[index + 16] << 8) + (UInt32)buffer[index + 17]);
            //UInt64 targetHardwareAddress = (UInt64)(((UInt64)buffer[index + 18] << 40) + ((UInt64)buffer[index + 19] << 32) + ((UInt64)buffer[index + 20] << 24) + ((UInt64)buffer[index + 21] << 16) + ((UInt64)buffer[index + 22] << 8) + (UInt64)buffer[index + 23]);
            UInt32 targetProtocolAddress = (UInt32)(((UInt32)buffer[index + 24] << 24) + ((UInt32)buffer[index + 25] << 16) + ((UInt32)buffer[index + 26] << 8) + (UInt32)buffer[index + 27]);

            // if the sender IP is already listed in our ARP cache then update the entry
            lock (_arpCacheLock)
            {
                ArpCacheEntry arpEntry = (ArpCacheEntry)_arpCache[senderProtocolAddress];
                if (arpEntry != null)
                {
                    arpEntry.PhysicalAddress = senderPhysicalAddress;
                    Int64 nowTicks = Microsoft.SPOT.Hardware.Utility.GetMachineTime().Ticks;
                    arpEntry.TimeoutTicks = nowTicks + (TimeSpan.TicksPerSecond * DEFAULT_ARP_CACHE_TIMEOUT_IN_SECONDS);
                    // NOTE: we do not update the LastUsedTicks property since this is not the result of a request for a non-existent cache entry
                }
                else
                {
                    // do nothing.  some implementation of ARP would create a cache entry for any incoming ARP packets...but we only cache a limited number of entries which we requested.
                }
            }

            if (operation == ArpOperation.ARP_OPERATION_REQUEST)
            {
                // if the request is asking for our IP protocol address, then send an ARP reply
                if (targetProtocolAddress == _ipv4ProtocolAddress)
                {
                    // we do not want to block our RX thread, so queue a response on a worker thread
                    SendArpGenericInBackground(senderPhysicalAddress, ArpOperation.ARP_OPERATION_REPLY, senderPhysicalAddress, senderProtocolAddress);
                }
            }
            else if (operation == ArpOperation.ARP_OPERATION_REPLY)
            {
                if (senderProtocolAddress == _currentArpRequestProtocolAddress)
                {
                    _currentArpRequestPhysicalAddress = senderPhysicalAddress;
                    _currentArpRequestAnsweredEvent.Set();
                }
            }
            else
            {
                // invalid operation; discard frame
            }
        }