Beispiel #1
0
 public override void Reset()
 {
     registers = new regs();
     MAC = new MACAddress();
     transmitDescriptorBase = 0;
     transmitDescriptorOffset = 0;
     receiveDescriptorBase = 0;
     transmitDescriptorOffset = 0;
 }
Beispiel #2
0
        /// <summary>
        /// Compares to.
        /// </summary>
        /// <param name="mac">The mac.</param>
        /// <returns></returns>
        public bool CompareTo(MACAddress mac)
        {
            //if ((mac.address == null) || (this.address == null)) return false;

            for (int i = 0; i < 6; i++)
                if (mac.address[i] != address[i])
                    return false;

            return true;
        }
        /// <summary>
        ///   Инициализация фрейма.
        /// </summary>
        /// <param name = "packet">Данные.</param>
        /// <param name = "proto">Тип данных (протокол).</param>
        /// <param name = "vlan">Номер vlan.</param>
        /// <param name = "source">MAC-адрес источника.</param>
        /// <param name = "destination">MAC-адрес назначения.</param>
        /// <exception cref = "ArgumentNullException"><paramref name = "packet" /> является <c>null</c>.</exception>
        /// <exception cref = "ArgumentNullException"><paramref name = "source" /> является <c>null</c>.</exception>
        /// <exception cref = "ArgumentNullException"><paramref name = "destination" /> является <c>null</c>.</exception>
        public TaggedEthernetFrame(IPacket packet,
            EtherType proto,
            short vlan,
            MACAddress source,
            MACAddress destination)
            : base(packet == null ? 0 : (packet.Data.Length + 4), EtherType.Tagged, source, destination)
        {
            if (packet == null)
                throw new ArgumentNullException ("packet");

            this.Data [14] = this.Data [15] = 0;
            this.CanonicalFormatIndicator = false;
            this.PriorityCodePoint = 0;

            this.VlanID = vlan;
            this.Type = proto;

            for (var i = 0; i < packet.Data.Length; ++i)
                this.Data [18 + i] = packet.Data [i];
        }
        /// <summary>
        ///   Инициализация фрейма.
        /// </summary>
        /// <param name = "packet">Данные.</param>
        /// <param name = "proto">Тип данных (протокол).</param>
        /// <param name = "source">MAC-адрес источника.</param>
        /// <param name = "destination">MAC-адрес назначения.</param>
        /// <exception cref = "ArgumentNullException"><paramref name = "packet" /> является <c>null</c>.</exception>
        /// <exception cref = "ArgumentNullException"><paramref name = "source" /> является <c>null</c>.</exception>
        /// <exception cref = "ArgumentNullException"><paramref name = "destination" /> является <c>null</c>.</exception>
        protected EthernetFrameBase(IPacket packet, EtherType proto, MACAddress source, MACAddress destination)
        {
            if (packet == null)
                throw new ArgumentNullException ("packet");

            if (source == null)
                throw new ArgumentNullException ("source");

            if (destination == null)
                throw new ArgumentNullException ("destination");

            this.Data = new Array <byte> (new byte[14 + packet.Data.Length]);

            this.Destination = destination;
            this.Source = source;
            this.SetType (proto);

            for (var i = 0; i < packet.Data.Length; ++i)
                this.Data [14 + i] = packet.Data [i];
        }
Beispiel #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MACAddress"/> class.
 /// </summary>
 /// <param name="mac">The mac.</param>
 public MACAddress(MACAddress mac)
     : this(mac.address)
 {
 }
Beispiel #6
0
        public VT6102(PCIDevice device)
        {
            if (device == null)
            {
                throw new ArgumentException("PCI Device is null. Unable to get VIA Rhine-II card");
            }
            pciCard = device;

            // We are handling this device
            pciCard.Claimed = true;

            // Setup interrupt handling
            //Interrupts.IRQ10 += HandleNetworkInterrupt;
            //Interrupts.AddIRQHandler(device.InterruptLine, HandleNetworkInterrupt);

            // Get IO Address from PCI Bus
            io = pciCard.GetAddressSpace(0) as Kernel.IOAddressSpace;

            // Enable the card
            pciCard.EnableDevice();

            // Get the EEPROM MAC Address and set it as the devices MAC
            byte[] eeprom_mac = new byte[6];
            UInt32 result     = io.Read32(0x00);

            eeprom_mac[0] = BinaryHelper.GetByteFrom32bit(result, 0);
            eeprom_mac[1] = BinaryHelper.GetByteFrom32bit(result, 8);
            eeprom_mac[2] = BinaryHelper.GetByteFrom32bit(result, 16);
            eeprom_mac[3] = BinaryHelper.GetByteFrom32bit(result, 24);
            result        = io.Read32(0x04);
            eeprom_mac[4] = BinaryHelper.GetByteFrom32bit(result, 0);
            eeprom_mac[5] = BinaryHelper.GetByteFrom32bit(result, 8);

            mac = new MACAddress(eeprom_mac);

            // Software Reset device
            SoftwareReset();

            // Configure Receive Config
            ReceiveConfigRegister = 0x1C;
            // Configure Transmit Config
            TransmitConfigRegister = 0x04;

            // Setup RX Descriptors
            mRxDescriptors = new ManagedMemorySpace(256, 16);

            // Setup TX Descriptors
            mTxDescriptors = new ManagedMemorySpace(256, 16);

            /* Initialize the RX and TX buffers, and set up the RX  descriptors to point
             * to the buffers. Also, mark the RX descriptors as being owned by the card so data
             * can be received in them */
            mRxBuffers = new List <ManagedMemorySpace>();
            for (uint rxd = 0; rxd < 16; rxd++)
            {
                uint xOffset = rxd * 16;

                ManagedMemorySpace buffer = new ManagedMemorySpace(2048);
                mRxDescriptors.Write32(xOffset + 12, mRxDescriptors.Offset + xOffset + 16);
                mRxDescriptors.Write32(xOffset + 8, buffer.Offset);
                mRxDescriptors.Write32(xOffset + 4, buffer.Size);
                mRxDescriptors.Write32(xOffset, 0x80000000);
                mRxBuffers.Add(buffer);
            }
            mRxDescriptors.Write32(252, mRxDescriptors.Offset);
            for (uint txd = 0; txd < 16; txd++)
            {
                uint xOffset = txd * 16;

                mTxDescriptors.Write32(xOffset + 12, mTxDescriptors.Offset + xOffset + 16);
                mTxDescriptors.Write32(xOffset + 8, 0);
                mTxDescriptors.Write32(xOffset + 4, 0);
                mTxDescriptors.Write32(xOffset, 0);
            }
            mTxDescriptors.Write32(252, mTxDescriptors.Offset);

            mNextTXDesc = 0;

            RxDescAddressRegister = mRxDescriptors.Offset;
            TxDescAddressRegister = mTxDescriptors.Offset;

            // Setup and clear interrupts
            IntMaskRegister   = 0xFFFF;
            IntStatusRegister = 0xFFFF;

            // Setup our Receive and Transmit Queues
            mTransmitBuffer = new Queue <byte[]>();
            mRecvBuffer     = new Queue <byte[]>();
        }
