// /** // * File: RFC1213.cs // * Author: haraldwolff // * // * This file and it's content is copyrighted by the Author and / or copyright holder. // * Any use wihtout proper permission is illegal and may lead to legal actions. // * // * // **/ using System; using System.Net; using System.Collections.Generic; using ln.snmp.endpoint; using ln.snmp.types; using System.Linq; using ln.types; using ln.logging; namespace ln.snmp.rfc1213 { public static class RFC1213 { public static Interface[] GetInterfaces(SnmpEndpoint endpoint) { Dictionary interfaces = new Dictionary(); ObjectIdentifier baseOID = new ObjectIdentifier("1.3.6.1.2.1.2.2.1.2"); Sequence[][] ifTable = endpoint.snmpWalk(new String[] { "1.3.6.1.2.1.2.2.1.2", "1.3.6.1.2.1.2.2.1.6" }); foreach (Sequence[] row in ifTable) { ObjectIdentifier index = (row[0].Items[0] as ObjectIdentifier); Interface intf = new Interface(); intf.Name = (row[0].Items[1] as OctetString).StringValue; intf.HWAddr = BitConverter.ToString((row[1].Items[1] as OctetString).Bytes); interfaces.Add(index.OIDValue[0],intf); } Sequence[][] ipTable = endpoint.snmpWalk(new String[] { "1.3.6.1.2.1.4.20.1.1", "1.3.6.1.2.1.4.20.1.2", "1.3.6.1.2.1.4.20.1.3" }); foreach (Sequence[] row in ipTable) { IPAddress ip = (row[0].Items[1] as IPAddr).IP; int ifIndex = (int)(row[1].Items[1] as Integer).LongValue; IPAddress mask = (row[2].Items[1] as IPAddr).IP; if (interfaces.ContainsKey(ifIndex)) { interfaces[ifIndex].AddIPAddress(new CIDR(ip,mask)); } } return interfaces.Values.ToArray(); } public class Interface { public String Name { get; set; } public String HWAddr { get => hwaddr; set => hwaddr = value; } public CIDR[] IPAddresses => _IPAddresses.ToArray(); private List _IPAddresses = new List(); private String hwaddr; public Interface() { } public void AddIPAddress(CIDR ip) { _IPAddresses.Add(ip); } public override string ToString() { return String.Format("[Interface Name={0} IPAddresses=({1})]",Name,String.Join(", ",IPAddresses.Select((x)=>x.ToString()))); } } } }