Skip to content

Commit d4ecbf6

Browse files
committed
snmp v3 with priv/auth working
1 parent 6d70356 commit d4ecbf6

5 files changed

Lines changed: 174 additions & 19 deletions

File tree

‎Source/NETworkManager/Models/Network/SNMP.cs‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using Lextm.SharpSnmpLib;
22
using Lextm.SharpSnmpLib.Messaging;
3+
using Lextm.SharpSnmpLib.Security;
34
using System;
45
using System.Collections.Generic;
56
using System.Net;
67
using System.Threading.Tasks;
8+
using System.Windows;
9+
710
namespace NETworkManager.Models.Network
811
{
912
public class SNMP
@@ -57,7 +60,7 @@ public void Getv1v2cAsync(SNMPVersion version, IPAddress ipAddress, string commu
5760
try
5861
{
5962
foreach (Variable result in Messenger.Get(version == SNMPVersion.v1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, options.Port), new OctetString(community), new List<Variable> { new Variable(new ObjectIdentifier(oid)) }, options.Timeout))
60-
OnReceived(new SNMPReceivedArgs(result.Id.ToString(), result.Data.ToString()));
63+
OnReceived(new SNMPReceivedArgs(result.Id, result.Data));
6164

6265
OnComplete();
6366
}
@@ -83,7 +86,7 @@ public void Walkv1v2cAsync(SNMPVersion version, IPAddress ipAddress, string comm
8386
Messenger.Walk(version == SNMPVersion.v1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, options.Port), new OctetString(community), new ObjectIdentifier(oid), results, options.Timeout, walkMode);
8487

8588
foreach (Variable result in results)
86-
OnReceived(new SNMPReceivedArgs(result.Id.ToString(), result.Data.ToString()));
89+
OnReceived(new SNMPReceivedArgs(result.Id, result.Data));
8790

8891
OnComplete();
8992
}
@@ -97,6 +100,47 @@ public void Walkv1v2cAsync(SNMPVersion version, IPAddress ipAddress, string comm
97100
}
98101
});
99102
}
103+
104+
public void Walkv3Async(IPAddress ipAddress, string oid, SNMPv3Security security, string username, SNMPv3AuthenticationProvider authProvider, string auth, SNMPv3PrivacyProvider privProvider, string priv, SNMPOptions options, WalkMode walkMode)
105+
{
106+
Task.Run(() =>
107+
{
108+
IPEndPoint ipEndpoint = new IPEndPoint(ipAddress, options.Port);
109+
110+
// Discovery
111+
Discovery discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu);
112+
ReportMessage report = discovery.GetResponse(options.Timeout, ipEndpoint);
113+
114+
IPrivacyProvider privacy = GetPrivacy(authProvider, auth, privProvider, priv);
115+
116+
var results = new List<Variable>();
117+
118+
Messenger.BulkWalk(VersionCode.V3, ipEndpoint, new OctetString(username), new ObjectIdentifier(oid), results, options.Timeout, 10, walkMode, privacy, report);
119+
120+
foreach (Variable result in results)
121+
OnReceived(new SNMPReceivedArgs(result.Id, result.Data));
122+
123+
OnComplete();
124+
});
125+
}
126+
127+
private IPrivacyProvider GetPrivacy(SNMPv3AuthenticationProvider authProvider, string auth, SNMPv3PrivacyProvider privProvider, string priv)
128+
{
129+
IAuthenticationProvider authenticationProvider;
130+
131+
// Authentication provider
132+
if (authProvider == SNMPv3AuthenticationProvider.MD5)
133+
authenticationProvider = new MD5AuthenticationProvider(new OctetString(auth));
134+
else
135+
authenticationProvider = new SHA1AuthenticationProvider(new OctetString(auth));
136+
137+
138+
139+
if (privProvider == SNMPv3PrivacyProvider.DES)
140+
return new DESPrivacyProvider(new OctetString(priv), authenticationProvider);
141+
else
142+
return new AESPrivacyProvider(new OctetString(priv), authenticationProvider);
143+
}
100144
#endregion
101145