Beispiel #7
0
        public DHCPDiscover(MACAddress mac_src)
            : base(Address.Zero, mac_src)
        {
            //Request
            mRawData[dataOffset + 8] = 0x01;

            //ethernet
            mRawData[dataOffset + 9] = 0x01;

            //Length mac
            mRawData[dataOffset + 10] = 0x06;

            //hops
            mRawData[dataOffset + 11] = 0x00;


            Random rnd = new Random();

            xID = rnd.Next(0, Int32.MaxValue);
            mRawData[dataOffset + 12] = (byte)((xID >> 24) & 0xFF);
            mRawData[dataOffset + 13] = (byte)((xID >> 16) & 0xFF);
            mRawData[dataOffset + 14] = (byte)((xID >> 8) & 0xFF);
            mRawData[dataOffset + 15] = (byte)((xID >> 0) & 0xFF);


            //option bootp
            for (int i = 16; i < 35; i++)
            {
                mRawData[dataOffset + i] = 0x00;
            }

            //Src mac
            mRawData[dataOffset + 36] = mac_src.bytes[0];
            mRawData[dataOffset + 37] = mac_src.bytes[1];
            mRawData[dataOffset + 38] = mac_src.bytes[2];
            mRawData[dataOffset + 39] = mac_src.bytes[3];
            mRawData[dataOffset + 40] = mac_src.bytes[4];
            mRawData[dataOffset + 41] = mac_src.bytes[5];

            //Fill 0
            for (int i = 42; i < 243; i++)
            {
                mRawData[dataOffset + i] = 0x00;
            }

            //DHCP Magic cookie
            mRawData[dataOffset + 244] = 0x63;
            mRawData[dataOffset + 245] = 0x82;
            mRawData[dataOffset + 246] = 0x53;
            mRawData[dataOffset + 247] = 0x63;

            //options

            //Discover
            mRawData[dataOffset + 248] = 0x35;
            mRawData[dataOffset + 249] = 0x01;
            mRawData[dataOffset + 250] = 0x01;

            //Parameters start here
            mRawData[dataOffset + 251] = 0x37;
            mRawData[dataOffset + 252] = 4;

            //Parameters
            mRawData[dataOffset + 253] = 0x01;
            mRawData[dataOffset + 254] = 0x03;
            mRawData[dataOffset + 255] = 0x0f;
            mRawData[dataOffset + 256] = 0x06;

            mRawData[dataOffset + 257] = 0xff; //ENDMARK

            //Fill 0
            //for (int i = 258; i < 272; i++)
            //{
            //    mRawData[dataOffset + i] = 0x00;
            //}

            initFields();
        }
        /// <inheritdoc />
        public override void Load(XmlNode data)
        {
            base.Load (data);

            this.MAC = MACAddress.Parse (data [XML_MACADDRESS_NODE].InnerText);
        }
Beispiel #9
0
        /// <summary>
        ///		Resolve a physical address to an IP address.
        /// </summary>
        /// <param name="address">
        ///		The physical address to resolve.
        ///	</param>
        /// <returns>
        ///		Returns the IP address belonging to the physical address.
        ///	</returns>
        /// <exception cref="ObjectDisposedException">
        ///		If the object has already been disposed then an ObjectDisposedException
        ///		will be thrown
        ///	</exception>
        /// <exception cref="Exception">
        ///		If the driver failed to start or was not bound, an exception will be thrown.
        ///	</exception>
        public IPAddress ResolveMACAddress(MACAddress address)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }

            if (!m_driver.DriverStarted)
            {
                throw new Exception("The driver has not been started.");
            }

            if (!m_driver.DriverBound)
            {
                throw new Exception("The driver has not yet been bound to a device.");
            }

            Ethernet802_3 ethernet = new Ethernet802_3();

            // construct the ethernet header
            ethernet.SourceMACAddress      = m_driver.BoundAdapter.MediaAccessControlAddress;
            ethernet.DestinationMACAddress = MACAddress.BroadcastAddress;
            ethernet.NetworkProtocol       = NetworkLayerProtocol.ARP;

            ArpPacket arp = new ArpPacket();

            // construct the ARP header
            arp.Type                  = ArpOpcode.ReverseRequest;
            arp.Protocol              = NetworkLayerProtocol.IP;
            arp.MediaType             = MediaType.Ethernet;
            arp.SourceMACAddress      = ethernet.SourceMACAddress;
            arp.SourceIPAddress       = m_driver.BoundAdapter.Interfaces[0].Address;
            arp.DestinationMACAddress = address;

            // serialize and send the packet
            ethernet.Data = arp.Serialize();
            m_driver.SendPacket(ethernet.Serialize());

            m_querying = true;

            // wait for the reply
            while (m_querying)
            {
                byte[] packet = m_driver.RecievePacket();

                Ethernet802_3 ethReply = new Ethernet802_3(packet);

                // if this is an ARP packet
                if (ethReply.NetworkProtocol == NetworkLayerProtocol.ARP)
                {
                    ArpPacket arpReply = new ArpPacket(ethReply.Data);

                    // if this is an ARP reply
                    if (arpReply.Type == ArpOpcode.Reply)
                    {
                        // if the address matches the one we requested
                        if (arpReply.DestinationIPAddress.Equals(address))
                        {
                            // return the IP address
                            return(arpReply.DestinationIPAddress);
                        }
                    }
                }
            }

            return(IPAddress.Any);
        }
Beispiel #10
0
 /// <summary>
 /// Create new inctanse of the <see cref="ARPRequest_Ethernet"/> class.
 /// </summary>
 /// <param name="ourMAC">Source MAC address.</param>
 /// <param name="ourIP">Source IP address.</param>
 /// <param name="targetMAC">Destination MAC address.</param>
 /// <param name="targetIP">Destination IP address.</param>
 /// <param name="arpTargetMAC">ARP destination MAC address.</param>
 /// <exception cref="ArgumentException">Thrown if RawData is invalid or null.</exception>
 internal ARPRequest_Ethernet(MACAddress ourMAC, Address ourIP, MACAddress targetMAC, Address targetIP, MACAddress arpTargetMAC)
     : base(1, ourMAC, ourIP, targetMAC, targetIP, 42, arpTargetMAC)
 {
 }
 protected virtual void initFields()
 {
     destMAC      = new MACAddress(RawData, 0);
     srcMAC       = new MACAddress(RawData, 6);
     EthernetType = (ushort)((RawData[12] << 8) | RawData[13]);
 }
Beispiel #12
0
 public UDPPacket(Address source, Address dest, UInt16 srcport, UInt16 destport, UInt16 datalength, MACAddress destmac)
     : base((ushort)(datalength + 8), 17, source, dest, 0x00, destmac)
 {
     MakePacket(srcport, destport, datalength);
     InitFields();
 }
Beispiel #13
0
        public DHCPRequest(MACAddress mac_src, Address RequestedAddress, Address DHCPServerAddress)
            : base(Address.Zero, mac_src)
        {
            //Request
            mRawData[dataOffset + 8] = 0x01;

            //ethernet
            mRawData[dataOffset + 9] = 0x01;

            //Length mac
            mRawData[dataOffset + 10] = 0x06;

            //hops
            mRawData[dataOffset + 11] = 0x00;


            Random rnd = new Random();

            xID = rnd.Next(0, Int32.MaxValue);
            mRawData[dataOffset + 12] = (byte)((xID >> 24) & 0xFF);
            mRawData[dataOffset + 13] = (byte)((xID >> 16) & 0xFF);
            mRawData[dataOffset + 14] = (byte)((xID >> 8) & 0xFF);
            mRawData[dataOffset + 15] = (byte)((xID >> 0) & 0xFF);


            //option bootp
            for (int i = 16; i < 35; i++)
            {
                mRawData[dataOffset + i] = 0x00;
            }

            //SourceMAC mac
            mRawData[dataOffset + 36] = mac_src.bytes[0];
            mRawData[dataOffset + 37] = mac_src.bytes[1];
            mRawData[dataOffset + 38] = mac_src.bytes[2];
            mRawData[dataOffset + 39] = mac_src.bytes[3];
            mRawData[dataOffset + 40] = mac_src.bytes[4];
            mRawData[dataOffset + 41] = mac_src.bytes[5];

            //Fill 0
            for (int i = 42; i < 243; i++)
            {
                mRawData[dataOffset + i] = 0x00;
            }

            //DHCP Magic cookie
            mRawData[dataOffset + 244] = 0x63;
            mRawData[dataOffset + 245] = 0x82;
            mRawData[dataOffset + 246] = 0x53;
            mRawData[dataOffset + 247] = 0x63;

            //options

            //Request
            mRawData[dataOffset + 248] = 53;
            mRawData[dataOffset + 249] = 1;
            mRawData[dataOffset + 250] = 3;

            //Requested Address
            mRawData[dataOffset + 251] = 50;
            mRawData[dataOffset + 252] = 4;

            mRawData[dataOffset + 253] = RequestedAddress.address[0];
            mRawData[dataOffset + 254] = RequestedAddress.address[1];
            mRawData[dataOffset + 255] = RequestedAddress.address[2];
            mRawData[dataOffset + 256] = RequestedAddress.address[3];

            mRawData[dataOffset + 257] = 54;
            mRawData[dataOffset + 258] = 4;

            mRawData[dataOffset + 259] = DHCPServerAddress.address[0];
            mRawData[dataOffset + 260] = DHCPServerAddress.address[1];
            mRawData[dataOffset + 261] = DHCPServerAddress.address[2];
            mRawData[dataOffset + 262] = DHCPServerAddress.address[3];

            //Parameters start here
            mRawData[dataOffset + 263] = 0x37;
            mRawData[dataOffset + 264] = 4;

            //Parameters
            mRawData[dataOffset + 265] = 0x01;
            mRawData[dataOffset + 266] = 0x03;
            mRawData[dataOffset + 267] = 0x0f;
            mRawData[dataOffset + 268] = 0x06;

            mRawData[dataOffset + 269] = 0xff; //ENDMARK

            initFields();
        }
