Skip to content

Commit 07ac7e4

Browse files
authored
Merge pull request #76 from BornToBeRoot/snmp-set
SNMP improved
2 parents 208970d + 275f8be commit 07ac7e4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+999
-1407
lines changed

Source/NETworkManager/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial class App : Application
1616
// Single instance unique identifier
1717
private const string Guid = "6A3F34B2-161F-4F70-A8BC-A19C40F79CFB";
1818
Mutex _mutex;
19-
19+
2020
private bool _singleInstanceClose = false;
2121

2222
public App()

Source/NETworkManager/Helpers/RegexHelper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,8 @@ public static class RegexHelper
4343

4444
// Test for http|https uris
4545
public const string httpAndHttpsUriRegex = @"^http(s)?:\/\/([\w-]+.)+[\w-]+(\/[\w- ./?%&=])?$";
46+
47+
// OID (SNMP)
48+
public const string OIDRegex = @"^([1-9][0-9]{0,3}|0)(\.([1-9][0-9]{0,3}|0)){5,13}$";
4649
}
4750
}

Source/NETworkManager/MainWindow.xaml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ private async void MetroWindowMain_Closing(object sender, CancelEventArgs e)
408408
TracerouteView tracerouteView;
409409
DNSLookupView dnsLookupView;
410410
RemoteDesktopView remoteDesktopView;
411-
SNMPHostView snmpHostView;
411+
SNMPView snmpView;
412412
WakeOnLANView wakeOnLANView;
413413
SubnetCalculatorHostView subnetCalculatorHostView;
414414
HTTPHeadersView httpHeadersView;
@@ -467,10 +467,10 @@ private void ChangeApplicationView(ApplicationViewManager.Name name)
467467
contentControlApplication.Content = remoteDesktopView;
468468
break;
469469
case ApplicationViewManager.Name.SNMP:
470-
if (snmpHostView == null)
471-
snmpHostView = new SNMPHostView();
470+
if (snmpView == null)
471+
snmpView = new SNMPView();
472472

473-
contentControlApplication.Content = snmpHostView;
473+
contentControlApplication.Content = snmpView;
474474
break;
475475
case ApplicationViewManager.Name.WakeOnLAN:
476476
if (wakeOnLANView == null)

Source/NETworkManager/Models/Network/SNMP.cs

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void Getv1v2cAsync(SNMPVersion version, IPAddress ipAddress, string commu
7474
});
7575
}
7676