102146
#region Enum
@@ -113,6 +157,18 @@ public enum SNMPv3Security
113157
AuthNoPriv,
114158
AuthPriv
115159
}
160+
161+
public enum SNMPv3AuthenticationProvider
162+
{
163+
MD5,
164+
SHA1
165+
}
166+
167+
public enum SNMPv3PrivacyProvider
168+
{
169+
DES,
170+
AES
171+
}
116172
#endregion
117173
}
118174
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace NETworkManager.Models.Network
1+
using Lextm.SharpSnmpLib;
2+
3+
namespace NETworkManager.Models.Network
24
{
35
public class SNMPReceivedArgs : System.EventArgs
46
{
@@ -10,10 +12,10 @@ public SNMPReceivedArgs()
1012

1113
}
1214

13-
public SNMPReceivedArgs(string oid, string data)
15+
public SNMPReceivedArgs(ObjectIdentifier oid, ISnmpData data)
1416
{
15-
OID = oid;
16-
Data = data;
17+
OID = oid.ToString();
18+
Data = data.ToString();
1719
}
1820
}
1921
}

‎Source/NETworkManager/Models/Settings/SettingsInfo.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,34 @@ public SNMPv3Security SNMP_v3_Security
15141514
}
15151515
}
15161516

1517+
private SNMPv3AuthenticationProvider _snmp_v3_AuthenticationProvider = SNMPv3AuthenticationProvider.SHA1;
1518+
public SNMPv3AuthenticationProvider SNMP_v3_AuthenticationProvider
1519+
{
1520+
get { return _snmp_v3_AuthenticationProvider; }
1521+
set
1522+
{
1523+
if (value == _snmp_v3_AuthenticationProvider)
1524+
return;
1525+
1526+
_snmp_v3_AuthenticationProvider = value;
1527+
SettingsChanged = true;
1528+
}
1529+
}
1530+
1531+
private SNMPv3PrivacyProvider _snmp_v3_PrivacyProvider = SNMPv3PrivacyProvider.AES;
1532+
public SNMPv3PrivacyProvider SNMP_v3_PrivacyProvider
1533+
{
1534+
get { return _snmp_v3_PrivacyProvider; }
1535+
set
1536+
{
1537+
if (value == _snmp_v3_PrivacyProvider)
1538+
return;
1539+
1540+
_snmp_v3_PrivacyProvider = value;
1541+
SettingsChanged = true;
1542+
}
1543+
}
1544+
15171545
private bool _snmp_v3_ExpandStatistics = true;
15181546
public bool SNMP_v3_ExpandStatistics
15191547
{

‎Source/NETworkManager/ViewModels/Applications/SNMPv3ViewModel.cs‎

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,25 @@ public string Username
137137
}
138138
}
139139

140-
private string _priv;
141-
public string Priv
140+
public List<SNMPv3AuthenticationProvider> AuthenticationProviders { get; set; }
141+
142+
private SNMPv3AuthenticationProvider _authenticationProvider;
143+
public SNMPv3AuthenticationProvider AuthenticationProvider
142144
{
143-
get { return _priv; }
145+
get { return _authenticationProvider; }
144146
set
145147
{
146-
if (value == _priv)
148+
if (value == _authenticationProvider)
147149
return;
148150

149-
_priv = value;
151+
if (!_isLoading)
152+
SettingsManager.Current.SNMP_v3_AuthenticationProvider = value;
153+
154+
_authenticationProvider = value;
150155
OnPropertyChanged();
151156
}
152157
}
153-
158+
154159
private string _auth;
155160
public string Auth
156161
{
@@ -165,6 +170,39 @@ public string Auth
165170
}
166171
}
167172

