ln.snmp/rfc1213/RFC1213.cs

105 lines
3.1 KiB
C#
Raw Normal View History

2019-03-12 00:54:39 +01:00
// /**
// * 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;
2019-03-13 08:21:11 +01:00
using ln.types;
2019-03-18 08:12:58 +01:00
using ln.logging;
2019-04-02 01:25:47 +02:00
using ln.types.net;
2019-03-12 00:54:39 +01:00
namespace ln.snmp.rfc1213
{
public static class RFC1213
{
2019-03-21 07:43:35 +01:00
public static Interface[] GetInterfaces(SnmpInterface endpoint)
2019-03-12 00:54:39 +01:00
{
Dictionary<int,Interface> interfaces = new Dictionary<int,Interface>();
2019-03-18 08:12:58 +01:00
ObjectIdentifier baseOID = new ObjectIdentifier("1.3.6.1.2.1.2.2.1.2");
2019-03-12 00:54:39 +01:00
Sequence[][] ifTable = endpoint.snmpWalk(new String[] {
"1.3.6.1.2.1.2.2.1.2",
2019-03-18 08:12:58 +01:00
"1.3.6.1.2.1.2.2.1.6"
2019-03-12 00:54:39 +01:00
});
foreach (Sequence[] row in ifTable)
{
ObjectIdentifier index = (row[0].Items[0] as ObjectIdentifier);
2019-03-18 08:12:58 +01:00
2019-03-12 00:54:39 +01:00
Interface intf = new Interface();
intf.Name = (row[0].Items[1] as OctetString).StringValue;
2019-04-02 01:25:47 +02:00
if (row[1] != null)
intf.HWAddr = BitConverter.ToString((row[1].Items[1] as OctetString).Bytes);
else
intf.HWAddr = "";
2019-03-18 08:12:58 +01:00
2019-03-12 00:54:39 +01:00
interfaces.Add(index.OIDValue[0],intf);
}
Sequence[][] ipTable = endpoint.snmpWalk(new String[]
{
"1.3.6.1.2.1.4.20.1.1",
2019-03-13 08:21:11 +01:00
"1.3.6.1.2.1.4.20.1.2",
"1.3.6.1.2.1.4.20.1.3"
2019-03-12 00:54:39 +01:00
});
foreach (Sequence[] row in ipTable)
{
IPAddress ip = (row[0].Items[1] as IPAddr).IP;
int ifIndex = (int)(row[1].Items[1] as Integer).LongValue;
2019-03-13 08:21:11 +01:00
IPAddress mask = (row[2].Items[1] as IPAddr).IP;
2019-03-12 00:54:39 +01:00
if (interfaces.ContainsKey(ifIndex))
{
2019-04-02 01:25:47 +02:00
IPv4 ipv4 = ip;
IPv4 mask4 = mask;
interfaces[ifIndex].Add(ipv4,mask4);
2019-03-12 00:54:39 +01:00
}
}
return interfaces.Values.ToArray();
}
public class Interface
{
public String Name { get; set; }
2019-03-18 08:12:58 +01:00
public String HWAddr { get => hwaddr; set => hwaddr = value; }
2019-04-02 01:25:47 +02:00
public IPv4[] IPAddresses => _IPAddresses.ToArray();
public IPv4[] Netmasks => _Netmasks.ToArray();
2019-03-12 00:54:39 +01:00
2019-04-02 01:25:47 +02:00
private List<IPv4> _IPAddresses = new List<IPv4>();
private List<IPv4> _Netmasks = new List<IPv4>();
2019-03-18 08:12:58 +01:00
private String hwaddr;
2019-03-12 00:54:39 +01:00
public Interface()
{
}
2019-04-02 01:25:47 +02:00
public void Add(IPv4 ip,IPv4 netmask)
2019-03-12 00:54:39 +01:00
{
_IPAddresses.Add(ip);
2019-04-02 01:25:47 +02:00
_Netmasks.Add(netmask);
2019-03-12 00:54:39 +01:00
}
public override string ToString()
{
return String.Format("[Interface Name={0} IPAddresses=({1})]",Name,String.Join(", ",IPAddresses.Select((x)=>x.ToString())));
}
}
}
}