Ejemplo n.º 1
1
        /// <summary>
        /// Generate LLDP packet for adapter
        /// </summary>
        /// <param name="adapter"></param>
        private Packet CreateLLDPPacket(NetworkInterface adapter, PacketInfo pinfo)
        {
            Debug.IndentLevel = 2;

            PhysicalAddress MACAddress = adapter.GetPhysicalAddress();

            IPInterfaceProperties ipProperties = adapter.GetIPProperties();
            IPv4InterfaceProperties ipv4Properties = null; // Ipv4
            IPv6InterfaceProperties ipv6Properties = null;// Ipv6

            // IPv6
            if (adapter.Supports(NetworkInterfaceComponent.IPv6))
            {
                try
                {
                    ipv6Properties = ipProperties.GetIPv6Properties();
                }
                catch (NetworkInformationException e)
                {
                    // Adapter doesn't probably have IPv6 enabled
                    Debug.WriteLine(e.Message, EventLogEntryType.Warning);
                }
            }

            // IPv4
            if (adapter.Supports(NetworkInterfaceComponent.IPv4))
            {
                try
                {
                    ipv4Properties = ipProperties.GetIPv4Properties();
                }
                catch (NetworkInformationException e)
                {
                    // Adapter doesn't probably have IPv4 enabled
                    Debug.WriteLine(e.Message, EventLogEntryType.Warning);
                }
            }

            // System description
            Dictionary<string, string> systemDescription = new Dictionary<string, string>();
            systemDescription.Add("OS", pinfo.OperatingSystem);
            systemDescription.Add("Ver", pinfo.OperatingSystemVersion);
            systemDescription.Add("User", pinfo.Username);
            systemDescription.Add("Uptime", pinfo.Uptime);

            // Port description
            Dictionary<string, string> portDescription = new Dictionary<string, string>();

            // adapter.Description is for example "Intel(R) 82579V Gigabit Network Connection"
            // Registry: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\<number>\Description value
            portDescription.Add("Vendor", adapter.Description);

            /*
             adapter.Id is GUID and can be found in several places:
             In this example it is "{87423023-7191-4C03-A049-B8E7DBB36DA4}"

             Registry: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
             - \NetworkCards\<number>\ServiceName value (in same tree as adapter.Description!)
             - \NetworkList\Nla\Cache\Intranet\<adapter.Id> key
             - \NetworkList\Nla\Cache\Intranet\<domain>\<adapter.Id> key

             Registry: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion
             - \NetworkCards\<number>\ServiceName value

             Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0007
               {4D36E972-E325-11CE-BFC1-08002BE10318} == Network and Sharing Center -> viewing adapter's "Properties" and selecting "Device class GUID" from dropdown menu
               {4D36E972-E325-11CE-BFC1-08002BE10318}\0007 == Network and Sharing Center -> viewing adapter's "Properties" and selecting "Driver key" from dropdown menu
             - \NetCfgInstanceId value
             - \Linkage\Export value (part of)
             - \Linkage\FilterList value (part of)
             - \Linkage\RootDevice value

             Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\DeviceClasses\{ad498944-762f-11d0-8dcb-00c04fc3358c}\##?#PCI#VEN_8086&DEV_1503&SUBSYS_849C1043&REV_06#3&11583659&0&C8#{ad498944-762f-11d0-8dcb-00c04fc3358c}\#{87423023-7191-4C03-A049-B8E7DBB36DA4}\SymbolicLink value (part of)
             Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{ 4D36E972 - E325 - 11CE - BFC1 - 08002BE10318}\{ 87423023 - 7191 - 4C03 - A049 - B8E7DBB36DA4}
             Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{ACB3F7A0-2E45-4435-854A-A4E120477E1D}\Connection\Name value (part of)
             Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\{ 87423023 - 7191 - 4C03 - A049 - B8E7DBB36DA4}
             Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\iphlpsvc\Parameters\Isatap\{ ACB3F7A0 - 2E45 - 4435 - 854A - A4E120477E1D}\InterfaceName value (part of)

             Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\<various names>\Linkage
             - \Bind value (part of)
             - \Export value (part of)
             - \Route value (part of)

            Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\NetBT\Parameters\Interfaces\Tcpip_{87423023-7191-4C03-A049-B8E7DBB36DA4}
            Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Psched\Parameters\NdisAdapters\{87423023-7191-4C03-A049-B8E7DBB36DA4}
            Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\RemoteAccess\Interfaces\<number>\InterfaceName value
            Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Adapters\{87423023-7191-4C03-A049-B8E7DBB36DA4}\IpConfig value (part of)

            IPv4 information:
            Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\{87423023-7191-4C03-A049-B8E7DBB36DA4}

            IPv6 information:
            Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\TCPIP6\Parameters\Interfaces\{87423023-7191-4c03-a049-b8e7dbb36da4}

            Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\WfpLwf\Parameters\NdisAdapters\{87423023-7191-4C03-A049-B8E7DBB36DA4}

            */
            portDescription.Add("ID", adapter.Id);

            // Gateway
            if (ipProperties.GatewayAddresses.Count > 0)
            {
                portDescription.Add("GW", String.Join(", ", ipProperties.GatewayAddresses.Select(i => i.Address.ToString()).ToArray()));
            }
            else
            {
                portDescription.Add("GW", "-");
            }

            // CIDR
            if (ipProperties.UnicastAddresses.Count > 0)
            {
                int[] mask = ipProperties.UnicastAddresses
                    .Where(
                      w => w.IPv4Mask.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork
                    )
                    .Select(x => getCIDRFromIPMaskAddress(x.IPv4Mask))
                    .ToArray()
                    ;

                portDescription.Add("CIDR", String.Join(", ", mask));
            }
            else
            {
                portDescription.Add("CIDR", "-");
            }

            // DNS server(s)
            if (ipProperties.DnsAddresses.Count > 0)
            {
                portDescription.Add("DNS", String.Join(", ", ipProperties.DnsAddresses.Select(i => i.ToString()).ToArray()));
            }
            else
            {
                portDescription.Add("DNS", "-");
            }

            // DHCP server
            if (ipProperties.DhcpServerAddresses.Count > 0)
            {
                portDescription.Add("DHCP", String.Join(", ", ipProperties.DhcpServerAddresses.Select(i => i.ToString()).ToArray()));
            }
            else
            {
                portDescription.Add("DHCP", "-");
            }

            // WINS server(s)
            if (ipProperties.WinsServersAddresses.Count > 0)
            {
                portDescription.Add("WINS", String.Join(", ", ipProperties.WinsServersAddresses.Select(i => i.ToString()).ToArray()));
            }

            // Link speed
            portDescription.Add("Speed", ReadableSize(adapter.Speed) + "ps");

            // Capabilities enabled
            List<CapabilityOptions> capabilitiesEnabled = new List<CapabilityOptions>();
            capabilitiesEnabled.Add(CapabilityOptions.StationOnly);

            if (ipv4Properties.IsForwardingEnabled)
            {
                capabilitiesEnabled.Add(CapabilityOptions.Router);
            }

            ushort expectedSystemCapabilitiesCapability = GetCapabilityOptionsBits(GetCapabilityOptions());
            ushort expectedSystemCapabilitiesEnabled = GetCapabilityOptionsBits(capabilitiesEnabled);

            // Constuct LLDP packet
            LLDPPacket lldpPacket = new LLDPPacket();
            lldpPacket.TlvCollection.Add(new ChassisID(ChassisSubTypes.MACAddress, MACAddress));
            lldpPacket.TlvCollection.Add(new PortID(PortSubTypes.LocallyAssigned, System.Text.Encoding.UTF8.GetBytes(adapter.Name)));
            lldpPacket.TlvCollection.Add(new TimeToLive(120));
            lldpPacket.TlvCollection.Add(new PortDescription(CreateTlvString(portDescription)));
            lldpPacket.TlvCollection.Add(new SystemName(Environment.MachineName));
            lldpPacket.TlvCollection.Add(new SystemDescription(CreateTlvString(systemDescription)));
            lldpPacket.TlvCollection.Add(new SystemCapabilities(expectedSystemCapabilitiesCapability, expectedSystemCapabilitiesEnabled));

            // Management
            var managementAddressObjectIdentifier = "Management";

            // Add management IPv4 address(es)
            if (null != ipv4Properties)
            {
                foreach (UnicastIPAddressInformation ip in ipProperties.UnicastAddresses)
                {
                    if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        lldpPacket.TlvCollection.Add(new ManagementAddress(new NetworkAddress(ip.Address), InterfaceNumbering.SystemPortNumber, Convert.ToUInt32(ipv4Properties.Index), managementAddressObjectIdentifier));
                    }
                }
            }

            // Add management IPv6 address(es)
            if (null != ipv6Properties)
            {
                foreach (UnicastIPAddressInformation ip in ipProperties.UnicastAddresses)
                {
                    if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                    {
                        lldpPacket.TlvCollection.Add(new ManagementAddress(new NetworkAddress(ip.Address), InterfaceNumbering.SystemPortNumber, Convert.ToUInt32(ipv6Properties.Index), managementAddressObjectIdentifier));
                    }
                }
            }

            // == Organization specific TLVs

            // Ethernet
            lldpPacket.TlvCollection.Add(new OrganizationSpecific(new byte[] { 0x0, 0x12, 0x0f }, 5, new byte[] { 0x5 }));

            var expectedOrganizationUniqueIdentifier = new byte[3] { 0, 0, 0 };
            var expectedOrganizationSpecificBytes = new byte[] { 0, 0, 0, 0 };

            //int orgSubType = 0;

            // IPv4 Information:
            //if (null != ipv4Properties)
            //{
            //    lldpPacket.TlvCollection.Add((new StringTLV(TLVTypes.OrganizationSpecific, String.Format("IPv4 DHCP: {0}", ipv4Properties.IsDhcpEnabled.ToString()))));
            //lldpPacket.TlvCollection.Add(new OrganizationSpecific(expectedOrganizationSpecificBytes, new StringTLV(), System.Text.Encoding.UTF8.GetBytes(String.Format("IPv4 DHCP: {0}", ipv4Properties.IsDhcpEnabled.ToString()))));
            //}

            // End of LLDP packet
            lldpPacket.TlvCollection.Add(new EndOfLLDPDU());

            if (0 == lldpPacket.TlvCollection.Count)
            {
                throw new Exception("Couldn't construct LLDP TLVs.");
            }

            if (lldpPacket.TlvCollection.Last().GetType() != typeof(EndOfLLDPDU))
            {
                throw new Exception("Last TLV must be type of 'EndOfLLDPDU'!");
            }

            foreach (TLV tlv in lldpPacket.TlvCollection)
            {
                Debug.WriteLine(tlv.ToString(), EventLogEntryType.Information);
            }

            // Generate packet
            Packet packet = new EthernetPacket(MACAddress, destinationHW, EthernetPacketType.LLDP);
            packet.PayloadData = lldpPacket.Bytes;

            return packet;
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Create a randomized LLDP packet with some basic TLVs
        /// </summary>
        /// <returns>
        ///     A <see cref="Packet" />
        /// </returns>
        public static LLDPPacket RandomPacket()
        {
            var rnd = new Random();

            var lldpPacket = new LLDPPacket();

            var physicalAddressBytes = new byte[EthernetFields.MacAddressLength];

            rnd.NextBytes(physicalAddressBytes);
            var physicalAddress = new PhysicalAddress(physicalAddressBytes);

            lldpPacket.TlvCollection.Add(new ChassisID(physicalAddress));

            var networkAddress = new byte[IPv4Fields.AddressLength];

            rnd.NextBytes(networkAddress);
            lldpPacket.TlvCollection.Add(new PortID(new NetworkAddress(new IPAddress(networkAddress))));

            var seconds = (ushort)rnd.Next(0, 120);

            lldpPacket.TlvCollection.Add(new TimeToLive(seconds));

            lldpPacket.TlvCollection.Add(new EndOfLLDPDU());

            return(lldpPacket);
        }