Beispiel #14
0
        /// <summary>
        /// Creates a new instance of this class by parsing the given data
        /// </summary>
        /// <param name="bData">The data to parse</param>
        public DHCPFrame(byte[] bData)
        {
            byte[] bAddressBytes = new byte[4];
            byte[] bMacBytes     = new byte[6];
            lTLVs = new List <DHCPTLVItem>();
            DHCPTLVItem dhcpItem;

            dhtMessageType  = (DHCPType)bData[0];
            dhtHardwareType = (HardwareAddressType)bData[1];
            sHardwarelen    = (short)bData[2];
            sHops           = (short)bData[3];
            iTransactionID  = BitConverter.ToInt32(bData, 4);
            iSecs           = BitConverter.ToInt16(bData, 8);
            bValidIPFlag    = (bData[10] & 0x80) == 1;

            for (int iC1 = 0; iC1 < 4; iC1++)
            {
                bAddressBytes[iC1] = bData[12 + iC1];
            }
            ipaClientAddress = new IPAddress(bAddressBytes);

            for (int iC1 = 0; iC1 < 4; iC1++)
            {
                bAddressBytes[iC1] = bData[16 + iC1];
            }
            ipaOwnAddress = new IPAddress(bAddressBytes);

            for (int iC1 = 0; iC1 < 4; iC1++)
            {
                bAddressBytes[iC1] = bData[20 + iC1];
            }
            ipaServerAddress = new IPAddress(bAddressBytes);

            for (int iC1 = 0; iC1 < 4; iC1++)
            {
                bAddressBytes[iC1] = bData[24 + iC1];
            }
            ipaRelayAddress = new IPAddress(bAddressBytes);

            for (int iC1 = 0; iC1 < 6; iC1++)
            {
                bMacBytes[iC1] = bData[28 + iC1];
            }
            macClientMac = new MACAddress(bMacBytes);

            strRequestedServerName = Encoding.ASCII.GetString(bData, 34, 64).Trim(new char[] { '\0' });
            strRequestedFile       = Encoding.ASCII.GetString(bData, 98, 128).Trim(new char[] { '\0' });

            if (bData[236] != 0x63 || bData[237] != 0x82 || bData[238] != 0x53 || bData[239] != 0x63)
            {
                throw new Exception("Invalid DHCP magic number");
            }

            int iC2 = 240;

            while (iC2 < bData.Length)
            {
                if (bData[iC2] == (int)DHCPOptions.End)
                {
                    break;
                }
                dhcpItem = new DHCPTLVItem(bData, iC2);
                lTLVs.Add(dhcpItem);
                iC2 += dhcpItem.Length;
            }
        }
Beispiel #15
0
        // Initialize a new instance of the AMD PCNet device driver
        public AMDPCNet(PCIDevice device)
        {
            if (device == null)
            {
                throw new ArgumentException("PCI Device is null. Unable to get AMD PCNet card");
            }
            pciCard = device;

            // We are handling this device
            pciCard.Claimed = true;

            // Setup interrupt handling
            //Interrupts.IRQ09 += HandleNetworkInterrupt;
            //Interrupts.AddIRQHandler(device.InterruptLine, HandleNetworkInterrupt);

            // Get IO Address from PCI Bus
            io = (Kernel.IOAddressSpace)pciCard.GetAddressSpace(0);
            // Enable the card
            pciCard.EnableDevice();
            // Set the device into 32-bit mode
            io.Write32(0x10, 0);

            // Get the EEPROM MAC Address and set it as the devices MAC
            byte[] eeprom_mac = new byte[6];
            UInt32 result     = io.Read32(0x00);

            eeprom_mac[0] = BinaryHelper.GetByteFrom32bit(result, 0);
            eeprom_mac[1] = BinaryHelper.GetByteFrom32bit(result, 8);
            eeprom_mac[2] = BinaryHelper.GetByteFrom32bit(result, 16);
            eeprom_mac[3] = BinaryHelper.GetByteFrom32bit(result, 24);
            result        = io.Read32(0x04);
            eeprom_mac[4] = BinaryHelper.GetByteFrom32bit(result, 0);
            eeprom_mac[5] = BinaryHelper.GetByteFrom32bit(result, 8);

            mac = new MACAddress(eeprom_mac);

            // Allocate 32 bytes for the 28 byte Initialization block that has to be aligned to a 4 byte boundary
            //UInt32 address = Heap.MemAlloc(0x20);
            //mInitBlock = new ManagedUInt32Array(7); // 7 UInt32's, aligned on a 4byte boundary
            mInitBlock = new ManagedMemorySpace(28, 4);

            /*Console.Write("Allocated 32 bytes for initialization block @ 0x" + address.ToHex(8));
             * Console.WriteLine("(Aligned to 0x" + aligned_address.ToHex(8) + ")");*/

            // Allocate 80 uints for the 16 RX and TX Descriptor rings. These addresses have to be aligned on a 16-byte boundary
            mTxDescriptor = new ManagedMemorySpace(256, 16);
            mRxDescriptor = new ManagedMemorySpace(256, 16);

            /*Console.Write("Allocated 320 bytes for RX ring descriptors @ 0x" + rd_address.ToHex(8));
             * Console.WriteLine("(Aligned to 0x" + mRxDescriptorAddress.ToHex(8) + ")");
             * Console.Write("Allocated 320 bytes for TX ring descriptors @ 0x" + tx_address.ToHex(8));
             * Console.WriteLine("(Aligned to 0x" + mTxDescriptorAddress.ToHex(8) + ")");*/

            // Fill in the Initialization block
            mInitBlock.Write32(0x00, (0x4 << 28) | (0x4 << 20));
            mInitBlock.Write32(0x04, (uint)(eeprom_mac[0] | (eeprom_mac[1] << 8) | (eeprom_mac[2] << 16) | (eeprom_mac[3] << 24)));
            mInitBlock.Write32(0x08, (uint)(eeprom_mac[4] | (eeprom_mac[5] << 8)));
            mInitBlock.Write32(0x0C, 0x0);
            mInitBlock.Write32(0x10, 0x0);
            mInitBlock.Write32(0x14, mRxDescriptor.Offset);
            mInitBlock.Write32(0x18, mTxDescriptor.Offset);

            // Write the Initialization blocks address to the registers on the card
            InitializationBlockAddress = mInitBlock.Offset;
            // Set the device to PCNet-PCI II Controller mode (full 32-bit mode)
            SoftwareStyleRegister = 0x03;

            /* Initialize the RX and TX buffers, and set up the RX and TX descriptors to point
             * to the buffers. Also, mark the RX descriptors as being owned by the card so data
             * can be received in them */
            mRxBuffers = new List <ManagedMemorySpace>();
            mTxBuffers = new List <ManagedMemorySpace>();
            for (uint rxd = 0; rxd < 16; rxd++)
            {
                uint xOffset = rxd * 16;

                ManagedMemorySpace buffer = new ManagedMemorySpace(2048);
                mRxDescriptor.Write32(xOffset + 8, buffer.Offset);
                UInt16 buffer_len = (UInt16)(~buffer.Size);
                buffer_len++;
                UInt32 flags = (UInt32)(buffer_len & 0x0FFF) | 0xF000 | 0x80000000;
                mRxDescriptor.Write32(xOffset + 4, flags);
                mRxBuffers.Add(buffer);
            }
            for (uint txd = 0; txd < 16; txd++)
            {
                uint xOffset = txd * 16;

                ManagedMemorySpace buffer = new ManagedMemorySpace(2048);
                mTxDescriptor.Write32(xOffset + 8, buffer.Offset);
                mTxBuffers.Add(buffer);
            }

            // Set TX Descriptor 0 as the first one to use... Increment this when we use one to use them in a circular fashion
            mNextTXDesc = 0;

            // Setup our Receive and Transmit Queues
            mTransmitBuffer = new Queue <byte[]>();
            mRecvBuffer     = new Queue <byte[]>();
        }