77-
public void Walkv1v2cAsync(SNMPVersion version, IPAddress ipAddress, string community, string oid, SNMPOptions options, WalkMode walkMode)
77+
public void Walkv1v2cAsync(SNMPVersion version, IPAddress ipAddress, string community, string oid, WalkMode walkMode, SNMPOptions options)
7878
{
7979
Task.Run(() =>
8080
{
@@ -100,6 +100,27 @@ public void Walkv1v2cAsync(SNMPVersion version, IPAddress ipAddress, string comm
100100
});
101101
}
102102

103+
public void Setv1v2cAsync(SNMPVersion version, IPAddress ipAddress, string communtiy, string oid, string data, SNMPOptions options)
104+
{
105+
Task.Run(() =>
106+
{
107+
try
108+
{
109+
Messenger.Set(version == SNMPVersion.v1 ? VersionCode.V1 : VersionCode.V2, new IPEndPoint(ipAddress, options.Port), new OctetString(communtiy), new List<Variable> { new Variable(new ObjectIdentifier(oid), new OctetString(data)) }, options.Timeout);
110+
111+
OnComplete();
112+
}
113+
catch (Lextm.SharpSnmpLib.Messaging.TimeoutException)
114+
{
115+
OnTimeout();
116+
}
117+
catch (ErrorException)
118+
{
119+
OnError();
120+
}
121+
});
122+
}
123+
103124
public void Getv3Async(IPAddress ipAddress, string oid, SNMPv3Security security, string username, SNMPv3AuthenticationProvider authProvider, string auth, SNMPv3PrivacyProvider privProvider, string priv, SNMPOptions options)
104125
{
105126
Task.Run(() =>
@@ -122,7 +143,9 @@ public void Getv3Async(IPAddress ipAddress, string oid, SNMPv3Security security,
122143
privacy = GetPrivacy();
123144

124145
GetRequestMessage request = new GetRequestMessage(VersionCode.V3, Messenger.NextMessageId, Messenger.NextRequestId, new OctetString(username), new List<Variable> { new Variable(new ObjectIdentifier(oid)) }, privacy, Messenger.MaxMessageSize, report);
125-
Variable result = request.GetResponse(options.Timeout, ipEndpoint).Pdu().Variables[0];
146+
ISnmpMessage reply = request.GetResponse(options.Timeout, ipEndpoint);
147+
148+
Variable result = reply.Pdu().Variables[0];
126149

127150
OnReceived(new SNMPReceivedArgs(result.Id, result.Data));
128151

@@ -139,7 +162,7 @@ public void Getv3Async(IPAddress ipAddress, string oid, SNMPv3Security security,
139162
});
140163
}
141164

142-
public void Walkv3Async(IPAddress ipAddress, string oid, SNMPv3Security security, string username, SNMPv3AuthenticationProvider authProvider, string auth, SNMPv3PrivacyProvider privProvider, string priv, SNMPOptions options, WalkMode walkMode)
165+
public void Walkv3Async(IPAddress ipAddress, string oid, SNMPv3Security security, string username, SNMPv3AuthenticationProvider authProvider, string auth, SNMPv3PrivacyProvider privProvider, string priv, WalkMode walkMode, SNMPOptions options)
143166
{
144167
Task.Run(() =>
145168
{
@@ -180,6 +203,43 @@ public void Walkv3Async(IPAddress ipAddress, string oid, SNMPv3Security security
180203
});
181204
}
182205

206+
public void Setv3Async(IPAddress ipAddress, string oid, SNMPv3Security security, string username, SNMPv3AuthenticationProvider authProvider, string auth, SNMPv3PrivacyProvider privProvider, string priv, string data, SNMPOptions options)
207+
{
208+
Task.Run(() =>
209+
{
210+
try
211+
{
212+
IPEndPoint ipEndpoint = new IPEndPoint(ipAddress, options.Port);
213+
214+
// Discovery
215+
Discovery discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu);
216+
ReportMessage report = discovery.GetResponse(options.Timeout, ipEndpoint);
217+
218+
IPrivacyProvider privacy;
219+
220+
if (security == SNMPv3Security.authPriv)
221+
privacy = GetPrivacy(authProvider, auth, privProvider, priv);
222+
else if (security == SNMPv3Security.authNoPriv)
223+
privacy = GetPrivacy(authProvider, auth);
224+
else // noAuthNoPriv
225+
privacy = GetPrivacy();
226+
227+
SetRequestMessage request = new SetRequestMessage(VersionCode.V3, Messenger.NextMessageId, Messenger.NextRequestId, new OctetString(username), new List<Variable> { new Variable(new ObjectIdentifier(oid), new OctetString(data)) }, privacy, Messenger.MaxMessageSize, report);
228+
ISnmpMessage reply = request.GetResponse(options.Timeout, ipEndpoint);
229+
230+
OnComplete();
231+
}
232+
catch (Lextm.SharpSnmpLib.Messaging.TimeoutException)
233+
{
234+
OnTimeout();
235+
}
236+
catch (ErrorException)
237+
{
238+
OnError();
239+
}
240+
});
241+
}
242+
183243
// noAuthNoPriv
184244
private IPrivacyProvider GetPrivacy()
185245
{
@@ -224,12 +284,14 @@ public enum SNMPVersion
224284
v3
225285
}
226286

287+
// Trap and Inform not implemented
227288
public enum SNMPMode
228289
{
229290
Get,
230291
Walk,
231292
Set,
232-
Trap
293+
Trap,
294+
Inform
233295
}
234296

235297
public enum SNMPv3Security

Source/NETworkManager/Models/Settings/SettingsInfo.cs

Lines changed: 40 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,170 +1532,114 @@ public bool SNMP_ResolveHostnamePreferIPv4
15321532
}
15331533
}
15341534

