forked from mvenditto/DotNetSnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnmpV3Message.cs
77 lines (59 loc) · 2.17 KB
/
SnmpV3Message.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using DotNetSnmp.Asn1.Serialization;
using DotNetSnmp.Common.Definitions;
using DotNetSnmp.Protocol.V3.Security;
using System.Formats.Asn1;
namespace DotNetSnmp.Protocol.V3
{
public class SnmpV3Message : SnmpMessage
{
public override ProtocolVersion ProtocolVersion => ProtocolVersion.SnmpV3;
public HeaderData GlobalData { get; set; }
public UsmSecurityParameters SecurityParameters { get; set; }
public ScopedPdu ScopedPdu { get; set; }
public ReadOnlyMemory<byte> EncryptedScopedPdu { get; set; }
public override void WriteTo(AsnWriter writer)
{
using (_ = writer.PushSequence())
{
// versiom
writer.WriteInteger((int)ProtocolVersion.SnmpV3);
GlobalData.WriteTo(writer);
SecurityParameters.WriteTo(writer);
ScopedPdu.WriteTo(writer);
}
}
public static SnmpV3Message ReadFrom(AsnReader reader)
{
// Message ::= SEQUENCE
var rootSeq = reader.ReadSequence(expectedTag: Asn1Tag.Sequence);
// version INTEGER
if (rootSeq.TryReadInt32(out int messageVersion) == false)
{
throw new SnmpDecodeException(
"Cannot read 'version' number");
}
var version = (ProtocolVersion)messageVersion;
if (version != ProtocolVersion.SnmpV3)
{
throw new ArgumentNullException(
$"expected version: 3 found: {version}");
}
var globalData = HeaderData.ReadFrom(rootSeq);
var usmSecurityParams = UsmSecurityParameters.ReadFrom(rootSeq);
var msg = new SnmpV3Message
{
GlobalData = globalData,
SecurityParameters = usmSecurityParams
};
if (globalData.MsgFlags.HasFlag(MsgFlags.Priv))
{
msg.EncryptedScopedPdu = rootSeq.ReadOctetString();
}
else
{
msg.ScopedPdu = ScopedPdu.ReadFrom(rootSeq);
}
return msg;
}
}
}