Beispiel #16
0
 /// <summary>
 /// 设置<see cref="MACAddress"/>
 /// </summary>
 /// <param name="index">索引</param>
 /// <param name="value">值</param>
 /// <returns></returns>
 public void SetMAC(Int32 index, MACAddress value)
 {
     this[index, MACAddress.Layout.Length] = value.Bytes;
 }
        /// <summary>
        /// Handles a DHCP frame and sends responses or leases addresses according to its contents
        /// </summary>
        /// <param name="dhcFrame">The DHCP frame to handle</param>
        /// <param name="udpFrame">The UDP frame</param>
        /// <param name="ipFrame">The IP frame</param>
        /// <param name="tdf">The traffic description frame</param>
        /// <param name="fInputFrame">The original root frame</param>
        protected virtual void HandleDHCPFrame(DHCPFrame dhcFrame, eExNetworkLibrary.UDP.UDPFrame udpFrame, eExNetworkLibrary.IP.IPFrame ipFrame, TrafficDescriptionFrame tdf, Frame fInputFrame)
        {
            bool bIsRequest  = false;
            bool bIsDiscover = false;

            foreach (DHCPTLVItem tlvItem in dhcFrame.GetDHCPTLVItems())
            {
                if (tlvItem.DHCPOptionType == DHCPOptions.DHCPMessageType)
                {
                    if (dhcFrame.MessageType == DHCPType.BootRequest && (DHCPMessageType)tlvItem.Data[0] == DHCPMessageType.Discover)
                    {
                        bIsDiscover = true;
                        break;
                    }
                    if (dhcFrame.MessageType == DHCPType.BootRequest && (DHCPMessageType)tlvItem.Data[0] == DHCPMessageType.Request && lOpenServerTransactions.Contains(dhcFrame.TransactionID))
                    {
                        bIsRequest = true;
                        break;
                    }
                }
            }

            if (bIsRequest)
            {
                #region Server Process request

                MACAddress mClientID = dhcFrame.ClientMac;

                if (tdf != null && tdf.SourceInterface != null)
                {
                    if (dictInterfacePool.ContainsKey(tdf.SourceInterface))
                    {
                        IPAddress ipaAddressRequestet = IPAddress.Any;
                        string    strHostname         = "";

                        foreach (DHCPTLVItem tlvItemSearch in dhcFrame.GetDHCPTLVItems())
                        {
                            if (tlvItemSearch.DHCPOptionType == DHCPOptions.AddressRequest)
                            {
                                ipaAddressRequestet = new IPAddress(tlvItemSearch.Data);
                            }
                            if (tlvItemSearch.DHCPOptionType == DHCPOptions.Hostname)
                            {
                                strHostname = ASCIIEncoding.ASCII.GetString(tlvItemSearch.Data);
                            }
                        }

                        DHCPPool     dhPool = dictInterfacePool[tdf.SourceInterface];
                        DHCPPoolItem dhItem = dhPool.GetItemForAddress(ipaAddressRequestet);

                        if (dhItem != null)
                        {
                            IPAddress ipaServer      = tdf.SourceInterface.IpAddresses[0];
                            IPAddress offeredAddress = dhItem.Address;

                            DHCPFrame newDHCPFrame = new DHCPFrame();
                            newDHCPFrame.ClientAddress       = IPAddress.Any;
                            newDHCPFrame.ClientMac           = mClientID;
                            newDHCPFrame.Hardwarelen         = 6;
                            newDHCPFrame.HardwareType        = eExNetworkLibrary.HardwareAddressType.Ethernet;
                            newDHCPFrame.Hops                = 0;
                            newDHCPFrame.MessageType         = DHCPType.BootReply;
                            newDHCPFrame.OfferedAddress      = offeredAddress;
                            newDHCPFrame.RelayAddress        = IPAddress.Any;
                            newDHCPFrame.RequestedFile       = "";
                            newDHCPFrame.RequestedServerName = "";
                            newDHCPFrame.Secs                = dhcFrame.Secs + 1;
                            newDHCPFrame.ServerAddress       = ipaServer;
                            newDHCPFrame.ValidIPFlag         = true;
                            newDHCPFrame.TransactionID       = dhcFrame.TransactionID;

                            DHCPTLVItem tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.DHCPMessageType;
                            tlvItem.Data           = new byte[] { (byte)DHCPMessageType.ACK };

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.ClientID;
                            byte[] bIDData = new byte[7];
                            bIDData[0] = (byte)HardwareAddressType.Ethernet;
                            mClientID.AddressBytes.CopyTo(bIDData, 1);
                            tlvItem.Data = bIDData;

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.SubnetMask;
                            tlvItem.Data           = dhItem.Netmask.MaskBytes;

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.Router;
                            tlvItem.Data           = dhItem.Gateway.GetAddressBytes();

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.DomainNameServer;
                            tlvItem.Data           = dhItem.DNSServer.GetAddressBytes();

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.LeaseTime;
                            tlvItem.Data           = BitConverter.GetBytes(iLeaseDuration);

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.DHCPServerID;
                            tlvItem.Data           = ipaServer.GetAddressBytes();

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            UDP.UDPFrame newUDPFrame = new eExNetworkLibrary.UDP.UDPFrame();
                            newUDPFrame.DestinationPort   = iDHCPInPort;
                            newUDPFrame.SourcePort        = iDHCPOutPort;
                            newUDPFrame.EncapsulatedFrame = newDHCPFrame;

                            IP.IPv4Frame newIPv4Frame = new eExNetworkLibrary.IP.IPv4Frame();
                            newIPv4Frame.Version            = 4;
                            newIPv4Frame.DestinationAddress = IPAddress.Broadcast;
                            newIPv4Frame.SourceAddress      = ipaServer;
                            newIPv4Frame.Protocol           = eExNetworkLibrary.IP.IPProtocol.UDP;
                            newIPv4Frame.EncapsulatedFrame  = newUDPFrame;
                            newIPv4Frame.Identification     = (uint)IncrementIPIDCounter();
                            newIPv4Frame.TimeToLive         = 128;

                            TrafficDescriptionFrame tdFrame = new TrafficDescriptionFrame(null, DateTime.Now);
                            tdFrame.EncapsulatedFrame = newIPv4Frame;

                            tdf.SourceInterface.Send(tdFrame, IPAddress.Broadcast);
                            dhItem.LeasedTo         = mClientID;
                            dhItem.LeasedToHostname = strHostname;
                            dhItem.LeaseDuration    = new TimeSpan(0, 0, 0, iLeaseDuration, 0);
                            lOpenServerTransactions.Remove(newDHCPFrame.TransactionID);
                            InvokeAddressLeased(new DHCPServerEventArgs(dhPool, dhItem, tdf.SourceInterface));
                        }
                    }
                }

                #endregion
            }
            else if (bIsDiscover)
            {
                #region Server Process discover

                MACAddress mClientID = dhcFrame.ClientMac;

                if (tdf != null && tdf.SourceInterface != null)
                {
                    if (dictInterfacePool.ContainsKey(tdf.SourceInterface))
                    {
                        DHCPPool     dhPool = dictInterfacePool[tdf.SourceInterface];
                        DHCPPoolItem dhItem = dhPool.GetNextFreeAddress();

                        if (dhItem != null)
                        {
                            IPAddress ipaServer      = tdf.SourceInterface.IpAddresses[0];
                            IPAddress offeredAddress = dhItem.Address;

                            DHCPFrame newDHCPFrame = new DHCPFrame();
                            newDHCPFrame.ClientAddress       = IPAddress.Any;
                            newDHCPFrame.ClientMac           = mClientID;
                            newDHCPFrame.Hardwarelen         = 6;
                            newDHCPFrame.HardwareType        = eExNetworkLibrary.HardwareAddressType.Ethernet;
                            newDHCPFrame.Hops                = 0;
                            newDHCPFrame.MessageType         = DHCPType.BootReply;
                            newDHCPFrame.OfferedAddress      = offeredAddress;
                            newDHCPFrame.RelayAddress        = IPAddress.Any;
                            newDHCPFrame.RequestedFile       = "";
                            newDHCPFrame.RequestedServerName = "";
                            newDHCPFrame.Secs                = dhcFrame.Secs + 1;
                            newDHCPFrame.ServerAddress       = ipaServer;
                            newDHCPFrame.ValidIPFlag         = true;
                            newDHCPFrame.TransactionID       = dhcFrame.TransactionID;

                            DHCPTLVItem tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.DHCPMessageType;
                            tlvItem.Data           = new byte[] { (byte)DHCPMessageType.Offer };

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.ClientID;
                            byte[] bIDData = new byte[7];
                            bIDData[0] = (byte)HardwareAddressType.Ethernet;
                            mClientID.AddressBytes.CopyTo(bIDData, 1);
                            tlvItem.Data = bIDData;

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.SubnetMask;
                            tlvItem.Data           = dhItem.Netmask.MaskBytes;

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.Router;
                            tlvItem.Data           = dhItem.Gateway.GetAddressBytes();

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.DomainNameServer;
                            tlvItem.Data           = dhItem.DNSServer.GetAddressBytes();

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.LeaseTime;
                            tlvItem.Data           = BitConverter.GetBytes(86400);

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            tlvItem = new DHCPTLVItem();
                            tlvItem.DHCPOptionType = DHCPOptions.DHCPServerID;
                            tlvItem.Data           = ipaServer.GetAddressBytes();

                            newDHCPFrame.AddDHCPTLVItem(tlvItem);

                            UDP.UDPFrame newUDPFrame = new eExNetworkLibrary.UDP.UDPFrame();
                            newUDPFrame.DestinationPort   = iDHCPInPort;
                            newUDPFrame.SourcePort        = iDHCPOutPort;
                            newUDPFrame.EncapsulatedFrame = newDHCPFrame;

                            IP.IPv4Frame newIPv4Frame = new eExNetworkLibrary.IP.IPv4Frame();
                            newIPv4Frame.Version            = 4;
                            newIPv4Frame.DestinationAddress = IPAddress.Broadcast;
                            newIPv4Frame.SourceAddress      = ipaServer;
                            newIPv4Frame.Protocol           = eExNetworkLibrary.IP.IPProtocol.UDP;
                            newIPv4Frame.EncapsulatedFrame  = newUDPFrame;
                            newIPv4Frame.Identification     = (uint)IncrementIPIDCounter();
                            newIPv4Frame.TimeToLive         = 128;

                            TrafficDescriptionFrame tdFrame = new TrafficDescriptionFrame(null, DateTime.Now);
                            tdFrame.EncapsulatedFrame = newIPv4Frame;

                            tdf.SourceInterface.Send(tdFrame, IPAddress.Broadcast);
                            lOpenServerTransactions.Add(newDHCPFrame.TransactionID);
                        }
                    }
                }

                #endregion
            }
        }