1535-
private ObservableCollection<string> _snmp_v1v2c_HostHistory = new ObservableCollection<string>();
1536-
public ObservableCollection<string> SNMP_v1v2c_HostHistory
1535+
private ObservableCollection<string> _snmp_HostHistory = new ObservableCollection<string>();
1536+
public ObservableCollection<string> SNMP_HostHistory
15371537
{
1538-
get { return _snmp_v1v2c_HostHistory; }
1538+
get { return _snmp_HostHistory; }
15391539
set
15401540
{
1541-
if (value == _snmp_v1v2c_HostHistory)
1541+
if (value == _snmp_HostHistory)
15421542
return;
15431543

1544-
_snmp_v1v2c_HostHistory = value;
1544+
_snmp_HostHistory = value;
15451545
SettingsChanged = true;
15461546
}
15471547
}
15481548

1549-
private ObservableCollection<string> _snmp_v1v2c_OIDHistory = new ObservableCollection<string>();
1550-
public ObservableCollection<string> SNMP_v1v2c_OIDHistory
1549+
private ObservableCollection<string> _snmp_OIDHistory = new ObservableCollection<string>();
1550+
public ObservableCollection<string> SNMP_OIDHistory
15511551
{
1552-
get { return _snmp_v1v2c_OIDHistory; }
1552+
get { return _snmp_OIDHistory; }
15531553
set
15541554
{
1555-
if (value == _snmp_v1v2c_OIDHistory)
1555+
if (value == _snmp_OIDHistory)
15561556
return;
15571557

1558-
_snmp_v1v2c_OIDHistory = value;
1558+
_snmp_OIDHistory = value;
15591559
SettingsChanged = true;
15601560
}
15611561
}
15621562

1563-
private SNMPMode _snmp_v1v2c_Mode = SNMPMode.Walk;
1564-
public SNMPMode SNMP_v1v2c_Mode
1563+
private SNMPMode _snmp_Mode = SNMPMode.Walk;
1564+
public SNMPMode SNMP_Mode
15651565
{
1566-
get { return _snmp_v1v2c_Mode; }
1566+
get { return _snmp_Mode; }
15671567
set
15681568
{
1569-
if (value == _snmp_v1v2c_Mode)
1569+
if (value == _snmp_Mode)
15701570
return;
15711571

1572-
_snmp_v1v2c_Mode = value;
1572+
_snmp_Mode = value;
15731573
SettingsChanged = true;
15741574
}
15751575
}
15761576

1577-
private SNMPVersion _snmp_v1v2c_Version = SNMPVersion.v2c;
1578-
public SNMPVersion SNMP_v1v2c_Version
1577+
private SNMPVersion _snmp_Version = SNMPVersion.v2c;
1578+
public SNMPVersion SNMP_Version
15791579
{
1580-
get { return _snmp_v1v2c_Version; }
1580+
get { return _snmp_Version; }
15811581
set
15821582
{
1583-
if (value == _snmp_v1v2c_Version)
1583+
if (value == _snmp_Version)
15841584
return;
15851585

1586-
_snmp_v1v2c_Version = value;
1586+
_snmp_Version = value;
15871587
SettingsChanged = true;
15881588
}
15891589
}
15901590

1591-
private bool _snmp_v1v2c_ExpandStatistics = true;
1592-
public bool SNMP_v1v2c_ExpandStatistics
1591+
private bool _snmp_ExpandStatistics = true;
1592+
public bool SNMP_ExpandStatistics
15931593
{
1594-
get { return _snmp_v1v2c_ExpandStatistics; }
1594+
get { return _snmp_ExpandStatistics; }
15951595
set
15961596
{
1597-
if (value == _snmp_v1v2c_ExpandStatistics)
1597+
if (value == _snmp_ExpandStatistics)
15981598
return;
15991599

1600-
_snmp_v1v2c_ExpandStatistics = value;
1600+
_snmp_ExpandStatistics = value;
16011601
SettingsChanged = true;
16021602
}
16031603
}
16041604

1605-
private ObservableCollection<string> _snmp_v3_HostHistory = new ObservableCollection<string>();
1606-
public ObservableCollection<string> SNMP_v3_HostHistory
1605+
private SNMPv3Security _snmp_Security = SNMPv3Security.authPriv;
1606+
public SNMPv3Security SNMP_Security
16071607
{
1608-
get { return _snmp_v3_HostHistory; }
1608+
get { return _snmp_Security; }
16091609
set
16101610
{
1611-
if (value == _snmp_v3_HostHistory)
1611+
if (value == _snmp_Security)
16121612
return;
16131613

1614-
_snmp_v3_HostHistory = value;
1614+
_snmp_Security = value;
16151615
SettingsChanged = true;
16161616
}
16171617
}
16181618

