Skip to content

Commit 39cd5bd

Browse files
committed
Enhance SNMP MIB registration and initialization
1 parent a875f14 commit 39cd5bd

File tree

15 files changed

+194
-27
lines changed

15 files changed

+194
-27
lines changed

Samples.Engine/Objects/IfTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public sealed class IfTable : TableObject
4444
/// <summary>
4545
/// Initializes a new instance of the <see cref="IfTable"/> class.
4646
/// </summary>
47-
public IfTable()
47+
public IfTable() : base("1.3.6.1.2.1.2.2")
4848
{
4949
NetworkChange.NetworkAddressChanged +=
5050
(sender, args) => LoadElements();

Samples.Engine/Objects/SysORTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public sealed class SysORTable : TableObject
3434
/// <summary>
3535
/// Initializes a new instance of the <see cref="SysORTable"/> class.
3636
/// </summary>
37-
public SysORTable()
37+
public SysORTable() : base("1.3.6.1.2.1.1.9.1")
3838
{
3939
_elements.Add(new SysORIndex(1));
4040
_elements.Add(new SysORIndex(2));

Samples.Engine/Pipeline/ISnmpObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace Samples.Pipeline
2626
/// </summary>
2727
public interface ISnmpObject
2828
{
29+
ObjectIdentifier Id { get; }
2930
/// <summary>
3031
/// Matches the GET NEXT criteria.
3132
/// </summary>

Samples.Engine/Pipeline/ObjectStore.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,15 @@ public virtual ScalarObject GetNextObject(ObjectIdentifier id)
5555
/// Adds the specified <see cref="ISnmpObject"/>.
5656
/// </summary>
5757
/// <param name="newObject">The object.</param>
58+
/// <exception cref="System.InvalidOperationException">
59+
/// Thrown if an object with the same ID already exists in the store.
60+
/// </exception>
5861
public virtual void Add(ISnmpObject newObject)
5962
{
63+
if (List.Any(o => o.Id == newObject.Id))
64+
{
65+
throw new System.InvalidOperationException($"An object with ID {newObject.Id} already exists in the store.");
66+
}
6067
List.Add(newObject);
6168
}
6269
}

Samples.Engine/Pipeline/ScalarObject.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@ public abstract ISnmpData Data
7373
get; set;
7474
}
7575

76-
/// <summary>
77-
/// Gets the ID.
78-
/// </summary>
79-
/// <value>The ID.</value>
80-
private ObjectIdentifier Id { get; set; }
81-
8276
/// <summary>
8377
/// Matches the GET NEXT criteria.
8478
/// </summary>

Samples.Engine/Pipeline/SnmpObjectBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace Samples.Pipeline
2626
/// </summary>
2727
public abstract class SnmpObjectBase : ISnmpObject
2828
{
29+
public ObjectIdentifier Id { get; protected set; }
2930
/// <summary>
3031
/// Matches the GET NEXT criteria.
3132
/// </summary>

Samples.Engine/Pipeline/TableObject.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ namespace Samples.Pipeline
2828
/// </summary>
2929
public abstract class TableObject : SnmpObjectBase
3030
{
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="TableObject"/> class.
33+
/// </summary>
34+
/// <param name="id">The ID.</param>
35+
protected TableObject(ObjectIdentifier id)
36+
{
37+
Id = id;
38+
}
39+
40+
/// <summary>
41+
/// Initializes a new instance of the <see cref="TableObject"/> class.
42+
/// </summary>
43+
/// <param name="dots">The ID string.</param>
44+
protected TableObject(string dots)
45+
: this(new ObjectIdentifier(dots))
46+
{
47+
}
48+
3149
/// <summary>
3250
/// Gets the objects in the table.
3351
/// </summary>

Samples.Engine/Samples.Engine.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
<ItemGroup Condition="'$(TargetFramework)'=='net471'">
2424
<Reference Include="System.Configuration" />
2525
</ItemGroup>
26-
</Project>
26+
</Project>
File renamed without changes.

Samples/CSharpCore/snmpd/Customized/IP-MIB.g.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3890,3 +3890,92 @@ public override ISnmpData Data
38903890
}
38913891
}
38923892
}
3893+
3894+
namespace Lextm.SharpSnmpPro.Mib
3895+
{
3896+
/// <summary>
3897+
/// Registration class for IP-MIB MIB objects.
3898+
/// </summary>
3899+
public static partial class ModuleRegister
3900+
{
3901+
/// <summary>
3902+
/// Registers all objects from this module to the specified object store.
3903+
/// </summary>
3904+
/// <param name="store">The object store to register objects with.</param>
3905+
public static void RegisterIP_MIB(ObjectStore store)
3906+
{
3907+
if (store == null)
3908+
{
3909+
throw new ArgumentNullException(nameof(store));
3910+
}
3911+
3912+
store.Add(new IP_MIB.ipForwarding());
3913+
store.Add(new IP_MIB.ipDefaultTTL());
3914+
store.Add(new IP_MIB.ipReasmTimeout());
3915+
store.Add(new IP_MIB.ipv6IpForwarding());
3916+
store.Add(new IP_MIB.ipv6IpDefaultHopLimit());
3917+
store.Add(new IP_MIB.ipv4InterfaceTableLastChange());
3918+
store.Add(new IP_MIB.ipv4InterfaceTable());
3919+
store.Add(new IP_MIB.ipv6InterfaceTableLastChange());
3920+
store.Add(new IP_MIB.ipv6InterfaceTable());
3921+
store.Add(new IP_MIB.ipSystemStatsTable());
3922+
store.Add(new IP_MIB.ipIfStatsTableLastChange());
3923+
store.Add(new IP_MIB.ipIfStatsTable());
3924+
store.Add(new IP_MIB.ipAddressPrefixTable());
3925+
store.Add(new IP_MIB.ipAddressSpinLock());
3926+
store.Add(new IP_MIB.ipAddressTable());
3927+
store.Add(new IP_MIB.ipNetToPhysicalTable());
3928+
store.Add(new IP_MIB.ipv6ScopeZoneIndexTable());
3929+
store.Add(new IP_MIB.ipDefaultRouterTable());
3930+
store.Add(new IP_MIB.ipv6RouterAdvertSpinLock());
3931+
store.Add(new IP_MIB.ipv6RouterAdvertTable());
3932+
store.Add(new IP_MIB.icmpStatsTable());
3933+
store.Add(new IP_MIB.icmpMsgStatsTable());
3934+
store.Add(new IP_MIB.ipInReceives());
3935+
store.Add(new IP_MIB.ipInHdrErrors());
3936+
store.Add(new IP_MIB.ipInAddrErrors());
3937+
store.Add(new IP_MIB.ipForwDatagrams());
3938+
store.Add(new IP_MIB.ipInUnknownProtos());
3939+
store.Add(new IP_MIB.ipInDiscards());
3940+
store.Add(new IP_MIB.ipInDelivers());
3941+
store.Add(new IP_MIB.ipOutRequests());
3942+
store.Add(new IP_MIB.ipOutDiscards());
3943+
store.Add(new IP_MIB.ipOutNoRoutes());
3944+
store.Add(new IP_MIB.ipReasmReqds());
3945+
store.Add(new IP_MIB.ipReasmOKs());
3946+
store.Add(new IP_MIB.ipReasmFails());
3947+
store.Add(new IP_MIB.ipFragOKs());
3948+
store.Add(new IP_MIB.ipFragFails());
3949+
store.Add(new IP_MIB.ipFragCreates());
3950+
store.Add(new IP_MIB.ipRoutingDiscards());
3951+
store.Add(new IP_MIB.ipAddrTable());
3952+
store.Add(new IP_MIB.ipNetToMediaTable());
3953+
store.Add(new IP_MIB.icmpInMsgs());
3954+
store.Add(new IP_MIB.icmpInErrors());
3955+
store.Add(new IP_MIB.icmpInDestUnreachs());
3956+
store.Add(new IP_MIB.icmpInTimeExcds());
3957+
store.Add(new IP_MIB.icmpInParmProbs());
3958+
store.Add(new IP_MIB.icmpInSrcQuenchs());
3959+
store.Add(new IP_MIB.icmpInRedirects());
3960+
store.Add(new IP_MIB.icmpInEchos());
3961+
store.Add(new IP_MIB.icmpInEchoReps());
3962+
store.Add(new IP_MIB.icmpInTimestamps());
3963+
store.Add(new IP_MIB.icmpInTimestampReps());
3964+
store.Add(new IP_MIB.icmpInAddrMasks());
3965+
store.Add(new IP_MIB.icmpInAddrMaskReps());
3966+
store.Add(new IP_MIB.icmpOutMsgs());
3967+
store.Add(new IP_MIB.icmpOutErrors());
3968+
store.Add(new IP_MIB.icmpOutDestUnreachs());
3969+
store.Add(new IP_MIB.icmpOutTimeExcds());
3970+
store.Add(new IP_MIB.icmpOutParmProbs());
3971+
store.Add(new IP_MIB.icmpOutSrcQuenchs());
3972+
store.Add(new IP_MIB.icmpOutRedirects());
3973+
store.Add(new IP_MIB.icmpOutEchos());
3974+
store.Add(new IP_MIB.icmpOutEchoReps());
3975+
store.Add(new IP_MIB.icmpOutTimestamps());
3976+
store.Add(new IP_MIB.icmpOutTimestampReps());
3977+
store.Add(new IP_MIB.icmpOutAddrMasks());
3978+
store.Add(new IP_MIB.icmpOutAddrMaskReps());
3979+
}
3980+
}
3981+
}

0 commit comments

Comments
 (0)