-
Notifications
You must be signed in to change notification settings - Fork 1
/
PacketHeader.cs
82 lines (74 loc) · 3.11 KB
/
PacketHeader.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
using System;
using System.IO;
using System.Net;
namespace TacacsSharp
{
public class PacketHeader
{
public Version Version { get; }
public PacketType Type { get; set; }
public byte SeqNumber { get; set; }
public EncryptionFlag Flags { get; }
public int SessionId { get; set; }
public int BodyLength { get; set; }
public PacketHeader(PacketType type, byte seqNumber, EncryptionFlag flags, int bodyLength, byte majorVersion = 0xc, byte minorVersion = 0x0)
:this(type, seqNumber, flags, bodyLength, new Random().Next(), majorVersion, minorVersion) { }
public PacketHeader(PacketType type, byte seqNumber, EncryptionFlag flags, int bodyLength, int sessionId, byte majorVersion = 0xc, byte minorVersion = 0x0)
:this(type, seqNumber, flags, bodyLength, new Random().Next(), new Version(majorVersion, minorVersion)) { }
public PacketHeader(PacketType type, byte seqNumber, EncryptionFlag flags, int bodyLength, int sessionId, Version version)
{
Type = type;
SeqNumber = seqNumber;
Flags = flags;
BodyLength = bodyLength;
Version = version;
SessionId = sessionId;
}
public byte[] ToArray()
{
using(var header = new MemoryStream(new byte[12]))
{
using(var bw = new BinaryWriter(header))
{
bw.Write(Version.Value); // version
bw.Write((byte)Type); // type
bw.Write((byte)SeqNumber); // seq number
bw.Write((byte)Flags); // flags
bw.Write(SessionId); // session_id
bw.Write(BitConverter.IsLittleEndian ? IPAddress.HostToNetworkOrder(BodyLength) : BodyLength); // length
}
return header.ToArray();
}
}
public static PacketHeader Parse(byte[] data)
{
using(var br = new BinaryReader(new MemoryStream(data)))
{
var version = br.ReadByte();
var type = br.ReadByte();
var seqNo = br.ReadByte();
var flags = br.ReadByte();
var sessionIdReply = br.ReadInt32();
var replyLength = IPAddress.NetworkToHostOrder(br.ReadInt32());
return new PacketHeader(
(PacketType)Enum.Parse(typeof(PacketType), type.ToString()),
seqNo,
(EncryptionFlag)Enum.Parse(typeof(EncryptionFlag), flags.ToString()),
replyLength,
sessionIdReply,
Version.Parse(version)
);
}
}
public override string ToString()
=> string.Format("major_version=0x{0:x}, minor_version=0x{1:x}, type={2}, seq_no={3}, flags={4}, session_id={5}, length={6}",
Version.Major,
Version.Minor,
Type,
SeqNumber,
Flags,
SessionId,
BodyLength
);
}
}