Ejemplo n.º 3
0
        public static LLDPPacket RandomPacket()
        {
            Random     random = new Random();
            LLDPPacket packet = new LLDPPacket();

            byte[] buffer = new byte[EthernetFields.MacAddressLength];
            random.NextBytes(buffer);
            PhysicalAddress mACAddress = new PhysicalAddress(buffer);

            packet.TlvCollection.Add(new ChassisID(mACAddress));
            byte[] buffer2 = new byte[IPv4Fields.AddressLength];
            random.NextBytes(buffer2);
            packet.TlvCollection.Add(new PortID(new NetworkAddress(new IPAddress(buffer2))));
            ushort seconds = (ushort)random.Next(0, 120);

            packet.TlvCollection.Add(new TimeToLive(seconds));
            packet.TlvCollection.Add(new EndOfLLDPDU());
            return(packet);
        }
        public void ConstructFromValues()
        {
            var expectedChassisIDType = ChassisSubTypes.NetworkAddress;
            var expectedChassisIDNetworkAddress = new NetworkAddress(new IPAddress(new byte[4] { 0x0A, 0x00, 0x01, 0x01 }));
            var expectedPortIDBytes = new byte[15] { 0x30, 0x30, 0x31, 0x42, 0x35, 0x34, 0x39, 0x34, 0x35, 0x41, 0x38, 0x42, 0x3a, 0x50, 0x32 };
            ushort expectedTimeToLive = 120;
            string expectedPortDescription = "Port Description";
            string expectedSystemName = "SystemName";
            string expectedSystemDescription = "System Description";
            ushort expectedSystemCapabilitiesCapability = 18;
            ushort expectedSystemCapabilitiesEnabled = 16;
            var managementAddressNetworkAddress = new NetworkAddress(new IPAddress(new byte[4] { 0x0A, 0x00, 0x01, 0x01 }));
            var managementAddressObjectIdentifier = "Object Identifier";
            uint managementAddressInterfaceNumber = 0x44060124;

            var expectedOrganizationUniqueIdentifier = new byte[3] { 0x24, 0x10, 0x12 };
            var expectedOrganizationSubType = 2;
            var expectedOrganizationSpecificBytes = new byte[4] { 0xBA, 0xAD, 0xF0, 0x0D };

            var valuesLLDPPacket = new LLDPPacket();
            Console.WriteLine("valuesLLDPPacket.ToString() {0}", valuesLLDPPacket.ToString());
            valuesLLDPPacket.TlvCollection.Add(new ChassisID(expectedChassisIDType, expectedChassisIDNetworkAddress));
            Console.WriteLine("valuesLLDPPacket.ToString() {0}", valuesLLDPPacket.ToString());
            //valuesLLDPPacket.TlvCollection.Add(new PortID(lldpPacket, PortSubTypes.MACAddress, new PhysicalAddress(new byte[6] { 0x00, 0x1C, 0x23, 0xAF, 0x08, 0xF3 })));
            valuesLLDPPacket.TlvCollection.Add(new PortID(PortSubTypes.LocallyAssigned, expectedPortIDBytes));
            valuesLLDPPacket.TlvCollection.Add(new TimeToLive(expectedTimeToLive));
            valuesLLDPPacket.TlvCollection.Add(new PortDescription(expectedPortDescription));
            valuesLLDPPacket.TlvCollection.Add(new SystemName(expectedSystemName));
            valuesLLDPPacket.TlvCollection.Add(new SystemDescription(expectedSystemDescription));
            valuesLLDPPacket.TlvCollection.Add(new SystemCapabilities(expectedSystemCapabilitiesCapability, expectedSystemCapabilitiesEnabled));
            valuesLLDPPacket.TlvCollection.Add(new ManagementAddress(managementAddressNetworkAddress,
                                                                     InterfaceNumbering.SystemPortNumber,
                                                                     managementAddressInterfaceNumber,
                                                                     managementAddressObjectIdentifier));
            valuesLLDPPacket.TlvCollection.Add(new OrganizationSpecific(expectedOrganizationUniqueIdentifier,
                                                                        expectedOrganizationSubType,
                                                                        expectedOrganizationSpecificBytes));
            valuesLLDPPacket.TlvCollection.Add(new EndOfLLDPDU());

            var lldpBytes = valuesLLDPPacket.Bytes;

            Console.WriteLine("valuesLLDPPacket.ToString() {0}", valuesLLDPPacket.ToString());

            // reparse these bytes back into a lldp packet
            var lldpPacket = new LLDPPacket(new ByteArraySegment(lldpBytes));

            Console.WriteLine("lldpPacket.ToString() {0}", lldpPacket.ToString());

            int expectedTlvCount = 10;
            Assert.AreEqual(expectedTlvCount, lldpPacket.TlvCollection.Count);

            int count = 1;
            foreach (TLV tlv in lldpPacket.TlvCollection)
            {
                Console.WriteLine("Type: " + tlv.GetType().ToString());
                switch(count)
                {
                    case 1:
                        Assert.AreEqual(typeof(ChassisID), tlv.GetType());
                        var chassisID = (ChassisID)tlv;
                        Assert.AreEqual(ChassisSubTypes.NetworkAddress, chassisID.SubType);
                        Assert.AreEqual(typeof(NetworkAddress), chassisID.SubTypeValue.GetType());
                    Console.WriteLine(expectedChassisIDNetworkAddress.ToString());
                    Console.WriteLine(chassisID.SubTypeValue.ToString());
                        Assert.AreEqual(expectedChassisIDNetworkAddress, chassisID.SubTypeValue);
                        break;
                    case 2:
                        Assert.AreEqual(typeof(PortID), tlv.GetType());
                        var portID = (PortID)tlv;
                        Assert.AreEqual(PortSubTypes.LocallyAssigned, portID.SubType);
                        Assert.AreEqual(expectedPortIDBytes, portID.SubTypeValue);
                        //Assert.AreEqual(PortSubTypes.MACAddress, portID.SubType);
                        //var macAddress = new PhysicalAddress(new byte[6] { 0x00, 0x1C, 0x23, 0xAF, 0x08, 0xF3 });
                        //Assert.AreEqual(macAddress, portID.SubTypeValue);
                        break;
                    case 3:
                        Assert.AreEqual(typeof(TimeToLive), tlv.GetType());
                        Assert.AreEqual(expectedTimeToLive, ((TimeToLive)tlv).Seconds);
                        break;
                    case 4:
                        Assert.AreEqual(typeof(PortDescription), tlv.GetType());
                        Assert.AreEqual(expectedPortDescription, ((PortDescription)tlv).Description);
                        break;
                    case 5:
                        Assert.AreEqual(typeof(SystemName), tlv.GetType());
                        Assert.AreEqual(expectedSystemName, ((SystemName)tlv).Name);
                        break;
                    case 6:
                        Assert.AreEqual(typeof(SystemDescription), tlv.GetType());
                        Assert.AreEqual(expectedSystemDescription, ((SystemDescription)tlv).Description);
                        break;
                    case 7:
                        Assert.AreEqual(typeof(SystemCapabilities), tlv.GetType());
                        var capabilities = (SystemCapabilities)tlv;
                        Assert.IsTrue(capabilities.IsCapable(CapabilityOptions.Repeater));
                        Assert.IsTrue(capabilities.IsEnabled(CapabilityOptions.Router));
                        Assert.IsFalse(capabilities.IsCapable(CapabilityOptions.Bridge));
                        Assert.IsFalse(capabilities.IsCapable(CapabilityOptions.DocsisCableDevice));
                        Assert.IsFalse(capabilities.IsCapable(CapabilityOptions.Other));
                        Assert.IsFalse(capabilities.IsCapable(CapabilityOptions.StationOnly));
                        Assert.IsFalse(capabilities.IsCapable(CapabilityOptions.Telephone));
                        Assert.IsFalse(capabilities.IsCapable(CapabilityOptions.WLanAP));
                        break;
                    case 8:
                        Assert.AreEqual(typeof(ManagementAddress), tlv.GetType());
                        var mgmtAdd = (ManagementAddress)tlv;
                        Assert.AreEqual(AddressFamily.IPv4, mgmtAdd.AddressSubType);
                        Assert.AreEqual(managementAddressNetworkAddress, mgmtAdd.MgmtAddress);
                        Assert.AreEqual(InterfaceNumbering.SystemPortNumber, mgmtAdd.InterfaceSubType);
                        Assert.AreEqual(managementAddressInterfaceNumber, mgmtAdd.InterfaceNumber);
                        int expectedObjIdLength = managementAddressObjectIdentifier.Length;
                        Assert.AreEqual(expectedObjIdLength, mgmtAdd.ObjIdLength);
                        Assert.AreEqual(managementAddressObjectIdentifier, mgmtAdd.ObjectIdentifier);
                        break;
                    case 9:
                        Assert.AreEqual(typeof(OrganizationSpecific), tlv.GetType());
                        var orgSpecifig = (OrganizationSpecific)tlv;
                        Assert.AreEqual(expectedOrganizationUniqueIdentifier, orgSpecifig.OrganizationUniqueID);
                        Assert.AreEqual(expectedOrganizationSubType, orgSpecifig.OrganizationDefinedSubType);
                        Assert.AreEqual(expectedOrganizationSpecificBytes, orgSpecifig.OrganizationDefinedInfoString);
                        break;
                    case 10:
                        Assert.AreEqual(typeof(EndOfLLDPDU), tlv.GetType());
                        break;
                    default:
                        throw new ArgumentException();
                }

                // increment the counter
                count++;
            }
        }
Ejemplo n.º 5
0
 public static LLDPPacket RandomPacket()
 {
     Random random = new Random();
     LLDPPacket packet = new LLDPPacket();
     byte[] buffer = new byte[EthernetFields.MacAddressLength];
     random.NextBytes(buffer);
     PhysicalAddress mACAddress = new PhysicalAddress(buffer);
     packet.TlvCollection.Add(new ChassisID(mACAddress));
     byte[] buffer2 = new byte[IPv4Fields.AddressLength];
     random.NextBytes(buffer2);
     packet.TlvCollection.Add(new PortID(new NetworkAddress(new IPAddress(buffer2))));
     ushort seconds = (ushort) random.Next(0, 120);
     packet.TlvCollection.Add(new TimeToLive(seconds));
     packet.TlvCollection.Add(new EndOfLLDPDU());
     return packet;
 }