forked from mvenditto/DotNetSnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeaderData.cs
58 lines (43 loc) · 1.56 KB
/
HeaderData.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
using DotNetSnmp.Asn1.Serialization;
using DotNetSnmp.Common.Definitions;
using DotNetSnmp.Protocol.V3.Security;
using System.ComponentModel.DataAnnotations;
using System.Formats.Asn1;
namespace DotNetSnmp.Protocol.V3
{
public class HeaderData : IAsnSerializable
{
public int MsgId { get; set; }
[Range(384, int.MaxValue)]
public int MsgMaxSize { get; set; } = 65536;
public MsgFlags MsgFlags { get; set; }
[Range(1, int.MaxValue)]
public SecurityModel MsgSecurityModel { get; set; } = SecurityModel.Usm;
public void WriteTo(AsnWriter writer)
{
using (_ = writer.PushSequence())
{
writer.WriteInteger(MsgId);
writer.WriteInteger(MsgMaxSize);
Span<byte> flags = stackalloc byte[1] { (byte) MsgFlags };
writer.WriteOctetString(flags);
writer.WriteInteger((byte) MsgSecurityModel);
}
}
public static HeaderData ReadFrom(AsnReader reader)
{
var rootSeq = reader.ReadSequence();
rootSeq.TryReadInt32(out var msgId);
rootSeq.TryReadInt32(out var msgMaxSize);
var flags = (MsgFlags) rootSeq.ReadOctetString()[0];
rootSeq.TryReadInt32(out var msgSecurityModel);
return new HeaderData
{
MsgId = msgId,
MsgMaxSize = msgMaxSize,
MsgFlags = flags,
MsgSecurityModel = (SecurityModel) msgSecurityModel
};
}
}
}