Beispiel #18
0
        /// <summary>
        ///		Create a new ARP packet.
        /// </summary>
        /// <param name="data">
        ///		The data representing the ARP packet.
        ///	</param>
        public ArpPacket(byte[] data)
        {
            int position = 0;

            m_hardware = (MediaType)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, position));
            position  += 2;

            m_protocol = (NetworkLayerProtocol)BitConverter.ToInt16(data, position);
            position  += 2;

            int macLength = data[position];

            position++;

            int ipLength = data[position];

            position++;

            m_type    = (ArpOpcode)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, position));
            position += 2;

            // copy out source MAC
            byte[] sourceMac = new byte[macLength];
            Array.Copy(data, position, sourceMac, 0, macLength);
            m_sourceMac = new MACAddress(sourceMac);
            position   += macLength;

            // copy out source IP
            byte[] sourceIP = new byte[ipLength];
            Array.Copy(data, position, sourceIP, 0, ipLength);

            string sIP = string.Empty;

            for (int i = 0; i < ipLength; i++)
            {
                sIP = string.Concat(sIP, sourceIP[i], ".");
            }

            m_sourceIP = IPAddress.Parse(sIP.Substring(0, sIP.Length - 1));
            position  += ipLength;

            // copy out destination MAC
            byte[] destMac = new byte[macLength];
            Array.Copy(data, position, destMac, 0, macLength);
            m_destMac = new MACAddress(destMac);
            position += macLength;

            // copy out destination IP
            byte[] destIP = new byte[ipLength];
            Array.Copy(data, position, destIP, 0, ipLength);

            sIP = string.Empty;

            for (int i = 0; i < ipLength; i++)
            {
                sIP = string.Concat(sIP, destIP[i], ".");
            }

            m_destIP  = IPAddress.Parse(sIP.Substring(0, sIP.Length - 1));
            position += ipLength;
        }
Beispiel #19
0
        public AMDPCNetII(PCIDeviceNormal device)
            : base()
        {
            if (device == null)
            {
                throw new ArgumentException("PCI Device is null. Unable to get AMD PCNet card");
            }

            this.pciCard         = device;
            this.pciCard.Claimed = true;
            this.pciCard.EnableDevice();

            this.io = new AMDPCNetIIIOGroup((ushort)this.pciCard.BaseAddresses[0].BaseAddress());
            this.io.RegisterData.DWord = 0;

            // Get the EEPROM MAC Address and set it as the devices MAC
            byte[] eeprom_mac = new byte[6];
            UInt32 result     = io.MAC1.DWord;

            eeprom_mac[0] = BinaryHelper.GetByteFrom32bit(result, 0);
            eeprom_mac[1] = BinaryHelper.GetByteFrom32bit(result, 8);
            eeprom_mac[2] = BinaryHelper.GetByteFrom32bit(result, 16);
            eeprom_mac[3] = BinaryHelper.GetByteFrom32bit(result, 24);
            result        = io.MAC2.DWord;
            eeprom_mac[4] = BinaryHelper.GetByteFrom32bit(result, 0);
            eeprom_mac[5] = BinaryHelper.GetByteFrom32bit(result, 8);

            mac = new MACAddress(eeprom_mac);

            mInitBlock    = new ManagedMemoryBlock(28, 4);
            mRxDescriptor = new ManagedMemoryBlock(256, 16);
            mTxDescriptor = new ManagedMemoryBlock(256, 16);

            mInitBlock.Write32(0x00, (0x4 << 28) | (0x4 << 20));
            mInitBlock.Write32(0x04, (UInt32)(eeprom_mac[0] | (eeprom_mac[1] << 8) | (eeprom_mac[2] << 16) | (eeprom_mac[3] << 24)));
            mInitBlock.Write32(0x08, (UInt32)(eeprom_mac[4] | (eeprom_mac[5] << 8)));
            mInitBlock.Write32(0x0C, 0x0);
            mInitBlock.Write32(0x10, 0x0);
            mInitBlock.Write32(0x14, mRxDescriptor.Offset);
            mInitBlock.Write32(0x18, mTxDescriptor.Offset);

            InitializationBlockAddress = mInitBlock.Offset;
            SoftwareStyleRegister      = 0x03;

            mRxBuffers = new List <ManagedMemoryBlock>();
            mTxBuffers = new List <ManagedMemoryBlock>();
            for (uint rxd = 0; rxd < 16; rxd++)
            {
                uint xOffset = rxd * 16;

                ManagedMemoryBlock buffer = new ManagedMemoryBlock(2048);
                mRxDescriptor.Write32(xOffset + 8, buffer.Offset);
                UInt16 buffer_len = (UInt16)(~buffer.Size);
                buffer_len++;
                UInt32 flags = (UInt32)(buffer_len & 0x0FFF) | 0xF000 | 0x80000000;
                mRxDescriptor.Write32(xOffset + 4, flags);
                mRxBuffers.Add(buffer);
            }
            for (uint txd = 0; txd < 16; txd++)
            {
                uint xOffset = txd * 16;

                ManagedMemoryBlock buffer = new ManagedMemoryBlock(2048);
                mTxDescriptor.Write32(xOffset + 8, buffer.Offset);
                mTxBuffers.Add(buffer);
            }

            mNextTXDesc = 0;

            // Setup our Receive and Transmit Queues
            mTransmitBuffer = new Queue <byte[]>();
            mRecvBuffer     = new Queue <byte[]>();

            INTs.SetIrqHandler(device.InterruptLine, HandleNetworkInterrupt);
        }
 /// <summary>
 /// Creates a new instance of this class
 /// </summary>
 /// <param name="macAddress">The IP address</param>
 /// <param name="ipAddress">The MAC address associated with the IP address</param>
 /// <param name="bStatic">A bool indicating whether this address entry is static</param>
 public ARPHostEntry(MACAddress macAddress, IPAddress ipAddress, bool bStatic)
     : this(macAddress, ipAddress, bStatic, bStatic ? new DateTime(0) : DateTime.Now.AddMinutes(1))
 {
 }