173+
public List<SNMPv3PrivacyProvider> PrivacyProviders { get; set; }
174+
175+
private SNMPv3PrivacyProvider _privacyProvider;
176+
public SNMPv3PrivacyProvider PrivacyProvider
177+
{
178+
get { return _privacyProvider; }
179+
set
180+
{
181+
if (value == _privacyProvider)
182+
return;
183+
184+
if (!_isLoading)
185+
SettingsManager.Current.SNMP_v3_PrivacyProvider = value;
186+
187+
_privacyProvider = value;
188+
OnPropertyChanged();
189+
}
190+
}
191+
192+
private string _priv;
193+
public string Priv
194+
{
195+
get { return _priv; }
196+
set
197+
{
198+
if (value == _priv)
199+
return;
200+
201+
_priv = value;
202+
OnPropertyChanged();
203+
}
204+
}
205+
168206
private bool _isQueryRunning;
169207
public bool IsQueryRunning
170208
{
@@ -324,6 +362,10 @@ public SNMPv3ViewModel()
324362
// Version v1 and v2c (default v2c)
325363
Securitys = new List<SNMPv3Security>() { SNMPv3Security.noAuthNoPriv, SNMPv3Security.AuthNoPriv, SNMPv3Security.AuthPriv };
326364

365+
// Auth / Priv
366+
AuthenticationProviders = new List<SNMPv3AuthenticationProvider>() { SNMPv3AuthenticationProvider.MD5, SNMPv3AuthenticationProvider.SHA1 };
367+
PrivacyProviders = new List<SNMPv3PrivacyProvider>() { SNMPv3PrivacyProvider.DES, SNMPv3PrivacyProvider.AES };
368+
327369
LoadSettings();
328370

329371
_isLoading = false;
@@ -338,6 +380,8 @@ private void LoadSettings()
338380
OIDHistory = new List<string>(SettingsManager.Current.SNMP_v3_OIDHistory);
339381

340382
Security = Securitys.FirstOrDefault(x => x == SettingsManager.Current.SNMP_v3_Security);
383+
AuthenticationProvider = AuthenticationProviders.FirstOrDefault(x => x == SettingsManager.Current.SNMP_v3_AuthenticationProvider);
384+
PrivacyProvider = PrivacyProviders.FirstOrDefault(x => x == SettingsManager.Current.SNMP_v3_PrivacyProvider);
341385
Walk = SettingsManager.Current.SNMP_v3_Walk;
342386
ExpandStatistics = SettingsManager.Current.SNMP_v3_ExpandStatistics;
343387
}
@@ -453,10 +497,10 @@ private async void Query()
453497
snmp.UserHasCanceled += Snmp_UserHasCanceled;
454498
snmp.Complete += Snmp_Complete;
455499

456-
//if (Walk)
457-
// snmp.Walkv1v2cAsync(Version, ipAddress, Community, OID, snmpOptions, SettingsManager.Current.SNMP_WalkMode);
458-
//else
459-
// snmp.Getv1v2cAsync(Version, ipAddress, Community, OID, snmpOptions);
500+
if (Walk)
501+
snmp.Walkv3Async(ipAddress, OID, Security, Username, AuthenticationProvider, Auth, PrivacyProvider, Priv, snmpOptions, SettingsManager.Current.SNMP_WalkMode);
502+
// else
503+
// snmp.Getv1v2cAsync(Version, ipAddress, Community, OID, snmpOptions);
460504

461505
// Add to history...
462506
HostnameHistory = new List<string>(HistoryListHelper.Modify(HostnameHistory, Hostname, SettingsManager.Current.Application_HistoryListEntries));

