ln.snmp/rfc1213/RFC1213.cs

105 lines
3.1 KiB
C#

// /**
// * 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;
using ln.types.net;
namespace ln.snmp.rfc1213
{
public static class RFC1213
{
public static Interface[] GetInterfaces(SnmpInterface endpoint)
{
Dictionary<int,Interface> interfaces = new Dictionary<int,Interface>();
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;
if (row[1] != null)
intf.HWAddr = BitConverter.ToString((row[1].Items[1] as OctetString).Bytes);
else
intf.HWAddr = "";
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))
{
IPv4 ipv4 = ip;
IPv4 mask4 = mask;
interfaces[ifIndex].Add(ipv4,mask4);
}
}
return interfaces.Values.ToArray();
}
public class Interface
{
public String Name { get; set; }
public String HWAddr { get => hwaddr; set => hwaddr = value; }
public IPv4[] IPAddresses => _IPAddresses.ToArray();
public IPv4[] Netmasks => _Netmasks.ToArray();
private List<IPv4> _IPAddresses = new List<IPv4>();
private List<IPv4> _Netmasks = new List<IPv4>();
private String hwaddr;
public Interface()
{
}
public void Add(IPv4 ip,IPv4 netmask)
{
_IPAddresses.Add(ip);
_Netmasks.Add(netmask);
}
public override string ToString()
{
return String.Format("[Interface Name={0} IPAddresses=({1})]",Name,String.Join(", ",IPAddresses.Select((x)=>x.ToString())));
}
}
}
}