Beispiel #21
0
        private void OnDeviceInformationFetchCompleted(DeviceInformation theDeviceInformation)
        {
            if (string.IsNullOrEmpty(MACAddress))
            {
                MACAddress = theDeviceInformation.MACAddress;
                if (string.IsNullOrEmpty(Name))
                {
                    Set(theDeviceInformation.Name, theDeviceInformation.Location);
                }
                else
                {
                    Set(Name, theDeviceInformation.Location);
                }
                Firmware     = theDeviceInformation.Firmware;
                ProductModel = theDeviceInformation.ProductModel;
                MACFetch     = MACResult.Matched;
                OnLogWriteEntry(EventLogEntryCodes.MACAddressSet, new string[] { IP, MACAddress });
            }
            else
            {
                if (theDeviceInformation.MACAddress.Replace(":", string.Empty).Equals(MACAddress.Replace(":", string.Empty), StringComparison.OrdinalIgnoreCase))
                {
                    if (string.IsNullOrEmpty(Name))
                    {
                        Set(theDeviceInformation.Name, theDeviceInformation.Location);
                    }
                    else
                    {
                        Set(Name, theDeviceInformation.Location);
                    }
                    Firmware     = theDeviceInformation.Firmware;
                    ProductModel = theDeviceInformation.ProductModel;

                    MACFetch = MACResult.Matched;
                }
                else
                {
                    MACFetch = MACResult.Unmatched;
                    OnLogWriteEntry(EventLogEntryCodes.MACMissMatch, new string[] { IP, MACAddress, theDeviceInformation.MACAddress });
                }
            }
        }
Beispiel #22
0
 /// <summary>
 /// Create new inctanse of the <see cref="ARPReply_Ethernet"/> class.
 /// </summary>
 /// <param name="ourMAC">Source MAC address.</param>
 /// <param name="ourIP">Source IP address.</param>
 /// <param name="targetMAC">Destination MAC address.</param>
 /// <param name="targetIP">Destination IP address.</param>
 /// <exception cref="ArgumentException">Thrown if RawData is invalid or null.</exception>
 internal ARPReply_Ethernet(MACAddress ourMAC, Address ourIP, MACAddress targetMAC, Address targetIP)
     : base(2, ourMAC, ourIP, targetMAC, targetIP, 42, MACAddress.None)
 {
 }
Beispiel #23
0
 /// <summary>
 /// Creates a new instance of this class with the given params
 /// </summary>
 /// <param name="ipaStart">The start IP address of the range to scan</param>
 /// <param name="ipaEnd">The end IP address of the range to scan</param>
 /// <param name="macLocal">The MAC address to spoof in the ARP frame. This should equal the MAC of the output interface.</param>
 /// <param name="ipLocal">The IP address which should be spoofed during scanning</param>
 /// <param name="thOut">The traffic handler to which the generated ARP frames should be forwarded. It is wise to assign an ARP net scanner here</param>
 public ARPScanTask(IPAddress ipaStart, IPAddress ipaEnd, MACAddress macLocal, IPAddress ipLocal, TrafficHandler thOut) : base(ipaStart, ipaEnd, ipLocal, thOut)
 {
     this.macLocal = macLocal;
 }
Beispiel #24
0
        /// <summary>
        /// Create new inctanse of the <see cref="ARPRequest_Ethernet"/> class.
        /// </summary>
        /// <param name="operation">Operation.</param>
        /// <param name="senderMAC">Source MAC address.</param>
        /// <param name="senderIP">Source IP address.</param>
        /// <param name="targetMAC">Destination MAC address.</param>
        /// <param name="targetIP">Destination IP address.</param>
        /// <param name="packet_size">Packet size.</param>
        /// <param name="arpTargetMAC">ARP destination MAC address.</param>
        /// <exception cref="ArgumentException">Thrown if RawData is invalid or null.</exception>
        protected ARPPacket_Ethernet(UInt16 operation, MACAddress senderMAC, Address senderIP,
                                     MACAddress targetMAC, Address targetIP, int packet_size, MACAddress arpTargetMAC)
            : base(targetMAC, senderMAC, 1, 0x0800, 6, 4, operation, packet_size)
        {
            for (int i = 0; i < 6; i++)
            {
                RawData[22 + i] = senderMAC.bytes[i];
                RawData[32 + i] = arpTargetMAC.bytes[i];
            }
            for (int i = 0; i < 4; i++)
            {
                RawData[28 + i] = senderIP.address[i];
                RawData[38 + i] = targetIP.address[i];
            }

            initFields();
        }
Beispiel #25
0
 /// <summary>
 ///   Инициализация фрейма.
 /// </summary>
 /// <param name = "packet">Данные.</param>
 /// <param name = "proto">Тип данных (протокол).</param>
 /// <param name = "source">MAC-адрес источника.</param>
 /// <param name = "destination">MAC-адрес назначения.</param>
 /// <exception cref = "ArgumentNullException"><paramref name = "packet" /> является <c>null</c>.</exception>
 /// <exception cref = "ArgumentNullException"><paramref name = "source" /> является <c>null</c>.</exception>
 /// <exception cref = "ArgumentNullException"><paramref name = "destination" /> является <c>null</c>.</exception>
 public EthernetFrame(IPacket packet, EtherType proto, MACAddress source, MACAddress destination)
     : base(packet, proto, source, destination)
 {
 }
 /// <summary>
 ///   Инициализирует интерфейс MAC-адресом.
 /// </summary>
 /// <param name = "macAddress">MAC-адрес интерфейса.</param>
 protected EthernetInterfaceBase(MACAddress macAddress)
 {
     this._mac = macAddress;
 }
 /// <summary>
 ///   Инициализирует интерфейс MAC-адресом.
 /// </summary>
 /// <param name = "macAddress">MAC-адрес интерфейса.</param>
 public GigabitEthernetInterface(MACAddress macAddress)
     : base(macAddress)
 {
 }
Beispiel #28
0
 protected IPPacket(UInt16 dataLength, byte protocol, Address source, Address dest, byte Flags, MACAddress broadcast)
     : this(MACAddress.None, broadcast, dataLength, protocol, source, dest, Flags)
 {
 }
Beispiel #29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MACAddress"/> class.
 /// </summary>
 /// <param name="mac">The mac.</param>
 public MACAddress(MACAddress mac) : this(mac.address)
 {
 }
 internal BluetoothDevice(Android.Bluetooth.BluetoothDevice device)
 {
     InternalDevice = device;
     MACAddress.TryParse(InternalDevice.Address, out MACAddress address);
     Address = address;
 }
