forked from mvenditto/DotNetSnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScopedPdu.cs
101 lines (80 loc) · 2.72 KB
/
ScopedPdu.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using DotNetSnmp.Asn1.Serialization;
using DotNetSnmp.Asn1.SyntaxObjects;
using DotNetSnmp.Common.Definitions;
using DotNetSnmp.Protocol.V1;
using System.Formats.Asn1;
using System.Text;
namespace DotNetSnmp.Protocol.V3
{
/// <summary>
/// A scopedPDU is a block of data containing a ContextEngineId, a
/// ContextName, and a Pdu.
/// </summary>
public class ScopedPdu : Pdu
{
public Memory<byte> ContextEngineId { get; set; }
public string ContextName { get; set; }
public Pdu Pdu { get; set; }
public override Asn1Tag PduType => Pdu.PduType;
public new int RequestId => Pdu.RequestId;
public new PduErrorStatus ErrorStatus => Pdu.ErrorStatus;
public new int ErrorIndex => Pdu.ErrorIndex;
public new VarBindList VariableBindings => Pdu.VariableBindings;
public new bool HasData => Pdu.VariableBindings?.IsEmpty == false;
public override object Clone()
{
return new ScopedPdu
{
ContextEngineId = ContextEngineId,
ContextName = ContextName,
Pdu = (Pdu) Pdu.Clone()
};
}
public override void WriteTo(AsnWriter writer)
{
using (_ = writer.PushSequence())
{
var enc = Encoding.UTF8;
writer.WriteOctetString(ContextEngineId.Span);
writer.WriteOctetString(
ContextName.GetBytesSpanOrDefault(enc));
Pdu.WriteTo(writer);
}
}
public static ScopedPdu ReadFrom(AsnReader reader)
{
var seq = reader.ReadSequence();
var ctxEngineId = seq.ReadOctetString();
var ctxName = seq.ReadOctetString();
var utf8 = Encoding.UTF8;
var pduType = seq.PeekTag();
Pdu pdu = null;
if (pduType == SnmpAsnTags.GetResponseMsg)
{
pdu = GetResponsePdu.ReadFrom(seq);
}
if (pduType == SnmpAsnTags.GetMsg)
{
pdu = GetRequestPdu.ReadFrom(seq);
}
else if (pduType == SnmpAsnTags.GetNextMsg)
{
pdu = GetNextRequestPdu.ReadFrom(seq);
}
else if (pduType == SnmpAsnTags.BulkMsg)
{
throw new NotImplementedException();
}
else if (pduType == SnmpAsnTags.InformMsg)
{
throw new NotImplementedException();
}
return new()
{
ContextEngineId = ctxEngineId,
ContextName = utf8.GetString(ctxName),
Pdu = pdu
};
}
}
}