‎Source/NETworkManager/Views/Applications/SNMPv3View.xaml‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,18 @@
8989
</Style>
9090
</TextBlock.Style>
9191
</TextBlock>
92-
<TextBox Grid.Column="6" Grid.Row="2" Grid.ColumnSpan="5" Name="txtAuth" IsEnabled="{Binding IsQueryRunning, Converter={StaticResource BooleanReverseConverter}}">
92+
<ComboBox Grid.Column="6" Grid.Row="2" ItemsSource="{Binding AuthenticationProviders}" IsEnabled="{Binding IsQueryRunning, Converter={StaticResource BooleanReverseConverter}}" SelectedItem="{Binding AuthenticationProvider}" >
93+
<ComboBox.Style>
94+
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource DefaultComboBox}">
95+
<Style.Triggers>
96+
<DataTrigger Binding="{Binding Security}" Value="noAuthNoPriv">
97+
<Setter Property="Visibility" Value="Collapsed" />
98+
</DataTrigger>
99+
</Style.Triggers>
100+
</Style>
101+
</ComboBox.Style>
102+
</ComboBox>
103+
<TextBox Grid.Column="8" Grid.Row="2" Grid.ColumnSpan="3" Name="txtAuth" IsEnabled="{Binding IsQueryRunning, Converter={StaticResource BooleanReverseConverter}}">
93104
<TextBox.Style>
94105
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource DefaultTextBox}">
95106
<Style.Triggers>
@@ -108,6 +119,20 @@
108119
</TextBox.Text>
109120
</TextBox>
110121
<TextBlock Grid.Column="0" Grid.Row="4" Text="{DynamicResource String_Username}" Style="{StaticResource CenterTextBlock}" />
122+
<ComboBox Grid.Column="6" Grid.Row="4" ItemsSource="{Binding PrivacyProviders}" IsEnabled="{Binding IsQueryRunning, Converter={StaticResource BooleanReverseConverter}}" SelectedItem="{Binding PrivacyProvider}" >
123+
<ComboBox.Style>
124+
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource DefaultComboBox}">
125+
<Style.Triggers>
126+
<DataTrigger Binding="{Binding Security}" Value="noAuthNoPriv">
127+
<Setter Property="Visibility" Value="Collapsed" />
128+
</DataTrigger>
129+
<DataTrigger Binding="{Binding Security}" Value="AuthNoPriv">
130+
<Setter Property="Visibility" Value="Collapsed" />
131+
</DataTrigger>
132+
</Style.Triggers>
133+
</Style>
134+
</ComboBox.Style>
135+
</ComboBox>
111136
<TextBox Grid.Column="2" Grid.Row="4" Name="txtUsername" mah:TextBoxHelper.Watermark="{DynamicResource String_Watermark_ExampleSNMPUsername}" IsEnabled="{Binding IsQueryRunning, Converter={StaticResource BooleanReverseConverter}}" Style="{StaticResource DefaultTextBox}">
112137
<TextBox.Text>
113138
<Binding Path="Username" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
@@ -131,7 +156,7 @@
131156
</Style>
132157
</TextBlock.Style>
133158
</TextBlock>
134-
<TextBox Grid.Column="6" Grid.Row="4" Grid.ColumnSpan="5" Name="txtPriv" IsEnabled="{Binding IsQueryRunning, Converter={StaticResource BooleanReverseConverter}}">
159+
<TextBox Grid.Column="8" Grid.Row="4" Grid.ColumnSpan="3" Name="txtPriv" IsEnabled="{Binding IsQueryRunning, Converter={StaticResource BooleanReverseConverter}}">
135160
<TextBox.Style>
136161
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource DefaultTextBox}">
137162
<Style.Triggers>
@@ -151,7 +176,7 @@
151176
</Binding.ValidationRules>
152177
</Binding>
153178
</TextBox.Text>
154-
</TextBox>
179+
</TextBox>
155180
<Button Grid.Column="12" Grid.Row="4" Command="{Binding QueryCommand}" IsDefault="{Binding IsQueryRunning, Converter={StaticResource BooleanReverseConverter}}" IsCancel="{Binding IsQueryRunning}" HorizontalAlignment="Right">
156181
<Button.Resources>
157182
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource ImageWithTextButton}">

0 commit comments

Comments
 (0)