Beispiel #31
0
        public RTL8168(PCIDevice device) : base()
        {
            if (device == null)
            {
                throw new ArgumentException("PCI Device is null. Unable to get Realtek 8168 card");
            }
            pciCard = device;

            // We are handling this device
            pciCard.Claimed = true;
            BaseAddress     = pciCard.BAR0 & (~0xFU);

            Console.WriteLine("Amount of bars: " + device.BaseAddressBar.Length);
            Console.WriteLine("BAR0: " + BaseAddress);

            // Enable the card
            pciCard.EnableDevice();

            SetIrqHandler(device.InterruptLine, HandleNetworkInterrupt);

            //Ports.OutB((ushort)(BaseAddress + 0xE0), 0x08);
            Console.WriteLine("Reset");
            Reset();

            // Get the MAC Address
            byte[] eeprom_mac = new byte[6];
            for (uint b = 0; b < 6; b++)
            {
                eeprom_mac[b] = Ports.InB((ushort)(BaseAddress + b));
            }

            mac = new MACAddress(eeprom_mac);
            Console.WriteLine("MAC: " + mac.ToString());
            Console.WriteLine("Init buffers");
            InitBuffers();

            Ports.OutD((ushort)(BaseAddress + 0x44), 0x0000E70F); // Enable RX

            Ports.OutD((ushort)(BaseAddress + 0x37), 0x04);

            Ports.OutD((ushort)(BaseAddress + 0x40), 0x03000700); // Enable TX

            Ports.OutD((ushort)(BaseAddress + 0xDA), 2048);       // Max rx packet size

            Ports.OutB((ushort)(BaseAddress + 0xEC), 0x3F);       // No early transmit

            Ports.OutD((ushort)(BaseAddress + 0x20), (uint)mTxDescriptor.Offset);
            Console.WriteLine("addresstx desc: " + mTxDescriptor.Offset);

            Ports.OutD((ushort)(BaseAddress + 0xE4), (uint)mRxDescriptor.Offset);
            Console.WriteLine("addressrx desc: " + mRxDescriptor.Offset);

            if (((GetMacVersion() & 0x7cf00000) == 0x54100000) || ((GetMacVersion() & 0x7cf00000) == 0x54000000))
            {
                Console.WriteLine("8168H Detected!");

                Ports.OutD((ushort)(BaseAddress + 0x40), Ports.InD((ushort)(BaseAddress + 0x40)) | (1 << 7)); // AUTO TX FIFO
            }

            Ports.OutW((ushort)(BaseAddress + 0x3C), 0xC3FF); //Activating all Interrupts

            Ports.OutB((ushort)(BaseAddress + 0x37), 0x0C);   // Enabling receive and transmit

            //Console.WriteLine("Netcard version: 0x" + System.Utils.Conversion.DecToHex((int)GetMacVersion() & 0x7cf00000));
            //Console.WriteLine("Netcard version: 0x" + System.Utils.Conversion.DecToHex((int)GetMacVersion() & 0x7c800000));
        }
Beispiel #32
0
        // Initialize a new instance of the AMD PCNet device driver
        public AMDPCNet(PCIDevice device)
        {
            if (device == null)
            {
                throw new ArgumentException("PCI Device is null. Unable to get AMD PCNet card");
            }
            pciCard = device;

            // We are handling this device
            pciCard.Claimed = true;

            // Setup interrupt handling
            //Interrupts.IRQ09 += HandleNetworkInterrupt;
            //Interrupts.AddIRQHandler(device.InterruptLine, HandleNetworkInterrupt);

            // Get IO Address from PCI Bus
            io = (Kernel.IOAddressSpace)pciCard.GetAddressSpace(0);
            // Enable the card
            pciCard.EnableDevice();
            // Set the device into 32-bit mode
            io.Write32(0x10, 0);

            // Get the EEPROM MAC Address and set it as the devices MAC
            byte[] eeprom_mac = new byte[6];
            UInt32 result = io.Read32(0x00);
            eeprom_mac[0] = BinaryHelper.GetByteFrom32bit(result, 0);
            eeprom_mac[1] = BinaryHelper.GetByteFrom32bit(result, 8);
            eeprom_mac[2] = BinaryHelper.GetByteFrom32bit(result, 16);
            eeprom_mac[3] = BinaryHelper.GetByteFrom32bit(result, 24);
            result = io.Read32(0x04);
            eeprom_mac[4] = BinaryHelper.GetByteFrom32bit(result, 0);
            eeprom_mac[5] = BinaryHelper.GetByteFrom32bit(result, 8);

            mac = new MACAddress(eeprom_mac);

            // Allocate 32 bytes for the 28 byte Initialization block that has to be aligned to a 4 byte boundary
            //UInt32 address = Heap.MemAlloc(0x20);
            //mInitBlock = new ManagedUInt32Array(7); // 7 UInt32's, aligned on a 4byte boundary
            mInitBlock = new ManagedMemorySpace(28, 4);
            /*Console.Write("Allocated 32 bytes for initialization block @ 0x" + address.ToHex(8));
            Console.WriteLine("(Aligned to 0x" + aligned_address.ToHex(8) + ")");*/

            // Allocate 80 uints for the 16 RX and TX Descriptor rings. These addresses have to be aligned on a 16-byte boundary
            mTxDescriptor = new ManagedMemorySpace(256, 16);
            mRxDescriptor = new ManagedMemorySpace(256, 16);
            /*Console.Write("Allocated 320 bytes for RX ring descriptors @ 0x" + rd_address.ToHex(8));
            Console.WriteLine("(Aligned to 0x" + mRxDescriptorAddress.ToHex(8) + ")");
            Console.Write("Allocated 320 bytes for TX ring descriptors @ 0x" + tx_address.ToHex(8));
            Console.WriteLine("(Aligned to 0x" + mTxDescriptorAddress.ToHex(8) + ")");*/

            // Fill in the Initialization block
            mInitBlock.Write32(0x00, (0x4 << 28) | (0x4 << 20));
            mInitBlock.Write32(0x04, (uint)(eeprom_mac[0] | (eeprom_mac[1] << 8) | (eeprom_mac[2] << 16) | (eeprom_mac[3] << 24)));
            mInitBlock.Write32(0x08, (uint)(eeprom_mac[4] | (eeprom_mac[5] << 8)));
            mInitBlock.Write32(0x0C, 0x0);
            mInitBlock.Write32(0x10, 0x0);
            mInitBlock.Write32(0x14, mRxDescriptor.Offset);
            mInitBlock.Write32(0x18, mTxDescriptor.Offset);

            // Write the Initialization blocks address to the registers on the card
            InitializationBlockAddress = mInitBlock.Offset;
            // Set the device to PCNet-PCI II Controller mode (full 32-bit mode)
            SoftwareStyleRegister = 0x03;

            /* Initialize the RX and TX buffers, and set up the RX and TX descriptors to point
               to the buffers. Also, mark the RX descriptors as being owned by the card so data 
               can be received in them */
            mRxBuffers = new List<ManagedMemorySpace>();
            mTxBuffers = new List<ManagedMemorySpace>();
            for (uint rxd = 0; rxd < 16; rxd++)
            {
                uint xOffset = rxd * 16;

                ManagedMemorySpace buffer = new ManagedMemorySpace(2048);
                mRxDescriptor.Write32(xOffset + 8, buffer.Offset);
                UInt16 buffer_len = (UInt16)(~buffer.Size);
                buffer_len++;
                UInt32 flags = (UInt32)(buffer_len & 0x0FFF) | 0xF000 | 0x80000000;
                mRxDescriptor.Write32(xOffset + 4, flags);
                mRxBuffers.Add(buffer);
            }
            for (uint txd = 0; txd < 16; txd++)
            {
                uint xOffset = txd * 16;

                ManagedMemorySpace buffer = new ManagedMemorySpace(2048);
                mTxDescriptor.Write32(xOffset + 8, buffer.Offset);
                mTxBuffers.Add(buffer);
            }

            // Set TX Descriptor 0 as the first one to use... Increment this when we use one to use them in a circular fashion
            mNextTXDesc = 0;

            // Setup our Receive and Transmit Queues
            mTransmitBuffer = new Queue<byte[]>();
            mRecvBuffer = new Queue<byte[]>();
        }