1619-
private ObservableCollection<string> _snmp_v3_OIDHistory = new ObservableCollection<string>();
1620-
public ObservableCollection<string> SNMP_v3_OIDHistory
1619+
private SNMPv3AuthenticationProvider _snmp_AuthenticationProvider = SNMPv3AuthenticationProvider.SHA1;
1620+
public SNMPv3AuthenticationProvider SNMP_AuthenticationProvider
16211621
{
1622-
get { return _snmp_v3_OIDHistory; }
1622+
get { return _snmp_AuthenticationProvider; }
16231623
set
16241624
{
1625-
if (value == _snmp_v3_OIDHistory)
1625+
if (value == _snmp_AuthenticationProvider)
16261626
return;
16271627

1628-
_snmp_v3_OIDHistory = value;
1628+
_snmp_AuthenticationProvider = value;
16291629
SettingsChanged = true;
16301630
}
16311631
}
16321632

1633-
private SNMPMode _snmp_v3_Mode = SNMPMode.Walk;
1634-
public SNMPMode SNMP_v3_Mode
1633+
private SNMPv3PrivacyProvider _snmp_PrivacyProvider = SNMPv3PrivacyProvider.AES;
1634+
public SNMPv3PrivacyProvider SNMP_PrivacyProvider
16351635
{
1636-
get { return _snmp_v3_Mode; }
1636+
get { return _snmp_PrivacyProvider; }
16371637
set
16381638
{
1639-
if (value == _snmp_v3_Mode)
1639+
if (value == _snmp_PrivacyProvider)
16401640
return;
16411641

1642-
_snmp_v3_Mode = value;
1643-
SettingsChanged = true;
1644-
}
1645-
}
1646-
1647-
private SNMPv3Security _snmp_v3_Security = SNMPv3Security.authPriv;
1648-
public SNMPv3Security SNMP_v3_Security
1649-
{
1650-
get { return _snmp_v3_Security; }
1651-
set
1652-
{
1653-
if (value == _snmp_v3_Security)
1654-
return;
1655-
1656-
_snmp_v3_Security = value;
1657-
SettingsChanged = true;
1658-
}
1659-
}
1660-
1661-
private SNMPv3AuthenticationProvider _snmp_v3_AuthenticationProvider = SNMPv3AuthenticationProvider.SHA1;
1662-
public SNMPv3AuthenticationProvider SNMP_v3_AuthenticationProvider
1663-
{
1664-
get { return _snmp_v3_AuthenticationProvider; }
1665-
set
1666-
{
1667-
if (value == _snmp_v3_AuthenticationProvider)
1668-
return;
1669-
1670-
_snmp_v3_AuthenticationProvider = value;
1671-
SettingsChanged = true;
1672-
}
1673-
}
1674-
1675-
private SNMPv3PrivacyProvider _snmp_v3_PrivacyProvider = SNMPv3PrivacyProvider.AES;
1676-
public SNMPv3PrivacyProvider SNMP_v3_PrivacyProvider
1677-
{
1678-
get { return _snmp_v3_PrivacyProvider; }
1679-
set
1680-
{
1681-
if (value == _snmp_v3_PrivacyProvider)
1682-
return;
1683-
1684-
_snmp_v3_PrivacyProvider = value;
1685-
SettingsChanged = true;
1686-
}
1687-
}
1688-
1689-
private bool _snmp_v3_ExpandStatistics = true;
1690-
public bool SNMP_v3_ExpandStatistics
1691-
{
1692-
get { return _snmp_v3_ExpandStatistics; }
1693-
set
1694-
{
1695-
if (value == _snmp_v3_ExpandStatistics)
1696-
return;
1697-
1698-
_snmp_v3_ExpandStatistics = value;
1642+
_snmp_PrivacyProvider = value;
16991643
SettingsChanged = true;
17001644
}
17011645
}

0 commit comments

Comments
 (0)