Beispiel #33
0
        /// <summary>
        /// Create new instance of the <see cref="DHCPPacket"/> class.
        /// </summary>
        /// <param name="client">Client IPv4 Address.</param>
        /// <param name="server">Server IPv4 Address.</param>
        /// <param name="mac_src">Source MAC Address.</param>
        /// <param name="dhcpDataSize">DHCP Data size</param>
        /// <exception cref="OverflowException">Thrown if data array length is greater than Int32.MaxValue.</exception>
        /// <exception cref="ArgumentException">Thrown if RawData is invalid or null.</exception>
        internal DHCPPacket(Address client, Address server, MACAddress mac_src, ushort dhcpDataSize)
            : base(client, server, 68, 67, (ushort)(dhcpDataSize + 240), MACAddress.Broadcast)
        {
            //Request
            RawData[42] = 0x01;

            //ethernet
            RawData[43] = 0x01;

            //Length mac
            RawData[44] = 0x06;

            //hops
            RawData[45] = 0x00;

            Random rnd = new Random();

            xID         = rnd.Next(0, Int32.MaxValue);
            RawData[46] = (byte)((xID >> 24) & 0xFF);
            RawData[47] = (byte)((xID >> 16) & 0xFF);
            RawData[48] = (byte)((xID >> 8) & 0xFF);
            RawData[49] = (byte)((xID >> 0) & 0xFF);

            //second elapsed
            RawData[50] = 0x00;
            RawData[51] = 0x00;

            //option bootp
            RawData[52] = 0x00;
            RawData[53] = 0x00;

            //client ip address
            RawData[54] = client.address[0];
            RawData[55] = client.address[1];
            RawData[56] = client.address[2];
            RawData[57] = client.address[3];

            for (int i = 0; i < 13; i++)
            {
                RawData[58 + i] = 0x00;
            }

            //Src mac
            RawData[70] = mac_src.bytes[0];
            RawData[71] = mac_src.bytes[1];
            RawData[72] = mac_src.bytes[2];
            RawData[73] = mac_src.bytes[3];
            RawData[74] = mac_src.bytes[4];
            RawData[75] = mac_src.bytes[5];

            //Fill 0
            for (int i = 0; i < 202; i++)
            {
                RawData[76 + i] = 0x00;
            }

            //DHCP Magic cookie
            RawData[278] = 0x63;
            RawData[279] = 0x82;
            RawData[280] = 0x53;
            RawData[281] = 0x63;

            InitFields();
        }
Beispiel #34
0
        public VT6102(PCIDevice device)
        {
            if (device == null)
            {
                throw new ArgumentException("PCI Device is null. Unable to get VIA Rhine-II card");
            }
            pciCard = device;

            // We are handling this device
            pciCard.Claimed = true;

            // Setup interrupt handling
            //Interrupts.IRQ10 += HandleNetworkInterrupt;
            //Interrupts.AddIRQHandler(device.InterruptLine, HandleNetworkInterrupt);

            // Get IO Address from PCI Bus
            io = pciCard.GetAddressSpace(0) as Kernel.IOAddressSpace;

            // Enable the card
            pciCard.EnableDevice();

            // Get the EEPROM MAC Address and set it as the devices MAC
            byte[] eeprom_mac = new byte[6];
            UInt32 result = io.Read32(0x00);
            eeprom_mac[0] = BinaryHelper.GetByteFrom32bit(result, 0);
            eeprom_mac[1] = BinaryHelper.GetByteFrom32bit(result, 8);
            eeprom_mac[2] = BinaryHelper.GetByteFrom32bit(result, 16);
            eeprom_mac[3] = BinaryHelper.GetByteFrom32bit(result, 24);
            result = io.Read32(0x04);
            eeprom_mac[4] = BinaryHelper.GetByteFrom32bit(result, 0);
            eeprom_mac[5] = BinaryHelper.GetByteFrom32bit(result, 8);

            mac = new MACAddress(eeprom_mac);

            // Software Reset device
            SoftwareReset();

            // Configure Receive Config
            ReceiveConfigRegister = 0x1C;
            // Configure Transmit Config
            TransmitConfigRegister = 0x04;

            // Setup RX Descriptors
            mRxDescriptors = new ManagedMemorySpace(256, 16);

            // Setup TX Descriptors
            mTxDescriptors = new ManagedMemorySpace(256, 16);

            /* Initialize the RX and TX buffers, and set up the RX  descriptors to point
               to the buffers. Also, mark the RX descriptors as being owned by the card so data 
               can be received in them */
            mRxBuffers = new List<ManagedMemorySpace>();
            for (uint rxd = 0; rxd < 16; rxd++)
            {
                uint xOffset = rxd * 16;

                ManagedMemorySpace buffer = new ManagedMemorySpace(2048);
                mRxDescriptors.Write32(xOffset + 12, mRxDescriptors.Offset + xOffset + 16);
                mRxDescriptors.Write32(xOffset + 8, buffer.Offset);
                mRxDescriptors.Write32(xOffset + 4, buffer.Size);
                mRxDescriptors.Write32(xOffset, 0x80000000);
                mRxBuffers.Add(buffer);
            }
            mRxDescriptors.Write32(252, mRxDescriptors.Offset);
            for (uint txd = 0; txd < 16; txd++)
            {
                uint xOffset = txd * 16;

                mTxDescriptors.Write32(xOffset + 12, mTxDescriptors.Offset + xOffset + 16);
                mTxDescriptors.Write32(xOffset + 8, 0);
                mTxDescriptors.Write32(xOffset + 4, 0);
                mTxDescriptors.Write32(xOffset, 0);
            }
            mTxDescriptors.Write32(252, mTxDescriptors.Offset);

            mNextTXDesc = 0;

            RxDescAddressRegister = mRxDescriptors.Offset;
            TxDescAddressRegister = mTxDescriptors.Offset;

            // Setup and clear interrupts
            IntMaskRegister = 0xFFFF;
            IntStatusRegister = 0xFFFF;

            // Setup our Receive and Transmit Queues
            mTransmitBuffer = new Queue<byte[]>();
            mRecvBuffer = new Queue<byte[]>();
        }
Beispiel #35
0
 /// <summary>
 /// Create new instance of the <see cref="DHCPPacket"/> class.
 /// </summary>
 /// <param name="mac_src">Source MAC Address.</param>
 /// <param name="dhcpDataSize">DHCP Data size</param>
 internal DHCPPacket(MACAddress mac_src, ushort dhcpDataSize)
     : this(Address.Zero, Address.Broadcast, mac_src, dhcpDataSize)
 {
 }
 /// <summary>
 ///   Инициализирует интерфейс MAC-адресом.
 /// </summary>
 /// <param name = "macAddress">MAC-адрес интерфейса.</param>
 public FastEthernetInterface(MACAddress macAddress)
     : base(macAddress)
 {
 }
Beispiel #37
0
 protected virtual void initFields()
 {
     destMAC    = new MACAddress(mRawData, 0);
     srcMAC     = new MACAddress(mRawData, 6);
     aEtherType = (UInt16)((mRawData[12] << 8) | mRawData[13]);
 }
 public void Init()
 {
     this.data = new Array <byte> (new byte [] { 11, 65, 23, 198, 0, 37 });
     this.address = new MACAddress (this.data);
 }
 /// <summary>
 /// Creates a new instance of this class
 /// </summary>
 /// <param name="macAddress">The IP address</param>
 /// <param name="ipAddress">The MAC address associated with the IP address</param>
 public ARPHostEntry(MACAddress macAddress, IPAddress ipAddress) : this(macAddress, ipAddress, false)
 {
 }
        /// <summary>
        ///   Инициализация фрейма.
        /// </summary>
        /// <param name = "length">Длина данных.</param>
        /// <param name = "proto">Тип данных (протокол).</param>
        /// <param name = "source">MAC-адрес источника.</param>
        /// <param name = "destination">MAC-адрес назначения.</param>
        /// <exception cref = "ArgumentNullException"><paramref name = "source" /> является <c>null</c>.</exception>
        /// <exception cref = "ArgumentNullException"><paramref name = "destination" /> является <c>null</c>.</exception>
        protected EthernetFrameBase(int length, EtherType proto, MACAddress source, MACAddress destination)
        {
            if (source == null)
                throw new ArgumentNullException ("source");

            if (destination == null)
                throw new ArgumentNullException ("destination");

            this.Data = new Array <byte> (new byte[14 + length]);

            this.Destination = destination;
            this.Source = source;
            this.SetType (proto);
        }